Skip to content

Commit

Permalink
setRequestUri fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
patriot1burke committed Sep 12, 2014
1 parent 797eb73 commit 218dbc9
Show file tree
Hide file tree
Showing 9 changed files with 534 additions and 437 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@

<chapter id="Migration_from_older_versions">
<title>Migration from older versions</title>
<sect1>
<title>Migrating from 3.0.7 to 3.0.9</title>
<itemizedlist>
<listitem>
ContainerRequestContext.setRequestUri() method behavior has been changed to match the behavior of the JAXRS RI.
The relative URI must be equal to are an extension of the base URI or it will not work. i.e.
<programlisting>
context.setRequestUri(URI.create("https://foo.com/base"),
URI.create("https://foo.com/base/path")); // legal
context.setRequestUri(URI.create("https://foo.com/base"),
URI.create("/path")); // "path" is ignored
// if base uri is "http://foo.com/base"
context.setRequestUri(URI.create("http://foo.com/base/path")); // legal
context.setRequestUri(URI.create("/path")); // ignored
</programlisting>
</listitem>
</itemizedlist>
</sect1>
<sect1>
<title>Migrating from 3.0.6 to 3.0.7</title>
<itemizedlist>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
Expand All @@ -35,8 +36,11 @@ public class SetRequestUriTest
@Path("resource")
public static class Resource {

@Context
protected UriInfo uriInfo;

@GET
@Path("setrequesturi1uri")
@Path("setrequesturi1/uri")
public String setRequestUri() {
return "OK";
}
Expand All @@ -46,6 +50,12 @@ public String setRequestUri() {
public String setRequestUriDidNotChangeUri() {
return "Filter did not change the uri to go to";
}

@GET
@Path("change")
public String changeProtocol() {
return uriInfo.getAbsolutePath().toString();
}
}

@Provider
Expand All @@ -56,16 +66,18 @@ public static class RequestFilter implements ContainerRequestFilter
@Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
URI uri = null;
try {
UriInfo info = requestContext.getUriInfo();
String path = new StringBuilder().append(info.getAbsolutePath())
.append("uri").toString();
uri = new java.net.URI(path);
} catch (URISyntaxException e) {
throw new IOException(e);
if ("https".equalsIgnoreCase(requestContext.getHeaderString("X-Forwarded-Proto")))
{
requestContext.setRequestUri(
requestContext.getUriInfo().getBaseUriBuilder().scheme("https").build(),
requestContext.getUriInfo().getRequestUriBuilder().scheme("https").build());
}
else
{
requestContext.setRequestUri(
requestContext.getUriInfo().getRequestUriBuilder().path("uri").build());
}
requestContext.setRequestUri(uri);


}
}
Expand Down Expand Up @@ -94,6 +106,16 @@ public static void after() throws Exception
client.close();
}

@Test
public void testSchemaChange() {
String uri = generateURL("/base/resource/change");
String httpsUri = uri.replace("http://", "https://");
Response response = client.target(uri).request().header("X-Forwarded-Proto", "https").get();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(httpsUri, response.readEntity(String.class));

}

@Test
public void testResolve()
{
Expand Down
67 changes: 48 additions & 19 deletions .../resteasy-jaxrs/src/main/java/org/jboss/resteasy/core/AcceptHeaderByFileSuffixFilter.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -46,38 +47,65 @@ public void setLanguageMappings(Map<String, String> languageMappings)
@Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
List<PathSegment> segments = null;
if (mediaTypeMappings != null || languageMappings != null)
{
segments = process(requestContext, segments);
}
if (segments == null)
if (mediaTypeMappings == null && languageMappings == null)
{
return;
}

StringBuilder preprocessedPath = new StringBuilder();
for (PathSegment pathSegment : segments)
URI uri = requestContext.getUriInfo().getRequestUri();
String rawPath = uri.getRawPath();
int lastSegment = rawPath.lastIndexOf('/');
if (lastSegment < 0)
{
preprocessedPath.append("/").append(pathSegment.getPath());
lastSegment = 0;
}
int index = rawPath.indexOf('.', lastSegment);
if (index < 0)
{
return;
}
if (! requestContext.getUriInfo().getQueryParameters().isEmpty())

boolean preprocessed = false;

String extension = rawPath.substring(index + 1);
String[] extensions = extension.split("\\.");

StringBuilder rebuilt = new StringBuilder();
for (String ext : extensions)
{
char sep = '?';
for (Map.Entry<String, List<String>> entry : requestContext.getUriInfo().getQueryParameters(false).entrySet()) {
for (String value : entry.getValue()) {
preprocessedPath.append(sep);
sep = '&';
preprocessedPath.append(entry.getKey()).append('=').append(value);
if (mediaTypeMappings != null)
{
String match = mediaTypeMappings.get(ext);
if (match != null)
{
requestContext.getHeaders().addFirst(HttpHeaders.ACCEPT, match);
preprocessed = true;
continue;
}
}
if (languageMappings != null)
{
String match = languageMappings.get(ext);
if (match != null)
{
requestContext.getHeaders().add(HttpHeaders.ACCEPT_LANGUAGE, match);
preprocessed = true;
continue;
}
}
rebuilt.append(".").append(ext);
}
URI requestUri = URI.create(preprocessedPath.toString());
requestContext.setRequestUri(requestUri);

if (!preprocessed) return;

rawPath = rawPath.substring(0, index) + rebuilt.toString();

URI newUri = requestContext.getUriInfo().getBaseUriBuilder().replacePath(rawPath).replaceQuery(uri.getRawQuery()).build();
requestContext.setRequestUri(newUri);

}

private List<PathSegment> process(ContainerRequestContext in, List<PathSegment> segments)
private List<PathSegment> process(ContainerRequestContext in)
{
String path = in.getUriInfo().getPath(false);
int lastSegment = path.lastIndexOf('/');
Expand Down Expand Up @@ -121,6 +149,7 @@ private List<PathSegment> process(ContainerRequestContext in, List<PathSegment>
}
rebuilt.append(".").append(ext);
}
List<PathSegment> segments = null;
if (preprocessed)
{
segments = PathSegmentImpl.parseSegments(rebuilt.toString(), false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.jboss.resteasy.plugins.server;

import org.jboss.resteasy.core.SynchronousDispatcher;
import org.jboss.resteasy.plugins.providers.FormUrlEncodedProvider;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.ResteasyUriInfo;
import org.jboss.resteasy.util.Encode;

Expand Down Expand Up @@ -37,7 +35,6 @@ public ResteasyUriInfo getUri()
}



public MultivaluedMap<String, String> getFormParameters()
{
if (formParameters != null) return formParameters;
Expand Down Expand Up @@ -76,18 +73,18 @@ public boolean isInitial()
return true;
}

@Override
public void setRequestUri(URI requestUri) throws IllegalStateException
{
uri = uri.setRequestUri(requestUri);
}

@Override
public void setRequestUri(URI baseUri, URI requestUri) throws IllegalStateException
{
uri = new ResteasyUriInfo(baseUri.resolve(requestUri));
}
@Override
public void setRequestUri(URI requestUri) throws IllegalStateException
{
uri.setRequestUri(requestUri);
}

@Override
public void setRequestUri(URI baseUri, URI requestUri) throws IllegalStateException
{
uri.setUri(baseUri, requestUri);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract public class ConfigurationBootstrap implements ResteasyConfiguration
private ResteasyDeployment deployment = new ResteasyDeployment();


public ResteasyDeployment createDeployment()
public ResteasyDeployment createDeployment()
{
String loggerTypeString = getParameter("resteasy.logger.type");
if (loggerTypeString != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import org.jboss.resteasy.logging.Logger;
import org.jboss.resteasy.plugins.providers.FormUrlEncodedProvider;
import org.jboss.resteasy.plugins.server.BaseHttpRequest;
import org.jboss.resteasy.specimpl.ResteasyHttpHeaders;
import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.specimpl.ResteasyHttpHeaders;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.ResteasyAsynchronousContext;
import org.jboss.resteasy.spi.ResteasyUriInfo;
Expand All @@ -22,7 +21,6 @@
import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
Expand Down
Loading

0 comments on commit 218dbc9

Please sign in to comment.