11package com .contentstack .utils .presets ;
22
3- import org .apache .http .client .utils .URIBuilder ;
43import org .jetbrains .annotations .NotNull ;
54import org .json .simple .JSONArray ;
65import org .json .simple .JSONObject ;
6+ import org .springframework .web .util .UriComponentsBuilder ;
77
8- import java .net .MalformedURLException ;
9- import java .net .URISyntaxException ;
108import java .util .List ;
119import java .util .concurrent .atomic .AtomicReference ;
1210
@@ -26,17 +24,12 @@ public String resolvePresetByPresetName(
2624 }
2725 List <JSONObject > presetOptionKEYS = extractMetadata (localExtensionUid , presetName , "name" );
2826 String assetUrl = (String ) asset .get (URL );
29- try {
30- assetUrl = findPresetOptions (presetOptionKEYS , assetUrl );
31- } catch (MalformedURLException | URISyntaxException e ) {
32- throw new InvalidUrlException (e .getLocalizedMessage ());
33- }
27+ assetUrl = findPresetOptions (presetOptionKEYS , assetUrl );
3428
3529 return assetUrl ;
3630 }
3731
38- String findPresetOptions (List <JSONObject > presetOptionKEYS , String assetUrl )
39- throws MalformedURLException , URISyntaxException {
32+ String findPresetOptions (List <JSONObject > presetOptionKEYS , String assetUrl ) {
4033 AtomicReference <JSONObject > optionsJson = new AtomicReference <>();
4134 if (!presetOptionKEYS .isEmpty ()) {
4235 presetOptionKEYS .forEach (options -> {
@@ -64,63 +57,63 @@ public String resolvePresetByPresetUID(
6457 return assetUrl ;
6558 }
6659
67- private String getImageURL (String assetUrl , JSONObject optionKEYS )
68- throws URISyntaxException , MalformedURLException {
69- URIBuilder uriBuilder = new URIBuilder (assetUrl );
60+ private String getImageURL (String assetUrl , @ NotNull JSONObject optionKEYS ) {
61+ UriComponentsBuilder urlBuilder = UriComponentsBuilder
62+ . fromUriString (assetUrl );
7063 if (optionKEYS .containsKey (TRANSFORM )) {
7164 JSONObject transform = (JSONObject ) optionKEYS .get (Constant .TRANSFORM );
72- getTransformParam (uriBuilder , transform );
65+ getTransformParam (urlBuilder , transform );
7366 }
7467 if (optionKEYS .containsKey (IMAGE_TYPE )) {
7568 String imageType = (String ) optionKEYS .get (Constant .IMAGE_TYPE );
76- uriBuilder . addParameter ("format" , imageType );
69+ urlBuilder . queryParam ("format" , imageType );
7770 }
7871 if (optionKEYS .containsKey (QUALITY )) {
7972 String quality = (String ) optionKEYS .get (Constant .QUALITY );
80- uriBuilder . addParameter (Constant .QUALITY , quality );
73+ urlBuilder . queryParam (Constant .QUALITY , quality );
8174 }
8275 if (optionKEYS .containsKey (EFFECTS )) {
8376 JSONObject effects = (JSONObject ) optionKEYS .get (Constant .EFFECTS );
84- getEffectsParams (uriBuilder , effects );
77+ getEffectsParams (urlBuilder , effects );
8578 }
86- return uriBuilder .build ().toURL ().toString ();
79+ return urlBuilder .build ().encode ().toUriString ();
8780 }
8881
89- private void getTransformParam (URIBuilder uriBuilder , JSONObject transform ) {
82+ private void getTransformParam (UriComponentsBuilder uriBuilder , JSONObject transform ) {
9083 if (transform .containsKey (HEIGHT )) {
9184 Object height = transform .get (Constant .HEIGHT );
92- uriBuilder .addParameter (Constant .HEIGHT , height .toString ());
85+ uriBuilder .queryParam (Constant .HEIGHT , height .toString ());
9386 }
9487 if (transform .containsKey (WIDTH )) {
9588 Object width = transform .get (Constant .WIDTH );
96- uriBuilder .addParameter (Constant .WIDTH , width .toString ());
89+ uriBuilder .queryParam (Constant .WIDTH , width .toString ());
9790 }
9891 if (transform .containsKey (FLIP_MODE )) {
9992 String flipMode = (String ) transform .get (Constant .FLIP_MODE );
10093 if (flipMode .equalsIgnoreCase ("both" )) {
101- uriBuilder .addParameter (ORIENT , "3" );
94+ uriBuilder .queryParam (ORIENT , "3" );
10295 }
10396 if (flipMode .equalsIgnoreCase ("horiz" )) {
104- uriBuilder .addParameter (ORIENT , "2" );
97+ uriBuilder .queryParam (ORIENT , "2" );
10598 }
10699 if (flipMode .equalsIgnoreCase ("verti" )) {
107- uriBuilder .addParameter (ORIENT , "4" );
100+ uriBuilder .queryParam (ORIENT , "4" );
108101 }
109102 }
110103 }
111104
112- private void getEffectsParams (URIBuilder uriBuilder , JSONObject effects ) {
105+ private void getEffectsParams (UriComponentsBuilder uriBuilder , JSONObject effects ) {
113106 if (effects .containsKey (BRIGHTNESS )) {
114- uriBuilder .addParameter (BRIGHTNESS , effects .get (Constant .BRIGHTNESS ).toString ());
107+ uriBuilder .queryParam (BRIGHTNESS , effects .get (Constant .BRIGHTNESS ).toString ());
115108 }
116109 if (effects .containsKey (CONTRAST )) {
117- uriBuilder .addParameter (CONTRAST , effects .get (Constant .CONTRAST ).toString ());
110+ uriBuilder .queryParam (CONTRAST , effects .get (Constant .CONTRAST ).toString ());
118111 }
119112 if (effects .containsKey (SHARPEN )) {
120- uriBuilder .addParameter ("saturation" , effects .get ("saturation" ).toString ());
113+ uriBuilder .queryParam ("saturation" , effects .get ("saturation" ).toString ());
121114 }
122115 if (effects .containsKey (BLUR )) {
123- uriBuilder .addParameter (BLUR , effects .get (Constant .BLUR ).toString ());
116+ uriBuilder .queryParam (BLUR , effects .get (Constant .BLUR ).toString ());
124117 }
125118 }
126119
0 commit comments