Skip to content

Commit

Permalink
CXF-8478: fixing jaxrs.ee.rs.container.requestcontext setRequestUriTw…
Browse files Browse the repository at this point in the history
…oUrisTest (#953)
  • Loading branch information
reta committed Jun 7, 2022
1 parent 96e7123 commit e32d69c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
Expand Up @@ -28,6 +28,7 @@
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;

import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.DelegatingInputStream;
import org.apache.cxf.jaxrs.utils.ExceptionUtils;
Expand All @@ -38,6 +39,7 @@ public class ContainerRequestContextImpl extends AbstractRequestContextImpl
implements ContainerRequestContext {

private static final String ENDPOINT_ADDRESS_PROPERTY = "org.apache.cxf.transport.endpoint.address";
private static final String ENDPOINT_URI_PROPERTY = "org.apache.cxf.transport.endpoint.uri";

private boolean preMatch;
public ContainerRequestContextImpl(Message message, boolean preMatch, boolean responseContext) {
Expand Down Expand Up @@ -108,7 +110,15 @@ public void setRequestUri(URI requestUri) throws IllegalStateException {
String baseUriString = new UriInfoImpl(m).getBaseUri().toString();
String requestUriString = requestUri.toString();
if (!requestUriString.startsWith(baseUriString)) {
setRequestUri(requestUri, URI.create("/"));
String path = requestUri.getRawPath();
if (StringUtils.isEmpty(path)) {
path = "/";
}
String query = requestUri.getRawQuery();
if (!StringUtils.isEmpty(query)) {
path = path + "?" + query;
}
setRequestUri(requestUri.resolve("/"), URI.create(path));
return;
}
requestUriString = requestUriString.substring(baseUriString.length());
Expand Down Expand Up @@ -139,7 +149,14 @@ public void setRequestUri(URI baseUri, URI requestUri) throws IllegalStateExcept
if (servletRequest != null) {
((javax.servlet.http.HttpServletRequest)servletRequest)
.setAttribute(ENDPOINT_ADDRESS_PROPERTY, baseUri.toString());

// The base URI and request URI should be treated differently
if (requestUri.isAbsolute() && baseUri.resolve("/").compareTo(requestUri.resolve("/")) != 0) {
((javax.servlet.http.HttpServletRequest)servletRequest)
.setAttribute(ENDPOINT_URI_PROPERTY, requestUri.resolve("/"));
}
}

}

@Override
Expand Down
Expand Up @@ -219,7 +219,7 @@ private String doGetPath(boolean decode, boolean addSlash) {
}

private String getAbsolutePathAsString() {
String address = getBaseUri().toString();
String address = URI.create(HttpUtils.getEndpointUri(message)).toString();
if (MessageUtils.isRequestor(message)) {
return address;
}
Expand Down
Expand Up @@ -485,6 +485,20 @@ public static String getBaseAddress(Message m) {
}
}

public static String getEndpointUri(Message m) {
final Object servletRequest = m.get(AbstractHTTPDestination.HTTP_REQUEST);

if (servletRequest != null) {
final Object property = ((javax.servlet.http.HttpServletRequest)servletRequest)
.getAttribute("org.apache.cxf.transport.endpoint.uri");
if (property != null) {
return property.toString();
}
}

return getEndpointAddress(m);
}

public static String getEndpointAddress(Message m) {
String address;
Destination d = m.getExchange().getDestination();
Expand Down
Expand Up @@ -147,6 +147,8 @@ public void filter(ContainerRequestContext context) throws IOException {

if ("wrongpath".equals(path)) {
context.setRequestUri(URI.create("/bookstore/bookheaders/simple"));
} else if ("absolutepath".equals(path)) {
context.setRequestUri(URI.create("http://xx.yy:888/bookstore/bookheaders/simple?q=1"));
} else if ("throwException".equals(path)) {
context.setProperty("filterexception", "prematch");
throw new InternalServerErrorException(
Expand Down
Expand Up @@ -355,6 +355,12 @@ public void testGetBookWrongPathAsync() throws Exception {
String address = "http://localhost:" + PORT + "/wrongpath";
doTestGetBookAsync(address, false);
}

@Test
public void testGetBookAbsolutePathAsync() throws Exception {
String address = "http://localhost:" + PORT + "/absolutepath";
doTestGetBookAsync(address, false);
}

@Test
public void testPostCollectionGenericEntity() throws Exception {
Expand Down

0 comments on commit e32d69c

Please sign in to comment.