Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RESTEASY-1075] HTTP redirect support in ApacheHttpClient engine #1305

Merged
merged 3 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ApacheHttpClient4Engine implements ClientHttpEngine
protected int responseBufferSize = 8192;
protected HttpHost defaultProxy = null;
protected boolean chunked = false;
protected boolean followRedirects = false;

/**
* For uploading File's over JAX-RS framework, this property, together with {@link #fileUploadMemoryUnit},
Expand Down Expand Up @@ -432,15 +433,6 @@ public String getMethod()
}
}

protected boolean isRedirectRequired(final ClientInvocation request, final HttpRequestBase httpMethod)
{
if (httpMethod instanceof HttpGet && false) // todo && request.followRedirects())
{
return true;
}
return false;
}

@SuppressWarnings("deprecation")
protected HttpClient createDefaultHttpClient()
{
Expand Down Expand Up @@ -468,7 +460,7 @@ protected void setRedirectNotRequired(final ClientInvocation request, HttpReques

protected void loadHttpMethod(final ClientInvocation request, HttpRequestBase httpMethod) throws Exception
{
if (isRedirectRequired(request,httpMethod)) // todo && request.followRedirects())
if (isFollowRedirects())
{
setRedirectRequired(request,httpMethod);
}
Expand Down Expand Up @@ -553,6 +545,16 @@ public void setChunked(boolean chunked)
{
this.chunked = chunked;
}

public boolean isFollowRedirects()
{
return followRedirects;
}

public void setFollowRedirects(boolean followRedirects)
{
this.followRedirects = followRedirects;
}

/**
* If passed httpMethod is of type HttpPost then obtain its entity. If the entity has an enclosing File then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.jboss.resteasy.test.client;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

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.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine;
import org.jboss.resteasy.test.client.resource.TestResource;
import org.jboss.resteasy.utils.PortProviderUtil;
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;

/**
* @tpSubChapter Resteasy-client
* @tpChapter Integration tests
* @tpSince RESTEasy 4.0.0
*/
@RunWith(Arquillian.class)
@RunAsClient
public class RedirectTest extends ClientTestBase
{
@Deployment
public static Archive<?> deploy()
{
WebArchive war = TestUtil.prepareArchive(RedirectTest.class.getSimpleName());
war.addClasses(PortProviderUtil.class);
return TestUtil.finishContainerPrepare(war, null, TestResource.class);
}

@Test
public void testRedirect()
{
ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine();
engine.setFollowRedirects(true);
Client client = new ResteasyClientBuilder().httpEngine(engine).build();
try
{
Response response = client.target(generateURL("/redirect/" + RedirectTest.class.getSimpleName())).request()
.get();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("OK", response.readEntity(String.class));
response.close();
}
finally
{
client.close();
}
}

@Test
public void testPostRedirect()
{
ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine();
engine.setFollowRedirects(true);
Client client = new ResteasyClientBuilder().httpEngine(engine).build();
try
{
Response response = client.target(generateURL("/post-redirect")).request()
.post(Entity.entity(RedirectTest.class.getSimpleName(), "text/plain"));
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("OK", response.readEntity(String.class));
response.close();
}
finally
{
client.close();
}
}

@Test
public void testNoRedirect()
{
Client client = ClientBuilder.newClient();
try
{
Response response = client.target(generateURL("/redirect/" + RedirectTest.class.getSimpleName())).request()
.get();
Assert.assertEquals(307, response.getStatus());
response.close();
}
finally
{
client.close();
}
}

@Test
public void testNoPostRedirect()
{
Client client = ClientBuilder.newClient();
try
{
Response response = client.target(generateURL("/post-redirect")).request()
.post(Entity.entity(RedirectTest.class.getSimpleName(), "text/plain"));
Assert.assertEquals(303, response.getStatus());
response.close();
}
finally
{
client.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.utils.PortProviderUtil;

@Path("")
public class TestResource
{
Expand Down Expand Up @@ -96,4 +99,25 @@ public Response methodEntity(String s)
{
return Response.ok(s).build();
}

@Path("redirect/{p}")
@GET
public Response redirect(@PathParam("p") String p)
{
return Response.temporaryRedirect(PortProviderUtil.createURI("/redirected", p)).build();
}

@Path("redirected")
@GET
public Response redirected()
{
return Response.ok("OK").build();
}

@Path("post-redirect")
@POST
public Response postRedirect(String p)
{
return Response.seeOther(PortProviderUtil.createURI("/redirected", p)).build();
}
}