简体   繁体   English

Apache CXF | JAX RS LoggingOutInterceptor-访问HttpServletRequest对象

[英]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. 我正在使用Apache CXF (JAX-RS)的LoggingInInterceptorLoggingOutInterceptor将请求和响应对象记录到我的Web服务中,并且还记录了响应时间。 For this, I have extended both these classes and done relevant configuration in the appropriate XML files. 为此,我扩展了这两个类,并在适当的XML文件中进行了相关的配置。 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. 但是,我也想在这两个拦截器中记录请求URL。 I was able to get the HttpServletRequest object (Inside the LoggingInInterceptor ) using the following: 我可以使用以下方法获取HttpServletRequest对象(在LoggingInInterceptor ):

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). 然后,从请求对象中,我可以获取请求URL(在我的情况下为REST URL)。 I was however, not able to get the request object in the LoggingOutInterceptor using this code (or by any other means). 但是,我无法使用此代码(或通过任何其他方式)在LoggingOutInterceptor获取请求对象。

Here is a summary of the issue: 这是问题的摘要:

I need to access the reqeuest URI inside the LoggingOutInterceptor (using HttpServletRequest object perhaps?). 我需要访问LoggingOutInterceptor内部的请求URI(也许使用HttpServletRequest对象?)。

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. 更新:由于您位于传出链中,因此您可能需要从获取请求URI的位置获取传入消息,因为对于传出响应消息,请求URI可能为空。

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: 然后,我认为您应该可以使用以下方法获取请求URI:

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. 如果这仍然没有工作,尝试运行PRE_STREAM相类似拦截Phase.PRE_STREAM在构造函数。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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