001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.proxy;
017:
018: import java.lang.reflect.InvocationHandler;
019: import java.lang.reflect.InvocationTargetException;
020: import java.lang.reflect.Method;
021: import java.lang.reflect.Proxy;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.servlet.ServletException;
027: import javax.servlet.ServletRequest;
028: import javax.servlet.http.HttpServletRequest;
029:
030: /**
031: * HttpServletRequest的代理
032: * 用于进行参数编码的统一处理
033: * @author liudong
034: */
035: public class RequestProxy implements InvocationHandler {
036: final static String METHOD_GP = "getParameter";
037: final static String METHOD_GPM = "getParameterMap";
038: final static String METHOD_GPN = "getParameterNames";
039: final static String METHOD_GPV = "getParameterValues";
040:
041: final static String ENC_8859_1 = "8859_1";
042: final static String ENC_UTF_8 = "UTF-8";
043:
044: protected String encoding;
045:
046: public String getEncoding() {
047: return encoding;
048: }
049:
050: public void setEncoding(String encoding) {
051: this .encoding = encoding;
052: }
053:
054: private ServletRequest req;
055:
056: /**
057: * 获取代理实例
058: * @param servlet
059: * @param request
060: * @return
061: * @throws ServletException
062: */
063: public final static RequestProxy getProxy(ServletRequest req,
064: String encoding) throws ServletException {
065: return new RequestProxy(req, encoding);
066: }
067:
068: private RequestProxy(ServletRequest req, String encoding) {
069: this .req = req;
070: this .encoding = encoding;
071: }
072:
073: /* (non-Javadoc)
074: * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
075: */
076: public Object invoke(Object proxy, Method m, Object[] args)
077: throws Throwable {
078: //调用相应的操作
079: Object obj = null;
080: String encode = (encoding == null) ? ENC_UTF_8 : encoding;
081: try {
082: obj = m.invoke(req, args);
083: if (obj != null) {
084: String mn = m.getName();
085: if (METHOD_GP.equals(mn)) {
086: String value = (String) obj;
087: obj = new String(value.getBytes(ENC_8859_1), encode);
088: } else if (METHOD_GPV.equals(mn)) {
089: String[] values = (String[]) obj;
090: for (int i = 0; i < values.length; i++)
091: values[i] = new String(values[i]
092: .getBytes(ENC_8859_1), encode);
093: obj = values;
094: } else if (METHOD_GPM.equals(mn)) {
095: Map params = (Map) obj;
096: HashMap new_params = new HashMap();
097: Iterator iter = params.keySet().iterator();
098: while (iter.hasNext()) {
099: String key = (String) iter.next();
100: Object oValue = params.get(key);
101: if (oValue.getClass().isArray()) {
102: String[] values = (String[]) params
103: .get(key);
104: for (int i = 0; i < values.length; i++)
105: values[i] = new String(values[i]
106: .getBytes(ENC_8859_1), encode);
107: new_params.put(key, values);
108: } else {
109: String value = (String) params.get(key);
110: String new_value = (value != null) ? new String(
111: value.getBytes(ENC_8859_1), encode)
112: : null;
113: new_params.put(key, new_value);
114: }
115: }
116: }
117: }
118: } catch (InvocationTargetException 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 getInstance() {
128: return (HttpServletRequest) Proxy.newProxyInstance(req
129: .getClass().getClassLoader(), request_cls, this );
130: }
131:
132: final static Class[] request_cls = new Class[] { HttpServletRequest.class };
133: }
|