1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletURLAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          try {
63              String portletURL = getPortletURL(request);
64  
65              ServletResponseUtil.write(response, portletURL);
66          }
67          catch (Exception e) {
68              PortalUtil.sendError(e, request, response);
69          }
70  
71          return null;
72      }
73  
74      protected String getPortletURL(HttpServletRequest request)
75          throws Exception {
76  
77          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
78              WebKeys.THEME_DISPLAY);
79  
80          String cacheability = ParamUtil.getString(request, "cacheability");
81          boolean copyCurrentRenderParameters = ParamUtil.getBoolean(
82              request, "copyCurrentRenderParameters");
83          long doAsUserId = ParamUtil.getLong(request, "doAsUserId");
84          boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
85          boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
86          String lifecycle = ParamUtil.getString(request, "lifecycle");
87          String name = ParamUtil.getString(request, "name");
88          boolean portletConfiguration = ParamUtil.getBoolean(
89              request, "portletConfiguration");
90          String portletId = ParamUtil.getString(request, "portletId");
91          String portletMode = ParamUtil.getString(request, "portletMode");
92          String resourceId = ParamUtil.getString(request, "resourceId");
93          String returnToFullPageURL = ParamUtil.getString(
94              request, "returnToFullPageURL");
95          boolean secure = ParamUtil.getBoolean(request, "secure");
96          String windowState = ParamUtil.getString(request, "windowState");
97  
98          PortletURLImpl portletURL = new PortletURLImpl(
99              request, portletId, themeDisplay.getPlid(), lifecycle);
100 
101         if (Validator.isNotNull(cacheability)) {
102             portletURL.setCacheability(cacheability);
103         }
104 
105         portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
106 
107         if (doAsUserId > 0) {
108             portletURL.setDoAsUserId(doAsUserId);
109         }
110 
111         portletURL.setEncrypt(encrypt);
112         portletURL.setEscapeXml(escapeXml);
113 
114         if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
115             Validator.isNotNull(name)) {
116 
117             portletURL.setParameter(ActionRequest.ACTION_NAME, name);
118         }
119 
120         portletURL.setPortletId(portletId);
121 
122         if (portletConfiguration) {
123             String portletResource = ParamUtil.getString(
124                 request, "portletResource");
125             String previewWidth = ParamUtil.getString(request, "previewWidth");
126 
127             portletURL.setParameter(
128                 "struts_action", "/portlet_configuration/edit_configuration");
129             portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
130             portletURL.setParameter("portletResource", portletResource);
131             portletURL.setParameter("previewWidth", previewWidth);
132         }
133 
134         if (Validator.isNotNull(portletMode)) {
135             portletURL.setPortletMode(
136                 PortletModeFactory.getPortletMode(portletMode));
137         }
138 
139         if (Validator.isNotNull(resourceId)) {
140             portletURL.setResourceID(resourceId);
141         }
142 
143         if (!themeDisplay.isStateMaximized()) {
144             if (Validator.isNotNull(returnToFullPageURL)) {
145                 portletURL.setParameter(
146                     "returnToFullPageURL", returnToFullPageURL);
147             }
148         }
149 
150         portletURL.setSecure(secure);
151 
152         if (Validator.isNotNull(windowState)) {
153             portletURL.setWindowState(
154                 WindowStateFactory.getWindowState(windowState));
155         }
156 
157         String parameterMapString = ParamUtil.getString(
158             request, "parameterMap");
159 
160         if (Validator.isNotNull(parameterMapString)) {
161             Map<String, String> parameterMap =
162                 (Map<String, String>)JSONFactoryUtil.deserialize(
163                     parameterMapString);
164 
165             Iterator<String> itr = parameterMap.keySet().iterator();
166 
167             while (itr.hasNext()) {
168                 String paramName = itr.next();
169 
170                 String paramValue = parameterMap.get(paramName);
171 
172                 portletURL.setParameter(paramName, paramValue);
173             }
174         }
175 
176         return portletURL.toString();
177     }
178 
179 }