Apache CXF | JAX RS LoggingOutinerCeptor-访问HttpServletRequest对象
各位 我正在使用ApacheApache CXF | JAX RS LoggingOutinerCeptor-访问HttpServletRequest对象,apache,web-services,logging,jax-rs,cxf,Apache,Web Services,Logging,Jax Rs,Cxf,各位 我正在使用ApacheCXF(JAX-RS)loggininterceptor和logginoutinterceptor将请求和响应对象记录到我的web服务中,并记录响应时间。 为此,我扩展了这两个类,并在适当的XML文件中进行了相关配置。这样,我就能够记录请求和响应对象。 但是,我还想在这两个拦截器中记录请求URL。我能够使用以下方法获取HttpServletRequest对象(在loggininterceptor中): HttpServletRequest request = (Htt
CXF
(JAX-RS)loggininterceptor
和logginoutinterceptor
将请求和响应对象记录到我的web服务中,并记录响应时间。
为此,我扩展了这两个类,并在适当的XML文件中进行了相关配置。这样,我就能够记录请求和响应对象。
但是,我还想在这两个拦截器中记录请求URL。我能够使用以下方法获取HttpServletRequest
对象(在loggininterceptor
中):
HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
然后,从请求对象中,我能够获得请求URL(在我的例子中是RESTURL)。但是,我无法使用此代码(或通过任何其他方式)在LoggingOutiterCeptor
中获取请求对象
以下是问题的摘要:
我需要访问LoggingOutiterCeptor
中的RequeTestURI(可能使用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);
}
}
}
更新:由于您处于Out链中,您可能需要获取in消息,从中可以获取请求URI,因为对于Out-Out响应消息,请求URI可能为null 您可以这样尝试获取传入消息:
Message incoming = message.getExchange().getInMessage();
然后,我认为您应该能够使用以下方法获取请求URI:
String requestURI = (String) incoming.get(Message.REQUEST_URI);
或
如果这仍然不起作用,请尝试在构造函数中的PRE_STREAM阶段运行拦截器,如阶段.PRE_STREAM
您还可以尝试从拦截器链获取消息,如下所示:
PhaseInterceptorChain chain = message.getInterceptorChain();
Message currentMessage = chain.getCurrentMessage();
HttpServletRequest req = (HttpServletRequest) currentMessage.get("HTTP.REQUEST");
更新:由于您处于Out链中,您可能需要获取in消息,从中可以获取请求URI,因为对于Out-Out响应消息,请求URI可能为null 您可以这样尝试获取传入消息:
Message incoming = message.getExchange().getInMessage();
然后,我认为您应该能够使用以下方法获取请求URI:
String requestURI = (String) incoming.get(Message.REQUEST_URI);
或
如果这仍然不起作用,请尝试在构造函数中的PRE_STREAM阶段运行拦截器,如阶段.PRE_STREAM
您还可以尝试从拦截器链获取消息,如下所示:
PhaseInterceptorChain chain = message.getInterceptorChain();
Message currentMessage = chain.getCurrentMessage();
HttpServletRequest req = (HttpServletRequest) currentMessage.get("HTTP.REQUEST");
您是否无法使用
message.get(message.ENDPOINT\u地址)获取信息
或message.get(message.REQUEST\u URI)代码>?“我想你应该去。”加里:是的,我试过这些。它们都解析为null。您是否无法使用message.get(message.ENDPOINT\u ADDRESS)获取
或message.get(message.REQUEST\u URI)代码>?“我想你应该去。”加里:是的,我试过这些。它们都解析为null。在构造函数中添加Phase.PRE_流没有帮助。使用PhaseInterceptorChain获取HttpSevletrequest也会产生一个空结果。@va1b4av。。将你的截取代码添加到原始帖子中。@va1b4av。。请检查,我已经更新了答案。@va1b4av。。只是想知道…你能通过吗?在构造函数中添加Phase.PRE_流没有帮助。使用PhaseInterceptorChain获取HttpSevletrequest也会产生一个空结果。@va1b4av。。将你的截取代码添加到原始帖子中。@va1b4av。。请检查,我已经更新了答案。@va1b4av。。只是想知道…你能通过吗?