Search code examples
javajax-rscxfhttp-redirect

In Apache CXF JAX-RS, how to turn on AUTO_REDIRECT_ALLOW_REL_URI flag?


I am using Apache CXF JAX-RS 3.1.11 WebClient. I am trying to get it to automatically follow redirects.

This is my code (a bit simplified):

final WebClient client = WebClient.create(address);
final HTTPConduit httpConduit = WebClient.getConfig(client).getHttpConduit();
httpConduit.getClient().setAutoRedirect(true);
return client
        .path("/api")
        .accept(MediaType.APPLICATION_JSON)
        .post(Entity.entity(requestPacket.toString(), MediaType.APPLICATION_JSON), String.class);

With this code I get the exception:

Caused by: java.io.IOException: Relative Redirect detected on Conduit '{http://1.2.3.4:5678}WebClient.http-conduit' on '//1.2.3.4:5678/v1/api'
at org.apache.cxf.transport.http.HTTPConduit.convertToAbsoluteUrlIfNeeded(HTTPConduit.java:1857)
at org.apache.cxf.transport.http.HTTPConduit.access$300(HTTPConduit.java:149)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.redirectRetransmit(HTTPConduit.java:1464)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.processRetransmit(HTTPConduit.java:1443)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleRetransmits(HTTPConduit.java:1420)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1554)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1356)
... 30 more

Now, looking at the source of the HTTPConduit class, I can see a property to set to fix this:

 private static final String AUTO_REDIRECT_ALLOW_REL_URI = "http.redirect.relative.uri";

But what I can't work out, is how do I set that property using the CXF API? How do I change my above code to turn this property on? There doesn't seem to be any methods on HTTPConduit or HTTPClientPolicy to turn this setting on.


Solution

  • You have to put it in the request context:

    WebClient.getConfig(client).getRequestContext().put("http.redirect.relative.uri", "true");