Skip to content

Commit 4996f5a

Browse files
committed
Improve exception safety of HttpUtil and add response message
1 parent 417886f commit 4996f5a

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

src/main/java/com/creatubbles/api/request/amazon/UploadS3ImageRequest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.creatubbles.api.core.CreatubblesRequest;
55
import com.creatubbles.api.response.amazon.UploadS3ImageResponse;
66
import com.creatubbles.api.util.HttpUtil;
7+
import com.creatubbles.api.util.HttpUtil.Response;
78

89
import java.io.IOException;
910

@@ -32,12 +33,13 @@ public CreatubblesRequest<UploadS3ImageResponse> execute() {
3233
resetResponse();
3334
UploadS3ImageResponse response = new UploadS3ImageResponse();
3435
try {
35-
response.success = isSuccessStatusCode(HttpUtil.uploadObject(data, url, HttpUtil.IMAGE_JPEG_CONTENT_TYPE));
36+
Response resp = HttpUtil.uploadObject(data, url, HttpUtil.IMAGE_JPEG_CONTENT_TYPE);
37+
response.success = isSuccessStatusCode(resp.code);
38+
response.message = resp.message;
3639
setResponseCache(response);
3740
} catch (IOException e) {
3841
e.printStackTrace();
3942
}
4043
return this;
4144
}
42-
43-
}
45+
}

src/main/java/com/creatubbles/api/util/HttpUtil.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,46 @@
77
import java.net.URL;
88

99
public class HttpUtil {
10+
11+
public static class Response {
12+
public String message;
13+
public int code;
14+
}
1015

1116
public static final String IMAGE_JPEG_CONTENT_TYPE = "image/jpeg";
1217

13-
public static int uploadObject(byte[] data, String url, String contentType) throws IOException {
14-
HttpURLConnection connection = (HttpURLConnection) (new URL(url).openConnection());
15-
connection.setDoOutput(true);
16-
connection.setUseCaches(false);
17-
connection.setRequestMethod(HttpMethod.PUT.name());
18-
connection.setRequestProperty("Content-Type", contentType);
19-
OutputStream out = connection.getOutputStream();
20-
ByteArrayInputStream in = new ByteArrayInputStream(data);
21-
byte[] buffer = new byte[4096];
22-
int bytesRead = -1;
23-
while ((bytesRead = in.read(buffer)) != -1) {
24-
out.write(buffer, 0, bytesRead);
18+
public static Response uploadObject(byte[] data, String url, String contentType) throws IOException {
19+
HttpURLConnection connection = null;
20+
Response ret = new Response();
21+
try {
22+
connection = (HttpURLConnection) (new URL(url).openConnection());
23+
connection.setDoOutput(true);
24+
connection.setUseCaches(false);
25+
connection.setRequestMethod(HttpMethod.PUT.name());
26+
connection.setRequestProperty("Content-Type", contentType);
27+
OutputStream out = null;
28+
try {
29+
out = connection.getOutputStream();
30+
ByteArrayInputStream in = new ByteArrayInputStream(data);
31+
byte[] buffer = new byte[4096];
32+
int bytesRead = -1;
33+
while ((bytesRead = in.read(buffer)) != -1) {
34+
out.write(buffer, 0, bytesRead);
35+
}
36+
in.close();
37+
} finally {
38+
if (out != null) {
39+
out.flush();
40+
out.close();
41+
}
42+
}
43+
} finally {
44+
if (connection != null) {
45+
ret.code = connection.getResponseCode();
46+
ret.message = connection.getResponseMessage();
47+
connection.disconnect();
48+
}
2549
}
26-
out.flush();
27-
in.close();
28-
out.close();
29-
return connection.getResponseCode();
50+
return ret;
3051
}
31-
3252
}

0 commit comments

Comments
 (0)