23
23
import org .apache .http .client .utils .URIBuilder ;
24
24
import org .apache .http .conn .ClientConnectionManager ;
25
25
26
- @ SuppressWarnings ({"rawtypes" , "unchecked" })
26
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
27
27
public class Cloudinary {
28
28
public final static String CF_SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net" ;
29
29
public final static String OLD_AKAMAI_SHARED_CDN = "cloudinary-a.akamaihd.net" ;
30
30
public final static String AKAMAI_SHARED_CDN = "res.cloudinary.com" ;
31
31
public final static String SHARED_CDN = AKAMAI_SHARED_CDN ;
32
-
33
- public final static String VERSION = "1.0.8 " ;
32
+
33
+ public final static String VERSION = "1.0.12 " ;
34
34
public final static String USER_AGENT = "cld-java-" + VERSION ;
35
-
35
+
36
36
private final Map config = new HashMap ();
37
-
37
+ private ClientConnectionManager connectionManager = null ;
38
+
38
39
public Cloudinary (Map config ) {
39
- this .config .putAll (config );
40
+ this .config .putAll (config );
40
41
}
41
-
42
+
42
43
public Cloudinary (String cloudinaryUrl ) {
43
44
initFromUrl (cloudinaryUrl );
44
45
}
@@ -48,67 +49,67 @@ public Cloudinary() {
48
49
if (cloudinaryUrl != null ) {
49
50
initFromUrl (cloudinaryUrl );
50
51
}
51
-
52
+
52
53
}
53
-
54
+
54
55
public Url url () {
55
56
return new Url (this );
56
57
}
57
-
58
+
58
59
public Uploader uploader () {
59
60
return new Uploader (this ).withConnectionManager (connectionManager );
60
61
}
61
62
62
63
public Api api () {
63
64
return new Api (this ).withConnectionManager (connectionManager );
64
65
}
65
-
66
+
66
67
public String cloudinaryApiUrl (String action , Map options ) {
67
- String cloudinary = asString (options .get ("upload_prefix" ), asString (this .config .get ("upload_prefix" ), "https://api.cloudinary.com" ));
68
- String cloud_name = asString (options .get ("cloud_name" ), asString (this .config .get ("cloud_name" )));
69
- if (cloud_name == null ) 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
- }
68
+ String cloudinary = asString (options .get ("upload_prefix" ), asString (this .config .get ("upload_prefix" ), "https://api.cloudinary.com" ));
69
+ String cloud_name = asString (options .get ("cloud_name" ), asString (this .config .get ("cloud_name" )));
70
+ if (cloud_name == null )
71
+ throw new IllegalArgumentException ("Must supply cloud_name in tag or in configuration" );
72
+ String resource_type = asString (options .get ("resource_type" ), "image" );
73
+ return StringUtils .join (new String [] { cloudinary , "v1_1" , cloud_name , resource_type , action }, "/" );
74
+ }
73
75
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
- }
76
+ private final static SecureRandom RND = new SecureRandom ();
81
77
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
- }
78
+ public String randomPublicId () {
79
+ byte [] bytes = new byte [8 ];
80
+ RND .nextBytes (bytes );
81
+ return Hex .encodeHexString (bytes );
82
+ }
86
83
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 if (param .getValue () instanceof String ) {
93
- String value = (String ) param .getValue ();
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
- }
104
- catch (NoSuchAlgorithmException e ) {
105
- throw new RuntimeException ("Unexpected exception" , e );
106
- }
107
- byte [] digest = md .digest ((to_sign + apiSecret ).getBytes ());
108
- return Hex .encodeHexString (digest );
109
- }
84
+ public String signedPreloadedImage (Map result ) {
85
+ return result .get ("resource_type" ) + "/upload/v" + result .get ("version" ) + "/" + result .get ("public_id" )
86
+ + (result .containsKey ("format" ) ? "." + result .get ("format" ) : "" ) + "#" + result .get ("signature" );
87
+ }
88
+
89
+ public String apiSignRequest (Map <String , Object > paramsToSign , String apiSecret ) {
90
+ Collection <String > params = new ArrayList <String >();
91
+ for (Map .Entry <String , Object > param : new TreeMap <String , Object >(paramsToSign ).entrySet ()) {
92
+ if (param .getValue () instanceof Collection ) {
93
+ params .add (param .getKey () + "=" + StringUtils .join ((Collection ) param .getValue (), "," ));
94
+ } else {
95
+ String value = param .getValue ().toString ();
96
+ if (StringUtils .isNotBlank (value )) {
97
+ params .add (param .getKey () + "=" + value );
98
+ }
99
+ }
100
+ }
101
+ String to_sign = StringUtils .join (params , "&" );
102
+ MessageDigest md = null ;
103
+ try {
104
+ md = MessageDigest .getInstance ("SHA-1" );
105
+ } catch (NoSuchAlgorithmException e ) {
106
+ throw new RuntimeException ("Unexpected exception" , e );
107
+ }
108
+ byte [] digest = md .digest ((to_sign + apiSecret ).getBytes ());
109
+ return Hex .encodeHexString (digest );
110
+ }
110
111
111
- public void signRequest (Map <String , Object > params , Map <String , Object > options ) {
112
+ public void signRequest (Map <String , Object > params , Map <String , Object > options ) {
112
113
String apiKey = Cloudinary .asString (options .get ("api_key" ), this .getStringConfig ("api_key" ));
113
114
if (apiKey == null )
114
115
throw new IllegalArgumentException ("Must supply api_key" );
@@ -122,11 +123,11 @@ public void signRequest(Map<String, Object> params, Map<String, Object> options)
122
123
}
123
124
}
124
125
params .put ("signature" , this .apiSignRequest (params , apiSecret ));
125
- params .put ("api_key" , apiKey );
126
- }
126
+ params .put ("api_key" , apiKey );
127
+ }
127
128
128
129
public String privateDownload (String publicId , String format , Map <String , Object > options ) throws URISyntaxException {
129
- Map <String , Object > params = new HashMap <String , Object >();
130
+ Map <String , Object > params = new HashMap <String , Object >();
130
131
params .put ("public_id" , publicId );
131
132
params .put ("format" , format );
132
133
params .put ("attachment" , options .get ("attachment" ));
@@ -139,9 +140,9 @@ public String privateDownload(String publicId, String format, Map<String, Object
139
140
}
140
141
return builder .toString ();
141
142
}
142
-
143
+
143
144
public String zipDownload (String tag , Map <String , Object > options ) throws URISyntaxException {
144
- Map <String , Object > params = new HashMap <String , Object >();
145
+ Map <String , Object > params = new HashMap <String , Object >();
145
146
params .put ("timestamp" , new Long (System .currentTimeMillis () / 1000L ).toString ());
146
147
params .put ("tag" , tag );
147
148
Object transformation = options .get ("transformation" );
@@ -159,7 +160,7 @@ public String zipDownload(String tag, Map<String, Object> options) throws URISyn
159
160
}
160
161
return builder .toString ();
161
162
}
162
-
163
+
163
164
protected void initFromUrl (String cloudinaryUrl ) {
164
165
URI cloudinaryUri = URI .create (cloudinaryUrl );
165
166
setConfig ("cloud_name" , cloudinaryUri .getHost ());
@@ -195,6 +196,11 @@ public String getStringConfig(String key) {
195
196
public void setConfig (String key , Object value ) {
196
197
this .config .put (key , value );
197
198
}
199
+
200
+ public Cloudinary withConnectionManager (ClientConnectionManager connectionManager ) {
201
+ this .connectionManager = connectionManager ;
202
+ return this ;
203
+ }
198
204
199
205
public static String asString (Object value ) {
200
206
if (value == null ) {
@@ -215,13 +221,13 @@ public static String asString(Object value, String defaultValue) {
215
221
public static List asArray (Object value ) {
216
222
if (value == null ) {
217
223
return Collections .EMPTY_LIST ;
218
- } else if (value instanceof int []){
224
+ } else if (value instanceof int []) {
219
225
List array = new ArrayList ();
220
- for (int i : (int []) value ) {
226
+ for (int i : (int []) value ) {
221
227
array .add (new Integer (i ));
222
228
}
223
229
return array ;
224
- } else if (value instanceof Object []){
230
+ } else if (value instanceof Object []) {
225
231
return Arrays .asList ((Object []) value );
226
232
} else if (value instanceof List ) {
227
233
return (List ) value ;
@@ -230,7 +236,7 @@ public static List asArray(Object value) {
230
236
array .add (value );
231
237
return array ;
232
238
}
233
- }
239
+ }
234
240
235
241
public static Boolean asBoolean (Object value , Boolean defaultValue ) {
236
242
if (value == null ) {
@@ -241,7 +247,7 @@ public static Boolean asBoolean(Object value, Boolean defaultValue) {
241
247
return "true" .equals (value );
242
248
}
243
249
}
244
-
250
+
245
251
public static Float asFloat (Object value ) {
246
252
if (value == null ) {
247
253
return null ;
@@ -251,23 +257,24 @@ public static Float asFloat(Object value) {
251
257
return Float .parseFloat (value .toString ());
252
258
}
253
259
}
254
-
255
- public static Map asMap (Object ...values ) {
256
- if (values .length % 2 != 0 ) throw new RuntimeException ("Usage - (key, value, key, value, ...)" );
260
+
261
+ public static Map asMap (Object ... values ) {
262
+ if (values .length % 2 != 0 )
263
+ throw new RuntimeException ("Usage - (key, value, key, value, ...)" );
257
264
Map result = new HashMap (values .length / 2 );
258
- for (int i = 0 ; i < values .length ; i += 2 ) {
259
- result .put (values [i ], values [i + 1 ]);
265
+ for (int i = 0 ; i < values .length ; i += 2 ) {
266
+ result .put (values [i ], values [i + 1 ]);
260
267
}
261
268
return result ;
262
269
}
263
-
270
+
264
271
public static Map emptyMap () {
265
272
return Collections .EMPTY_MAP ;
266
273
}
267
-
274
+
268
275
public static String encodeMap (Object arg ) {
269
276
if (arg != null && arg instanceof Map ) {
270
- Map <String ,String > mapArg = (Map <String ,String >) arg ;
277
+ Map <String , String > mapArg = (Map <String , String >) arg ;
271
278
HashSet out = new HashSet ();
272
279
for (Map .Entry <String , String > entry : mapArg .entrySet ()) {
273
280
out .add (entry .getKey () + "=" + entry .getValue ());
@@ -279,7 +286,7 @@ public static String encodeMap(Object arg) {
279
286
return arg .toString ();
280
287
}
281
288
}
282
-
289
+
283
290
public static Map <String , ? extends Object > only (Map <String , ? extends Object > hash , String ... keys ) {
284
291
Map <String , Object > result = new HashMap <String , Object >();
285
292
for (String key : keys ) {
@@ -290,10 +297,4 @@ public static String encodeMap(Object arg) {
290
297
return result ;
291
298
}
292
299
293
- public Cloudinary withConnectionManager (ClientConnectionManager connectionManager ) {
294
- this .connectionManager = connectionManager ;
295
- return this ;
296
- }
297
-
298
- private ClientConnectionManager connectionManager = null ;
299
300
}
0 commit comments