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 ;
43- import com .google .api .client .json .JsonFactory ;
4440import com .google .api .client .json .JsonObjectParser ;
4541import com .google .api .client .json .gson .GsonFactory ;
46- import com .google .api .client .util .GenericData ;
4742import com .google .api .gax .tracing .ApiTracer ;
4843import com .google .auth .Credentials ;
4944import com .google .auth .http .HttpCredentialsAdapter ;
5045import com .google .auto .value .AutoValue ;
51- import com .google .common .base .Strings ;
5246import java .io .ByteArrayInputStream ;
5347import java .io .IOException ;
5448import java .io .InputStream ;
@@ -154,8 +148,6 @@ public void run() {
154148 }
155149
156150 HttpRequest createHttpRequest () throws IOException {
157- GenericData tokenRequest = new GenericData ();
158-
159151 HttpRequestFormatter <RequestT > requestFormatter = methodDescriptor .getRequestFormatter ();
160152
161153 HttpRequestFactory requestFactory ;
@@ -166,24 +158,18 @@ HttpRequest createHttpRequest() throws IOException {
166158 requestFactory = httpTransport .createRequestFactory ();
167159 }
168160
169- JsonFactory jsonFactory = GsonFactory .getDefaultInstance ();
170161 // 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- }
162+ HttpContent httpContent = requestFormatter .getHttpContent (request );
183163
184164 // Populate URL path and query parameters.
185- String normalizedEndpoint = normalizeEndpoint (endpoint );
186- GenericUrl url = new GenericUrl (normalizedEndpoint + requestFormatter .getPath (request ));
165+ String path = requestFormatter .getPath (request );
166+ GenericUrl url ;
167+ if (path .startsWith ("http://" ) || path .startsWith ("https://" )) {
168+ url = new GenericUrl (path );
169+ } else {
170+ String normalizedEndpoint = normalizeEndpoint (endpoint );
171+ url = new GenericUrl (normalizedEndpoint + path );
172+ }
187173 Map <String , List <String >> queryParams = requestFormatter .getQueryParamNames (request );
188174 for (Entry <String , List <String >> queryParam : queryParams .entrySet ()) {
189175 if (queryParam .getValue () != null ) {
@@ -196,20 +182,20 @@ HttpRequest createHttpRequest() throws IOException {
196182 tracer .requestUrlResolved (url .build ());
197183 }
198184
199- HttpRequest httpRequest = buildRequest (requestFactory , url , jsonHttpContent );
185+ HttpRequest httpRequest = buildRequest (requestFactory , url , httpContent );
200186
201187 for (Map .Entry <String , Object > entry : headers .getHeaders ().entrySet ()) {
202188 HttpHeadersUtils .setHeader (
203189 httpRequest .getHeaders (), entry .getKey (), (String ) entry .getValue ());
204190 }
205191
206- httpRequest .setParser (new JsonObjectParser (jsonFactory ));
192+ httpRequest .setParser (new JsonObjectParser (GsonFactory . getDefaultInstance () ));
207193
208194 return httpRequest ;
209195 }
210196
211197 private HttpRequest buildRequest (
212- HttpRequestFactory requestFactory , GenericUrl url , HttpContent jsonHttpContent )
198+ HttpRequestFactory requestFactory , GenericUrl url , HttpContent httpContent )
213199 throws IOException {
214200 // A workaround to support PATCH request. This assumes support of "X-HTTP-Method-Override"
215201 // header on the server side, which GCP services usually do.
@@ -235,7 +221,7 @@ private HttpRequest buildRequest(
235221 if (HttpMethods .PATCH .equals (actualHttpMethod )) {
236222 actualHttpMethod = HttpMethods .POST ;
237223 }
238- HttpRequest httpRequest = requestFactory .buildRequest (actualHttpMethod , url , jsonHttpContent );
224+ HttpRequest httpRequest = requestFactory .buildRequest (actualHttpMethod , url , httpContent );
239225 if (originalHttpMethod != null && !originalHttpMethod .equals (actualHttpMethod )) {
240226 HttpHeadersUtils .setHeader (
241227 httpRequest .getHeaders (), "X-HTTP-Method-Override" , originalHttpMethod );
0 commit comments