From a7e3971fddc7512fb1bd00aa2ea4388664656b2b Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Tue, 26 Jan 2010 14:18:19 +0000 Subject: [PATCH] Added setting of content-length in requests even if the body is non existent. Fixed logs for requests. Ensured that streams are not inadvertently closed while copying output. --- .../gson/webservice/client/Preconditions.java | 2 +- .../gson/webservice/client/RequestSender.java | 17 +++++++++-------- .../google/gson/webservice/client/Streams.java | 6 +++--- .../webservice/client/WebServiceClient.java | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/wsclient/src/main/java/com/google/gson/webservice/client/Preconditions.java b/wsclient/src/main/java/com/google/gson/webservice/client/Preconditions.java index b88220ba..95b3b94b 100644 --- a/wsclient/src/main/java/com/google/gson/webservice/client/Preconditions.java +++ b/wsclient/src/main/java/com/google/gson/webservice/client/Preconditions.java @@ -70,7 +70,7 @@ final class Preconditions { private static byte[] readInByteArray(InputStream src) { ByteArrayOutputStream dst = new ByteArrayOutputStream(); try { - Streams.copy(src, dst); + Streams.copy(src, dst, true, true); } catch (IOException e) { // ignore } diff --git a/wsclient/src/main/java/com/google/gson/webservice/client/RequestSender.java b/wsclient/src/main/java/com/google/gson/webservice/client/RequestSender.java index bcf9cea5..69c8b472 100644 --- a/wsclient/src/main/java/com/google/gson/webservice/client/RequestSender.java +++ b/wsclient/src/main/java/com/google/gson/webservice/client/RequestSender.java @@ -42,7 +42,8 @@ public final class RequestSender { public RequestSender(Gson gson) { this(gson, null); } - public RequestSender(Gson gson, Level logLevel) { + + public RequestSender(Gson gson, Level logLevel) { this.gson = gson; logger = logLevel == null ? null : Logger.getLogger(RequestSender.class.getName()); this.logLevel = logLevel; @@ -58,18 +59,18 @@ public final class RequestSender { // before sending any data on the connection. conn.setDoInput(true); - addRequestParams(conn, request.getHeaders()); RequestBody requestBody = request.getBody(); - String contentLength = "0"; - String requestBodyContents = null; + String requestBodyContents = ""; + // Android Java VM ignore Content-Length if setDoOutput is not set + conn.setDoOutput(true); if (requestBody.getSpec().size() > 0) { - conn.setDoOutput(true); requestBodyContents = gson.toJson(requestBody); - contentLength = String.valueOf(requestBodyContents.length()); } + String contentLength = String.valueOf(requestBodyContents.length()); setHeader(conn, "Content-Length", contentLength, true); - if (requestBody.getSpec().size() > 0) { - Streams.copy(requestBodyContents, conn.getOutputStream()); + addRequestParams(conn, request.getHeaders()); + if (requestBodyContents != null) { + Streams.copy(requestBodyContents, conn.getOutputStream(), false); } // Initiate the sending of the request. diff --git a/wsclient/src/main/java/com/google/gson/webservice/client/Streams.java b/wsclient/src/main/java/com/google/gson/webservice/client/Streams.java index 93e9142e..3431e43d 100644 --- a/wsclient/src/main/java/com/google/gson/webservice/client/Streams.java +++ b/wsclient/src/main/java/com/google/gson/webservice/client/Streams.java @@ -22,14 +22,14 @@ import java.io.OutputStream; final class Streams { - static void copy(String str, OutputStream dst) throws IOException { + static void copy(String str, OutputStream dst, boolean closeOutput) throws IOException { byte[] bytes = str.getBytes("UTF-8"); - copy(new ByteArrayInputStream(bytes), dst); + copy(new ByteArrayInputStream(bytes), dst, true, closeOutput); } /** * Copy contents of src to dst. Exhausts src completely, and closes both streams. */ - static void copy(InputStream src, OutputStream dst) throws IOException { + static void copy(InputStream src, OutputStream dst, boolean closeInput, boolean closeOutput) throws IOException { try { final byte[] buf = new byte[2048]; int count; diff --git a/wsclient/src/main/java/com/google/gson/webservice/client/WebServiceClient.java b/wsclient/src/main/java/com/google/gson/webservice/client/WebServiceClient.java index 31485d70..028e0f32 100644 --- a/wsclient/src/main/java/com/google/gson/webservice/client/WebServiceClient.java +++ b/wsclient/src/main/java/com/google/gson/webservice/client/WebServiceClient.java @@ -71,7 +71,7 @@ public final class WebServiceClient { .registerTypeAdapter(ResponseBody.class, new ResponseBodyGsonConverter(callSpec.getResponseSpec().getBodySpec())) .create(); - RequestSender requestSender = new RequestSender(gson); + RequestSender requestSender = new RequestSender(gson, logLevel); requestSender.send(conn, request); ResponseReceiver responseReceiver = new ResponseReceiver(gson, callSpec.getResponseSpec(), logLevel);