Cleaned up some duplication
The legacy method was doing the exact same thing as addIfBodyExists. Now merged the two and added an override to support the different source of byte stream
@@ -130,10 +130,11 @@ public class HurlStack implements HttpStack {
/**
* Checks if a response message contains a body.
- * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
+ *
* @param requestMethod request method
* @param responseCode response status code
* @return whether the response has a body
+ * @see <a href="https://tools.ietf.org/html/rfc7230#section-3.3">RFC 7230 section 3.3</a>
*/
private static boolean hasResponseBody(int requestMethod, int responseCode) {
return requestMethod != Request.Method.HEAD
@@ -144,6 +145,7 @@ public class HurlStack implements HttpStack {
/**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
+ *
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
@@ -178,6 +180,7 @@ public class HurlStack implements HttpStack {
/**
* Opens an {@link HttpURLConnection} with parameters.
+ *
* @param url
* @return an open connection
* @throws IOException
@@ -209,16 +212,8 @@ public class HurlStack implements HttpStack {
// GET. Otherwise, it is assumed that the request is a POST.
byte[] postBody = request.getPostBody();
if (postBody != null) {
- // Prepare output. There is no need to set Content-Length explicitly,
- // since this is handled by HttpURLConnection using the size of the prepared
- // output stream.
- connection.setDoOutput(true);
connection.setRequestMethod("POST");
- connection.addRequestProperty(HEADER_CONTENT_TYPE,
- request.getPostBodyContentType());
- DataOutputStream out = new DataOutputStream(connection.getOutputStream());
- out.write(postBody);
- out.close();
+ addBodyIfExists(connection, request, postBody);
}
break;
case Method.GET:
@@ -259,6 +254,16 @@ public class HurlStack implements HttpStack {
throws IOException, AuthFailureError {
byte[] body = request.getBody();
if (body != null) {
+ addBodyIfExists(connection, request, body);
+ }
+ }
+
+ private static void addBodyIfExists(HttpURLConnection connection, Request<?> request, byte[] body)
+ throws IOException, AuthFailureError {
+ if (body != null) {
+ // Prepare output. There is no need to set Content-Length explicitly,
+ // since this is handled by HttpURLConnection using the size of the prepared
+ // output stream.
connection.setDoOutput(true);
connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
