22
22
import org .apache .commons .lang .StringUtils ;
23
23
import org .apache .http .client .utils .URIBuilder ;
24
24
25
- @ SuppressWarnings ({"rawtypes" , "unchecked" })
25
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
26
26
public class Cloudinary {
27
27
public final static String CF_SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net" ;
28
28
public final static String OLD_AKAMAI_SHARED_CDN = "cloudinary-a.akamaihd.net" ;
29
29
public final static String AKAMAI_SHARED_CDN = "res.cloudinary.com" ;
30
30
public final static String SHARED_CDN = AKAMAI_SHARED_CDN ;
31
-
31
+
32
32
public final static String VERSION = "1.0.11" ;
33
33
public final static String USER_AGENT = "cld-java-" + VERSION ;
34
-
34
+
35
35
private final Map config = new HashMap ();
36
-
36
+
37
37
public Cloudinary (Map config ) {
38
- this .config .putAll (config );
38
+ this .config .putAll (config );
39
39
}
40
-
40
+
41
41
public Cloudinary (String cloudinaryUrl ) {
42
42
initFromUrl (cloudinaryUrl );
43
43
}
@@ -47,67 +47,67 @@ public Cloudinary() {
47
47
if (cloudinaryUrl != null ) {
48
48
initFromUrl (cloudinaryUrl );
49
49
}
50
-
50
+
51
51
}
52
-
52
+
53
53
public Url url () {
54
54
return new Url (this );
55
55
}
56
-
56
+
57
57
public Uploader uploader () {
58
58
return new Uploader (this );
59
59
}
60
60
61
61
public Api api () {
62
62
return new Api (this );
63
63
}
64
-
64
+
65
65
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 ) {
111
111
String apiKey = Cloudinary .asString (options .get ("api_key" ), this .getStringConfig ("api_key" ));
112
112
if (apiKey == null )
113
113
throw new IllegalArgumentException ("Must supply api_key" );
@@ -121,11 +121,11 @@ public void signRequest(Map<String, Object> params, Map<String, Object> options)
121
121
}
122
122
}
123
123
params .put ("signature" , this .apiSignRequest (params , apiSecret ));
124
- params .put ("api_key" , apiKey );
125
- }
124
+ params .put ("api_key" , apiKey );
125
+ }
126
126
127
127
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 >();
129
129
params .put ("public_id" , publicId );
130
130
params .put ("format" , format );
131
131
params .put ("attachment" , options .get ("attachment" ));
@@ -138,9 +138,9 @@ public String privateDownload(String publicId, String format, Map<String, Object
138
138
}
139
139
return builder .toString ();
140
140
}
141
-
141
+
142
142
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 >();
144
144
params .put ("timestamp" , new Long (System .currentTimeMillis () / 1000L ).toString ());
145
145
params .put ("tag" , tag );
146
146
Object transformation = options .get ("transformation" );
@@ -158,7 +158,7 @@ public String zipDownload(String tag, Map<String, Object> options) throws URISyn
158
158
}
159
159
return builder .toString ();
160
160
}
161
-
161
+
162
162
protected void initFromUrl (String cloudinaryUrl ) {
163
163
URI cloudinaryUri = URI .create (cloudinaryUrl );
164
164
setConfig ("cloud_name" , cloudinaryUri .getHost ());
@@ -214,13 +214,13 @@ public static String asString(Object value, String defaultValue) {
214
214
public static List asArray (Object value ) {
215
215
if (value == null ) {
216
216
return Collections .EMPTY_LIST ;
217
- } else if (value instanceof int []){
217
+ } else if (value instanceof int []) {
218
218
List array = new ArrayList ();
219
- for (int i : (int []) value ) {
219
+ for (int i : (int []) value ) {
220
220
array .add (new Integer (i ));
221
221
}
222
222
return array ;
223
- } else if (value instanceof Object []){
223
+ } else if (value instanceof Object []) {
224
224
return Arrays .asList ((Object []) value );
225
225
} else if (value instanceof List ) {
226
226
return (List ) value ;
@@ -229,7 +229,7 @@ public static List asArray(Object value) {
229
229
array .add (value );
230
230
return array ;
231
231
}
232
- }
232
+ }
233
233
234
234
public static Boolean asBoolean (Object value , Boolean defaultValue ) {
235
235
if (value == null ) {
@@ -240,7 +240,7 @@ public static Boolean asBoolean(Object value, Boolean defaultValue) {
240
240
return "true" .equals (value );
241
241
}
242
242
}
243
-
243
+
244
244
public static Float asFloat (Object value ) {
245
245
if (value == null ) {
246
246
return null ;
@@ -250,23 +250,24 @@ public static Float asFloat(Object value) {
250
250
return Float .parseFloat (value .toString ());
251
251
}
252
252
}
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, ...)" );
256
257
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 ]);
259
260
}
260
261
return result ;
261
262
}
262
-
263
+
263
264
public static Map emptyMap () {
264
265
return Collections .EMPTY_MAP ;
265
266
}
266
-
267
+
267
268
public static String encodeMap (Object arg ) {
268
269
if (arg != null && arg instanceof Map ) {
269
- Map <String ,String > mapArg = (Map <String ,String >) arg ;
270
+ Map <String , String > mapArg = (Map <String , String >) arg ;
270
271
HashSet out = new HashSet ();
271
272
for (Map .Entry <String , String > entry : mapArg .entrySet ()) {
272
273
out .add (entry .getKey () + "=" + entry .getValue ());
@@ -278,14 +279,14 @@ public static String encodeMap(Object arg) {
278
279
return arg .toString ();
279
280
}
280
281
}
281
-
282
+
282
283
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
+ }
291
292
}
0 commit comments