KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > filter > EncRequestWrapper


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.net.filter;
26
27 import org.radeox.util.logging.Logger;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37
38 public class EncRequestWrapper extends HttpServletRequestWrapper JavaDoc {
39   String JavaDoc encoding = "UTF-8";
40   Map JavaDoc paramMap = null;
41
42   public EncRequestWrapper(HttpServletRequest JavaDoc request) throws UnsupportedEncodingException JavaDoc {
43     this(request, request.getCharacterEncoding());
44   }
45
46   public EncRequestWrapper(HttpServletRequest JavaDoc request, String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
47     super(request);
48     "".getBytes(enc);
49     encoding = enc;
50
51     Map JavaDoc params = request.getParameterMap();
52     Iterator JavaDoc iterator = params.keySet().iterator();
53     Map JavaDoc encodedParams = new HashMap JavaDoc();
54     while (iterator.hasNext()) {
55       String JavaDoc key = (String JavaDoc) iterator.next();
56       String JavaDoc[] values = (String JavaDoc[]) params.get(key);
57       for(int n = 0; n < values.length; n++) {
58         values[n] = getEncodedString(values[n]);
59       }
60       encodedParams.put(key, values);
61     }
62     paramMap = Collections.unmodifiableMap(encodedParams);
63   }
64
65   private String JavaDoc getEncodedString(String JavaDoc src) {
66     if (src != null) {
67       try {
68         return new String JavaDoc(src.getBytes("iso-8859-1"), encoding);
69       } catch (UnsupportedEncodingException JavaDoc e) {
70         System.err.println(e);
71         Logger.warn("Error: illegal encoding: " + e);
72       }
73     }
74     return src;
75   }
76
77   public String JavaDoc getHeader(String JavaDoc name) {
78     return getEncodedString(super.getHeader(name));
79   }
80
81   public String JavaDoc getQueryString() {
82     return getEncodedString(super.getQueryString());
83   }
84
85   public String JavaDoc getRequestURI() {
86     return getEncodedString(super.getRequestURI());
87   }
88
89   public String JavaDoc getPathInfo() {
90     return getEncodedString(super.getPathInfo());
91   }
92
93   public String JavaDoc getParameter(String JavaDoc name) {
94     String JavaDoc[] values = (String JavaDoc[])paramMap.get(name);
95     if(null != values && values.length > 0) {
96       return ((String JavaDoc[])paramMap.get(name))[0];
97     }
98     return null;
99   }
100
101   public Map JavaDoc getParameterMap() {
102     return new HashMap JavaDoc(paramMap);
103   }
104
105   public Enumeration JavaDoc getParameterNames() {
106     final Iterator JavaDoc nameIt = paramMap.keySet().iterator();
107     return new Enumeration JavaDoc() {
108       public boolean hasMoreElements() {
109         return nameIt.hasNext();
110       }
111
112       public Object JavaDoc nextElement() {
113         return nameIt.next();
114       }
115     };
116   }
117
118   public String JavaDoc[] getParameterValues(String JavaDoc name) {
119     return (String JavaDoc[])paramMap.get(name);
120   }
121 }
122
Popular Tags