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  package org.seasar.tuigwaa.util.filter;
17  
18  import java.util.ArrayList;
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletRequestWrapper;
27  
28  import com.isenshi.util.CharUtil;
29  
30  public class EncodingRequest extends HttpServletRequestWrapper {
31  
32  	private Map parameters = new HashMap();
33  
34  	private Map parameterValues = new HashMap();
35  
36  	public EncodingRequest(HttpServletRequest request, String encode) {
37  		super(request);
38  
39  		String query = request.getQueryString();
40  		if (query == null)
41  			return;
42  
43  		Map tmpParameterValue = new HashMap();
44  
45  		String[] params = query.split("&");
46  		for (int i = 0; i < params.length; i++) {
47  			String param = params[i];
48  			String[] array = param.split("=");
49  			if (array.length > 1) {
50  				String value = array[1];
51  				value = CharUtil.urlDecode(value, encode);
52  				parameters.put(array[0], value);
53  				addValue(tmpParameterValue, array[0], value);
54  			}
55  		}
56  		toParameterValues(tmpParameterValue);
57  	}
58  
59  	public String getParameter(String name) {
60  		String value = (String) parameters.get(name);
61  		if (value == null) {
62  			return super.getParameter(name);
63  		} else {
64  			return value;
65  		}
66  	}
67  
68  	public String[] getParameterValues(String name) {
69  		String[] values = (String[]) parameterValues.get(name);
70  		if (values == null) {
71  			return super.getParameterValues(name);
72  		} else {
73  			return values;
74  		}
75  	}
76  
77  	public Map getParameterMap() {
78  		Map map = new HashMap();
79  		Enumeration enm = getParameterNames();
80  
81  		while(enm.hasMoreElements()){
82  			String key = (String)enm.nextElement();
83  			String[] value = getParameterValues(key);
84  			map.put(key, value);
85  		}
86  		return map;
87  	}
88  	
89  	
90  	private void toParameterValues(Map tmpParamValues) {
91  		Iterator itr = tmpParamValues.keySet().iterator();
92  
93  		while (itr.hasNext()) {
94  			String name = (String) itr.next();
95  			List list = (List) tmpParamValues.get(name);
96  			parameterValues.put(name, list.toArray(new String[list.size()]));
97  		}
98  	}
99  
100 	private void addValue(Map params, String name, String value) {
101 		List list = (List) params.get(name);
102 		if (list == null) {
103 			list = new ArrayList();
104 			params.put(name, list);
105 		}
106 		list.add(value);
107 	}
108 
109 }