Skip to content
Advertisement

Using google-http-client and google-http-client-apache-v2 behind a proxy produces NonRepeatableRequestException

I’m using google-http-client and google-http-client-apache-v2 libraries to make a POST request behind a proxy.

// 1.- Setting ssl and proxy
HttpClientBuilder builder = HttpClientBuilder.create();
            
SSLContext sslContext = SslUtils.getTlsSslContext();
SslUtils.initSslContext(sslContext, GoogleUtils.getCertificateTrustStore(), SslUtils.getPkixTrustManagerFactory());
builder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext));
            
builder.setProxy(new HttpHost(host, port));
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(user, pass));
builder.setDefaultCredentialsProvider(credentialsProvider);

// 2.- Build request
HttpTransport httpTransport = new ApacheHttpTransport(builder.build());
HttpRequestFactory factory = httpTransport.createRequestFactory(credential);

HttpContent httpContent = new ByteArrayContent("application/json", "{}")
HttpRequest request = factory.buildRequest("POST", new GenericUrl(url), httpContent);

// 3.- Execute request
HttpResponse httpResponse = request.execute();

That request produces a NonRepeatableRequestException:

org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.13.jar!/:4.5.13]
    at com.google.api.client.http.apache.v2.ApacheHttpRequest.execute(ApacheHttpRequest.java:73) ~[google-http-client-apache-v2-1.39.2.jar!/:?]
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012) ~[google-http-client-1.39.2.jar!/:1.39.2]
    at 
    ...
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity.
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) ~[httpclient-4.5.13.jar!/:4.5.13]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.13.jar!/:4.5.13]
    at com.google.api.client.http.apache.v2.ApacheHttpRequest.execute(ApacheHttpRequest.java:73) ~[google-http-client-apache-v2-1.39.2.jar!/:?]
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012) ~[google-http-client-1.39.2.jar!/:1.39.2]

        

It seems like ApacheHttpRequest wraps ByteArrayContent that is repeatable (see JavaDoc) inside a ContentEntity that is non-repeatable.

enter image description here

Debuging execution inside google libraries, proxy is returning “407 Proxy Authentication Required”, then it tries to repeat the request (guess that including the credentials) and that exception arises because ContentEntity used by google library is non-repeatable.

Is there any way to avoid handshake with proxy including credentials in first request to avoid reuse of the entity?

Is there any way to tell google libraries that uses a repeatable entity?

Tryed with follwing library versions:

  • google-api-client-1.31.5
  • google-http-client-jackson2-1.39.2
  • google-oauth-client-1.31.5
  • google-http-client-apache-v2-1.39.2
  • google-http-client-1.39.2
  • httpclient-4.5.13
  • httpcore-4.4.14

Advertisement

Answer

Workaround I posted on github in case it helps someone:

As workaround what I’m trying is to wrap ApacheHttpTransport in CustomApacheHttpTransport, which delegate the result of methods to ApacheHttpTransport except for buildRequest method.

This buildRequest method in CustomApacheHttpTransport builds a custom request of type CustomApacheHttpRequest.

public final class CustomApacheHttpTransport extends HttpTransport {
    
    private ApacheHttpTransport apacheHttpTransport;
    
    public CustomApacheHttpTransport (HttpClient httpClient) {
        this.apacheHttpTransport = new ApacheHttpTransport(httpClient);
    }
    
    @Override
    protected LowLevelHttpRequest buildRequest (String method, String url) {
        HttpRequestBase requestBase;
        if (method.equals("DELETE")) {
            requestBase = new HttpDelete(url);
        } else if (method.equals("GET")) {
            requestBase = new HttpGet(url);
        } else if (method.equals("HEAD")) {
            requestBase = new HttpHead(url);
        } else if (method.equals("PATCH")) {
            requestBase = new HttpPatch(url);
        } else if (method.equals("POST")) {
            ..
        }
        return new CustomApacheHttpRequest(apacheHttpTransport.getHttpClient(), requestBase);
    }
}

This custom request is like ApacheHttpRequest except for when it is executed it creates a custom entity, CustomContentEntity, which will be repeatable depending on whether the request content supports retries.

final class CustomApacheHttpRequest extends LowLevelHttpRequest {
    
    private final HttpClient httpClient;
    private final HttpRequestBase request;
    private RequestConfig.Builder requestConfig;
    
    CustomApacheHttpRequest (HttpClient httpClient, HttpRequestBase request) {
        this.httpClient = httpClient;
        this.request = request;
        this.requestConfig = RequestConfig.custom().setRedirectsEnabled(false).setNormalizeUri(false).setStaleConnectionCheckEnabled(false);
    }
    
    ...
        
    @Override
    public LowLevelHttpResponse execute () throws IOException {
        if (this.getStreamingContent() != null) {
            Preconditions.checkState(request instanceof HttpEntityEnclosingRequest, "Apache HTTP client does not support %s requests with content.", request.getRequestLine().getMethod());
            
            CustomContentEntity entity = new CustomContentEntity(this.getContentLength(), this.getStreamingContent());
            entity.setContentEncoding(this.getContentEncoding());
            entity.setContentType(this.getContentType());
            if (this.getContentLength() == -1L) {
                entity.setChunked(true);
            }
            ((HttpEntityEnclosingRequest) request).setEntity(entity);
        }
        
        request.setConfig(requestConfig.build());
        return new CustomApacheHttpResponse(request, httpClient.execute(request));
    }
}

The key in CustomContentEntity is isRepeatable method wich do not returns always false as ContentEntity does.

final class CustomContentEntity extends AbstractHttpEntity {
    
    private final long contentLength;
    private final StreamingContent streamingContent;
    
    CustomContentEntity (long contentLength, StreamingContent streamingContent) {
        this.contentLength = contentLength;
        this.streamingContent = streamingContent;
    }
    
    @Override
    public boolean isRepeatable () {
        return ((HttpContent) streamingContent).retrySupported();
    }
    ...
}

Also I have to create CustomApacheHttpResponse as response for CustomApacheHttpRequest because ApacheHttpResponse is package-private (CustomApacheHttpResponse is exactly like ApacheHttpResponse).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement