01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * 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;
19: import java.lang.reflect.InvocationTargetException;
20: import java.lang.reflect.Method;
21: import java.lang.reflect.Proxy;
22:
23: import javax.servlet.ServletException;
24: import javax.servlet.ServletResponse;
25: import javax.servlet.http.HttpServletResponse;
26:
27: /**
28: * 接管HttpServletResponse
29: * 该代理类屏蔽了setContentType方法,这样在页面设置contentType就无效了。
30: * @author liudong
31: */
32: public class ResponseProxy implements InvocationHandler {
33: private ServletResponse response;
34:
35: private final static String SET_CONTENT_TYPE = "setContentType";
36: private final static String ENCODE_URL = "encodeURL";
37:
38: /**
39: * 获取代理实例
40: * @param servlet
41: * @param request
42: * @return
43: * @throws ServletException
44: */
45: public static ResponseProxy getProxy(ServletResponse response)
46: throws ServletException {
47: return new ResponseProxy(response);
48: }
49:
50: private ResponseProxy(ServletResponse response) {
51: this .response = response;
52: }
53:
54: /* (non-Javadoc)
55: * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
56: */
57: public Object invoke(Object proxy, Method m, Object[] args)
58: throws Throwable {
59: //调用相应的操作
60: Object obj = null;
61: if (SET_CONTENT_TYPE.equals(m.getName()))
62: return null;
63: if (ENCODE_URL.equals(m.getName()))
64: return args[0];
65: try {
66: obj = m.invoke(response, args);
67: } catch (InvocationTargetException e) {
68: throw e.getTargetException();
69: }
70: return obj;
71: }
72:
73: /* (non-Javadoc)
74: * @see ibibio.goweb.http.ObjectProxy#getInstance()
75: */
76: public HttpServletResponse getInstance() {
77: return (HttpServletResponse) Proxy.newProxyInstance(response
78: .getClass().getClassLoader(), response_cls, this );
79: }
80:
81: final static Class[] response_cls = new Class[] { HttpServletResponse.class };
82:
83: }
|