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) {
ByteArrayOutputStream dst = new ByteArrayOutputStream();
try {
Streams.copy(src, dst);
Streams.copy(src, dst, true, true);
} catch (IOException e) {
// ignore
}

View File

@ -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.

View File

@ -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;

View File

@ -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);