1 11 package org.jboss.portal.server.servlet; 12 13 import org.apache.log4j.Logger; 14 15 import javax.servlet.Filter ; 16 import javax.servlet.FilterConfig ; 17 import javax.servlet.ServletException ; 18 import javax.servlet.ServletRequest ; 19 import javax.servlet.ServletResponse ; 20 import javax.servlet.FilterChain ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import java.io.IOException ; 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.InvocationTargetException ; 26 27 31 public class CommandFilter implements Filter 32 { 33 34 private static final Logger log = Logger.getLogger(CommandFilter.class); 35 36 public void init(FilterConfig cfg) throws ServletException 37 { 38 } 39 40 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException , ServletException 41 { 42 Object cmd = req.getAttribute(FilterCommand.REQ_ATT_KEY); 43 if (cmd != null) 44 { 45 try 46 { 47 req.removeAttribute(FilterCommand.REQ_ATT_KEY); 48 Method methods = cmd.getClass().getMethod( 49 "execute", 50 new Class []{ 51 HttpServletRequest .class, 52 HttpServletResponse .class, 53 FilterChain .class}); 54 Object result = methods.invoke(cmd, new Object []{req,resp,chain}); 55 req.setAttribute(FilterCommand.REQ_ATT_KEY, result); 56 } 57 catch (NoSuchMethodException e) 58 { 59 throw new ServletException ("No execute method found on the filter command", e); 60 } 61 catch (InvocationTargetException e) 62 { 63 Throwable wrappee = e.getTargetException(); 65 log.error("Exception in command invocation", wrappee); 66 67 if (wrappee instanceof ServletException ) 69 { 70 throw (ServletException )wrappee; 71 } 72 if (wrappee instanceof IOException ) 74 { 75 throw (IOException )wrappee; 76 } 77 if (wrappee instanceof RuntimeException ) 79 { 80 throw (RuntimeException )wrappee; 81 } 82 if (wrappee instanceof Error ) 84 { 85 throw (Error )wrappee; 86 } 87 throw new ServletException ("The invoked command threw an exception", wrappee); 89 } 90 catch (IllegalAccessException e) 91 { 92 throw new ServletException ("Unexpected IllegalAccessException during command invocation", e); 93 } 94 } 95 else 96 { 97 chain.doFilter(req, resp); 99 } 100 } 101 102 public void destroy() 103 { 104 } 105 } 106 | Popular Tags |