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.
This commit is contained in:
Inderjeet Singh 2010-01-26 14:18:19 +00:00
parent dbca5571e0
commit a7e3971fdd
4 changed files with 14 additions and 13 deletions

View File

@ -70,7 +70,7 @@ final class Preconditions {
private static byte[] readInByteArray(InputStream src) { private static byte[] readInByteArray(InputStream src) {
ByteArrayOutputStream dst = new ByteArrayOutputStream(); ByteArrayOutputStream dst = new ByteArrayOutputStream();
try { try {
Streams.copy(src, dst); Streams.copy(src, dst, true, true);
} catch (IOException e) { } catch (IOException e) {
// ignore // ignore
} }

View File

@ -42,7 +42,8 @@ public final class RequestSender {
public RequestSender(Gson gson) { public RequestSender(Gson gson) {
this(gson, null); this(gson, null);
} }
public RequestSender(Gson gson, Level logLevel) {
public RequestSender(Gson gson, Level logLevel) {
this.gson = gson; this.gson = gson;
logger = logLevel == null ? null : Logger.getLogger(RequestSender.class.getName()); logger = logLevel == null ? null : Logger.getLogger(RequestSender.class.getName());
this.logLevel = logLevel; this.logLevel = logLevel;
@ -58,18 +59,18 @@ public final class RequestSender {
// before sending any data on the connection. // before sending any data on the connection.
conn.setDoInput(true); conn.setDoInput(true);
addRequestParams(conn, request.getHeaders());
RequestBody requestBody = request.getBody(); RequestBody requestBody = request.getBody();
String contentLength = "0"; String requestBodyContents = "";
String requestBodyContents = null; // Android Java VM ignore Content-Length if setDoOutput is not set
conn.setDoOutput(true);
if (requestBody.getSpec().size() > 0) { if (requestBody.getSpec().size() > 0) {
conn.setDoOutput(true);
requestBodyContents = gson.toJson(requestBody); requestBodyContents = gson.toJson(requestBody);
contentLength = String.valueOf(requestBodyContents.length());
} }
String contentLength = String.valueOf(requestBodyContents.length());
setHeader(conn, "Content-Length", contentLength, true); setHeader(conn, "Content-Length", contentLength, true);
if (requestBody.getSpec().size() > 0) { addRequestParams(conn, request.getHeaders());
Streams.copy(requestBodyContents, conn.getOutputStream()); if (requestBodyContents != null) {
Streams.copy(requestBodyContents, conn.getOutputStream(), false);
} }
// Initiate the sending of the request. // Initiate the sending of the request.

View File

@ -22,14 +22,14 @@ import java.io.OutputStream;
final class Streams { 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"); 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. * 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 { try {
final byte[] buf = new byte[2048]; final byte[] buf = new byte[2048];
int count; int count;

View File

@ -71,7 +71,7 @@ public final class WebServiceClient {
.registerTypeAdapter(ResponseBody.class, .registerTypeAdapter(ResponseBody.class,
new ResponseBodyGsonConverter(callSpec.getResponseSpec().getBodySpec())) new ResponseBodyGsonConverter(callSpec.getResponseSpec().getBodySpec()))
.create(); .create();
RequestSender requestSender = new RequestSender(gson); RequestSender requestSender = new RequestSender(gson, logLevel);
requestSender.send(conn, request); requestSender.send(conn, request);
ResponseReceiver responseReceiver = ResponseReceiver responseReceiver =
new ResponseReceiver(gson, callSpec.getResponseSpec(), logLevel); new ResponseReceiver(gson, callSpec.getResponseSpec(), logLevel);