1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  /**
24   * The contents of this file are subject to the terms of the Common Development
25   * and Distribution License (the License). You may not use this file except in
26   * compliance with the License.
27   *
28   * You can obtain a copy of the License at http://www.sun.com/cddl/cddl.html and
29   * legal/CDDLv1.0.txt. See the License for the specific language governing
30   * permission and limitations under the License.
31   *
32   * When distributing Covered Code, include this CDDL Header Notice in each file
33   * and include the License file at legal/CDDLv1.0.txt.
34   *
35   * If applicable, add the following below the CDDL Header, with the fields
36   * enclosed by brackets [] replaced by your own identifying information:
37   * "Portions Copyrighted [year] [name of copyright owner]"
38   *
39   * Copyright 2009 Sun Microsystems Inc. All rights reserved.
40   */
41  
42  package com.liferay.portal.portletcontainer;
43  
44  import com.liferay.portal.kernel.portlet.LiferayPortletMode;
45  import com.liferay.portal.kernel.portlet.LiferayWindowState;
46  import com.liferay.portal.kernel.util.ReleaseInfo;
47  import com.liferay.portal.model.Portlet;
48  import com.liferay.portal.wsrp.WSRPFactoryUtil;
49  
50  import com.sun.portal.container.ChannelMode;
51  import com.sun.portal.container.ChannelState;
52  import com.sun.portal.container.ChannelURLFactory;
53  import com.sun.portal.container.Container;
54  import com.sun.portal.container.ContainerFactory;
55  import com.sun.portal.container.ContainerRequest;
56  import com.sun.portal.container.ContainerType;
57  import com.sun.portal.container.EntityID;
58  import com.sun.portal.container.ExecuteActionRequest;
59  import com.sun.portal.container.GetMarkupRequest;
60  import com.sun.portal.container.GetResourceRequest;
61  import com.sun.portal.container.PortletWindowContext;
62  import com.sun.portal.container.WindowRequestReader;
63  
64  import java.util.ArrayList;
65  import java.util.List;
66  
67  import javax.portlet.PortletMode;
68  import javax.portlet.PortletRequest;
69  import javax.portlet.WindowState;
70  
71  import javax.servlet.http.HttpServletRequest;
72  
73  /**
74   * <a href="ContainerRequestFactory.java.html"><b><i>View Source</i></b></a>
75   *
76   * @author Deepak Gothe
77   * @author Brian Wing Shun Chan
78   *
79   */
80  public class ContainerRequestFactory {
81  
82      static List<ChannelMode> portletModes = new ArrayList<ChannelMode>();
83      static List<ChannelState> windowStates = new ArrayList<ChannelState>();
84  
85      static {
86          portletModes.add(ChannelMode.EDIT);
87          portletModes.add(ChannelMode.HELP);
88          portletModes.add(ChannelMode.VIEW);
89          portletModes.add(
90              new ChannelMode(LiferayPortletMode.ABOUT.toString()));
91          portletModes.add(
92              new ChannelMode(LiferayPortletMode.CONFIG.toString()));
93          portletModes.add(
94              new ChannelMode(LiferayPortletMode.EDIT_DEFAULTS.toString()));
95          portletModes.add(
96              new ChannelMode(LiferayPortletMode.PREVIEW.toString()));
97          portletModes.add(
98              new ChannelMode(LiferayPortletMode.PRINT.toString()));
99  
100         windowStates.add(ChannelState.MAXIMIZED);
101         windowStates.add(ChannelState.MINIMIZED);
102         windowStates.add(ChannelState.NORMAL);
103         windowStates.add(
104             new ChannelState(LiferayWindowState.EXCLUSIVE.toString()));
105         windowStates.add(
106             new ChannelState(LiferayWindowState.POP_UP.toString()));
107     }
108 
109     public static ExecuteActionRequest createExecuteActionRequest(
110             HttpServletRequest request, Portlet portlet,
111             WindowState windowState, PortletMode portletMode,
112             long plid, boolean facesPortlet, boolean remotePortlet)
113         throws Exception {
114 
115         return (ExecuteActionRequest)_createContainerRequest(
116             request, portlet, windowState, portletMode, plid,
117             facesPortlet, remotePortlet, PortletRequest.ACTION_PHASE);
118     }
119 
120     public static GetMarkupRequest createGetMarkUpRequest(
121             HttpServletRequest request, Portlet portlet,
122             WindowState windowState, PortletMode portletMode,
123             long plid, boolean facesPortlet, boolean remotePortlet)
124         throws Exception {
125 
126         return (GetMarkupRequest)_createContainerRequest(
127             request, portlet, windowState, portletMode, plid,
128             facesPortlet, remotePortlet, PortletRequest.RENDER_PHASE);
129     }
130 
131     public static GetResourceRequest createGetResourceRequest(
132             HttpServletRequest request, Portlet portlet,
133             WindowState windowState, PortletMode portletMode,
134             long plid, boolean facesPortlet, boolean remotePortlet)
135         throws Exception {
136 
137         return (GetResourceRequest)_createContainerRequest(
138             request, portlet, windowState, portletMode, plid,
139             facesPortlet, remotePortlet, PortletRequest.RESOURCE_PHASE);
140     }
141 
142     private static ContainerRequest _createContainerRequest(
143             HttpServletRequest request, Portlet portlet,
144             WindowState windowState, PortletMode portletMode,
145             long plid, boolean facesPortlet, boolean remotePortlet,
146             String lifecycle)
147         throws Exception {
148 
149         EntityID entityID = WindowInvokerUtil.getEntityID(portlet);
150 
151         ChannelState channelWindowState = new ChannelState(
152             windowState.toString());
153 
154         ChannelMode channelPortletMode = new ChannelMode(
155             portletMode.toString());
156 
157         PortletWindowContext portletWindowContext =
158             PortletWindowContextFactoryUtil.create(request, portlet, lifecycle);
159 
160         ChannelURLFactory channelURLFactory = null;
161 
162         if (!remotePortlet) {
163             channelURLFactory = new PortletWindowURLFactory(
164                 request, portlet, channelWindowState, channelPortletMode, plid);
165         }
166         else {
167             channelURLFactory = WSRPFactoryUtil.getWindowChannelURLFactory(
168                 request, portlet, channelWindowState, channelPortletMode, plid);
169         }
170 
171         WindowRequestReader windowRequestReader = null;
172 
173         if (lifecycle.equals(PortletRequest.ACTION_PHASE) ||
174             lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
175 
176             if (!remotePortlet) {
177                 windowRequestReader = new PortletWindowRequestReader(
178                     facesPortlet);
179             }
180             else {
181                 windowRequestReader = WSRPFactoryUtil.getWindowRequestReader();
182             }
183 
184             ChannelState newWindowState =
185                 windowRequestReader.readNewWindowState(request);
186 
187             if (newWindowState != null) {
188                 channelWindowState = newWindowState;
189             }
190 
191             ChannelMode newPortletWindowMode =
192                 windowRequestReader.readNewPortletWindowMode(request);
193 
194             if (newPortletWindowMode != null) {
195                 channelPortletMode = newPortletWindowMode;
196             }
197         }
198 
199         Container container = null;
200 
201         if (remotePortlet) {
202             container = WSRPFactoryUtil.getContainer();
203         }
204         else {
205             container = ContainerFactory.getContainer(
206                 ContainerType.PORTLET_CONTAINER);
207         }
208 
209         ContainerRequest containerRequest = null;
210 
211         if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
212             containerRequest = container.createExecuteActionRequest(
213                 request, entityID, channelWindowState, channelPortletMode,
214                 portletWindowContext, channelURLFactory, windowRequestReader);
215         }
216         else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
217             containerRequest = container.createGetMarkUpRequest(
218                 request, entityID, channelWindowState, channelPortletMode,
219                 portletWindowContext, channelURLFactory);
220         }
221         else if (lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
222             containerRequest = container.createGetResourceRequest(
223                 request, entityID, channelWindowState, channelPortletMode,
224                 portletWindowContext, channelURLFactory, windowRequestReader);
225         }
226 
227         if (containerRequest != null) {
228             String namespace = WindowInvokerUtil.getPortletNamespace(
229                 entityID);
230 
231             String windowID = WindowInvokerUtil.getPortletWindowID(
232                 entityID, plid);
233 
234             containerRequest.setAllowableWindowStates(windowStates);
235             containerRequest.setAllowableChannelModes(portletModes);
236             containerRequest.setNamespace(namespace);
237             containerRequest.setPortalInfo(ReleaseInfo.getReleaseInfo());
238             containerRequest.setWindowID(windowID);
239         }
240 
241         return containerRequest;
242     }
243 
244 }