KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > proxy > ResponseProxy


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
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.ServletResponse JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27
28 /**
29  * 接管HttpServletResponse
30  * 该代理类屏蔽了setContentType方法,这样在页面设置contentType就无效了。
31  * @author liudong
32  */

33 public class ResponseProxy implements InvocationHandler JavaDoc
34 {
35     private ServletResponse JavaDoc response;
36     
37     private final static String JavaDoc SET_CONTENT_TYPE = "setContentType";
38     private final static String JavaDoc ENCODE_URL = "encodeURL";
39
40     /**
41      * 获取代理实例
42      * @param servlet
43      * @param request
44      * @return
45      * @throws ServletException
46      */

47     public static ResponseProxy getProxy(ServletResponse JavaDoc response) throws ServletException JavaDoc
48     {
49         return new ResponseProxy(response);
50     }
51     
52     private ResponseProxy(ServletResponse JavaDoc response)
53     {
54         this.response = response;
55     }
56
57     /* (non-Javadoc)
58      * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
59      */

60     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc m, Object JavaDoc[] args) throws Throwable JavaDoc
61     {
62         //调用相应的操作
63
Object JavaDoc obj = null;
64         if(SET_CONTENT_TYPE.equals(m.getName()))
65             return null;
66         if(ENCODE_URL.equals(m.getName()))
67             return args[0];
68         try{
69             obj = m.invoke(response, args);
70         }catch(InvocationTargetException JavaDoc e){
71             throw e.getTargetException();
72         }
73         return obj;
74     }
75
76     /* (non-Javadoc)
77      * @see ibibio.goweb.http.ObjectProxy#getInstance()
78      */

79     public HttpServletResponse JavaDoc getInstance(){
80         return (HttpServletResponse JavaDoc)Proxy.newProxyInstance(
81                 response.getClass().getClassLoader(),
82                 response_cls,
83                 this);
84     }
85
86     final static Class JavaDoc[] response_cls = new Class JavaDoc[]{HttpServletResponse JavaDoc.class};
87
88 }
Popular Tags