1   /**
2    * Copyright (c) 2000-2008 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  package com.liferay.portal.action;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.portlet.PortletModeFactory;
27  import com.liferay.portal.kernel.portlet.WindowStateFactory;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.PortletURLImpl;
34  import com.liferay.util.servlet.ServletResponseUtil;
35  
36  import java.util.Iterator;
37  import java.util.Map;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.PortletRequest;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.Action;
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="PortletURLAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Eduardo Lundgren
54   *
55   */
56  public class PortletURLAction extends Action {
57  
58      public ActionForward execute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          try {
64              String portletURL = getPortletURL(request);
65  
66              ServletResponseUtil.write(response, portletURL);
67          }
68          catch (Exception e) {
69              PortalUtil.sendError(e, request, response);
70          }
71  
72          return null;
73      }
74  
75      protected String getPortletURL(HttpServletRequest request)
76          throws Exception {
77  
78          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
79              WebKeys.THEME_DISPLAY);
80  
81          String cacheability = ParamUtil.getString(request, "cacheability");
82          boolean copyCurrentRenderParameters = ParamUtil.getBoolean(
83              request, "copyCurrentRenderParameters");
84          long doAsUserId = ParamUtil.getLong(request, "doAsUserId");
85          boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
86          boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
87          String lifecycle = ParamUtil.getString(request, "lifecycle");
88          String name = ParamUtil.getString(request, "name");
89          boolean portletConfiguration = ParamUtil.getBoolean(
90              request, "portletConfiguration");
91          String portletId = ParamUtil.getString(request, "portletId");
92          String portletMode = ParamUtil.getString(request, "portletMode");
93          String resourceId = ParamUtil.getString(request, "resourceId");
94          String returnToFullPageURL = ParamUtil.getString(
95              request, "returnToFullPageURL");
96          boolean secure = ParamUtil.getBoolean(request, "secure");
97          String windowState = ParamUtil.getString(request, "windowState");
98  
99          PortletURLImpl portletURL = new PortletURLImpl(
100             request, portletId, themeDisplay.getPlid(), lifecycle);
101 
102         if (Validator.isNotNull(cacheability)) {
103             portletURL.setCacheability(cacheability);
104         }
105 
106         portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
107 
108         if (doAsUserId > 0) {
109             portletURL.setDoAsUserId(doAsUserId);
110         }
111 
112         portletURL.setEncrypt(encrypt);
113         portletURL.setEscapeXml(escapeXml);
114 
115         if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
116             Validator.isNotNull(name)) {
117 
118             portletURL.setParameter(ActionRequest.ACTION_NAME, name);
119         }
120 
121         portletURL.setPortletId(portletId);
122 
123         if (portletConfiguration) {
124             String portletResource = ParamUtil.getString(
125                 request, "portletResource");
126             String previewWidth = ParamUtil.getString(request, "previewWidth");
127 
128             portletURL.setParameter(
129                 "struts_action", "/portlet_configuration/edit_configuration");
130             portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
131             portletURL.setParameter("portletResource", portletResource);
132             portletURL.setParameter("previewWidth", previewWidth);
133         }
134 
135         if (Validator.isNotNull(portletMode)) {
136             portletURL.setPortletMode(
137                 PortletModeFactory.getPortletMode(portletMode));
138         }
139 
140         if (Validator.isNotNull(resourceId)) {
141             portletURL.setResourceID(resourceId);
142         }
143 
144         if (!themeDisplay.isStateMaximized()) {
145             if (Validator.isNotNull(returnToFullPageURL)) {
146                 portletURL.setParameter(
147                     "returnToFullPageURL", returnToFullPageURL);
148             }
149         }
150 
151         portletURL.setSecure(secure);
152 
153         if (Validator.isNotNull(windowState)) {
154             portletURL.setWindowState(
155                 WindowStateFactory.getWindowState(windowState));
156         }
157 
158         String parameterMapString = ParamUtil.getString(
159             request, "parameterMap");
160 
161         if (Validator.isNotNull(parameterMapString)) {
162             Map<String, String> parameterMap =
163                 (Map<String, String>)JSONFactoryUtil.deserialize(
164                     parameterMapString);
165 
166             Iterator<String> itr = parameterMap.keySet().iterator();
167 
168             while (itr.hasNext()) {
169                 String paramName = itr.next();
170 
171                 String paramValue = parameterMap.get(paramName);
172 
173                 portletURL.setParameter(paramName, paramValue);
174             }
175         }
176 
177         return portletURL.toString();
178     }
179 
180 }