View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  /*
17   * Created on 2005/08/25
18   *
19   * TODO To change the template for this generated file go to
20   * Window - Preferences - Java - Code Style - Code Templates
21   */
22  package org.seasar.tuigwaa.util.filter;
23  
24  import java.io.IOException;
25  import java.util.regex.Pattern;
26  
27  import javax.servlet.Filter;
28  import javax.servlet.FilterChain;
29  import javax.servlet.FilterConfig;
30  import javax.servlet.ServletException;
31  import javax.servlet.ServletRequest;
32  import javax.servlet.ServletResponse;
33  import javax.servlet.http.HttpServletRequest;
34  
35  /***
36   * @author nishioka
37   */
38  public class EncodingFilter implements Filter {
39  
40  	// private Log log = LogFactory.getLog(getClass());
41  
42  	private String defaultPostEncoding;
43  
44  	private String defaultGetEncoding;
45  
46  	private String extraGetEncoding;
47  
48  	private Pattern extraGetPathPattern;
49  
50  	public void init(FilterConfig config) throws ServletException {
51  		this.defaultPostEncoding = config
52  				.getInitParameter("defaultPostEncoding");
53  		this.defaultGetEncoding = config.getInitParameter("defaultGetEncoding");
54  
55  		String extraPath = config.getInitParameter("extraGetPath");
56  		this.extraGetEncoding = config.getInitParameter("extraGetEncoding");
57  		this.extraGetPathPattern = Pattern.compile(extraPath);
58  	}
59  
60  	public void doFilter(ServletRequest request, ServletResponse response,
61  			FilterChain chain) throws IOException, ServletException {
62  
63  		HttpServletRequest hRequest = (HttpServletRequest) request;
64  		String servletPath = hRequest.getServletPath();
65  
66  		String encode = defaultGetEncoding;
67  		if (extraGetPathPattern.matcher(servletPath).find()) {
68  			encode = extraGetEncoding;
69  		}
70  		EncodingRequest eRequest = new EncodingRequest(hRequest, encode);
71  
72  		eRequest.setCharacterEncoding(this.defaultPostEncoding);
73  		
74  		//java.util.Enumeration enm = request.getParameterNames();
75  		//		while(enm.hasMoreElements()){
76  			//String value  = (String)hRequest.getParameter((String)enm.nextElement());
77  			//System.out.println(value);
78  		//}
79  		
80  		chain.doFilter(eRequest, response);
81  	}
82  
83  	public void destroy() {
84  	}
85  }