1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.action;
21  
22  import com.liferay.portal.kernel.json.JSONFactoryUtil;
23  import com.liferay.portal.kernel.portlet.PortletModeFactory;
24  import com.liferay.portal.kernel.portlet.WindowStateFactory;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.PortletURLImpl;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import java.util.Iterator;
34  import java.util.Map;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.PortletRequest;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  import org.apache.struts.action.Action;
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="PortletURLAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Eduardo Lundgren
51   *
52   */
53  public class PortletURLAction extends Action {
54  
55      public ActionForward execute(
56              ActionMapping mapping, ActionForm form, HttpServletRequest request,
57              HttpServletResponse response)
58          throws Exception {
59  
60          try {
61              String portletURL = getPortletURL(request);
62  
63              ServletResponseUtil.write(response, portletURL);
64          }
65          catch (Exception e) {
66              PortalUtil.sendError(e, request, response);
67          }
68  
69          return null;
70      }
71  
72      protected String getPortletURL(HttpServletRequest request)
73          throws Exception {
74  
75          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
76              WebKeys.THEME_DISPLAY);
77  
78          String cacheability = ParamUtil.getString(request, "cacheability");
79          boolean copyCurrentRenderParameters = ParamUtil.getBoolean(
80              request, "copyCurrentRenderParameters");
81          long doAsUserId = ParamUtil.getLong(request, "doAsUserId");
82          boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
83          boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
84          String lifecycle = ParamUtil.getString(request, "lifecycle");
85          String name = ParamUtil.getString(request, "name");
86          boolean portletConfiguration = ParamUtil.getBoolean(
87              request, "portletConfiguration");
88          String portletId = ParamUtil.getString(request, "portletId");
89          String portletMode = ParamUtil.getString(request, "portletMode");
90          String resourceId = ParamUtil.getString(request, "resourceId");
91          String returnToFullPageURL = ParamUtil.getString(
92              request, "returnToFullPageURL");
93          boolean secure = ParamUtil.getBoolean(request, "secure");
94          String windowState = ParamUtil.getString(request, "windowState");
95  
96          PortletURLImpl portletURL = new PortletURLImpl(
97              request, portletId, themeDisplay.getPlid(), lifecycle);
98  
99          if (Validator.isNotNull(cacheability)) {
100             portletURL.setCacheability(cacheability);
101         }
102 
103         portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
104 
105         if (doAsUserId > 0) {
106             portletURL.setDoAsUserId(doAsUserId);
107         }
108 
109         portletURL.setEncrypt(encrypt);
110         portletURL.setEscapeXml(escapeXml);
111 
112         if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
113             Validator.isNotNull(name)) {
114 
115             portletURL.setParameter(ActionRequest.ACTION_NAME, name);
116         }
117 
118         portletURL.setPortletId(portletId);
119 
120         if (portletConfiguration) {
121             String portletResource = ParamUtil.getString(
122                 request, "portletResource");
123             String previewWidth = ParamUtil.getString(request, "previewWidth");
124 
125             portletURL.setParameter(
126                 "struts_action", "/portlet_configuration/edit_configuration");
127             portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
128             portletURL.setParameter("portletResource", portletResource);
129             portletURL.setParameter("previewWidth", previewWidth);
130         }
131 
132         if (Validator.isNotNull(portletMode)) {
133             portletURL.setPortletMode(
134                 PortletModeFactory.getPortletMode(portletMode));
135         }
136 
137         if (Validator.isNotNull(resourceId)) {
138             portletURL.setResourceID(resourceId);
139         }
140 
141         if (!themeDisplay.isStateMaximized()) {
142             if (Validator.isNotNull(returnToFullPageURL)) {
143                 portletURL.setParameter(
144                     "returnToFullPageURL", returnToFullPageURL);
145             }
146         }
147 
148         portletURL.setSecure(secure);
149 
150         if (Validator.isNotNull(windowState)) {
151             portletURL.setWindowState(
152                 WindowStateFactory.getWindowState(windowState));
153         }
154 
155         String parameterMapString = ParamUtil.getString(
156             request, "parameterMap");
157 
158         if (Validator.isNotNull(parameterMapString)) {
159             Map<String, String> parameterMap =
160                 (Map<String, String>)JSONFactoryUtil.deserialize(
161                     parameterMapString);
162 
163             Iterator<String> itr = parameterMap.keySet().iterator();
164 
165             while (itr.hasNext()) {
166                 String paramName = itr.next();
167 
168                 String paramValue = parameterMap.get(paramName);
169 
170                 portletURL.setParameter(paramName, paramValue);
171             }
172         }
173 
174         return portletURL.toString();
175     }
176 
177 }