KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > theme > LayoutDispatcher


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

9 package org.jboss.portal.server.theme;
10
11 import org.apache.log4j.Logger;
12 import org.jboss.portal.server.PortalRequest;
13 import org.jboss.portal.server.PortalResponse;
14 import org.jboss.portal.server.servlet.FilterCommand;
15
16 import javax.servlet.FilterChain JavaDoc;
17 import javax.servlet.RequestDispatcher JavaDoc;
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * Dispatches the request to the target layout. The major side effect is to change the context path
26  * returned by the request to the value returned by <code>PortalLayout#getContextPath()</code> so the layout
27  * can safely use the getContextPath in order to designates resources located in the same web application.
28  *
29  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
30  * @version $Revision: 1.2 $
31  */

32 public class LayoutDispatcher implements FilterCommand
33 {
34
35    private static final Logger log = Logger.getLogger(LayoutDispatcher.class);
36
37    /**
38     * The layout
39     */

40    private final PortalLayout layout;
41    private String JavaDoc uri;
42
43    /**
44     * @param layout the layout to dispatch to
45     * @throws IllegalArgumentException if the layout is null
46     */

47    public LayoutDispatcher(PortalLayout layout, String JavaDoc uri) throws IllegalArgumentException JavaDoc
48    {
49       if (layout == null)
50       {
51          throw new IllegalArgumentException JavaDoc("No null layout allowed here");
52       }
53       this.layout = layout;
54       this.uri = uri;
55    }
56
57    public Object JavaDoc execute(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp, FilterChain JavaDoc chain) throws ServletException JavaDoc, IOException JavaDoc
58    {
59       // This request returns a context path which is the one of the war file in which the target is
60
HttpServletRequest JavaDoc wrapper = new HttpServletRequestWrapper JavaDoc(req)
61       {
62          public String JavaDoc getContextPath()
63          {
64             return layout.getContextPath();
65          }
66       };
67
68       // Execute the target
69
chain.doFilter(wrapper, resp);
70       return null;
71    }
72
73    /**
74     * Perform the dispatch to the target layout
75     */

76    public void include(PortalRequest req, PortalResponse resp) throws IOException JavaDoc, ServletException JavaDoc
77    {
78       try
79       {
80          RequestDispatcher JavaDoc dispatcher = layout.getServletContext().getRequestDispatcher(uri);
81          log.debug("got request dispatcher for layout resource: " + (dispatcher != null));
82          if (dispatcher == null)
83          {
84             throw new IOException JavaDoc("No dispatcher found for : " + layout.getName() + " [" + uri + "]");
85          }
86          req.setAttribute(FilterCommand.REQ_ATT_KEY, this);
87          dispatcher.include(req, resp);
88          log.debug("done with the layout");
89       }
90       finally
91       {
92          req.removeAttribute(FilterCommand.REQ_ATT_KEY);
93       }
94    }
95 }
96
Popular Tags