Using servlet filters as alternatives to JAX-RS 1.1 handlers

How to use servlet filters with a Java API for RESTful Web Services (JAX-RS) class.

About this task

JAX-RS is implemented as a servlet when used in web applications. Thus, you can use servlet filters with your JAX-RS class.

Procedure

  1. Implement your filter class according to the servlet specification.
  2. Specify the URL of the JAX-RS class:
    @WebFilter("/rest/myurl")
    public class MyFilter implements Filter {
      ......
    }
  3. JAX-RS flushes the response. You can put the response on the wire before the filter method processing completes. If you need the filter method to completely process the response before putting it on the wire, wrap the response in another class to suppress flushing until the filter is done.
    @WebFilter("/rest/myurl")
    public class MyFilter implements Filter {
      	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {		
    		
    		ServletResponse newResponse = response;
    		if (request instanceof HttpServletRequest) {
    		      // wrap the Response in a new class
    		      newResponse = new NoFlushResponseWrapper((HttpServletResponse) response);
    		{		
    		
            // pass the request along to the next thing in the chain.
            // note that response has been replaced with a new class.
            chain.doFilter(request, newResponse); 
    
    		// on the way back, process the response.  Webcontainer will flush after filter has run.
    		.......			
    		
    	}
      
    }
    // a class that intercepts flush methods so response will not be sent 
    // until webcontainer is done with Filter processing. 
    public class NoFlushResponseWrapper extends HttpServletResponseWrapper{	
        ServletOutputStream myOutputStream = null;
    	public NoFlushResponseWrapper(HttpServletResponse response) {		
    		super(response);
    		try {
    			myOutputStream = new NoFlushServletOutputStream(response.getOutputStream());
    		} catch (IOException e) {			
    			e.printStackTrace();  
    		}		
    	}
    	
       // intercept the buffer flush so servlet cannot do it, will be deferred to web container
       @Override
       public void flushBuffer(){	   
       }
       
       // return our own wrapper of the servlet output stream
       @Override
       public ServletOutputStream getOutputStream(){
    	   return myOutputStream;
       } 
       
       // Inner class - wrapper of servlet output stream that will not flush. 
       public class NoFlushServletOutputStream extends ServletOutputStream{
    	   ServletOutputStream wrappedStream = null;
    	   NoFlushServletOutputStream(ServletOutputStream stream){
    		   wrappedStream = stream;		   
    	   }
    	   
    		@Override
    		public void write(int b) throws IOException {
    			wrappedStream.write(b);		
    		}
    		
    		@Override
    		public void flush(){			
    		}
    	   
       }
    }   

Results

Servlet filters can now be used with your JAX-RS class.


Icon that indicates the type of topic Task topic



Timestamp icon Last updated: March 5, 2017 17:29
File name: twbs_jaxrs_handlers_servlet_filters.html