简体   繁体   中英

Apache CXF | JAX RS LoggingOutInterceptor - Access HttpServletRequest object

Folks,

I'm using Apache CXF (JAX-RS)'s LoggingInInterceptor and LoggingOutInterceptor to log the request and response objects to my web service and also to log the response time. For this, I have extended both these classes and done relevant configuration in the appropriate XML files. Doing this, I was able to log the request and response object. However, I also want to log the request URL in both these interceptors. I was able to get the HttpServletRequest object (Inside the LoggingInInterceptor ) using the following:

HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);

Then, from the request object I was able to get the request URL (REST URL in my case). I was however, not able to get the request object in the LoggingOutInterceptor using this code (or by any other means).

Here is a summary of the issue:

I need to access the reqeuest URI inside the LoggingOutInterceptor (using HttpServletRequest object perhaps?).

Would appreciate any help on this.

Update: Adding the interceptor code.

public class StorefrontRestInboundInterceptor extends LoggingInInterceptor {

    /**
     * constructor.
     */
    public StorefrontRestInboundInterceptor() {
        super();
    }

    @Override
    public void handleMessage(final Message message) throws Fault {
        HttpServletRequest httpRequest = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
        if (isLoggingRequired()) {
            String requestUrl = (String) message.getExchange().get("requestUrl");
            Date requestTime = timeService.getCurrentTime();
            LOG.info("Performance Monitor started for session id:" + customerSession.getGuid());
            LOG.info(httpRequest.getRequestURI() + " Start time for SessionID " + customerSession.getGuid() + ": "
                + requestTime.toString());
        }
        try {
            InputStream inputStream = message.getContent(InputStream.class);
            CachedOutputStream outputStream = new CachedOutputStream();
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            message.setContent(InputStream.class, outputStream.getInputStream());
            LOG.info("Request object for " + httpRequest.getRequestURI() + " :" +  outputStream.getInputStream());
            inputStream.close();
            outputStream.close();
        } catch (Exception ex) {
            LOG.info("Error occured reading the input stream for " + httpRequest.getRequestURI());
        }
    }


public class StorefrontRestOutboundInterceptor extends LoggingOutInterceptor {

    /**
     * logger implementation.
     */
    protected static final Logger LOG = Logger.getLogger(StorefrontRestOutboundInterceptor.class);

    /**
     * constructor.
     */
    public StorefrontRestOutboundInterceptor() {
        super(Phase.PRE_STREAM);
    }

    @Override
    public void handleMessage(final Message message) throws Fault {
        if (isLoggingRequired()) {
            LOG.info(requestUrl + " End time for SessionID " + customerGuid + ": " + (timeService.getCurrentTime().getTime() - requestTime)
                    + " milliseconds taken.");
            LOG.info("Performance Monitor ends for session id:" + customerGuid);
        }
        OutputStream out = message.getContent(OutputStream.class);
        final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(out);
        message.setContent(OutputStream.class, newOut);
        newOut.registerCallback(new LoggingCallback(requestUrl));
    }

    public class LoggingCallback implements CachedOutputStreamCallback {

        private final String requestUrl;

        /**
         * 
         * @param requestUrl requestUrl.
         */
        public LoggingCallback(final String requestUrl) {
            this.requestUrl = requestUrl;
        }

        /**
         * @param cos CachedOutputStream.
         */
        public void onFlush(final CachedOutputStream cos) { //NOPMD

        }

        /**
         * @param cos CachedOutputStream.
         */
        public void onClose(final CachedOutputStream cos) {
            try {
                StringBuilder builder = new StringBuilder();
                cos.writeCacheTo(builder, limit);
                LOG.info("Request object for " + requestUrl + " :" + builder.toString());
            } catch (Exception e) {
                LOG.info("Error occured writing the response object for " + requestUrl);
            }
        }
    }

Update:Since you are in Out chain you may need to get the In message from where you can get the request URI since the Request URI may null for out going response message.

You may try like this to get the Incoming message:

Message incoming = message.getExchange().getInMessage();

Then I think you should be able to get the Request URI using:

String requestURI = (String) incoming.get(Message.REQUEST_URI);

or

String endpointURI = (String) incoming.get(Message.ENDPOINT_ADDRESS);

If this is still not working, try to run the interceptor in PRE_STREAM phase like Phase.PRE_STREAM in your constructor.

You can also try to get the message from Interceptor Chain like this:

PhaseInterceptorChain chain = message.getInterceptorChain(); 
Message currentMessage = chain.getCurrentMessage();
HttpServletRequest req = (HttpServletRequest) currentMessage.get("HTTP.REQUEST");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM