1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.action;
16  
17  import com.liferay.portal.kernel.json.JSONFactoryUtil;
18  import com.liferay.portal.kernel.portlet.PortletModeFactory;
19  import com.liferay.portal.kernel.portlet.WindowStateFactory;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.PortletURLImpl;
26  import com.liferay.util.servlet.ServletResponseUtil;
27  
28  import java.util.Iterator;
29  import java.util.Map;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.PortletRequest;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  import org.apache.struts.action.Action;
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="PortletURLAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Eduardo Lundgren
46   */
47  public class PortletURLAction extends Action {
48  
49      public ActionForward execute(
50              ActionMapping mapping, ActionForm form, HttpServletRequest request,
51              HttpServletResponse response)
52          throws Exception {
53  
54          try {
55              String portletURL = getPortletURL(request);
56  
57              ServletResponseUtil.write(response, portletURL);
58          }
59          catch (Exception e) {
60              PortalUtil.sendError(e, request, response);
61          }
62  
63          return null;
64      }
65  
66      protected String getPortletURL(HttpServletRequest request)
67          throws Exception {
68  
69          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
70              WebKeys.THEME_DISPLAY);
71  
72          String cacheability = ParamUtil.getString(request, "cacheability");
73          boolean copyCurrentRenderParameters = ParamUtil.getBoolean(
74              request, "copyCurrentRenderParameters");
75          long doAsUserId = ParamUtil.getLong(request, "doAsUserId");
76          String doAsUserLanguageId = ParamUtil.getString(
77              request, "doAsUserLanguageId");
78          boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
79          boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
80          String lifecycle = ParamUtil.getString(request, "lifecycle");
81          String name = ParamUtil.getString(request, "name");
82          boolean portletConfiguration = ParamUtil.getBoolean(
83              request, "portletConfiguration");
84          String portletId = ParamUtil.getString(request, "portletId");
85          String portletMode = ParamUtil.getString(request, "portletMode");
86          String resourceId = ParamUtil.getString(request, "resourceId");
87          String returnToFullPageURL = ParamUtil.getString(
88              request, "returnToFullPageURL");
89          boolean secure = ParamUtil.getBoolean(request, "secure");
90          String windowState = ParamUtil.getString(request, "windowState");
91  
92          PortletURLImpl portletURL = new PortletURLImpl(
93              request, portletId, themeDisplay.getPlid(), lifecycle);
94  
95          if (Validator.isNotNull(cacheability)) {
96              portletURL.setCacheability(cacheability);
97          }
98  
99          portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
100 
101         if (doAsUserId > 0) {
102             portletURL.setDoAsUserId(doAsUserId);
103         }
104 
105         if (Validator.isNotNull(doAsUserLanguageId)) {
106             portletURL.setDoAsUserLanguageId(doAsUserLanguageId);
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 }