1 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 33 public class ResponseProxy implements InvocationHandler 34 { 35 private ServletResponse response; 36 37 private final static String SET_CONTENT_TYPE = "setContentType"; 38 private final static String ENCODE_URL = "encodeURL"; 39 40 47 public static ResponseProxy getProxy(ServletResponse response) throws ServletException 48 { 49 return new ResponseProxy(response); 50 } 51 52 private ResponseProxy(ServletResponse response) 53 { 54 this.response = response; 55 } 56 57 60 public Object invoke(Object proxy, Method m, Object [] args) throws Throwable 61 { 62 Object 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 e){ 71 throw e.getTargetException(); 72 } 73 return obj; 74 } 75 76 79 public HttpServletResponse getInstance(){ 80 return (HttpServletResponse )Proxy.newProxyInstance( 81 response.getClass().getClassLoader(), 82 response_cls, 83 this); 84 } 85 86 final static Class [] response_cls = new Class []{HttpServletResponse .class}; 87 88 } | Popular Tags |