2929 */
3030package com .google .api .gax .httpjson ;
3131
32- import com .google .api .client .http .EmptyContent ;
3332import com .google .api .client .http .GenericUrl ;
3433import com .google .api .client .http .HttpContent ;
35- import com .google .api .client .http .HttpMediaType ;
3634import com .google .api .client .http .HttpMethods ;
3735import com .google .api .client .http .HttpRequest ;
3836import com .google .api .client .http .HttpRequestFactory ;
3937import com .google .api .client .http .HttpResponse ;
4038import com .google .api .client .http .HttpResponseException ;
4139import com .google .api .client .http .HttpTransport ;
42- import com .google .api .client .http .json .JsonHttpContent ;
4340import com .google .api .client .json .JsonFactory ;
4441import com .google .api .client .json .JsonObjectParser ;
4542import com .google .api .client .json .gson .GsonFactory ;
46- import com .google .api .client .util .GenericData ;
4743import com .google .api .gax .tracing .ApiTracer ;
4844import com .google .auth .Credentials ;
4945import com .google .auth .http .HttpCredentialsAdapter ;
5046import com .google .auto .value .AutoValue ;
51- import com .google .common .base .Strings ;
5247import java .io .ByteArrayInputStream ;
5348import java .io .IOException ;
5449import java .io .InputStream ;
@@ -154,8 +149,6 @@ public void run() {
154149 }
155150
156151 HttpRequest createHttpRequest () throws IOException {
157- GenericData tokenRequest = new GenericData ();
158-
159152 HttpRequestFormatter <RequestT > requestFormatter = methodDescriptor .getRequestFormatter ();
160153
161154 HttpRequestFactory requestFactory ;
@@ -166,20 +159,8 @@ HttpRequest createHttpRequest() throws IOException {
166159 requestFactory = httpTransport .createRequestFactory ();
167160 }
168161
169- JsonFactory jsonFactory = GsonFactory .getDefaultInstance ();
170162 // Create HTTP request body.
171- String requestBody = requestFormatter .getRequestBody (request );
172- HttpContent jsonHttpContent ;
173- if (!Strings .isNullOrEmpty (requestBody )) {
174- jsonFactory .createJsonParser (requestBody ).parse (tokenRequest );
175- jsonHttpContent =
176- new JsonHttpContent (jsonFactory , tokenRequest )
177- .setMediaType ((new HttpMediaType ("application/json; charset=utf-8" )));
178- } else {
179- // Force underlying HTTP lib to set Content-Length header to avoid 411s.
180- // See EmptyContent.java.
181- jsonHttpContent = new EmptyContent ();
182- }
163+ HttpContent httpContent = requestFormatter .getHttpContent (request );
183164
184165 // Populate URL path and query parameters.
185166 String normalizedEndpoint = normalizeEndpoint (endpoint );
@@ -196,20 +177,21 @@ HttpRequest createHttpRequest() throws IOException {
196177 tracer .requestUrlResolved (url .build ());
197178 }
198179
199- HttpRequest httpRequest = buildRequest (requestFactory , url , jsonHttpContent );
180+ HttpRequest httpRequest = buildRequest (requestFactory , url , httpContent );
200181
201182 for (Map .Entry <String , Object > entry : headers .getHeaders ().entrySet ()) {
202183 HttpHeadersUtils .setHeader (
203184 httpRequest .getHeaders (), entry .getKey (), (String ) entry .getValue ());
204185 }
205186
187+ JsonFactory jsonFactory = GsonFactory .getDefaultInstance ();
206188 httpRequest .setParser (new JsonObjectParser (jsonFactory ));
207189
208190 return httpRequest ;
209191 }
210192
211193 private HttpRequest buildRequest (
212- HttpRequestFactory requestFactory , GenericUrl url , HttpContent jsonHttpContent )
194+ HttpRequestFactory requestFactory , GenericUrl url , HttpContent httpContent )
213195 throws IOException {
214196 // A workaround to support PATCH request. This assumes support of "X-HTTP-Method-Override"
215197 // header on the server side, which GCP services usually do.
@@ -235,7 +217,7 @@ private HttpRequest buildRequest(
235217 if (HttpMethods .PATCH .equals (actualHttpMethod )) {
236218 actualHttpMethod = HttpMethods .POST ;
237219 }
238- HttpRequest httpRequest = requestFactory .buildRequest (actualHttpMethod , url , jsonHttpContent );
220+ HttpRequest httpRequest = requestFactory .buildRequest (actualHttpMethod , url , httpContent );
239221 if (originalHttpMethod != null && !originalHttpMethod .equals (actualHttpMethod )) {
240222 HttpHeadersUtils .setHeader (
241223 httpRequest .getHeaders (), "X-HTTP-Method-Override" , originalHttpMethod );
0 commit comments