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.util.PortalUtil;
49  import com.liferay.portlet.PortletSessionImpl;
50  
51  import com.sun.portal.container.ChannelMode;
52  import com.sun.portal.container.ChannelState;
53  import com.sun.portal.container.ChannelURLFactory;
54  import com.sun.portal.container.Container;
55  import com.sun.portal.container.ContainerFactory;
56  import com.sun.portal.container.ContainerRequest;
57  import com.sun.portal.container.ContainerType;
58  import com.sun.portal.container.EntityID;
59  import com.sun.portal.container.ExecuteActionRequest;
60  import com.sun.portal.container.GetMarkupRequest;
61  import com.sun.portal.container.GetResourceRequest;
62  import com.sun.portal.container.PortletWindowContext;
63  import com.sun.portal.container.WindowRequestReader;
64  
65  import java.util.ArrayList;
66  import java.util.List;
67  
68  import javax.portlet.PortletMode;
69  import javax.portlet.PortletRequest;
70  import javax.portlet.WindowState;
71  
72  import javax.servlet.http.HttpServletRequest;
73  
74  /**
75   * <a href="ContainerRequestFactory.java.html"><b><i>View Source</i></b></a>
76   *
77   * @author Deepak Gothe
78   * @author Brian Wing Shun Chan
79   *
80   */
81  public class ContainerRequestFactory {
82  
83      static List<ChannelMode> portletModes = new ArrayList<ChannelMode>();
84      static List<ChannelState> windowStates = new ArrayList<ChannelState>();
85  
86      static {
87          portletModes.add(ChannelMode.EDIT);
88          portletModes.add(ChannelMode.HELP);
89          portletModes.add(ChannelMode.VIEW);
90          portletModes.add(
91              new ChannelMode(LiferayPortletMode.ABOUT.toString()));
92          portletModes.add(
93              new ChannelMode(LiferayPortletMode.CONFIG.toString()));
94          portletModes.add(
95              new ChannelMode(LiferayPortletMode.EDIT_DEFAULTS.toString()));
96          portletModes.add(
97              new ChannelMode(LiferayPortletMode.PREVIEW.toString()));
98          portletModes.add(
99              new ChannelMode(LiferayPortletMode.PRINT.toString()));
100 
101         windowStates.add(ChannelState.MAXIMIZED);
102         windowStates.add(ChannelState.MINIMIZED);
103         windowStates.add(ChannelState.NORMAL);
104         windowStates.add(
105             new ChannelState(LiferayWindowState.EXCLUSIVE.toString()));
106         windowStates.add(
107             new ChannelState(LiferayWindowState.POP_UP.toString()));
108     }
109 
110     public static ExecuteActionRequest createExecuteActionRequest(
111             HttpServletRequest request, Portlet portlet,
112             WindowState windowState, PortletMode portletMode,
113             long plid, boolean facesPortlet, boolean remotePortlet)
114         throws Exception {
115 
116         return (ExecuteActionRequest)_createContainerRequest(
117             request, portlet, windowState, portletMode, plid,
118             facesPortlet, remotePortlet, PortletRequest.ACTION_PHASE);
119     }
120 
121     public static GetMarkupRequest createGetMarkUpRequest(
122             HttpServletRequest request, Portlet portlet,
123             WindowState windowState, PortletMode portletMode,
124             long plid, boolean facesPortlet, boolean remotePortlet)
125         throws Exception {
126 
127         return (GetMarkupRequest)_createContainerRequest(
128             request, portlet, windowState, portletMode, plid,
129             facesPortlet, remotePortlet, PortletRequest.RENDER_PHASE);
130     }
131 
132     public static GetResourceRequest createGetResourceRequest(
133             HttpServletRequest request, Portlet portlet,
134             WindowState windowState, PortletMode portletMode,
135             long plid, boolean facesPortlet, boolean remotePortlet)
136         throws Exception {
137 
138         return (GetResourceRequest)_createContainerRequest(
139             request, portlet, windowState, portletMode, plid,
140             facesPortlet, remotePortlet, PortletRequest.RESOURCE_PHASE);
141     }
142 
143     private static ContainerRequest _createContainerRequest(
144             HttpServletRequest request, Portlet portlet,
145             WindowState windowState, PortletMode portletMode,
146             long plid, boolean facesPortlet, boolean remotePortlet,
147             String lifecycle)
148         throws Exception {
149 
150         EntityID entityID = WindowInvokerUtil.getEntityID(portlet);
151 
152         ChannelState channelWindowState = new ChannelState(
153             windowState.toString());
154 
155         ChannelMode channelPortletMode = new ChannelMode(
156             portletMode.toString());
157 
158         PortletWindowContext portletWindowContext =
159             new PortletWindowContextImpl(request, portlet, lifecycle);
160 
161         ChannelURLFactory channelURLFactory = null;
162 
163         if (!remotePortlet) {
164             channelURLFactory = new PortletWindowURLFactory(
165                 request, portlet, channelWindowState, channelPortletMode, plid);
166         }
167 
168         WindowRequestReader windowRequestReader = null;
169 
170         if (lifecycle.equals(PortletRequest.ACTION_PHASE) ||
171             lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
172 
173             if (!remotePortlet) {
174                 windowRequestReader = new PortletWindowRequestReader(
175                     facesPortlet);
176             }
177 
178             ChannelState newWindowState =
179                 windowRequestReader.readNewWindowState(request);
180 
181             if (newWindowState != null) {
182                 channelWindowState = newWindowState;
183             }
184 
185             ChannelMode newPortletWindowMode =
186                 windowRequestReader.readNewPortletWindowMode(request);
187 
188             if (newPortletWindowMode != null) {
189                 channelPortletMode = newPortletWindowMode;
190             }
191         }
192 
193         Container container = ContainerFactory.getContainer(
194             ContainerType.PORTLET_CONTAINER);
195 
196         ContainerRequest containerRequest = null;
197 
198         if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
199             containerRequest = container.createExecuteActionRequest(
200                 request, entityID, channelWindowState, channelPortletMode,
201                 portletWindowContext, channelURLFactory, windowRequestReader);
202         }
203         else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
204             containerRequest = container.createGetMarkUpRequest(
205                 request, entityID, channelWindowState, channelPortletMode,
206                 portletWindowContext, channelURLFactory);
207         }
208         else if (lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
209             containerRequest = container.createGetResourceRequest(
210                 request, entityID, channelWindowState, channelPortletMode,
211                 portletWindowContext, channelURLFactory, windowRequestReader);
212         }
213 
214         if (containerRequest != null) {
215             String namespace = PortalUtil.getPortletNamespace(
216                 portlet.getPortletId());
217 
218             StringBuilder windowID = new StringBuilder();
219 
220             windowID.append(portlet.getPortletId());
221             windowID.append(PortletSessionImpl.LAYOUT_SEPARATOR);
222             windowID.append(plid);
223 
224             containerRequest.setAllowableWindowStates(windowStates);
225             containerRequest.setAllowableChannelModes(portletModes);
226             containerRequest.setNamespace(namespace);
227             containerRequest.setPortalInfo(ReleaseInfo.getReleaseInfo());
228             containerRequest.setWindowID(windowID.toString());
229         }
230 
231         return containerRequest;
232     }
233 
234 }