2222import org .apache .commons .lang .StringUtils ;
2323import org .apache .http .client .utils .URIBuilder ;
2424
25- @ SuppressWarnings ({"rawtypes" , "unchecked" })
25+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
2626public class Cloudinary {
2727 public final static String CF_SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net" ;
2828 public final static String OLD_AKAMAI_SHARED_CDN = "cloudinary-a.akamaihd.net" ;
2929 public final static String AKAMAI_SHARED_CDN = "res.cloudinary.com" ;
3030 public final static String SHARED_CDN = AKAMAI_SHARED_CDN ;
31-
31+
3232 public final static String VERSION = "1.0.11" ;
3333 public final static String USER_AGENT = "cld-java-" + VERSION ;
34-
34+
3535 private final Map config = new HashMap ();
36-
36+
3737 public Cloudinary (Map config ) {
38- this .config .putAll (config );
38+ this .config .putAll (config );
3939 }
40-
40+
4141 public Cloudinary (String cloudinaryUrl ) {
4242 initFromUrl (cloudinaryUrl );
4343 }
@@ -47,67 +47,67 @@ public Cloudinary() {
4747 if (cloudinaryUrl != null ) {
4848 initFromUrl (cloudinaryUrl );
4949 }
50-
50+
5151 }
52-
52+
5353 public Url url () {
5454 return new Url (this );
5555 }
56-
56+
5757 public Uploader uploader () {
5858 return new Uploader (this );
5959 }
6060
6161 public Api api () {
6262 return new Api (this );
6363 }
64-
64+
6565 public String cloudinaryApiUrl (String action , Map options ) {
66- String cloudinary = asString (options .get ("upload_prefix" ), asString (this .config .get ("upload_prefix" ), "https://api.cloudinary.com" ));
67- String cloud_name = asString (options .get ("cloud_name" ), asString (this .config .get ("cloud_name" )));
68- if (cloud_name == null ) throw new IllegalArgumentException ( "Must supply cloud_name in tag or in configuration" );
69- String resource_type = asString ( options . get ( "resource_type" ), "image" );
70- return StringUtils . join ( new String []{ cloudinary , "v1_1" , cloud_name , resource_type , action }, "/ " );
71- }
72-
73- private final static SecureRandom RND = new SecureRandom ();
74-
75- public String randomPublicId () {
76- byte [] bytes = new byte [ 8 ];
77- RND . nextBytes ( bytes ) ;
78- return Hex . encodeHexString (bytes );
79- }
80-
81- public String signedPreloadedImage ( Map result ) {
82- return result . get ( "resource_type" ) + "/upload/v" + result . get ( "version" ) + "/" + result . get ( "public_id" ) +
83- ( result .containsKey ( "format " ) ? ". " + result .get ("format " ) : "" ) + "# " + result .get ("signature" );
84- }
85-
86- public String apiSignRequest ( Map < String , Object > paramsToSign , String apiSecret ) {
87- Collection <String > params = new ArrayList < String >();
88- for ( Map . Entry <String , Object > param : new TreeMap <String , Object >( paramsToSign ). entrySet ()) {
89- if ( param . getValue () instanceof Collection ) {
90- params . add (param .getKey () + "=" + StringUtils . join (( Collection ) param . getValue (), "," ));
91- } else if ( param .getValue () instanceof String ) {
92- String value = ( String ) param . getValue ();
93- if ( StringUtils . isNotBlank ( value )) {
94- params . add ( param . getKey () + "=" + value );
95- }
96- }
97- }
98- String to_sign = StringUtils . join ( params , "&" );
99- MessageDigest md = null ;
100- try {
101- md = MessageDigest . getInstance ( "SHA-1" );
102- }
103- catch (NoSuchAlgorithmException e ) {
104- throw new RuntimeException ("Unexpected exception" , e );
105- }
106- byte [] digest = md .digest ((to_sign + apiSecret ).getBytes ());
107- return Hex .encodeHexString (digest );
108- }
109-
110- public void signRequest (Map <String , Object > params , Map <String , Object > options ) {
66+ String cloudinary = asString (options .get ("upload_prefix" ), asString (this .config .get ("upload_prefix" ), "https://api.cloudinary.com" ));
67+ String cloud_name = asString (options .get ("cloud_name" ), asString (this .config .get ("cloud_name" )));
68+ if (cloud_name == null )
69+ throw new IllegalArgumentException ( "Must supply cloud_name in tag or in configuration" );
70+ String resource_type = asString ( options . get ( "resource_type" ), "image " );
71+ return StringUtils . join ( new String [] { cloudinary , "v1_1" , cloud_name , resource_type , action }, "/" );
72+ }
73+
74+ private final static SecureRandom RND = new SecureRandom ();
75+
76+ public String randomPublicId () {
77+ byte [] bytes = new byte [ 8 ] ;
78+ RND . nextBytes (bytes );
79+ return Hex . encodeHexString ( bytes );
80+ }
81+
82+ public String signedPreloadedImage ( Map result ) {
83+ return result .get ( "resource_type " ) + "/upload/v " + result .get ("version " ) + "/ " + result .get ("public_id" )
84+ + ( result . containsKey ( "format" ) ? "." + result . get ( "format" ) : "" ) + "#" + result . get ( "signature" );
85+ }
86+
87+ public String apiSignRequest ( Map <String , Object > paramsToSign , String apiSecret ) {
88+ Collection <String > params = new ArrayList <String >();
89+ for ( Map . Entry < String , Object > param : new TreeMap < String , Object >( paramsToSign ). entrySet () ) {
90+ if (param .getValue () instanceof Collection ) {
91+ params . add ( param . getKey () + "=" + StringUtils . join (( Collection ) param .getValue (), "," ));
92+ } else {
93+ String value = param . getValue (). toString ();
94+ if ( StringUtils . isNotBlank ( value )) {
95+ params . add ( param . getKey () + "=" + value );
96+ }
97+ }
98+ }
99+ String to_sign = StringUtils . join ( params , "&" ) ;
100+ MessageDigest md = null ;
101+ try {
102+ md = MessageDigest . getInstance ( "SHA-1" );
103+ } catch (NoSuchAlgorithmException e ) {
104+ throw new RuntimeException ("Unexpected exception" , e );
105+ }
106+ byte [] digest = md .digest ((to_sign + apiSecret ).getBytes ());
107+ return Hex .encodeHexString (digest );
108+ }
109+
110+ public void signRequest (Map <String , Object > params , Map <String , Object > options ) {
111111 String apiKey = Cloudinary .asString (options .get ("api_key" ), this .getStringConfig ("api_key" ));
112112 if (apiKey == null )
113113 throw new IllegalArgumentException ("Must supply api_key" );
@@ -121,11 +121,11 @@ public void signRequest(Map<String, Object> params, Map<String, Object> options)
121121 }
122122 }
123123 params .put ("signature" , this .apiSignRequest (params , apiSecret ));
124- params .put ("api_key" , apiKey );
125- }
124+ params .put ("api_key" , apiKey );
125+ }
126126
127127 public String privateDownload (String publicId , String format , Map <String , Object > options ) throws URISyntaxException {
128- Map <String , Object > params = new HashMap <String , Object >();
128+ Map <String , Object > params = new HashMap <String , Object >();
129129 params .put ("public_id" , publicId );
130130 params .put ("format" , format );
131131 params .put ("attachment" , options .get ("attachment" ));
@@ -138,9 +138,9 @@ public String privateDownload(String publicId, String format, Map<String, Object
138138 }
139139 return builder .toString ();
140140 }
141-
141+
142142 public String zipDownload (String tag , Map <String , Object > options ) throws URISyntaxException {
143- Map <String , Object > params = new HashMap <String , Object >();
143+ Map <String , Object > params = new HashMap <String , Object >();
144144 params .put ("timestamp" , new Long (System .currentTimeMillis () / 1000L ).toString ());
145145 params .put ("tag" , tag );
146146 Object transformation = options .get ("transformation" );
@@ -158,7 +158,7 @@ public String zipDownload(String tag, Map<String, Object> options) throws URISyn
158158 }
159159 return builder .toString ();
160160 }
161-
161+
162162 protected void initFromUrl (String cloudinaryUrl ) {
163163 URI cloudinaryUri = URI .create (cloudinaryUrl );
164164 setConfig ("cloud_name" , cloudinaryUri .getHost ());
@@ -214,13 +214,13 @@ public static String asString(Object value, String defaultValue) {
214214 public static List asArray (Object value ) {
215215 if (value == null ) {
216216 return Collections .EMPTY_LIST ;
217- } else if (value instanceof int []){
217+ } else if (value instanceof int []) {
218218 List array = new ArrayList ();
219- for (int i : (int []) value ) {
219+ for (int i : (int []) value ) {
220220 array .add (new Integer (i ));
221221 }
222222 return array ;
223- } else if (value instanceof Object []){
223+ } else if (value instanceof Object []) {
224224 return Arrays .asList ((Object []) value );
225225 } else if (value instanceof List ) {
226226 return (List ) value ;
@@ -229,7 +229,7 @@ public static List asArray(Object value) {
229229 array .add (value );
230230 return array ;
231231 }
232- }
232+ }
233233
234234 public static Boolean asBoolean (Object value , Boolean defaultValue ) {
235235 if (value == null ) {
@@ -240,7 +240,7 @@ public static Boolean asBoolean(Object value, Boolean defaultValue) {
240240 return "true" .equals (value );
241241 }
242242 }
243-
243+
244244 public static Float asFloat (Object value ) {
245245 if (value == null ) {
246246 return null ;
@@ -250,23 +250,24 @@ public static Float asFloat(Object value) {
250250 return Float .parseFloat (value .toString ());
251251 }
252252 }
253-
254- public static Map asMap (Object ...values ) {
255- if (values .length % 2 != 0 ) throw new RuntimeException ("Usage - (key, value, key, value, ...)" );
253+
254+ public static Map asMap (Object ... values ) {
255+ if (values .length % 2 != 0 )
256+ throw new RuntimeException ("Usage - (key, value, key, value, ...)" );
256257 Map result = new HashMap (values .length / 2 );
257- for (int i = 0 ; i < values .length ; i += 2 ) {
258- result .put (values [i ], values [i + 1 ]);
258+ for (int i = 0 ; i < values .length ; i += 2 ) {
259+ result .put (values [i ], values [i + 1 ]);
259260 }
260261 return result ;
261262 }
262-
263+
263264 public static Map emptyMap () {
264265 return Collections .EMPTY_MAP ;
265266 }
266-
267+
267268 public static String encodeMap (Object arg ) {
268269 if (arg != null && arg instanceof Map ) {
269- Map <String ,String > mapArg = (Map <String ,String >) arg ;
270+ Map <String , String > mapArg = (Map <String , String >) arg ;
270271 HashSet out = new HashSet ();
271272 for (Map .Entry <String , String > entry : mapArg .entrySet ()) {
272273 out .add (entry .getKey () + "=" + entry .getValue ());
@@ -278,14 +279,14 @@ public static String encodeMap(Object arg) {
278279 return arg .toString ();
279280 }
280281 }
281-
282+
282283 public static Map <String , ? extends Object > only (Map <String , ? extends Object > hash , String ... keys ) {
283- Map <String , Object > result = new HashMap <String , Object >();
284- for (String key : keys ) {
285- if (hash .containsKey (key )) {
286- result .put (key , hash .get (key ));
287- }
288- }
289- return result ;
290- }
284+ Map <String , Object > result = new HashMap <String , Object >();
285+ for (String key : keys ) {
286+ if (hash .containsKey (key )) {
287+ result .put (key , hash .get (key ));
288+ }
289+ }
290+ return result ;
291+ }
291292}
0 commit comments