1

I am implementing Servlet Filter around Rest and SOAP services to be able to capture the request and response. The filter is invoked before the call goes to rest service but I am unable to get the elements of request from the payload. So the filter initializes then the call goes to doFilter and then to rest service but on the request and response variables I don't see anything related to the rest request and response. Hope the question is clear.

Filter Class

public class ESignLoggingInterceptor implements  Filter {

private static final Logger logger = Logger.getLogger( ESignLoggingInterceptor.class.getSimpleName() );

@Override
public void init( FilterConfig config ) throws ServletException {
    System.out.println( "Logging filter initiated" );

}

@Override
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
    String path = null;
    if( request != null && request instanceof HttpServletRequest ) {
         path = ( ( HttpServletRequest ) request).getRequestURL().toString();
    }
    chain.doFilter( request, response );
    System.out.println("Request URL:" + " " + path );
}

@Override
public void destroy() {
}

}

web.xml

<servlet>
    <servlet-name>Jersey Spring</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.mercuryinsurance.esignature.client.rest.service</param-value>
    </init-param>
    <init-param>        
        <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>        
        <param-value>/WEB-INF/jsps/rest</param-value>    
    </init-param>    
    <init-param>
        <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>        
        <param-value>/(resources|(WEB-INF/jsp))/.*</param-value>    
    </init-param> 
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Spring</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>LoggingFilter</filter-name>
    <filter-class>com.mercuryinsurance.esignature.common.logging.ESignLoggingInterceptor</filter-class>
</filter>

<filter-mapping>
    <filter-name>LoggingFilter</filter-name>
     <url-pattern>/rest/*</url-pattern>
</filter-mapping>
Mike
  • 777
  • 3
  • 16
  • 41

1 Answers1

1

While the JAX-RS stack is a layer on top of servlets, you're better off to use the standards. There are a few ways to do this. If you Google for something like "Jersey JAX-RS logging" you can find Jersey specific ways to log - i.e. like this link. Otherwise, if you want to stick with JEE standards, you'll need a ContainerRequestFilter for the inputs and a WriterInterceptor for the outputs. But a disclaimer - I have no idea how this interacts with Spring since that's not JEE.

I learned a great amount of information from this stackoverflow answer too but, again, that is the standards way.

Community
  • 1
  • 1
stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • The problem with JAX-RS is that it doesn't go along with SOAP services right. – Mike Jan 11 '17 at 23:29
  • So you need JAX-WS logging. I found http://java.globinch.com/enterprise-java/web-services/jax-ws/logging-tracing-web-service-xml-request-response-jax-ws/ but have not tested it. – stdunbar Jan 11 '17 at 23:37
  • Actually I got both type services running and I need to log both. Therefore initially I thought Servlet Filter would be a better approach to do this. – Mike Jan 12 '17 at 15:18