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.Iterator;
029    import java.util.Map;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.PortletRequest;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    import org.apache.struts.action.Action;
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Eduardo Lundgren
044     */
045    public class PortletURLAction extends Action {
046    
047            public ActionForward execute(
048                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
049                            HttpServletResponse response)
050                    throws Exception {
051    
052                    try {
053                            String portletURL = getPortletURL(request);
054    
055                            ServletResponseUtil.write(response, portletURL);
056                    }
057                    catch (Exception e) {
058                            PortalUtil.sendError(e, request, response);
059                    }
060    
061                    return null;
062            }
063    
064            protected String getPortletURL(HttpServletRequest request)
065                    throws Exception {
066    
067                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
068                            WebKeys.THEME_DISPLAY);
069    
070                    String cacheability = ParamUtil.getString(request, "cacheability");
071                    boolean copyCurrentRenderParameters = ParamUtil.getBoolean(
072                            request, "copyCurrentRenderParameters");
073                    long doAsUserId = ParamUtil.getLong(request, "doAsUserId");
074                    String doAsUserLanguageId = ParamUtil.getString(
075                            request, "doAsUserLanguageId");
076                    boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
077                    boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
078                    String lifecycle = ParamUtil.getString(request, "lifecycle");
079                    String name = ParamUtil.getString(request, "name");
080                    boolean portletConfiguration = ParamUtil.getBoolean(
081                            request, "portletConfiguration");
082                    String portletId = ParamUtil.getString(request, "portletId");
083                    String portletMode = ParamUtil.getString(request, "portletMode");
084                    String resourceId = ParamUtil.getString(request, "resourceId");
085                    String returnToFullPageURL = ParamUtil.getString(
086                            request, "returnToFullPageURL");
087                    boolean secure = ParamUtil.getBoolean(request, "secure");
088                    String windowState = ParamUtil.getString(request, "windowState");
089    
090                    PortletURLImpl portletURL = new PortletURLImpl(
091                            request, portletId, themeDisplay.getPlid(), lifecycle);
092    
093                    if (Validator.isNotNull(cacheability)) {
094                            portletURL.setCacheability(cacheability);
095                    }
096    
097                    portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
098    
099                    if (doAsUserId > 0) {
100                            portletURL.setDoAsUserId(doAsUserId);
101                    }
102    
103                    if (Validator.isNotNull(doAsUserLanguageId)) {
104                            portletURL.setDoAsUserLanguageId(doAsUserLanguageId);
105                    }
106    
107                    portletURL.setEncrypt(encrypt);
108                    portletURL.setEscapeXml(escapeXml);
109    
110                    if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
111                            Validator.isNotNull(name)) {
112    
113                            portletURL.setParameter(ActionRequest.ACTION_NAME, name);
114                    }
115    
116                    portletURL.setPortletId(portletId);
117    
118                    if (portletConfiguration) {
119                            String portletResource = ParamUtil.getString(
120                                    request, "portletResource");
121                            String previewWidth = ParamUtil.getString(request, "previewWidth");
122    
123                            portletURL.setParameter(
124                                    "struts_action", "/portlet_configuration/edit_configuration");
125                            portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
126                            portletURL.setParameter("portletResource", portletResource);
127                            portletURL.setParameter("previewWidth", previewWidth);
128                    }
129    
130                    if (Validator.isNotNull(portletMode)) {
131                            portletURL.setPortletMode(
132                                    PortletModeFactory.getPortletMode(portletMode));
133                    }
134    
135                    if (Validator.isNotNull(resourceId)) {
136                            portletURL.setResourceID(resourceId);
137                    }
138    
139                    if (!themeDisplay.isStateMaximized()) {
140                            if (Validator.isNotNull(returnToFullPageURL)) {
141                                    portletURL.setParameter(
142                                            "returnToFullPageURL", returnToFullPageURL);
143                            }
144                    }
145    
146                    portletURL.setSecure(secure);
147    
148                    if (Validator.isNotNull(windowState)) {
149                            portletURL.setWindowState(
150                                    WindowStateFactory.getWindowState(windowState));
151                    }
152    
153                    String parameterMapString = ParamUtil.getString(
154                            request, "parameterMap");
155    
156                    if (Validator.isNotNull(parameterMapString)) {
157                            Map<String, String> parameterMap =
158                                    (Map<String, String>)JSONFactoryUtil.deserialize(
159                                            parameterMapString);
160    
161                            Iterator<String> itr = parameterMap.keySet().iterator();
162    
163                            while (itr.hasNext()) {
164                                    String paramName = itr.next();
165    
166                                    String paramValue = parameterMap.get(paramName);
167    
168                                    portletURL.setParameter(paramName, paramValue);
169                            }
170                    }
171    
172                    return portletURL.toString();
173            }
174    
175    }