Filter that uses a response wrapper to convert all output to uppercase : Filter « Servlet « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Servlet » Filter 




import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class UpperCaseFilter implements Filter {
  protected FilterConfig config;

  public void init(FilterConfig configthrows ServletException {
    this.config = config;
  }

  public void destroy() {
  }

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws ServletException, IOException {
    ServletResponse newResponse = response;

    if (request instanceof HttpServletRequest) {
      newResponse = new CharResponseWrapper((HttpServletResponseresponse);
    }

    chain.doFilter(request, newResponse);

    if (newResponse instanceof CharResponseWrapper) {
      String text = newResponse.toString();
      if (text != null) {
        text = text.toUpperCase();
        response.getWriter().write(text);
      }
    }
  }
}

class CharResponseWrapper extends HttpServletResponseWrapper {
  protected CharArrayWriter charWriter;

  protected PrintWriter writer;

  protected boolean getOutputStreamCalled;

  protected boolean getWriterCalled;

  public CharResponseWrapper(HttpServletResponse response) {
    super(response);

    charWriter = new CharArrayWriter();
  }

  public ServletOutputStream getOutputStream() throws IOException {
    if (getWriterCalled) {
      throw new IllegalStateException("getWriter already called");
    }

    getOutputStreamCalled = true;
    return super.getOutputStream();
  }

  public PrintWriter getWriter() throws IOException {
    if (writer != null) {
      return writer;
    }
    if (getOutputStreamCalled) {
      throw new IllegalStateException("getOutputStream already called");
    }
    getWriterCalled = true;
    writer = new PrintWriter(charWriter);
    return writer;
  }

  public String toString() {
    String s = null;

    if (writer != null) {
      s = charWriter.toString();
    }
    return s;
  }
}

Simple response wrapper that utilizes a local CharArrayWriter for output


<filter>
  <filter-name>Upper Case Filter</filter-name>
  <filter-class>com.omh.filters.UpperCaseFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>Upper Case Filter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

*/














25.17.Filter
25.17.1.Your First Filter
25.17.2.Filter Response
25.17.3.Servlet Filter Init Parameter
25.17.4.Use Filter to log Configuration
25.17.5.Use Filter to check Request Parameter
25.17.6.Use Filter to Check Date
25.17.7.Simple filter for measuring servlet response times
25.17.8.Filter that uses a response wrapper to convert all output to uppercase
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.