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