Skip to content

Commit 3dceeb0

Browse files
author
Amir Tocker
committed
Whitespace
1 parent 779b185 commit 3dceeb0

File tree

10 files changed

+771
-781
lines changed

10 files changed

+771
-781
lines changed

cloudinary-http42/src/main/java/com/cloudinary/http42/ApiStrategy.java

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -31,95 +31,90 @@
3131
import com.cloudinary.utils.ObjectUtils;
3232
import com.cloudinary.utils.StringUtils;
3333

34-
public class ApiStrategy extends AbstractApiStrategy {
35-
36-
37-
@SuppressWarnings({ "rawtypes", "unchecked" })
38-
public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception {
39-
if (options == null)
40-
options = ObjectUtils.emptyMap();
41-
42-
String prefix = ObjectUtils.asString(options.get("upload_prefix"), ObjectUtils.asString(this.api.cloudinary.config.uploadPrefix, "https://api.cloudinary.com"));
43-
String cloudName = ObjectUtils.asString(options.get("cloud_name"), this.api.cloudinary.config.cloudName);
44-
if (cloudName == null)
45-
throw new IllegalArgumentException("Must supply cloud_name");
46-
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
47-
if (apiKey == null)
48-
throw new IllegalArgumentException("Must supply api_key");
34+
public class ApiStrategy extends AbstractApiStrategy {
35+
36+
@SuppressWarnings({"rawtypes", "unchecked"})
37+
public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception {
38+
if (options == null) options = ObjectUtils.emptyMap();
39+
40+
String prefix = ObjectUtils.asString(options.get("upload_prefix"), ObjectUtils.asString(this.api.cloudinary.config.uploadPrefix, "https://api.cloudinary.com"));
41+
String cloudName = ObjectUtils.asString(options.get("cloud_name"), this.api.cloudinary.config.cloudName);
42+
if (cloudName == null) throw new IllegalArgumentException("Must supply cloud_name");
43+
String apiKey = ObjectUtils.asString(options.get("api_key"), this.api.cloudinary.config.apiKey);
44+
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
4945
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
50-
if (apiSecret == null)
51-
throw new IllegalArgumentException("Must supply api_secret");
46+
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
5247

5348
int timeout = ObjectUtils.asInteger(options.get("timeout"), this.api.cloudinary.config.timeout);
5449

5550
String apiUrl = StringUtils.join(Arrays.asList(prefix, "v1_1", cloudName), "/");
56-
for (String component : uri) {
57-
apiUrl = apiUrl + "/" + component;
58-
}
59-
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
60-
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
61-
if (param.getValue() instanceof Iterable) {
62-
for (String single : (Iterable<String>) param.getValue()) {
63-
apiUrlBuilder.addParameter(param.getKey() + "[]", single);
64-
}
65-
} else {
66-
apiUrlBuilder.addParameter(param.getKey(), ObjectUtils.asString(param.getValue()));
67-
}
68-
}
69-
ClientConnectionManager connectionManager = (ClientConnectionManager) this.api.cloudinary.config.properties.get("connectionManager");
51+
for (String component : uri) {
52+
apiUrl = apiUrl + "/" + component;
53+
}
54+
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
55+
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
56+
if (param.getValue() instanceof Iterable) {
57+
for (String single : (Iterable<String>) param.getValue()) {
58+
apiUrlBuilder.addParameter(param.getKey() + "[]", single);
59+
}
60+
} else {
61+
apiUrlBuilder.addParameter(param.getKey(), ObjectUtils.asString(param.getValue()));
62+
}
63+
}
64+
ClientConnectionManager connectionManager = (ClientConnectionManager) this.api.cloudinary.config.properties.get("connectionManager");
7065

7166
DefaultHttpClient client = new DefaultHttpClient(connectionManager);
7267
if (timeout > 0) {
7368
HttpParams httpParams = client.getParams();
74-
HttpConnectionParams.setConnectionTimeout(httpParams, timeout );
75-
HttpConnectionParams.setSoTimeout(httpParams, timeout );
69+
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
70+
HttpConnectionParams.setSoTimeout(httpParams, timeout);
7671
}
7772

78-
URI apiUri = apiUrlBuilder.build();
79-
HttpUriRequest request = null;
80-
switch (method) {
81-
case GET:
82-
request = new HttpGet(apiUri);
83-
break;
84-
case PUT:
85-
request = new HttpPut(apiUri);
86-
break;
87-
case POST:
88-
request = new HttpPost(apiUri);
89-
break;
90-
case DELETE:
91-
request = new HttpDelete(apiUri);
92-
break;
93-
}
94-
request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
95-
request.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2");
96-
97-
HttpResponse response = client.execute(request);
98-
99-
int code = response.getStatusLine().getStatusCode();
100-
InputStream responseStream = response.getEntity().getContent();
101-
String responseData = StringUtils.read(responseStream);
102-
103-
Class<? extends Exception> exceptionClass = Api.CLOUDINARY_API_ERROR_CLASSES.get(code);
104-
if (code != 200 && exceptionClass == null) {
105-
throw new GeneralError("Server returned unexpected status code - " + code + " - " + responseData);
106-
}
107-
Map result;
108-
109-
try {
110-
JSONObject responseJSON = new JSONObject(responseData);
111-
result= ObjectUtils.toMap(responseJSON);
112-
} catch (JSONException e) {
113-
throw new RuntimeException("Invalid JSON response from server " + e.getMessage());
114-
}
115-
116-
if (code == 200) {
117-
return new Response(response, result);
118-
} else {
119-
String message = (String) ((Map) result.get("error")).get("message");
120-
Constructor<? extends Exception> exceptionConstructor = exceptionClass.getConstructor(String.class);
121-
throw exceptionConstructor.newInstance(message);
122-
}
123-
}
73+
URI apiUri = apiUrlBuilder.build();
74+
HttpUriRequest request = null;
75+
switch (method) {
76+
case GET:
77+
request = new HttpGet(apiUri);
78+
break;
79+
case PUT:
80+
request = new HttpPut(apiUri);
81+
break;
82+
case POST:
83+
request = new HttpPost(apiUri);
84+
break;
85+
case DELETE:
86+
request = new HttpDelete(apiUri);
87+
break;
88+
}
89+
request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
90+
request.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2");
91+
92+
HttpResponse response = client.execute(request);
93+
94+
int code = response.getStatusLine().getStatusCode();
95+
InputStream responseStream = response.getEntity().getContent();
96+
String responseData = StringUtils.read(responseStream);
97+
98+
Class<? extends Exception> exceptionClass = Api.CLOUDINARY_API_ERROR_CLASSES.get(code);
99+
if (code != 200 && exceptionClass == null) {
100+
throw new GeneralError("Server returned unexpected status code - " + code + " - " + responseData);
101+
}
102+
Map result;
103+
104+
try {
105+
JSONObject responseJSON = new JSONObject(responseData);
106+
result = ObjectUtils.toMap(responseJSON);
107+
} catch (JSONException e) {
108+
throw new RuntimeException("Invalid JSON response from server " + e.getMessage());
109+
}
110+
111+
if (code == 200) {
112+
return new Response(response, result);
113+
} else {
114+
String message = (String) ((Map) result.get("error")).get("message");
115+
Constructor<? extends Exception> exceptionConstructor = exceptionClass.getConstructor(String.class);
116+
throw exceptionConstructor.newInstance(message);
117+
}
118+
}
124119

125120
}

cloudinary-http42/src/main/java/com/cloudinary/http42/UploaderStrategy.java

Lines changed: 100 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -30,105 +30,105 @@
3030

3131
public class UploaderStrategy extends AbstractUploaderStrategy {
3232

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+
}
133133

134134
}

0 commit comments

Comments
 (0)