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