Search code examples
javacxf

Apache CXF Interceptor to capture (and eventually change) the outgoing URL


I have an application running in tomcat that consumes a SOAP webservice. I need to be able to modify, via interceptor, the URI of the remote webservice in the interceptor chain.

The URI has pre-configured parameters in the endpoint that I need to strip out in my application, as a library I use sees parameters in the endpoint passed to it as switches to perform various internal functions. So I am stripping the parameters from the endpoint, passing it to the library, which in turn uses CXF to perform the outgoing connection.

I need to, via interceptor as I cannot modify the library or override the classes in the library, put those parameters back into the endpoint.

I have tried to access the headers directly in an interceptor using:

public class EndpointInterceptor extends AbstractPhaseInterceptor {
    
    private static final Logger LOGGER = LogManager.getLogger(EndpointInterceptor.class.getName());

    public EndpointInterceptor() {
        super(Phase.PRE_STREAM);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        
        String requestURI = (String) message.get(Message.class.getName() + ".REQUEST_URI");
        LOGGER.debug("requestURI: " + requestURI);
        Map<String, List<String>> headers = CastUtils.cast((Map) message.get(Message.PROTOCOL_HEADERS));
        List sa = null;
        String hostName=null;
        if (headers != null) {
            sa = headers.get("host");
            for (Map.Entry<String,List<String>> entry : headers.entrySet()) {
                LOGGER.debug("entry key: " + entry.getKey());
                for (String s : entry.getValue()) {
                    LOGGER.debug("   value: " + s);
                }
            }
        }

        if (sa != null && sa.size() == 1) {
            hostName = "https://"+ sa.get(0).toString()+requestURI;
        }
        LOGGER.debug("hostname: " + hostName);
    }   
    
}

But that only gets me the Accept and Connect headers, and only (so far) in the PRE_STREAM and further phases.

So, is this even possible, and if it is, how can I access that URI in the interceptor to at least log, and hopefully modify it.


Solution

  • I was able to find this. Just needed to use the right value from Message.java in package org.apache.cxf.message;

    public class EndpointInterceptor extends AbstractPhaseInterceptor {
    
        private static final Logger LOGGER = LogManager.getLogger(EndpointInterceptor.class.getName());
    
        public EndpointInterceptor() {
            super(Phase.POST_LOGICAL);
        }
    
        @Override
        public void handleMessage(Message message) throws Fault {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            String address = (String)message.get(Message.ENDPOINT_ADDRESS);
            address = "test.com"
            message.put(Message.ENDPOINT_ADDRESS, address);
        }                  
    }