Skip to content

Commit fd61d44

Browse files
author
Yaniv Inbar
committed
[Issue 130] Ability to set zero-length content on an HTTP GET request
[Issue 168] New method HttpResponse.disconnect() [Issue 169] Streaming mode for HTTP request content for HttpURLConnection http://codereview.appspot.com/4354051/
1 parent 196a969 commit fd61d44

File tree

9 files changed

+68
-14
lines changed

9 files changed

+68
-14
lines changed

google-api-client-extensions/src/main/java/com/google/api/client/extensions/appengine/http/urlfetch/UrlFetchRequest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ public LowLevelHttpResponse execute() throws IOException {
7474
if (contentLength >= 0) {
7575
addHeader("Content-Length", Long.toString(contentLength));
7676
}
77-
ByteArrayOutputStream out = new ByteArrayOutputStream();
78-
content.writeTo(out);
79-
request.setPayload(out.toByteArray());
77+
if (contentLength != 0) {
78+
ByteArrayOutputStream out = new ByteArrayOutputStream();
79+
content.writeTo(out);
80+
request.setPayload(out.toByteArray());
81+
}
8082
}
8183
// connect
8284
URLFetchService service = URLFetchServiceFactory.getURLFetchService();

google-api-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ public InputStream getContent() throws IOException {
224224
}
225225

226226
/**
227-
* Gets the content of the HTTP response from {@link #getContent()} and ignores the content if
228-
* there is any.
229-
*
230-
* @throws IOException I/O exception
227+
* Closes the the content of the HTTP response from {@link #getContent()}, ignoring any content.
231228
*/
232229
public void ignore() throws IOException {
233230
InputStream content = getContent();
@@ -236,6 +233,15 @@ public void ignore() throws IOException {
236233
}
237234
}
238235

236+
/**
237+
* Disconnect using {@link LowLevelHttpResponse#disconnect()}.
238+
*
239+
* @since 1.4
240+
*/
241+
public void disconnect() throws IOException {
242+
response.disconnect();
243+
}
244+
239245
/**
240246
* Returns the HTTP response content parser to use for the content type of this HTTP response or
241247
* {@code null} for none.

google-api-client/src/main/java/com/google/api/client/http/LowLevelHttpResponse.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,14 @@ public abstract class LowLevelHttpResponse {
6868

6969
/** Returns the HTTP response header value at the given zero-based index. */
7070
public abstract String getHeaderValue(int index);
71+
72+
/**
73+
* Default implementation does nothing, but subclasses may override to attempt to abort the
74+
* connection or release allocated system resources for this connection.
75+
*
76+
* @throws IOException I/O exception
77+
* @since 1.4
78+
*/
79+
public void disconnect() throws IOException {
80+
}
7181
}

google-api-client/src/main/java/com/google/api/client/http/apache/ApacheHttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
5555

5656
@Override
5757
public LowLevelHttpResponse execute() throws IOException {
58-
return new ApacheHttpResponse(httpClient.execute(request));
58+
return new ApacheHttpResponse(request, httpClient.execute(request));
5959
}
6060

6161
@Override

google-api-client/src/main/java/com/google/api/client/http/apache/ApacheHttpResponse.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818

1919
import org.apache.http.Header;
2020
import org.apache.http.HttpEntity;
21+
import org.apache.http.HttpResponse;
2122
import org.apache.http.StatusLine;
23+
import org.apache.http.client.methods.HttpRequestBase;
2224

2325
import java.io.IOException;
2426
import java.io.InputStream;
2527

2628
final class ApacheHttpResponse extends LowLevelHttpResponse {
2729

28-
private final org.apache.http.HttpResponse response;
30+
private final HttpRequestBase request;
31+
private final HttpResponse response;
2932
private final Header[] allHeaders;
3033

31-
ApacheHttpResponse(org.apache.http.HttpResponse response) {
34+
ApacheHttpResponse(HttpRequestBase request, HttpResponse response) {
35+
this.request = request;
3236
this.response = response;
3337
allHeaders = response.getAllHeaders();
3438
}
@@ -105,4 +109,14 @@ public String getHeaderName(int index) {
105109
public String getHeaderValue(int index) {
106110
return allHeaders[index].getValue();
107111
}
112+
113+
/**
114+
* Aborts execution of the request.
115+
*
116+
* @since 1.4
117+
*/
118+
@Override
119+
public void disconnect() {
120+
request.abort();
121+
}
108122
}

google-api-client/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public ApacheHttpRequest buildPutRequest(String url) {
135135
/**
136136
* Shuts down the connection manager and releases allocated resources. This includes closing all
137137
* connections, whether they are currently used or not.
138+
*
139+
* @since 1.4
138140
*/
139141
@Override
140142
public void shutdown() {

google-api-client/src/main/java/com/google/api/client/http/apache/ContentEntity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public boolean isStreaming() {
5252
}
5353

5454
public void writeTo(OutputStream out) throws IOException {
55-
content.writeTo(out);
55+
if (contentLength != 0) {
56+
content.writeTo(out);
57+
}
5658
}
5759
}

google-api-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public void setTimeout(int connectTimeout, int readTimeout) {
5353
public LowLevelHttpResponse execute() throws IOException {
5454
HttpURLConnection connection = this.connection;
5555
// write content
56-
HttpContent content = this.content;
5756
if (content != null) {
58-
connection.setDoOutput(true);
5957
String contentType = content.getType();
6058
if (contentType != null) {
6159
addHeader("Content-Type", contentType);
@@ -68,7 +66,17 @@ public LowLevelHttpResponse execute() throws IOException {
6866
if (contentLength >= 0) {
6967
addHeader("Content-Length", Long.toString(contentLength));
7068
}
71-
content.writeTo(connection.getOutputStream());
69+
if (contentLength != 0) {
70+
// setDoOutput(true) will change a GET method to POST, so only if contentLength != 0
71+
connection.setDoOutput(true);
72+
// see http://developer.android.com/reference/java/net/HttpURLConnection.html
73+
if (contentLength >= 0 && contentLength <= Integer.MAX_VALUE) {
74+
connection.setFixedLengthStreamingMode((int) contentLength);
75+
} else {
76+
connection.setChunkedStreamingMode(0);
77+
}
78+
content.writeTo(connection.getOutputStream());
79+
}
7280
}
7381
// connect
7482
connection.connect();

google-api-client/src/main/java/com/google/api/client/http/javanet/NetHttpResponse.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,14 @@ public String getHeaderName(int index) {
104104
public String getHeaderValue(int index) {
105105
return headerValues.get(index);
106106
}
107+
108+
/**
109+
* Closes the connection to the HTTP server.
110+
*
111+
* @since 1.4
112+
*/
113+
@Override
114+
public void disconnect() {
115+
connection.disconnect();
116+
}
107117
}

0 commit comments

Comments
 (0)