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:
parent
dbca5571e0
commit
a7e3971fdd
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ 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());
|
||||||
|
@ -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
|
||||||
if (requestBody.getSpec().size() > 0) {
|
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
requestBodyContents = gson.toJson(requestBody);
|
|
||||||
contentLength = String.valueOf(requestBodyContents.length());
|
|
||||||
}
|
|
||||||
setHeader(conn, "Content-Length", contentLength, true);
|
|
||||||
if (requestBody.getSpec().size() > 0) {
|
if (requestBody.getSpec().size() > 0) {
|
||||||
Streams.copy(requestBodyContents, conn.getOutputStream());
|
requestBodyContents = gson.toJson(requestBody);
|
||||||
|
}
|
||||||
|
String contentLength = String.valueOf(requestBodyContents.length());
|
||||||
|
setHeader(conn, "Content-Length", contentLength, true);
|
||||||
|
addRequestParams(conn, request.getHeaders());
|
||||||
|
if (requestBodyContents != null) {
|
||||||
|
Streams.copy(requestBodyContents, conn.getOutputStream(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initiate the sending of the request.
|
// Initiate the sending of the request.
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user