[英]Apache CXF | JAX RS LoggingOutInterceptor - Access HttpServletRequest object
伙計們,
我正在使用Apache CXF
(JAX-RS)的LoggingInInterceptor
和LoggingOutInterceptor
將請求和響應對象記錄到我的Web服務中,並且還記錄了響應時間。 為此,我擴展了這兩個類,並在適當的XML文件中進行了相關的配置。 這樣做,我能夠記錄請求和響應對象。 但是,我也想在這兩個攔截器中記錄請求URL。 我可以使用以下方法獲取HttpServletRequest
對象(在LoggingInInterceptor
):
HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
然后,從請求對象中,我可以獲取請求URL(在我的情況下為REST URL)。 但是,我無法使用此代碼(或通過任何其他方式)在LoggingOutInterceptor
獲取請求對象。
這是問題的摘要:
我需要訪問LoggingOutInterceptor
內部的請求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);
}
}
}
更新:由於您位於傳出鏈中,因此您可能需要從獲取請求URI的位置獲取傳入消息,因為對於傳出響應消息,請求URI可能為空。
您可以這樣嘗試獲取傳入消息:
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
在構造函數。
您也可以嘗試從攔截器鏈獲取消息,如下所示:
PhaseInterceptorChain chain = message.getInterceptorChain();
Message currentMessage = chain.getCurrentMessage();
HttpServletRequest req = (HttpServletRequest) currentMessage.get("HTTP.REQUEST");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.