我正在开发一个日志框架。日志框架是使用cxf和javax.ws.rs.core.Response编写的,我使用spring mvc编写了一个拦截器。 我正在尝试将HttpServletResponse发送到javax.ws.rs.core.Response,因为HttpServletResponse没有getStatus()方法,如何将状态设置为javax.ws.rs.core.Response。
package com.logging;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.LocalDateTime;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@Component
public class RequestLoggingInterceptor extends HandlerInterceptorAdapter {
private static final Log logger =
LogFactory.getLog(RequestConversationPersister.class); private static final
String HEADER_DEVICEID = "deviceid";
private static final String HEADER_DEVICEID_NOTPROVIDED_VALUE =
"DEVICEID_NOTPROVIDED";
private static final String HEADER_POLICYNUMBER = "policynumber";
private static final String HEADER_POLICYNUMBER_NOTPROVIDED_VALUE =
"POLICYNUMBER_NOTPROVIDED";
private static final String HEADER_USERID = "userid";
private static final String HEADER_USERID_NOTPROVIDED_VALUE =
"USERID_NOTPROVIDED";
private static final String INSERT_STATEMENT = "Insert Into LOGNMSWS
(CONVERSATION_ID, DEVICE_ID, REQUEST_URL, RESPONSE_STATUS, DATETIME,
POLICYNUMBER, USERID) Values (?, ?, ?, ?, ?, ?, ?)";
protected static ThreadLocal<RequestGendroidServiceConversation>
conversation = new ThreadLocal<RequestGendroidServiceConversation>();
// A spring abstraction of for JDBC, it handles exceptions and opening
//and
// closing connections
RequestConversationContainer container;
JdbcTemplate jdbcTemplate;
public RequestLoggingInterceptor( DataSource dataSource,
RequestConversationContainer container) {
if ((dataSource == null) || (container == null)) {
throw new IllegalArgumentException("Neither dataSource or
container may be null");
}
jdbcTemplate = new JdbcTemplate(dataSource);
this.container = container;
}
public RequestLoggingInterceptor() {
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) throws Exception {
String deviceid = request.getHeader(HEADER_DEVICEID) != null ?
request.getHeader(HEADER_DEVICEID) : HEADER_DEVICEID_NOTPROVIDED_VALUE;
String policynumber = request.getHeader(HEADER_POLICYNUMBER) != null
? request.getHeader(HEADER_POLICYNUMBER) :
HEADER_POLICYNUMBER_NOTPROVIDED_VALUE;
String userid = request.getHeader(HEADER_USERID) != null ?
request.getHeader(HEADER_USERID) : HEADER_USERID_NOTPROVIDED_VALUE;
int status = response.SC_OK;
System.out.println("---------> status"+ status);
RequestGendroidServiceConversation gsc = new
RequestGendroidServiceConversation(deviceid,
request.getRequestURL().toString(),policynumber,userid);
logger.debug("Handling GendroidConversation with id: " +
gsc.getConversationID());
// We don't want to interrupt or fail the response being handled by the
// executing thread so we'll catch and log any exceptions.
try {
container.add(gsc);
conversation.set(gsc);
} catch (Exception e) {
logger.error(e);
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse
response, Object handler, ModelAndView mav) throws Exception {
RequestGendroidServiceConversation gsc = conversation.get();
if (gsc != null) {
try {
gsc.setResponseTime(new LocalDateTime());
gsc.setResponseStatus(HttpServletResponse.SC_OK); //----------->
//here I hard coded with SC_OK but I need to send status
} catch (Exception e) {
logger.error("Could not retrieve Gendroid service conversation
due to exception :" + e.getMessage());
} finally {
conversation.remove();
}
}
}
}