Skip to content

Commit

Permalink
RESTEASY-2324: RESTEasy client should disable Apache Http Client cook…
Browse files Browse the repository at this point in the history
…ie management feature by default (#2135)

* Disable cookie management by default

Signed-off-by: NicoNes <nicolas.nesmon@gmail.com>

* Fix tests

Signed-off-by: NicoNes <nicolas.nesmon@gmail.com>

* - Use ClientBuilder and ResteasyClientBuilder cast instead of ResteasyClientBuilderImpl.
- Enrich userguide

Signed-off-by: NicoNes <nicolas.nesmon@gmail.com>
  • Loading branch information
NicoNes authored and asoldano committed Sep 20, 2019
1 parent d3e703d commit 2edbeaa
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 8 deletions.
10 changes: 10 additions & 0 deletions docbook/reference/en/en-US/modules/RESTEasy_Client_Framework.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@
supports chunked mode. See Section <link linkend='transport_layer'>Apache HTTP Client 4.x and other backends</link>
for more information.
</para>

<note>
<para>
To follow REST principles and not introduce no state management in applications, <classname>javax.ws.rs.client.Client</classname> instance does not provide support for cookie management by default.
However you can enable it if necessary using <classname>ResteasyClientBuilder</classname>:
</para>
<programlisting>
Client client = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).cookieManagementEnabled(true).build()
</programlisting>
</note>
</section>
<para>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ public enum HostnameVerificationPolicy {
*/
public abstract ResteasyClientBuilder defaultProxy(String hostname, int port, String scheme);

/**
* Enable state (cookie) management.
*
* @param cookieManagementEnabled
* @return the updated client builder instance
*/
public abstract ResteasyClientBuilder cookieManagementEnabled(boolean cookieManagementEnabled);

public abstract boolean isCookieManagementEnabled();

public abstract SSLContext getSSLContext();

public abstract KeyStore getKeyStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,31 @@ protected ClientHttpEngine createEngine(final HttpClientConnectionManager cm, fi
rcBuilder.setProxy(defaultProxy);
if (System.getSecurityManager() == null)
{
httpClient = HttpClientBuilder.create()
HttpClientBuilder httpClientBuilder= HttpClientBuilder.create()
.setConnectionManager(cm)
.setDefaultRequestConfig(rcBuilder.build())
.disableContentCompression().build();
.disableContentCompression();
if (!that.isCookieManagementEnabled())
{
httpClientBuilder.disableCookieManagement();
}
httpClient = httpClientBuilder.build();
}
else {
httpClient = AccessController.doPrivileged(new PrivilegedAction<HttpClient>()
{
@Override
public HttpClient run()
{
return HttpClientBuilder.create()
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
.setConnectionManager(cm)
.setDefaultRequestConfig(rcBuilder.build())
.disableContentCompression().build();
.disableContentCompression();
if (!that.isCookieManagementEnabled())
{
httpClientBuilder.disableCookieManagement();
}
return httpClientBuilder.build();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class ResteasyClientBuilderImpl extends ResteasyClientBuilder
protected int responseBufferSize;
protected List<String> sniHostNames = new ArrayList<>();
protected boolean trustSelfSignedCertificates = true;
protected boolean cookieManagementEnabled;

/**
* Changing the providerFactory will wipe clean any registered components or properties.
Expand Down Expand Up @@ -655,4 +656,17 @@ public HostnameVerifier getHostnameVerifier()
return verifier;
}

@Override
public ResteasyClientBuilder cookieManagementEnabled(boolean cookieManagementEnabled)
{
this.cookieManagementEnabled = cookieManagementEnabled;
return this;
}

@Override
public boolean isCookieManagementEnabled()
{
return cookieManagementEnabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.jboss.resteasy.test.cdi.util.Counter;
import org.jboss.resteasy.test.cdi.util.PersistenceUnitProducer;
import org.jboss.resteasy.test.cdi.util.UtilityProducer;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.spi.HttpResponseCodes;
import org.jboss.resteasy.utils.PermissionUtil;
import org.jboss.resteasy.utils.PortProviderUtil;
Expand Down Expand Up @@ -276,7 +277,7 @@ public void testEJBs() throws Exception {
public void testSessionScope() throws Exception {
log.info("starting testSessionScope()");
client.close();
client = ClientBuilder.newClient();
client = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).cookieManagementEnabled(true).build();

// Need to supply each ClientRequest with a single ClientExecutor to maintain a single
// cookie cache, which keeps the session alive.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.jboss.resteasy.test.client;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;

/**
* @author Nicolas NESMON
* @tpSubChapter Resteasy-client
* @tpChapter Integration tests
* @tpTestCaseDetails Test for cookie management support in Resteasy client.
* @tpSince RESTEasy
*
*/
@RunWith(Arquillian.class)
@RunAsClient
public class ClientCookieTest extends ClientTestBase
{

@Path("/")
@Produces(MediaType.TEXT_PLAIN)
public static class ClienCookieResource
{

@GET
@Path("createCookie")
public Response createCookie()
{
return Response.ok().cookie(new NewCookie("Cookie", "CookieValue")).build();
}

@GET
@Path("getCookiesCount")
public Response getCookiesCount(@Context HttpHeaders httpHeaders)
{
return Response.ok(httpHeaders.getCookies().size()).build();
}

}

@Deployment
public static Archive<?> deploy()
{
WebArchive war = TestUtil.prepareArchive(ClientCookieTest.class.getSimpleName());
war.addClass(ClientTestBase.class);
return TestUtil.finishContainerPrepare(war, null, ClienCookieResource.class);
}

@Test
public void client_Should_NotStoreCookie_When_NotConfigured()
{
Client client = ClientBuilder.newClient();
try
{

try (Response response = client.target(generateURL("/createCookie")).request(MediaType.TEXT_PLAIN_TYPE).get())
{
NewCookie cookie = response.getCookies().get("Cookie");
Assert.assertNotNull(cookie);
}

int cookiesCount = client.target(generateURL("/getCookiesCount")).request(MediaType.TEXT_PLAIN_TYPE)
.get(Integer.class);
Assert.assertEquals(0, cookiesCount);

}
finally
{
client.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.logging.Logger;
import org.jboss.resteasy.category.NotForForwardCompatibility;
import org.jboss.resteasy.client.jaxrs.ProxyBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.test.resource.param.resource.CookieInjectionResource;
import org.jboss.resteasy.spi.HttpResponseCodes;
import org.jboss.resteasy.utils.PortProviderUtil;
Expand Down Expand Up @@ -39,7 +39,6 @@
@RunAsClient
public class CookieInjectionTest {

private final Logger logger = Logger.getLogger(CookieInjectionTest.class);
static Client client;

public interface CookieProxy {
Expand Down Expand Up @@ -68,7 +67,7 @@ public static Archive<?> deploy() throws Exception {

@BeforeClass
public static void init() {
client = ClientBuilder.newClient();
client = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).cookieManagementEnabled(true).build();
}

@AfterClass
Expand Down

0 comments on commit 2edbeaa

Please sign in to comment.