民间,
我使用Apache CXF
(JAX-RS)' LoggingInInterceptor
和LoggingOutInterceptor
将请求和响应对象记录到我的Web服务中,也记录到记录响应时间。
为此,我扩展了这两个类,并在相应的XML文件中完成了相关配置。这样做,我能够记录请求和响应对象。
但是,我还想在这两个拦截器中记录请求URL。我能够使用以下内容获取HttpServletRequest
对象(在LoggingInInterceptor
内):
HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
然后,从请求对象我能够获取请求URL(在我的情况下是REST URL)。但是,我无法使用此代码(或通过任何其他方式)在LoggingOutInterceptor
中获取请求对象。
以下是该问题的摘要:
我需要访问LoggingOutInterceptor
中的reqeuest URI(或许使用HttpServletRequest
对象?)。
非常感谢您的帮助。
更新:添加拦截器代码。
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);
}
}
}
答案 0 :(得分:0)
更新:由于您在Out链中,您可能需要从您可以获取请求URI的位置获取In消息,因为Request URI可能为null以获取响应消息。
您可以尝试这样来获取传入消息:
Message incoming = message.getExchange().getInMessage();
然后我认为您应该能够使用以下方式获取请求URI:
String requestURI = (String) incoming.get(Message.REQUEST_URI);
或
String endpointURI = (String) incoming.get(Message.ENDPOINT_ADDRESS);
如果仍然无效,请尝试在PRE_STREAM阶段运行拦截器,如构造函数中的Phase.PRE_STREAM
。
您也可以尝试从Interceptor Chain获取消息,如下所示:
PhaseInterceptorChain chain = message.getInterceptorChain();
Message currentMessage = chain.getCurrentMessage();
HttpServletRequest req = (HttpServletRequest) currentMessage.get("HTTP.REQUEST");