java—如何将httpservletresponse转换为javax.ws.rs.core.response

gmol1639  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(266)

我正在开发一个日志框架。日志框架是使用cxf和javax.ws.rs.core.response编写的,我使用springmvc编写了一个拦截器。我试图将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();
        }
     }

  }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题