Skip to content

Commit 301ad93

Browse files
committed
Remove JsonHttpClient.builder and add enableGZipContent to JsonHttpRequest
http://codereview.appspot.com/6297045/
1 parent 22ac075 commit 301ad93

File tree

4 files changed

+164
-75
lines changed

4 files changed

+164
-75
lines changed

google-http-client/src/main/java/com/google/api/client/http/json/JsonHttpClient.java

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
* Implementation is not thread-safe.
3838
* </p>
3939
*
40+
* <p>
41+
* Upgrade warning: prior to version 1.10 there was a {@code builder} method in
42+
* {@link JsonHttpClient}, this has been removed in version 1.10. The Builder can now be
43+
* instantiated with
44+
* {@link Builder#Builder(HttpTransport, JsonFactory, String, String, HttpRequestInitializer)}.
45+
* </p>
46+
*
4047
* @since 1.6
4148
* @author Ravi Mistry
4249
*/
@@ -240,14 +247,14 @@ protected void initialize(JsonHttpRequest jsonHttpRequest) throws IOException {
240247
* Constructor with required parameters.
241248
*
242249
* <p>
243-
* Use {@link #builder} if you need to specify any of the optional parameters.
250+
* Use {@link Builder} if you need to specify any of the optional parameters.
244251
* </p>
245252
*
246253
* @param transport The transport to use for requests
247254
* @param jsonFactory A factory for creating JSON parsers and serializers
248255
* @param baseUrl The base URL of the service. Must end with a "/"
249-
* @deprecated (scheduled to be removed in 1.11) Use
250-
* {@link #JsonHttpClient(HttpTransport, JsonFactory, String, String)}.
256+
* @deprecated (scheduled to be removed in 1.11) Use {@link #JsonHttpClient(HttpTransport,
257+
* JsonFactory, String, String, HttpRequestInitializer)}.
251258
*/
252259
@Deprecated
253260
public JsonHttpClient(HttpTransport transport, JsonFactory jsonFactory, String baseUrl) {
@@ -258,19 +265,20 @@ public JsonHttpClient(HttpTransport transport, JsonFactory jsonFactory, String b
258265
* Constructor with required parameters.
259266
*
260267
* <p>
261-
* Use {@link #builder} if you need to specify any of the optional parameters.
268+
* Use {@link Builder} if you need to specify any of the optional parameters.
262269
* </p>
263270
*
264271
* @param transport The transport to use for requests
265272
* @param jsonFactory A factory for creating JSON parsers and serializers
266273
* @param rootUrl The root URL of the service. Must end with a "/"
267274
* @param servicePath The service path of the service. Must end with a "/" and not begin with a
268275
* "/". It is allowed to be an empty string {@code ""}
276+
* @param httpRequestInitializer The HTTP request initializer or {@code null} for none
269277
* @since 1.10
270278
*/
271-
public JsonHttpClient(
272-
HttpTransport transport, JsonFactory jsonFactory, String rootUrl, String servicePath) {
273-
this(transport, null, null, jsonFactory, null, rootUrl, servicePath, null);
279+
public JsonHttpClient(HttpTransport transport, JsonFactory jsonFactory, String rootUrl,
280+
String servicePath, HttpRequestInitializer httpRequestInitializer) {
281+
this(transport, null, httpRequestInitializer, jsonFactory, null, rootUrl, servicePath, null);
274282
}
275283

276284
/**
@@ -513,36 +521,6 @@ protected InputStream executeAsInputStream(HttpMethod method, GenericUrl url, Ob
513521
return response.getContent();
514522
}
515523

516-
/**
517-
* Returns an instance of a new builder.
518-
*
519-
* @param transport The transport to use for requests
520-
* @param jsonFactory A factory for creating JSON parsers and serializers
521-
* @param baseUrl The base URL of the service. Must end with a "/"
522-
* @deprecated (scheduled to be removed in 1.11) Use
523-
* {@link #builder(HttpTransport, JsonFactory, GenericUrl, String)} instead.
524-
*/
525-
@Deprecated
526-
public static Builder builder(
527-
HttpTransport transport, JsonFactory jsonFactory, GenericUrl baseUrl) {
528-
return new Builder(transport, jsonFactory, baseUrl);
529-
}
530-
531-
/**
532-
* Returns an instance of a new builder.
533-
*
534-
* @param transport The transport to use for requests
535-
* @param jsonFactory A factory for creating JSON parsers and serializers
536-
* @param rootUrl The root URL of the service. Must end with a "/"
537-
* @param servicePath The service path of the service. Must end with a "/" and not begin with a
538-
* "/". It is allowed to be an empty string {@code ""}
539-
* @since 1.10
540-
*/
541-
public static Builder builder(
542-
HttpTransport transport, JsonFactory jsonFactory, GenericUrl rootUrl, String servicePath) {
543-
return new Builder(transport, jsonFactory, rootUrl, servicePath);
544-
}
545-
546524
/**
547525
* Builder for {@link JsonHttpClient}.
548526
*
@@ -578,7 +556,7 @@ public static class Builder {
578556
private GenericUrl baseUrl;
579557

580558
/** The root URL of the service, for example {@code "https://www.googleapis.com/"}. */
581-
private GenericUrl rootUrl;
559+
private String rootUrl;
582560

583561
/** The service path of the service, for example {@code "tasks/v1/"}. */
584562
private String servicePath;
@@ -602,8 +580,8 @@ public static class Builder {
602580
* @param jsonFactory A factory for creating JSON parsers and serializers
603581
* @param baseUrl The base URL of the service. Must end with a "/"
604582
*
605-
* @deprecated (scheduled to be removed in 1.11) Use
606-
* {@link #Builder(HttpTransport, JsonFactory, GenericUrl, String)} instead.
583+
* @deprecated (scheduled to be removed in 1.11) Use {@link #Builder(HttpTransport, JsonFactory,
584+
* String, String, HttpRequestInitializer)} instead.
607585
*/
608586
@Deprecated
609587
protected Builder(HttpTransport transport, JsonFactory jsonFactory, GenericUrl baseUrl) {
@@ -621,14 +599,17 @@ protected Builder(HttpTransport transport, JsonFactory jsonFactory, GenericUrl b
621599
* @param rootUrl The root URL of the service. Must end with a "/"
622600
* @param servicePath The service path of the service. Must end with a "/" and not begin with a
623601
* "/". It is allowed to be an empty string {@code ""}
602+
* @param httpRequestInitializer The HTTP request initializer or {@code null} for none
624603
* @since 1.10
625604
*/
626-
protected Builder(
627-
HttpTransport transport, JsonFactory jsonFactory, GenericUrl rootUrl, String servicePath) {
605+
public Builder(HttpTransport transport, JsonFactory jsonFactory, String rootUrl,
606+
String servicePath,
607+
HttpRequestInitializer httpRequestInitializer) {
628608
this.transport = transport;
629609
this.jsonFactory = jsonFactory;
630610
setRootUrl(rootUrl);
631611
setServicePath(servicePath);
612+
this.httpRequestInitializer = httpRequestInitializer;
632613
}
633614

634615
/**
@@ -652,13 +633,13 @@ public JsonHttpClient build() {
652633
httpRequestInitializer,
653634
jsonFactory,
654635
jsonObjectParser,
655-
rootUrl.build(),
636+
rootUrl,
656637
servicePath,
657638
applicationName);
658639
}
659640

660641
/**
661-
* Returns if baseUrl is used instead of {@code servicePath} and {@code rootUrl}.
642+
* Returns if {@code baseUrl} is used instead of {@code rootUrl} and {@code servicePath}.
662643
*
663644
* @since 1.10
664645
* @deprecated (scheduled to be removed in 1.11) Use {@link #getRootUrl} and
@@ -714,7 +695,8 @@ public Builder setObjectParser(JsonObjectParser parser) {
714695
* This is determined when the library is generated and normally should not be changed.
715696
*
716697
* <p>
717-
* Use this method only if {@link #builder(HttpTransport, JsonFactory, GenericUrl)} was used.
698+
* Use this method only if {@code baseUrl} is used instead of {@code rootUrl} and
699+
* {@code servicePath}.
718700
* </p>
719701
*
720702
* @deprecated (scheduled to be removed in 1.11) Use {@link #getRootUrl} and
@@ -732,7 +714,8 @@ public final GenericUrl getBaseUrl() {
732714
* and normally should not be changed. Subclasses should override by calling super.
733715
*
734716
* <p>
735-
* Use this method only if {@link #builder(HttpTransport, JsonFactory, GenericUrl)} was used.
717+
* Use this method only if {@code baseUrl} is used instead of {@code rootUrl} and
718+
* {@code servicePath}.
736719
* </p>
737720
*
738721
* @since 1.7
@@ -752,15 +735,15 @@ public Builder setBaseUrl(GenericUrl baseUrl) {
752735
* URL-encoded and must end with a "/".
753736
*
754737
* <p>
755-
* Use this method only if {@link #builder(HttpTransport, JsonFactory, GenericUrl, String)} was
756-
* used.
738+
* Use this method only if {@code rootUrl} and {@code servicePath} are used instead of
739+
* {@code baseUrl}.
757740
* </p>
758741
*
759742
* @since 1.10
760743
*/
761744
public final String getRootUrl() {
762745
Preconditions.checkArgument(!baseUrlUsed);
763-
return rootUrl.build();
746+
return rootUrl;
764747
}
765748

766749
/**
@@ -769,16 +752,16 @@ public final String getRootUrl() {
769752
* normally should be changed. Subclasses should override by calling super.
770753
*
771754
* <p>
772-
* Use this method only if {@link #builder(HttpTransport, JsonFactory, GenericUrl, String)} was
773-
* used.
755+
* Use this method only if {@code rootUrl} and {@code servicePath} are used instead of
756+
* {@code baseUrl}.
774757
* </p>
775758
*
776759
* @since 1.10
777760
*/
778-
public Builder setRootUrl(GenericUrl rootUrl) {
761+
public Builder setRootUrl(String rootUrl) {
779762
Preconditions.checkArgument(!baseUrlUsed);
780763
Preconditions.checkNotNull(rootUrl);
781-
Preconditions.checkArgument(rootUrl.build().endsWith("/"));
764+
Preconditions.checkArgument(rootUrl.endsWith("/"));
782765
this.rootUrl = rootUrl;
783766
return this;
784767
}
@@ -790,8 +773,8 @@ public Builder setRootUrl(GenericUrl rootUrl) {
790773
* {@code ""}.
791774
*
792775
* <p>
793-
* Use this method only if {@link #builder(HttpTransport, JsonFactory, GenericUrl, String)} was
794-
* used.
776+
* Use this method only if {@code rootUrl} and {@code servicePath} are used instead of
777+
* {@code baseUrl}.
795778
* </p>
796779
*
797780
* @since 1.10
@@ -808,8 +791,8 @@ public final String getServicePath() {
808791
* changed. Subclasses should override by calling super.
809792
*
810793
* <p>
811-
* Use this method only if {@link #builder(HttpTransport, JsonFactory, GenericUrl, String)} was
812-
* used.
794+
* Use this method only if {@code rootUrl} and {@code servicePath} are used instead of
795+
* {@code baseUrl}.
813796
* </p>
814797
*
815798
* @since 1.10

google-http-client/src/main/java/com/google/api/client/http/json/JsonHttpRequest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class JsonHttpRequest extends GenericData {
5151
/** The HTTP headers of the last response or {@code null} for none. */
5252
private HttpHeaders lastResponseHeaders;
5353

54+
/** Whether to enable GZip compression of HTTP content. */
55+
private boolean enableGZipContent = true;
56+
5457
/**
5558
* Builds an instance of {@link JsonHttpRequest}.
5659
*
@@ -70,6 +73,30 @@ public JsonHttpRequest(JsonHttpClient client, HttpMethod method, String uriTempl
7073
this.content = content;
7174
}
7275

76+
/**
77+
* Sets whether to enable GZip compression of HTTP content. Subclasses should override by
78+
* calling super.
79+
*
80+
* <p>
81+
* By default it is {@code true}.
82+
* </p>
83+
*
84+
* @since 1.10
85+
*/
86+
public JsonHttpRequest setEnableGZipContent(boolean enableGZipContent) {
87+
this.enableGZipContent = enableGZipContent;
88+
return this;
89+
}
90+
91+
/**
92+
* Returns whether to enable GZip compression of HTTP content.
93+
*
94+
* @since 1.10
95+
*/
96+
public final boolean getEnableGZipContent() {
97+
return enableGZipContent;
98+
}
99+
73100
/** Returns the HTTP Method type. */
74101
public final HttpMethod getMethod() {
75102
return method;
@@ -176,6 +203,7 @@ public HttpRequest buildHttpRequest() throws IOException {
176203
*/
177204
public HttpResponse executeUnparsed() throws IOException {
178205
HttpRequest request = buildHttpRequest();
206+
request.setEnableGZipContent(enableGZipContent);
179207
HttpResponse response = client.executeUnparsed(request);
180208
lastResponseHeaders = response.getHeaders();
181209
return response;

google-http-client/src/test/java/com/google/api/client/http/json/JsonHttpClientTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void testJsonHttpClientBuilderWithBaseUrl() {
6565
String applicationName = "Test Application";
6666

6767
JsonHttpClient.Builder builder =
68-
JsonHttpClient.builder(transport, jsonFactory, baseUrl)
68+
new JsonHttpClient.Builder(transport, jsonFactory, baseUrl)
6969
.setJsonHttpRequestInitializer(jsonHttpRequestInitializer)
7070
.setApplicationName(applicationName);
7171
try {
@@ -87,7 +87,7 @@ public void testJsonHttpClientBuilderWithBaseUrl() {
8787
// expected
8888
}
8989
try {
90-
builder.setRootUrl(new GenericUrl("http://www.test.com/"));
90+
builder.setRootUrl("http://www.test.com/");
9191
fail("Expected " + IllegalArgumentException.class);
9292
} catch (IllegalArgumentException e) {
9393
// expected
@@ -117,13 +117,13 @@ public void testJsonHttpClientBuilderWithBaseUrl() {
117117
public void testJsonHttpClientBuilderWithRootUrlAndServicePath() {
118118
HttpTransport transport = new NetHttpTransport();
119119
JsonFactory jsonFactory = new JacksonFactory();
120-
GenericUrl rootUrl = new GenericUrl("http://www.testgoogleapis.com/");
120+
String rootUrl = "http://www.testgoogleapis.com/";
121121
String servicePath = "test/path/v1/";
122122
JsonHttpRequestInitializer jsonHttpRequestInitializer = new TestRemoteRequestInitializer();
123123
String applicationName = "Test Application";
124124

125-
JsonHttpClient.Builder builder = JsonHttpClient.builder(
126-
transport, jsonFactory, rootUrl, servicePath)
125+
JsonHttpClient.Builder builder = new JsonHttpClient.Builder(
126+
transport, jsonFactory, rootUrl, servicePath, null)
127127
.setJsonHttpRequestInitializer(jsonHttpRequestInitializer)
128128
.setApplicationName(applicationName);
129129
try {
@@ -133,14 +133,14 @@ public void testJsonHttpClientBuilderWithRootUrlAndServicePath() {
133133
// expected
134134
}
135135
try {
136-
builder.setBaseUrl(rootUrl);
136+
builder.setBaseUrl(new GenericUrl(rootUrl));
137137
fail("Expected " + IllegalArgumentException.class);
138138
} catch (IllegalArgumentException e) {
139139
// expected
140140
}
141141
try {
142142
// With no "/" at the end.
143-
builder.setRootUrl(new GenericUrl("http://www.testgoogleapis.com"));
143+
builder.setRootUrl("http://www.testgoogleapis.com");
144144
fail("Expected " + IllegalArgumentException.class);
145145
} catch (IllegalArgumentException e) {
146146
//expected
@@ -164,19 +164,19 @@ public void testJsonHttpClientBuilderWithRootUrlAndServicePath() {
164164

165165
JsonHttpClient client = builder.build();
166166

167-
assertEquals(rootUrl.build(), client.getRootUrl());
167+
assertEquals(rootUrl, client.getRootUrl());
168168
assertEquals("", client.getServicePath());
169169
assertEquals(applicationName, client.getApplicationName());
170170
assertEquals(jsonFactory, client.getJsonFactory());
171171
assertEquals(jsonHttpRequestInitializer, client.getJsonHttpRequestInitializer());
172-
assertEquals(rootUrl.build(), client.getBaseUrl());
172+
assertEquals(rootUrl, client.getBaseUrl());
173173
}
174174

175175
@SuppressWarnings("deprecation")
176176
public void testBaseServerAndBasePathBuilder() {
177177
JsonHttpClient client =
178-
JsonHttpClient
179-
.builder(new NetHttpTransport(), new JacksonFactory(),
178+
new JsonHttpClient
179+
.Builder(new NetHttpTransport(), new JacksonFactory(),
180180
new GenericUrl("http://www.testgoogleapis.com/test/path/v1/"))
181181
.setBaseUrl(new GenericUrl("http://www.googleapis.com/test/path/v2/"))
182182
.build();
@@ -187,7 +187,7 @@ public void testBaseServerAndBasePathBuilder() {
187187
@SuppressWarnings("deprecation")
188188
public void testInitializeWithBaseUrl() throws IOException {
189189
TestRemoteRequestInitializer remoteRequestInitializer = new TestRemoteRequestInitializer();
190-
JsonHttpClient client = JsonHttpClient.builder(
190+
JsonHttpClient client = new JsonHttpClient.Builder(
191191
new NetHttpTransport(), new JacksonFactory(), new GenericUrl("http://www.test.com/"))
192192
.setJsonHttpRequestInitializer(remoteRequestInitializer)
193193
.setApplicationName("Test Application").build();
@@ -197,9 +197,9 @@ public void testInitializeWithBaseUrl() throws IOException {
197197

198198
public void testInitializeWithRootUrl() throws IOException {
199199
TestRemoteRequestInitializer remoteRequestInitializer = new TestRemoteRequestInitializer();
200-
JsonHttpClient client = JsonHttpClient.builder(
201-
new NetHttpTransport(), new JacksonFactory(), new GenericUrl("http://www.test.com/"),
202-
"test/").setJsonHttpRequestInitializer(remoteRequestInitializer)
200+
JsonHttpClient client = new JsonHttpClient.Builder(
201+
new NetHttpTransport(), new JacksonFactory(), "http://www.test.com/", "test/",
202+
null).setJsonHttpRequestInitializer(remoteRequestInitializer)
203203
.setApplicationName("Test Application").build();
204204
client.initialize(null);
205205
assertTrue(remoteRequestInitializer.isCalled);
@@ -224,7 +224,7 @@ public LowLevelHttpResponse execute() {
224224
}
225225
};
226226
JsonHttpClient client =
227-
JsonHttpClient.builder(
227+
new JsonHttpClient.Builder(
228228
transport, new JacksonFactory(), new GenericUrl(testBaseUrl)).build();
229229
client.executeUnparsed(HttpMethod.GET, new GenericUrl(testBaseUrl + testUriTemplate), null);
230230
}
@@ -247,8 +247,8 @@ public LowLevelHttpResponse execute() {
247247
};
248248
}
249249
};
250-
JsonHttpClient client = JsonHttpClient.builder(
251-
transport, new JacksonFactory(), new GenericUrl(testRootUrl), testServicePath).build();
250+
JsonHttpClient client = new JsonHttpClient.Builder(
251+
transport, new JacksonFactory(), testRootUrl, testServicePath, null).build();
252252
client.executeUnparsed(
253253
HttpMethod.GET, new GenericUrl(testRootUrl + testServicePath + testUriTemplate), null);
254254
}

0 commit comments

Comments
 (0)