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  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          String doAsUserLanguageId = ParamUtil.getString(
86              request, "doAsUserLanguageId");
87          boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
88          boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
89          String lifecycle = ParamUtil.getString(request, "lifecycle");
90          String name = ParamUtil.getString(request, "name");
91          boolean portletConfiguration = ParamUtil.getBoolean(
92              request, "portletConfiguration");
93          String portletId = ParamUtil.getString(request, "portletId");
94          String portletMode = ParamUtil.getString(request, "portletMode");
95          String resourceId = ParamUtil.getString(request, "resourceId");
96          String returnToFullPageURL = ParamUtil.getString(
97              request, "returnToFullPageURL");
98          boolean secure = ParamUtil.getBoolean(request, "secure");
99          String windowState = ParamUtil.getString(request, "windowState");
100 
101         PortletURLImpl portletURL = new PortletURLImpl(
102             request, portletId, themeDisplay.getPlid(), lifecycle);
103 
104         if (Validator.isNotNull(cacheability)) {
105             portletURL.setCacheability(cacheability);
106         }
107 
108         portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
109 
110         if (doAsUserId > 0) {
111             portletURL.setDoAsUserId(doAsUserId);
112         }
113 
114         if (Validator.isNotNull(doAsUserLanguageId)) {
115             portletURL.setDoAsUserLanguageId(doAsUserLanguageId);
116         }
117 
118         portletURL.setEncrypt(encrypt);
119         portletURL.setEscapeXml(escapeXml);
120 
121         if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
122             Validator.isNotNull(name)) {
123 
124             portletURL.setParameter(ActionRequest.ACTION_NAME, name);
125         }
126 
127         portletURL.setPortletId(portletId);
128 
129         if (portletConfiguration) {
130             String portletResource = ParamUtil.getString(
131                 request, "portletResource");
132             String previewWidth = ParamUtil.getString(request, "previewWidth");
133 
134             portletURL.setParameter(
135                 "struts_action", "/portlet_configuration/edit_configuration");
136             portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
137             portletURL.setParameter("portletResource", portletResource);
138             portletURL.setParameter("previewWidth", previewWidth);
139         }
140 
141         if (Validator.isNotNull(portletMode)) {
142             portletURL.setPortletMode(
143                 PortletModeFactory.getPortletMode(portletMode));
144         }
145 
146         if (Validator.isNotNull(resourceId)) {
147             portletURL.setResourceID(resourceId);
148         }
149 
150         if (!themeDisplay.isStateMaximized()) {
151             if (Validator.isNotNull(returnToFullPageURL)) {
152                 portletURL.setParameter(
153                     "returnToFullPageURL", returnToFullPageURL);
154             }
155         }
156 
157         portletURL.setSecure(secure);
158 
159         if (Validator.isNotNull(windowState)) {
160             portletURL.setWindowState(
161                 WindowStateFactory.getWindowState(windowState));
162         }
163 
164         String parameterMapString = ParamUtil.getString(
165             request, "parameterMap");
166 
167         if (Validator.isNotNull(parameterMapString)) {
168             Map<String, String> parameterMap =
169                 (Map<String, String>)JSONFactoryUtil.deserialize(
170                     parameterMapString);
171 
172             Iterator<String> itr = parameterMap.keySet().iterator();
173 
174             while (itr.hasNext()) {
175                 String paramName = itr.next();
176 
177                 String paramValue = parameterMap.get(paramName);
178 
179                 portletURL.setParameter(paramName, paramValue);
180             }
181         }
182 
183         return portletURL.toString();
184     }
185 
186 }