|
5 | 5 | import java.io.File; |
6 | 6 | import java.io.IOException; |
7 | 7 | import java.io.InputStream; |
8 | | -import java.io.OutputStream; |
9 | 8 | import java.net.HttpURLConnection; |
10 | 9 | import java.util.Collection; |
11 | 10 | import java.util.Map; |
@@ -48,66 +47,63 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec |
48 | 47 | } |
49 | 48 | } |
50 | 49 | String apiUrl = this.cloudinary().cloudinaryApiUrl(action, options); |
51 | | - MultipartUtility multipart = new MultipartUtility(apiUrl, "UTF-8", this.cloudinary().randomPublicId(), (Map<String, String>) options.get("extra_headers")); |
52 | 50 |
|
53 | | - // Remove blank parameters |
54 | | - for (Map.Entry<String, Object> param : params.entrySet()) { |
55 | | - if (param.getValue() instanceof Collection) { |
56 | | - for (Object value : (Collection) param.getValue()) { |
57 | | - multipart.addFormField(param.getKey() + "[]", ObjectUtils.asString(value)); |
58 | | - } |
59 | | - } else { |
60 | | - if (StringUtils.isNotBlank(param.getValue())) { |
61 | | - multipart.addFormField(param.getKey(), param.getValue().toString()); |
| 51 | + MultipartUtility multipart = null; |
| 52 | + HttpURLConnection connection; |
| 53 | + |
| 54 | + try { |
| 55 | + multipart = new MultipartUtility(apiUrl, "UTF-8", this.cloudinary().randomPublicId(), (Map<String, String>) options.get("extra_headers")); |
| 56 | + |
| 57 | + // Remove blank parameters |
| 58 | + for (Map.Entry<String, Object> param : params.entrySet()) { |
| 59 | + if (param.getValue() instanceof Collection) { |
| 60 | + for (Object value : (Collection) param.getValue()) { |
| 61 | + multipart.addFormField(param.getKey() + "[]", ObjectUtils.asString(value)); |
| 62 | + } |
| 63 | + } else { |
| 64 | + if (StringUtils.isNotBlank(param.getValue())) { |
| 65 | + multipart.addFormField(param.getKey(), param.getValue().toString()); |
| 66 | + } |
62 | 67 | } |
63 | 68 | } |
64 | | - } |
65 | 69 |
|
66 | | - if (file instanceof String && !((String) file).matches("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) { |
67 | | - file = new File((String) file); |
68 | | - } |
69 | | - String filename = (String) options.get("filename"); |
70 | | - if (file instanceof File) { |
71 | | - multipart.addFilePart("file", (File) file, filename); |
72 | | - } else if (file instanceof String) { |
73 | | - multipart.addFormField("file", (String) file); |
74 | | - } else if (file instanceof InputStream) { |
75 | | - multipart.addFilePart("file", (InputStream) file, filename); |
76 | | - } else if (file instanceof byte[]) { |
77 | | - multipart.addFilePart("file", new ByteArrayInputStream((byte[]) file), filename); |
| 70 | + if (file instanceof String && !((String) file).matches("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n=]+)")) { |
| 71 | + file = new File((String) file); |
| 72 | + } |
| 73 | + String filename = (String) options.get("filename"); |
| 74 | + if (file instanceof File) { |
| 75 | + multipart.addFilePart("file", (File) file, filename); |
| 76 | + } else if (file instanceof String) { |
| 77 | + multipart.addFormField("file", (String) file); |
| 78 | + } else if (file instanceof InputStream) { |
| 79 | + multipart.addFilePart("file", (InputStream) file, filename); |
| 80 | + } else if (file instanceof byte[]) { |
| 81 | + multipart.addFilePart("file", new ByteArrayInputStream((byte[]) file), filename); |
| 82 | + } |
| 83 | + |
| 84 | + connection = multipart.execute(); |
| 85 | + } finally { |
| 86 | + if (multipart != null){ |
| 87 | + // Closing more than once has no effect so we can call it safely without having to check state |
| 88 | + multipart.close(); |
| 89 | + } |
78 | 90 | } |
79 | 91 |
|
80 | | - HttpURLConnection connection = multipart.execute(); |
81 | 92 | int code; |
82 | | - |
83 | | - OutputStream outputStream = null; |
84 | | - |
85 | 93 | try { |
86 | 94 | code = connection.getResponseCode(); |
87 | | - outputStream = connection.getOutputStream(); |
88 | | - } catch (Exception e) { |
| 95 | + } catch (IOException e) { |
89 | 96 | if (e.getMessage().equals("No authentication challenges found")) { |
90 | 97 | // Android trying to be clever... |
91 | 98 | code = 401; |
92 | 99 | } else { |
93 | 100 | throw e; |
94 | 101 | } |
95 | | - } finally { |
96 | | - if (outputStream != null) { |
97 | | - try { |
98 | | - outputStream.close(); |
99 | | - } catch (Exception e) {} |
100 | | - } |
101 | 102 | } |
102 | | - |
103 | 103 | InputStream responseStream = code >= 400 ? connection.getErrorStream() : connection.getInputStream(); |
104 | 104 | String responseData = readFully(responseStream); |
105 | 105 | connection.disconnect(); |
106 | 106 |
|
107 | | - try { |
108 | | - responseStream.close(); |
109 | | - } catch (Exception e) {} |
110 | | - |
111 | 107 | if (code != 200 && code != 400 && code != 404 && code != 500) { |
112 | 108 | throw new RuntimeException("Server returned unexpected status code - " + code + " - " + responseData); |
113 | 109 | } |
|
0 commit comments