2323import org .apache .http .client .utils .URIBuilder ;
2424import org .apache .http .conn .ClientConnectionManager ;
2525
26- @ SuppressWarnings ({"rawtypes" , "unchecked" })
26+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
2727public class Cloudinary {
2828 public final static String CF_SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net" ;
2929 public final static String OLD_AKAMAI_SHARED_CDN = "cloudinary-a.akamaihd.net" ;
3030 public final static String AKAMAI_SHARED_CDN = "res.cloudinary.com" ;
3131 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 " ;
3434 public final static String USER_AGENT = "cld-java-" + VERSION ;
35-
35+
3636 private final Map config = new HashMap ();
37-
37+ private ClientConnectionManager connectionManager = null ;
38+
3839 public Cloudinary (Map config ) {
39- this .config .putAll (config );
40+ this .config .putAll (config );
4041 }
41-
42+
4243 public Cloudinary (String cloudinaryUrl ) {
4344 initFromUrl (cloudinaryUrl );
4445 }
@@ -48,67 +49,67 @@ public Cloudinary() {
4849 if (cloudinaryUrl != null ) {
4950 initFromUrl (cloudinaryUrl );
5051 }
51-
52+
5253 }
53-
54+
5455 public Url url () {
5556 return new Url (this );
5657 }
57-
58+
5859 public Uploader uploader () {
5960 return new Uploader (this ).withConnectionManager (connectionManager );
6061 }
6162
6263 public Api api () {
6364 return new Api (this ).withConnectionManager (connectionManager );
6465 }
65-
66+
6667 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+ }
7375
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 ();
8177
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+ }
8683
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+ }
110111
111- public void signRequest (Map <String , Object > params , Map <String , Object > options ) {
112+ public void signRequest (Map <String , Object > params , Map <String , Object > options ) {
112113 String apiKey = Cloudinary .asString (options .get ("api_key" ), this .getStringConfig ("api_key" ));
113114 if (apiKey == null )
114115 throw new IllegalArgumentException ("Must supply api_key" );
@@ -122,11 +123,11 @@ public void signRequest(Map<String, Object> params, Map<String, Object> options)
122123 }
123124 }
124125 params .put ("signature" , this .apiSignRequest (params , apiSecret ));
125- params .put ("api_key" , apiKey );
126- }
126+ params .put ("api_key" , apiKey );
127+ }
127128
128129 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 >();
130131 params .put ("public_id" , publicId );
131132 params .put ("format" , format );
132133 params .put ("attachment" , options .get ("attachment" ));
@@ -139,9 +140,9 @@ public String privateDownload(String publicId, String format, Map<String, Object
139140 }
140141 return builder .toString ();
141142 }
142-
143+
143144 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 >();
145146 params .put ("timestamp" , new Long (System .currentTimeMillis () / 1000L ).toString ());
146147 params .put ("tag" , tag );
147148 Object transformation = options .get ("transformation" );
@@ -159,7 +160,7 @@ public String zipDownload(String tag, Map<String, Object> options) throws URISyn
159160 }
160161 return builder .toString ();
161162 }
162-
163+
163164 protected void initFromUrl (String cloudinaryUrl ) {
164165 URI cloudinaryUri = URI .create (cloudinaryUrl );
165166 setConfig ("cloud_name" , cloudinaryUri .getHost ());
@@ -195,6 +196,11 @@ public String getStringConfig(String key) {
195196 public void setConfig (String key , Object value ) {
196197 this .config .put (key , value );
197198 }
199+
200+ public Cloudinary withConnectionManager (ClientConnectionManager connectionManager ) {
201+ this .connectionManager = connectionManager ;
202+ return this ;
203+ }
198204
199205 public static String asString (Object value ) {
200206 if (value == null ) {
@@ -215,13 +221,13 @@ public static String asString(Object value, String defaultValue) {
215221 public static List asArray (Object value ) {
216222 if (value == null ) {
217223 return Collections .EMPTY_LIST ;
218- } else if (value instanceof int []){
224+ } else if (value instanceof int []) {
219225 List array = new ArrayList ();
220- for (int i : (int []) value ) {
226+ for (int i : (int []) value ) {
221227 array .add (new Integer (i ));
222228 }
223229 return array ;
224- } else if (value instanceof Object []){
230+ } else if (value instanceof Object []) {
225231 return Arrays .asList ((Object []) value );
226232 } else if (value instanceof List ) {
227233 return (List ) value ;
@@ -230,7 +236,7 @@ public static List asArray(Object value) {
230236 array .add (value );
231237 return array ;
232238 }
233- }
239+ }
234240
235241 public static Boolean asBoolean (Object value , Boolean defaultValue ) {
236242 if (value == null ) {
@@ -241,7 +247,7 @@ public static Boolean asBoolean(Object value, Boolean defaultValue) {
241247 return "true" .equals (value );
242248 }
243249 }
244-
250+
245251 public static Float asFloat (Object value ) {
246252 if (value == null ) {
247253 return null ;
@@ -251,23 +257,24 @@ public static Float asFloat(Object value) {
251257 return Float .parseFloat (value .toString ());
252258 }
253259 }
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, ...)" );
257264 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 ]);
260267 }
261268 return result ;
262269 }
263-
270+
264271 public static Map emptyMap () {
265272 return Collections .EMPTY_MAP ;
266273 }
267-
274+
268275 public static String encodeMap (Object arg ) {
269276 if (arg != null && arg instanceof Map ) {
270- Map <String ,String > mapArg = (Map <String ,String >) arg ;
277+ Map <String , String > mapArg = (Map <String , String >) arg ;
271278 HashSet out = new HashSet ();
272279 for (Map .Entry <String , String > entry : mapArg .entrySet ()) {
273280 out .add (entry .getKey () + "=" + entry .getValue ());
@@ -279,7 +286,7 @@ public static String encodeMap(Object arg) {
279286 return arg .toString ();
280287 }
281288 }
282-
289+
283290 public static Map <String , ? extends Object > only (Map <String , ? extends Object > hash , String ... keys ) {
284291 Map <String , Object > result = new HashMap <String , Object >();
285292 for (String key : keys ) {
@@ -290,10 +297,4 @@ public static String encodeMap(Object arg) {
290297 return result ;
291298 }
292299
293- public Cloudinary withConnectionManager (ClientConnectionManager connectionManager ) {
294- this .connectionManager = connectionManager ;
295- return this ;
296- }
297-
298- private ClientConnectionManager connectionManager = null ;
299300}
0 commit comments