KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > invocation > portal > TargetInterceptor


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.portal;
10
11 import java.util.Enumeration JavaDoc;
12
13 import org.jboss.portal.server.Component;
14 import org.jboss.portal.server.Instance;
15 import org.jboss.portal.server.PortalRequest;
16 import org.jboss.portal.server.PortalServer;
17 import org.jboss.portal.server.ServerObject;
18 import org.jboss.portal.server.Window;
19 import org.jboss.portal.server.WindowContext;
20 import org.jboss.portal.server.PortalResponse;
21 import org.jboss.portal.server.WindowURL;
22 import org.jboss.portal.server.invocation.AttachmentKey;
23 import org.jboss.portal.server.invocation.Interceptor;
24 import org.jboss.portal.server.invocation.Invocation;
25 import org.jboss.portal.server.invocation.InvocationType;
26 import org.jboss.portal.server.output.RedirectionResult;
27 import org.jboss.portal.server.output.Result;
28 import org.jboss.portal.server.output.UnavailableResult;
29 import org.jboss.portal.server.output.StreamResult;
30 import org.jboss.portal.server.output.ActionResult;
31 import org.jboss.portal.server.output.NoResult;
32 import org.jboss.portal.server.output.HTTPRedirectionResult;
33 import org.jboss.portal.server.user.UserContext;
34 import org.jboss.portal.server.util.Properties;
35 import org.jboss.portal.server.util.Parameters;
36
37 /**
38  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
39  * @version $Revision: 1.2 $
40  */

41 public class TargetInterceptor implements Interceptor
42 {
43
44    private boolean redirect = false;
45
46    public Object JavaDoc invoke(Invocation invocation)
47    {
48       PortalRequest req = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
49       PortalResponse resp = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE);
50       UserContext userContext = (UserContext)invocation.getAttachment(AttachmentKey.USER_CONTEXT);
51
52       //
53
Window window = null;
54       Instance instance = null;
55       WindowContext wctx = null;
56
57       //
58
Result targetResult = null;
59
60       // Get the target
61
ServerObject target = req.getTarget();
62       if (target instanceof Window)
63       {
64          window = (Window)target;
65          instance = window.getInstance();
66          wctx = (WindowContext)userContext.getContext(window);
67          targetResult = invokeWindow(invocation, window, wctx, instance);
68          resp.setTargetResult(targetResult);
69       }
70
71       //
72
if (targetResult instanceof ActionResult)
73       {
74          ActionResult actionResult = (ActionResult)targetResult;
75          Parameters newParameters = actionResult.getParameters();
76          WindowURL url = (WindowURL)window.createURL();
77          url.setType(WindowURL.TYPE_RENDER);
78          url.setWindowState(actionResult.getWindowState());
79          url.setMode(actionResult.getMode());
80          if (newParameters != null)
81          {
82             for (Enumeration JavaDoc e = newParameters.getParameterNames();e.hasMoreElements();)
83             {
84                String JavaDoc name = (String JavaDoc)e.nextElement();
85                String JavaDoc[] values = actionResult.getParameters().getParameterValues(name);
86                url.setTargetParameter(name, values);
87             }
88          }
89
90          //
91
if (redirect)
92          {
93             String JavaDoc location = resp.createURL(url, true);
94             HTTPRedirectionResult redirection = new HTTPRedirectionResult(targetResult.getProducer(), location);
95             resp.setTargetResult(redirection);
96             return null;
97          }
98          else
99          {
100             req.getControlParameters().setParameters(url.getControlParameters());
101             req.getTargetParameters().setParameters(url.getTargetParameters());
102             targetResult = invokeWindow(invocation, window, wctx, instance);
103             resp.setTargetResult(targetResult);
104          }
105       }
106       else if (targetResult instanceof NoResult)
107       {
108          WindowURL url = (WindowURL)window.createURL();
109          url.setType(WindowURL.TYPE_RENDER);
110
111          //
112
if (redirect)
113          {
114             String JavaDoc location = resp.createURL(url, true);
115             HTTPRedirectionResult redirection = new HTTPRedirectionResult(targetResult.getProducer(), location);
116             resp.setTargetResult(redirection);
117             return null;
118          }
119          else
120          {
121             req.getControlParameters().setParameters(url.getControlParameters());
122             req.getTargetParameters().setParameters(url.getTargetParameters());
123             targetResult = invokeWindow(invocation, window, wctx, instance);
124             resp.setTargetResult(targetResult);
125          }
126       }
127       else if (targetResult instanceof RedirectionResult)
128       {
129          // Redirect
130
return null;
131       }
132       else if (targetResult instanceof StreamResult)
133       {
134          // Send bytes
135
return null;
136       }
137       else if (targetResult instanceof UnavailableResult)
138       {
139          try
140          {
141             // Stop component
142
Component component = instance.getComponent();
143             PortalServer portalServer = component.getServer();
144             portalServer.stop(component.getID());
145          }
146          catch (Exception JavaDoc e)
147          {
148             e.printStackTrace();
149          }
150       }
151
152       // Continue invocation
153
invocation.invokeNext();
154
155       return null;
156    }
157
158    private Result invokeWindow(Invocation invocation, Window window, WindowContext wctx, Instance instance)
159    {
160       Component component = instance.getComponent();
161       try
162       {
163          invocation.setAttachment(AttachmentKey.WINDOW_CONTEXT, wctx);
164          invocation.setAttachment(AttachmentKey.WINDOW, window);
165          invocation.setAttachment(AttachmentKey.INVOCATION_TYPE, InvocationType.PROCESS_TARGET);
166          invocation.setAttachment(AttachmentKey.RESPONSE_PROPERTIES, new Properties());
167          Result targetResult = (Result)component.invoke(invocation);
168          return targetResult;
169       }
170       finally
171       {
172          invocation.removeAttachment(AttachmentKey.WINDOW);
173          invocation.removeAttachment(AttachmentKey.WINDOW_CONTEXT);
174          invocation.removeAttachment(AttachmentKey.INVOCATION_TYPE);
175          invocation.removeAttachment(AttachmentKey.RESPONSE_PROPERTIES);
176       }
177    }
178 }
179
Popular Tags