KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > invocation > component > ContextDispatcherInterceptor


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.invocation.component;
10
11 import java.io.IOException JavaDoc;
12
13 import javax.servlet.RequestDispatcher JavaDoc;
14 import javax.servlet.ServletContext JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19 import org.jboss.portal.server.Application;
20 import org.jboss.portal.server.Component;
21 import org.jboss.portal.server.Instance;
22 import org.jboss.portal.server.PortalRequest;
23 import org.jboss.portal.server.PortalResponse;
24 import org.jboss.portal.server.Window;
25 import org.jboss.portal.server.WindowContext;
26 import org.jboss.portal.server.invocation.AttachmentKey;
27 import org.jboss.portal.server.invocation.Interceptor;
28 import org.jboss.portal.server.invocation.Invocation;
29 import org.jboss.portal.server.output.InternalErrorResult;
30 import org.jboss.portal.server.servlet.ServletCommand;
31 import org.jboss.portal.server.servlet.CommandServlet;
32
33 /**
34  * This interceptor dispatch the call to the target web application.
35  *
36  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
37  * @version $Revision: 1.3 $
38  */

39 public class ContextDispatcherInterceptor implements Interceptor
40 {
41
42    public Object JavaDoc invoke(Invocation invocation)
43    {
44       PortalRequest preq = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
45       PortalResponse presp = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE);
46
47       // Get the servlet context that we are dispatching to
48
Window window = (Window)invocation.getAttachment(AttachmentKey.WINDOW);
49       Instance instance = window.getInstance();
50       Component component = instance.getComponent();
51       Application application = component.getApplication();
52       ServletContext JavaDoc servletContext = application.getServletContext();
53
54       try
55       {
56          preq.setAttribute(ServletCommand.REQ_ATT_KEY, new InvokeNextCommand(invocation));
57          RequestDispatcher JavaDoc switcher = servletContext.getNamedDispatcher("CommandServlet");
58          switcher.include(preq, presp);
59          Object JavaDoc result = preq.getAttribute(ServletCommand.REQ_ATT_KEY);
60          return result;
61       }
62       catch (Exception JavaDoc e)
63       {
64          WindowContext ctx = (WindowContext)invocation.getAttachment(AttachmentKey.WINDOW_CONTEXT);
65          return new InternalErrorResult(ctx, e);
66       }
67       finally
68       {
69          preq.setAttribute(ServletCommand.REQ_ATT_KEY, null);
70       }
71    }
72
73    public static class InvokeNextCommand implements ServletCommand
74    {
75
76       private Invocation invocation;
77
78       public InvokeNextCommand(Invocation invocation)
79       {
80          this.invocation = invocation;
81       }
82
83       public Invocation getInvocation()
84       {
85          return invocation;
86       }
87
88       public Object JavaDoc execute(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
89       {
90          try
91          {
92             invocation.setAttachment(AttachmentKey.DISPATCHED_REQUEST, req);
93             invocation.setAttachment(AttachmentKey.DISPATCHED_RESPONSE, resp);
94             return invocation.invokeNext();
95          }
96          finally
97          {
98             invocation.removeAttachment(AttachmentKey.DISPATCHED_REQUEST);
99             invocation.removeAttachment(AttachmentKey.DISPATCHED_RESPONSE);
100          }
101       }
102    }
103 }
104
Popular Tags