001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.portlet.PortletModeFactory;
019    import com.liferay.portal.kernel.portlet.WindowStateFactory;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.PortletURLImpl;
026    import com.liferay.util.servlet.ServletResponseUtil;
027    
028    import java.util.Map;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.PortletRequest;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    import org.apache.struts.action.Action;
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Eduardo Lundgren
043     */
044    public class PortletURLAction extends Action {
045    
046            public ActionForward execute(
047                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
048                            HttpServletResponse response)
049                    throws Exception {
050    
051                    try {
052                            String portletURL = getPortletURL(request);
053    
054                            ServletResponseUtil.write(response, portletURL);
055                    }
056                    catch (Exception e) {
057                            PortalUtil.sendError(e, request, response);
058                    }
059    
060                    return null;
061            }
062    
063            protected String getPortletURL(HttpServletRequest request)
064                    throws Exception {
065    
066                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
067                            WebKeys.THEME_DISPLAY);
068    
069                    String cacheability = ParamUtil.getString(request, "cacheability");
070                    boolean copyCurrentRenderParameters = ParamUtil.getBoolean(
071                            request, "copyCurrentRenderParameters");
072                    long doAsUserId = ParamUtil.getLong(request, "doAsUserId");
073                    String doAsUserLanguageId = ParamUtil.getString(
074                            request, "doAsUserLanguageId");
075                    boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
076                    boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
077                    String lifecycle = ParamUtil.getString(request, "lifecycle");
078                    String name = ParamUtil.getString(request, "name");
079                    boolean portletConfiguration = ParamUtil.getBoolean(
080                            request, "portletConfiguration");
081                    String portletId = ParamUtil.getString(request, "portletId");
082                    String portletMode = ParamUtil.getString(request, "portletMode");
083                    String resourceId = ParamUtil.getString(request, "resourceId");
084                    String returnToFullPageURL = ParamUtil.getString(
085                            request, "returnToFullPageURL");
086                    boolean secure = ParamUtil.getBoolean(request, "secure");
087                    String windowState = ParamUtil.getString(request, "windowState");
088    
089                    PortletURLImpl portletURL = new PortletURLImpl(
090                            request, portletId, themeDisplay.getPlid(), lifecycle);
091    
092                    if (Validator.isNotNull(cacheability)) {
093                            portletURL.setCacheability(cacheability);
094                    }
095    
096                    portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
097    
098                    if (doAsUserId > 0) {
099                            portletURL.setDoAsUserId(doAsUserId);
100                    }
101    
102                    if (Validator.isNotNull(doAsUserLanguageId)) {
103                            portletURL.setDoAsUserLanguageId(doAsUserLanguageId);
104                    }
105    
106                    portletURL.setEncrypt(encrypt);
107                    portletURL.setEscapeXml(escapeXml);
108    
109                    if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
110                            Validator.isNotNull(name)) {
111    
112                            portletURL.setParameter(ActionRequest.ACTION_NAME, name);
113                    }
114    
115                    portletURL.setPortletId(portletId);
116    
117                    if (portletConfiguration) {
118                            String portletResource = ParamUtil.getString(
119                                    request, "portletResource");
120                            String previewWidth = ParamUtil.getString(request, "previewWidth");
121    
122                            portletURL.setParameter(
123                                    "struts_action", "/portlet_configuration/edit_configuration");
124                            portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
125                            portletURL.setParameter("portletResource", portletResource);
126                            portletURL.setParameter("previewWidth", previewWidth);
127                    }
128    
129                    if (Validator.isNotNull(portletMode)) {
130                            portletURL.setPortletMode(
131                                    PortletModeFactory.getPortletMode(portletMode));
132                    }
133    
134                    if (Validator.isNotNull(resourceId)) {
135                            portletURL.setResourceID(resourceId);
136                    }
137    
138                    if (!themeDisplay.isStateMaximized()) {
139                            if (Validator.isNotNull(returnToFullPageURL)) {
140                                    portletURL.setParameter(
141                                            "returnToFullPageURL", returnToFullPageURL);
142                            }
143                    }
144    
145                    portletURL.setSecure(secure);
146    
147                    if (Validator.isNotNull(windowState)) {
148                            portletURL.setWindowState(
149                                    WindowStateFactory.getWindowState(windowState));
150                    }
151    
152                    String parameterMapString = ParamUtil.getString(
153                            request, "parameterMap");
154    
155                    if (Validator.isNotNull(parameterMapString)) {
156                            Map<String, String> parameterMap =
157                                    (Map<String, String>)JSONFactoryUtil.deserialize(
158                                            parameterMapString);
159    
160                            for (Map.Entry<String, String> entry : parameterMap.entrySet()) {
161                                    portletURL.setParameter(entry.getKey(), entry.getValue());
162                            }
163                    }
164    
165                    return portletURL.toString();
166            }
167    
168    }