KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > servlet > CommandFilter


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.server.servlet;
12
13 import org.apache.log4j.Logger;
14
15 import javax.servlet.Filter JavaDoc;
16 import javax.servlet.FilterConfig JavaDoc;
17 import javax.servlet.ServletException JavaDoc;
18 import javax.servlet.ServletRequest JavaDoc;
19 import javax.servlet.ServletResponse JavaDoc;
20 import javax.servlet.FilterChain JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26
27 /**
28  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
29  * @version $Revision: 1.1 $
30  */

31 public class CommandFilter implements Filter JavaDoc
32 {
33
34    private static final Logger log = Logger.getLogger(CommandFilter.class);
35
36    public void init(FilterConfig JavaDoc cfg) throws ServletException JavaDoc
37    {
38    }
39
40    public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc resp, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc
41    {
42       Object JavaDoc cmd = req.getAttribute(FilterCommand.REQ_ATT_KEY);
43       if (cmd != null)
44       {
45          try
46          {
47             req.removeAttribute(FilterCommand.REQ_ATT_KEY);
48             Method JavaDoc methods = cmd.getClass().getMethod(
49                   "execute",
50                   new Class JavaDoc[]{
51                      HttpServletRequest JavaDoc.class,
52                      HttpServletResponse JavaDoc.class,
53                      FilterChain JavaDoc.class});
54             Object JavaDoc result = methods.invoke(cmd, new Object JavaDoc[]{req,resp,chain});
55             req.setAttribute(FilterCommand.REQ_ATT_KEY, result);
56          }
57          catch (NoSuchMethodException JavaDoc e)
58          {
59             throw new ServletException JavaDoc("No execute method found on the filter command", e);
60          }
61          catch (InvocationTargetException JavaDoc e)
62          {
63             // Log the wrappee
64
Throwable JavaDoc wrappee = e.getTargetException();
65             log.error("Exception in command invocation", wrappee);
66
67             // Rethrow the checked ServletException
68
if (wrappee instanceof ServletException JavaDoc)
69             {
70                throw (ServletException JavaDoc)wrappee;
71             }
72             // Rethrow the checked IOException
73
if (wrappee instanceof IOException JavaDoc)
74             {
75                throw (IOException JavaDoc)wrappee;
76             }
77             // Rethrow RuntimeException
78
if (wrappee instanceof RuntimeException JavaDoc)
79             {
80                throw (RuntimeException JavaDoc)wrappee;
81             }
82             // Rethrow Error
83
if (wrappee instanceof Error JavaDoc)
84             {
85                throw (Error JavaDoc)wrappee;
86             }
87             // Here we wrap it and rethrow
88
throw new ServletException JavaDoc("The invoked command threw an exception", wrappee);
89          }
90          catch (IllegalAccessException JavaDoc e)
91          {
92             throw new ServletException JavaDoc("Unexpected IllegalAccessException during command invocation", e);
93          }
94       }
95       else
96       {
97          // Just invoke the next in the chain
98
chain.doFilter(req, resp);
99       }
100    }
101
102    public void destroy()
103    {
104    }
105 }
106
Popular Tags