|
30 | 30 |
|
31 | 31 | public class UploaderStrategy extends AbstractUploaderStrategy { |
32 | 32 |
|
33 | | - @SuppressWarnings({ "rawtypes", "unchecked" }) |
34 | | - @Override |
35 | | - public Map callApi(String action, Map<String, Object> params, Map options, Object file) throws IOException { |
36 | | - // initialize options if passed as null |
37 | | - if (options == null) { |
38 | | - options = ObjectUtils.emptyMap(); |
39 | | - } |
40 | | - |
41 | | - boolean returnError = ObjectUtils.asBoolean(options.get("return_error"), false); |
42 | | - |
43 | | - if (options.get("unsigned") == null || Boolean.FALSE.equals(options.get("unsigned"))) { |
44 | | - uploader.signRequestParams(params, options); |
45 | | - } else { |
46 | | - Util.clearEmpty(params); |
47 | | - } |
48 | | - |
49 | | - String apiUrl = uploader.cloudinary().cloudinaryApiUrl(action, options); |
50 | | - |
51 | | - ClientConnectionManager connectionManager = (ClientConnectionManager) this.uploader.cloudinary().config.properties.get("connectionManager"); |
52 | | - HttpClient client = new DefaultHttpClient(connectionManager); |
53 | | - |
54 | | - // If the configuration specifies a proxy then apply it to the client |
55 | | - if (uploader.cloudinary().config.proxyHost != null && uploader.cloudinary().config.proxyPort != 0) { |
56 | | - HttpHost proxy = new HttpHost(uploader.cloudinary().config.proxyHost, uploader.cloudinary().config.proxyPort); |
57 | | - client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); |
58 | | - } |
59 | | - |
60 | | - HttpPost postMethod = new HttpPost(apiUrl); |
61 | | - postMethod.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2"); |
62 | | - |
63 | | - Map<String,String> extraHeaders = (Map<String,String>) options.get("extra_headers"); |
64 | | - if (extraHeaders != null) { |
65 | | - for (Map.Entry<String,String> header : extraHeaders.entrySet()) { |
66 | | - postMethod.setHeader(header.getKey(), header.getValue()); |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - Charset utf8 = Charset.forName("UTF-8"); |
71 | | - |
72 | | - MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); |
73 | | - // Remove blank parameters |
74 | | - for (Map.Entry<String, Object> param : params.entrySet()) { |
75 | | - if (param.getValue() instanceof Collection) { |
76 | | - for (Object value : (Collection) param.getValue()) { |
77 | | - multipart.addPart(param.getKey() + "[]", new StringBody(ObjectUtils.asString(value), utf8)); |
78 | | - } |
79 | | - } else { |
80 | | - String value = param.getValue().toString(); |
81 | | - if (StringUtils.isNotBlank(value)) { |
82 | | - multipart.addPart(param.getKey(), new StringBody(value, utf8)); |
83 | | - } |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - if (file instanceof String && !((String) file).matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) { |
88 | | - file = new File((String) file); |
89 | | - } |
90 | | - String filename = (String) options.get("filename"); |
91 | | - if (file instanceof File) { |
92 | | - multipart.addPart("file", new FileBody((File) file, filename, "application/octet-stream", null)); |
93 | | - } else if (file instanceof String) { |
94 | | - multipart.addPart("file", new StringBody((String) file, utf8)); |
95 | | - } else if (file instanceof byte[]) { |
96 | | - if (filename == null) filename = "file"; |
97 | | - multipart.addPart("file", new ByteArrayBody((byte[]) file, filename)); |
98 | | - } else if (file == null) { |
99 | | - // no-problem |
100 | | - } else { |
101 | | - throw new IOException("Uprecognized file parameter " + file); |
102 | | - } |
103 | | - postMethod.setEntity(multipart); |
104 | | - |
105 | | - HttpResponse response = client.execute(postMethod); |
106 | | - int code = response.getStatusLine().getStatusCode(); |
107 | | - InputStream responseStream = response.getEntity().getContent(); |
108 | | - String responseData = StringUtils.read(responseStream); |
109 | | - |
110 | | - if (code != 200 && code != 400 && code != 500) { |
111 | | - throw new RuntimeException("Server returned unexpected status code - " + code + " - " + responseData); |
112 | | - } |
113 | | - |
114 | | - Map result; |
115 | | - |
116 | | - try { |
117 | | - JSONObject responseJSON = new JSONObject(responseData); |
118 | | - result= ObjectUtils.toMap(responseJSON); |
119 | | - } catch (JSONException e) { |
120 | | - throw new RuntimeException("Invalid JSON response from server " + e.getMessage()); |
121 | | - } |
122 | | - |
123 | | - if (result.containsKey("error")) { |
124 | | - Map error = (Map) result.get("error"); |
125 | | - if (returnError) { |
126 | | - error.put("http_code", code); |
127 | | - } else { |
128 | | - throw new RuntimeException((String) error.get("message")); |
129 | | - } |
130 | | - } |
131 | | - return result; |
132 | | - } |
| 33 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 34 | + @Override |
| 35 | + public Map callApi(String action, Map<String, Object> params, Map options, Object file) throws IOException { |
| 36 | + // initialize options if passed as null |
| 37 | + if (options == null) { |
| 38 | + options = ObjectUtils.emptyMap(); |
| 39 | + } |
| 40 | + |
| 41 | + boolean returnError = ObjectUtils.asBoolean(options.get("return_error"), false); |
| 42 | + |
| 43 | + if (options.get("unsigned") == null || Boolean.FALSE.equals(options.get("unsigned"))) { |
| 44 | + uploader.signRequestParams(params, options); |
| 45 | + } else { |
| 46 | + Util.clearEmpty(params); |
| 47 | + } |
| 48 | + |
| 49 | + String apiUrl = uploader.cloudinary().cloudinaryApiUrl(action, options); |
| 50 | + |
| 51 | + ClientConnectionManager connectionManager = (ClientConnectionManager) this.uploader.cloudinary().config.properties.get("connectionManager"); |
| 52 | + HttpClient client = new DefaultHttpClient(connectionManager); |
| 53 | + |
| 54 | + // If the configuration specifies a proxy then apply it to the client |
| 55 | + if (uploader.cloudinary().config.proxyHost != null && uploader.cloudinary().config.proxyPort != 0) { |
| 56 | + HttpHost proxy = new HttpHost(uploader.cloudinary().config.proxyHost, uploader.cloudinary().config.proxyPort); |
| 57 | + client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); |
| 58 | + } |
| 59 | + |
| 60 | + HttpPost postMethod = new HttpPost(apiUrl); |
| 61 | + postMethod.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2"); |
| 62 | + |
| 63 | + Map<String, String> extraHeaders = (Map<String, String>) options.get("extra_headers"); |
| 64 | + if (extraHeaders != null) { |
| 65 | + for (Map.Entry<String, String> header : extraHeaders.entrySet()) { |
| 66 | + postMethod.setHeader(header.getKey(), header.getValue()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Charset utf8 = Charset.forName("UTF-8"); |
| 71 | + |
| 72 | + MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); |
| 73 | + // Remove blank parameters |
| 74 | + for (Map.Entry<String, Object> param : params.entrySet()) { |
| 75 | + if (param.getValue() instanceof Collection) { |
| 76 | + for (Object value : (Collection) param.getValue()) { |
| 77 | + multipart.addPart(param.getKey() + "[]", new StringBody(ObjectUtils.asString(value), utf8)); |
| 78 | + } |
| 79 | + } else { |
| 80 | + String value = param.getValue().toString(); |
| 81 | + if (StringUtils.isNotBlank(value)) { |
| 82 | + multipart.addPart(param.getKey(), new StringBody(value, utf8)); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (file instanceof String && !((String) file).matches("ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) { |
| 88 | + file = new File((String) file); |
| 89 | + } |
| 90 | + String filename = (String) options.get("filename"); |
| 91 | + if (file instanceof File) { |
| 92 | + multipart.addPart("file", new FileBody((File) file, filename, "application/octet-stream", null)); |
| 93 | + } else if (file instanceof String) { |
| 94 | + multipart.addPart("file", new StringBody((String) file, utf8)); |
| 95 | + } else if (file instanceof byte[]) { |
| 96 | + if (filename == null) filename = "file"; |
| 97 | + multipart.addPart("file", new ByteArrayBody((byte[]) file, filename)); |
| 98 | + } else if (file == null) { |
| 99 | + // no-problem |
| 100 | + } else { |
| 101 | + throw new IOException("Uprecognized file parameter " + file); |
| 102 | + } |
| 103 | + postMethod.setEntity(multipart); |
| 104 | + |
| 105 | + HttpResponse response = client.execute(postMethod); |
| 106 | + int code = response.getStatusLine().getStatusCode(); |
| 107 | + InputStream responseStream = response.getEntity().getContent(); |
| 108 | + String responseData = StringUtils.read(responseStream); |
| 109 | + |
| 110 | + if (code != 200 && code != 400 && code != 500) { |
| 111 | + throw new RuntimeException("Server returned unexpected status code - " + code + " - " + responseData); |
| 112 | + } |
| 113 | + |
| 114 | + Map result; |
| 115 | + |
| 116 | + try { |
| 117 | + JSONObject responseJSON = new JSONObject(responseData); |
| 118 | + result = ObjectUtils.toMap(responseJSON); |
| 119 | + } catch (JSONException e) { |
| 120 | + throw new RuntimeException("Invalid JSON response from server " + e.getMessage()); |
| 121 | + } |
| 122 | + |
| 123 | + if (result.containsKey("error")) { |
| 124 | + Map error = (Map) result.get("error"); |
| 125 | + if (returnError) { |
| 126 | + error.put("http_code", code); |
| 127 | + } else { |
| 128 | + throw new RuntimeException((String) error.get("message")); |
| 129 | + } |
| 130 | + } |
| 131 | + return result; |
| 132 | + } |
133 | 133 |
|
134 | 134 | } |
0 commit comments