Skip to content

Commit

Permalink
[RESTEASY-1669] Deprecating ApacheHttClient*Engine constructors allow…
Browse files Browse the repository at this point in the history
…ing to specify a HttpContext instance and explaining potential issues with that approach in the javadoc; added a new HttpContextProvider abstraction to allow users controlling the HttpContext lifecycle.
  • Loading branch information
asoldano committed Jul 5, 2017
1 parent e21edf3 commit b40eeee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,28 @@ public ApacheHttpClient43Engine(final HttpClient httpClient, final boolean close
super(httpClient, closeHttpClient);
}

/**
* Creates a client engine instance using the specified {@link org.apache.http.client.HttpClient}
* and {@link org.apache.http.protocol.HttpContext} instances.
* Note that the same instance of httpContext is passed to the engine, which may store thread unsafe
* attributes in it. It is hence recommended to override the HttpClient
* <pre>execute(HttpUriRequest request, HttpContext context)</pre> method to perform a deep
* copy of the context before executing the request.
*
* @param httpClient The http client
* @param httpContext The context to be used for executing requests
*/
@Deprecated
public ApacheHttpClient43Engine(final HttpClient httpClient, final HttpContext httpContext)
{
super(httpClient, httpContext);
}

public ApacheHttpClient43Engine(HttpClient httpClient, HttpContextProvider httpContextProvider)
{
this.httpClient = httpClient;
this.httpContextProvider = httpContextProvider;
}

@Override
protected HttpClient createDefaultHttpClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class ApacheHttpClient4Engine implements ClientHttpEngine
protected HttpClient httpClient;
protected boolean createdHttpClient;
protected HttpContext httpContext;
protected HttpContextProvider httpContextProvider;
protected boolean closed;
protected SSLContext sslContext;
protected HostnameVerifier hostnameVerifier;
Expand Down Expand Up @@ -146,12 +147,30 @@ public ApacheHttpClient4Engine(HttpClient httpClient, boolean closeHttpClient)
}


/**
* Creates a client engine instance using the specified {@link org.apache.http.client.HttpClient}
* and {@link org.apache.http.protocol.HttpContext} instances.
* Note that the same instance of httpContext is passed to the engine, which may store thread unsafe
* attributes in it. It is hence recommended to override the HttpClient
* <pre>execute(HttpUriRequest request, HttpContext context)</pre> method to perform a deep
* copy of the context before executing the request.
*
* @param httpClient The http client
* @param httpContext The context to be used for executing requests
*/
@Deprecated
public ApacheHttpClient4Engine(HttpClient httpClient, HttpContext httpContext)
{
this.httpClient = httpClient;
this.httpContext = httpContext;
}

public ApacheHttpClient4Engine(HttpClient httpClient, HttpContextProvider httpContextProvider)
{
this.httpClient = httpClient;
this.httpContextProvider = httpContextProvider;
}

/**
* Response stream is wrapped in a BufferedInputStream. Default is 8192. Value of 0 will not wrap it.
* Value of -1 will use a SelfExpandingBufferedInputStream
Expand Down Expand Up @@ -213,11 +232,13 @@ public HttpClient getHttpClient()
return httpClient;
}

@Deprecated
public HttpContext getHttpContext()
{
return httpContext;
}

@Deprecated
public void setHttpContext(HttpContext httpContext)
{
this.httpContext = httpContext;
Expand Down Expand Up @@ -275,7 +296,6 @@ protected InputStream createBufferedStream(InputStream is)
return new BufferedInputStream(is, responseBufferSize);
}

@SuppressWarnings("unchecked")
public ClientResponse invoke(ClientInvocation request)
{
String uri = request.getUri().toString();
Expand All @@ -285,7 +305,12 @@ public ClientResponse invoke(ClientInvocation request)
{
loadHttpMethod(request, httpMethod);

res = httpClient.execute(httpMethod, httpContext);
HttpContext ctx = httpContext;
if (ctx == null && httpContextProvider != null)
{
ctx = httpContextProvider.getContext();
}
res = httpClient.execute(httpMethod, ctx);
}
catch (Exception e)
{
Expand Down Expand Up @@ -653,9 +678,6 @@ private void handleFileNotDeletedError(File tempRequestFile, Exception ex)
tempRequestFile.deleteOnExit();
}




/**
* We use {@link org.apache.http.entity.FileEntity} as the {@link HttpEntity} implementation when the request OutputStream has been
* saved to a File on disk (because it was too large to fit into memory see however, we have to delete
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jboss.resteasy.client.jaxrs.engines;

import org.apache.http.protocol.HttpContext;

public interface HttpContextProvider
{
HttpContext getContext();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.Configurable;
import org.apache.http.protocol.HttpContext;
import org.jboss.resteasy.client.jaxrs.ClientHttpEngine;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine;
Expand Down

0 comments on commit b40eeee

Please sign in to comment.