KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > proxy > RequestProxy


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.proxy;
17
18 import java.lang.reflect.InvocationHandler JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30
31 /**
32  * HttpServletRequest的代理
33  * 用于进行参数编码的统一处理
34  * @author liudong
35  */

36 public class RequestProxy implements InvocationHandler JavaDoc
37 {
38     final static String JavaDoc METHOD_GP = "getParameter";
39     final static String JavaDoc METHOD_GPM = "getParameterMap";
40     final static String JavaDoc METHOD_GPN = "getParameterNames";
41     final static String JavaDoc METHOD_GPV = "getParameterValues";
42     
43     final static String JavaDoc ENC_8859_1 = "8859_1";
44     final static String JavaDoc ENC_UTF_8 = "UTF-8";
45     
46     protected String JavaDoc encoding;
47
48     public String JavaDoc getEncoding() {
49         return encoding;
50     }
51     public void setEncoding(String JavaDoc encoding) {
52         this.encoding = encoding;
53     }
54     
55     private ServletRequest JavaDoc req;
56
57     /**
58      * 获取代理实例
59      * @param servlet
60      * @param request
61      * @return
62      * @throws ServletException
63      */

64     public final static RequestProxy getProxy(ServletRequest JavaDoc req, String JavaDoc encoding) throws ServletException JavaDoc{
65         return new RequestProxy(req, encoding);
66     }
67     
68     private RequestProxy(ServletRequest JavaDoc req, String JavaDoc encoding){
69         this.req = req;
70         this.encoding = encoding;
71     }
72
73     /* (non-Javadoc)
74      * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
75      */

76     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc m, Object JavaDoc[] args) throws Throwable JavaDoc{
77         //调用相应的操作
78
Object JavaDoc obj = null;
79         String JavaDoc encode = (encoding==null)?ENC_UTF_8:encoding;
80         try{
81             obj = m.invoke(req, args);
82             if(obj!=null){
83                 String JavaDoc mn = m.getName();
84                 if(METHOD_GP.equals(mn)){
85                     String JavaDoc value = (String JavaDoc)obj;
86                     obj = new String JavaDoc(value.getBytes(ENC_8859_1),encode);
87                 }
88                 else
89                 if(METHOD_GPV.equals(mn)){
90                     String JavaDoc[] values = (String JavaDoc[])obj;
91                     for(int i=0;i<values.length;i++)
92                         values[i] = new String JavaDoc(values[i].getBytes(ENC_8859_1),encode);
93                     obj = values;
94                 }
95                 else
96                 if(METHOD_GPM.equals(mn)){
97                     Map JavaDoc params = (Map JavaDoc)obj;
98                     HashMap JavaDoc new_params = new HashMap JavaDoc();
99                     Iterator JavaDoc iter = params.keySet().iterator();
100                     while(iter.hasNext()){
101                         String JavaDoc key = (String JavaDoc)iter.next();
102                         Object JavaDoc oValue = params.get(key);
103                         if(oValue.getClass().isArray()){
104                             String JavaDoc[] values = (String JavaDoc[])params.get(key);
105                             for(int i=0;i<values.length;i++)
106                                 values[i] = new String JavaDoc(values[i].getBytes(ENC_8859_1),encode);
107                             new_params.put(key, values);
108                         }
109                         else{
110                             String JavaDoc value = (String JavaDoc)params.get(key);
111                             String JavaDoc new_value = (value!=null)?
112                                     new String JavaDoc(value.getBytes(ENC_8859_1),encode):null;
113                             new_params.put(key,new_value);
114                         }
115                     }
116                 }
117             }
118         }catch(InvocationTargetException JavaDoc e){
119             throw e.getTargetException();
120         }
121         return obj;
122     }
123
124     /* (non-Javadoc)
125      * @see ibibio.goweb.http.ObjectProxy#getInstance()
126      */

127     public HttpServletRequest JavaDoc getInstance(){
128         return (HttpServletRequest JavaDoc)Proxy.newProxyInstance(
129                 req.getClass().getClassLoader(),
130                 request_cls,
131                 this);
132     }
133     final static Class JavaDoc[] request_cls = new Class JavaDoc[]{HttpServletRequest JavaDoc.class};
134 }
Popular Tags