KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > PortletContainerImpl


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.pluto;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.portlet.ActionRequest;
23 import javax.portlet.ActionResponse;
24 import javax.portlet.PortletException;
25 import javax.portlet.PortletMode;
26 import javax.portlet.RenderRequest;
27 import javax.portlet.RenderResponse;
28 import javax.portlet.WindowState;
29 import javax.servlet.ServletConfig JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.pluto.core.InternalActionResponse;
34 import org.apache.pluto.factory.PortletObjectAccess;
35 import org.apache.pluto.invoker.PortletInvoker;
36 import org.apache.pluto.invoker.PortletInvokerAccess;
37 import org.apache.pluto.om.window.PortletWindow;
38 import org.apache.pluto.services.PortletContainerEnvironment;
39 import org.apache.pluto.services.information.DynamicInformationProvider;
40 import org.apache.pluto.services.information.InformationProviderAccess;
41 import org.apache.pluto.services.information.PortletURLProvider;
42 import org.apache.pluto.services.log.LogService;
43 import org.apache.pluto.services.log.Logger;
44
45 /**
46  * Implements the Pluto Container.
47  *
48  * @version $Id: PortletContainerImpl.java 156743 2005-03-10 05:50:30Z ddewolf $
49  */

50 public class PortletContainerImpl implements PortletContainer
51 {
52
53     private String JavaDoc uniqueContainerName;
54     private boolean initialized;
55
56     private Logger log = null;
57
58     public void init(String JavaDoc uniqueContainerName,
59                      ServletConfig JavaDoc servletConfig,
60                      PortletContainerEnvironment environment,
61                      Properties JavaDoc properties)
62     throws PortletContainerException
63     {
64         this.uniqueContainerName = uniqueContainerName;
65         PortletContainerServices.createReference(uniqueContainerName, environment);
66         initialized = true;
67
68         // Initialize the Logger that we will use
69
// from here forward for this Container:
70
log = ((LogService)environment
71                 .getContainerService(LogService.class))
72                 .getLogger(getClass());
73     }
74
75     public void shutdown() throws PortletContainerException
76     {
77         PortletContainerServices.destroyReference(uniqueContainerName);
78     }
79
80
81     public void renderPortlet(PortletWindow portletWindow,
82                               HttpServletRequest JavaDoc servletRequest,
83                               HttpServletResponse JavaDoc servletResponse )
84     throws PortletException, IOException JavaDoc, PortletContainerException
85     {
86         PortletContainerServices.prepare(uniqueContainerName);
87         PortletInvoker invoker = null;
88         
89         if (log.isDebugEnabled())
90         {
91             log.debug("PortletContainerImpl.portletService(" + portletWindow.getId() + ") called.");
92         }
93
94         try
95         {
96             RenderRequest renderRequest = PortletObjectAccess.getRenderRequest(portletWindow,
97                                                                                servletRequest,
98                                                                                servletResponse);
99
100             RenderResponse renderResponse = PortletObjectAccess.getRenderResponse(portletWindow,
101                                                                                   servletRequest,
102                                                                                   servletResponse);
103
104             invoker = PortletInvokerAccess.getPortletInvoker(portletWindow.getPortletEntity().getPortletDefinition());
105             invoker.render(renderRequest, renderResponse);
106         }
107         finally
108         {
109             PortletInvokerAccess.releasePortletInvoker(invoker);
110             PortletContainerServices.release();
111         }
112
113     }
114
115
116     public void processPortletAction(PortletWindow portletWindow,
117                                      HttpServletRequest JavaDoc servletRequest,
118                                      HttpServletResponse JavaDoc servletResponse)
119     throws PortletException, IOException JavaDoc, PortletContainerException
120     {
121         PortletContainerServices.prepare(uniqueContainerName);
122         PortletInvoker invoker = null;
123
124         if (log.isDebugEnabled())
125         {
126             log.debug("PortletContainerImpl.performPortletAction(" + portletWindow.getId() + ") called.");
127         }
128
129         String JavaDoc location = null;
130
131         InternalActionResponse _actionResponse = null;
132         ActionRequest actionRequest = null;
133
134         try
135         {
136             /*ActionRequest*/
137             actionRequest =
138             PortletObjectAccess.getActionRequest(portletWindow,
139                                                  servletRequest,
140                                                  servletResponse);
141
142             ActionResponse actionResponse = PortletObjectAccess.getActionResponse(portletWindow,
143                                                                                   servletRequest,
144                                                                                   servletResponse);
145             invoker = PortletInvokerAccess.getPortletInvoker(portletWindow.getPortletEntity().getPortletDefinition());
146
147             _actionResponse = (InternalActionResponse)actionResponse;
148
149             // call action() at the portlet
150
invoker.action(actionRequest, actionResponse);
151
152             location = _actionResponse.getRedirectLocation();
153         }
154         catch (PortletException e)
155         {
156             throw e;
157         }
158         catch (IOException JavaDoc e)
159         {
160             throw e;
161         }
162         catch (RuntimeException JavaDoc e)
163         {
164             throw e;
165         }
166         finally
167         {
168             try
169             {
170                 redirect(location, portletWindow, servletRequest, servletResponse, _actionResponse);
171             }
172             finally
173             {
174                 PortletInvokerAccess.releasePortletInvoker(invoker);
175                 PortletContainerServices.release();
176             }
177         }
178     }
179
180     public void portletLoad ( PortletWindow portletWindow,
181                               HttpServletRequest JavaDoc servletRequest,
182                               HttpServletResponse JavaDoc servletResponse )
183     throws PortletException, PortletContainerException
184     {
185         PortletContainerServices.prepare(uniqueContainerName);
186         PortletInvoker invoker = null;
187
188         if (log.isDebugEnabled())
189             log.debug("PortletContainerImpl.portletLoad("+portletWindow.getId()+") called.");
190
191         RenderRequest renderRequest = PortletObjectAccess.getRenderRequest(portletWindow,
192                                                                            servletRequest,
193                                                                            servletResponse);
194
195         RenderResponse renderResponse = PortletObjectAccess.getRenderResponse(portletWindow,
196                                                                               servletRequest,
197                                                                               servletResponse);
198
199         invoker = PortletInvokerAccess.getPortletInvoker(portletWindow.getPortletEntity().getPortletDefinition());
200
201         try
202         {
203             invoker.load(renderRequest, renderResponse);
204         }
205         finally
206         {
207             PortletInvokerAccess.releasePortletInvoker(invoker);
208             PortletContainerServices.release();
209         }
210
211     }
212
213     public boolean isInitialized()
214     {
215         return initialized;
216     }
217
218     protected void redirect(String JavaDoc location,
219                               PortletWindow portletWindow,
220                               HttpServletRequest JavaDoc servletRequest,
221                               HttpServletResponse JavaDoc servletResponse,
222                               InternalActionResponse _actionResponse)
223     throws IOException JavaDoc {
224         if (location == null && _actionResponse != null)
225         {
226             DynamicInformationProvider provider = InformationProviderAccess.getDynamicProvider(servletRequest);
227     
228             //TODO: don't send changes in case of exception -> PORTLET:SPEC:17
229

230             PortletMode portletMode = provider.getPortletMode(portletWindow);
231             WindowState windowState = provider.getWindowState(portletWindow);
232             
233             // get the changings of this portlet entity that might be set during action handling
234
// change portlet mode
235
if (_actionResponse.getChangedPortletMode() != null)
236             {
237                 portletMode = _actionResponse.getChangedPortletMode();
238                 InformationProviderAccess.getDynamicProvider(servletRequest)
239                     .getPortletActionProvider(portletWindow)
240                     .changePortletMode(portletMode);
241             }
242             // change window state
243
if (_actionResponse.getChangedWindowState() != null)
244             {
245                 windowState = _actionResponse.getChangedWindowState();
246                 InformationProviderAccess.getDynamicProvider(servletRequest)
247                     .getPortletActionProvider(portletWindow)
248                     .changePortletWindowState(windowState);
249             }
250             // get render parameters
251
Map JavaDoc renderParameter = _actionResponse.getRenderParameters();
252     
253             PortletURLProvider redirectURL = provider.getPortletURLProvider(portletWindow);
254     
255             if (provider.getPortletMode(portletWindow)!=null)
256             {
257                 redirectURL.setPortletMode(portletMode);
258             }
259             if (provider.getWindowState(portletWindow)!=null)
260             {
261                 redirectURL.setWindowState(windowState);
262             }
263             if (servletRequest.isSecure())
264             {
265                 redirectURL.setSecure(); // TBD
266
}
267             redirectURL.clearParameters();
268             redirectURL.setParameters(renderParameter);
269     
270             location = servletResponse.encodeRedirectURL(redirectURL.toString());
271         }
272     
273         javax.servlet.http.HttpServletResponse JavaDoc redirectResponse = servletResponse;
274         while (redirectResponse instanceof javax.servlet.http.HttpServletResponseWrapper JavaDoc)
275         {
276             redirectResponse = (javax.servlet.http.HttpServletResponse JavaDoc)
277                                ((javax.servlet.http.HttpServletResponseWrapper JavaDoc)redirectResponse).getResponse();
278         }
279         redirectResponse.sendRedirect(location);
280     
281         }
282 }
283
Popular Tags