001    /**
002     * Copyright (c) 2000-present 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.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.PortletURLImpl;
028    
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     * @deprecated As of 6.2.0, with no direct replacement
045     */
046    @Deprecated
047    public class PortletURLAction extends Action {
048    
049            @Override
050            public ActionForward execute(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            HttpServletRequest request, HttpServletResponse response)
053                    throws Exception {
054    
055                    if (!PropsValues.PORTLET_URL_GENERATE_BY_PATH_ENABLED) {
056                            response.sendError(HttpServletResponse.SC_FORBIDDEN);
057    
058                            return null;
059                    }
060    
061                    try {
062                            String portletURL = getPortletURL(request);
063    
064                            ServletResponseUtil.write(response, portletURL);
065                    }
066                    catch (Exception e) {
067                            PortalUtil.sendError(e, request, response);
068                    }
069    
070                    return null;
071            }
072    
073            protected String getPortletURL(HttpServletRequest request)
074                    throws Exception {
075    
076                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
077                            WebKeys.THEME_DISPLAY);
078    
079                    String cacheability = ParamUtil.getString(request, "cacheability");
080                    boolean copyCurrentRenderParameters = ParamUtil.getBoolean(
081                            request, "copyCurrentRenderParameters");
082                    long doAsGroupId = ParamUtil.getLong(request, "doAsGroupId");
083                    long doAsUserId = ParamUtil.getLong(request, "doAsUserId");
084                    String doAsUserLanguageId = ParamUtil.getString(
085                            request, "doAsUserLanguageId");
086                    //boolean encrypt = ParamUtil.getBoolean(request, "encrypt");
087                    boolean escapeXml = ParamUtil.getBoolean(request, "escapeXml");
088                    String lifecycle = ParamUtil.getString(request, "lifecycle");
089                    String name = ParamUtil.getString(request, "name");
090                    boolean portletConfiguration = ParamUtil.getBoolean(
091                            request, "portletConfiguration");
092                    String portletId = ParamUtil.getString(request, "portletId");
093                    String portletMode = ParamUtil.getString(request, "portletMode");
094                    String resourceId = ParamUtil.getString(request, "resourceId");
095                    String returnToFullPageURL = ParamUtil.getString(
096                            request, "returnToFullPageURL");
097                    boolean secure = ParamUtil.getBoolean(
098                            request, "secure", request.isSecure());
099                    String windowState = ParamUtil.getString(request, "windowState");
100    
101                    PortletURLImpl portletURL = new PortletURLImpl(
102                            request, portletId, themeDisplay.getPlid(), lifecycle);
103    
104                    if (Validator.isNotNull(cacheability)) {
105                            portletURL.setCacheability(cacheability);
106                    }
107    
108                    portletURL.setCopyCurrentRenderParameters(copyCurrentRenderParameters);
109    
110                    if (doAsGroupId > 0) {
111                            portletURL.setDoAsGroupId(doAsGroupId);
112                    }
113    
114                    if (doAsUserId > 0) {
115                            //portletURL.setDoAsUserId(doAsUserId);
116                    }
117    
118                    if (Validator.isNotNull(doAsUserLanguageId)) {
119                            portletURL.setDoAsUserLanguageId(doAsUserLanguageId);
120                    }
121    
122                    //portletURL.setEncrypt(encrypt);
123                    portletURL.setEscapeXml(escapeXml);
124    
125                    if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
126                            Validator.isNotNull(name)) {
127    
128                            portletURL.setParameter(ActionRequest.ACTION_NAME, name);
129                    }
130    
131                    portletURL.setPortletId(portletId);
132    
133                    if (portletConfiguration) {
134                            String portletResource = ParamUtil.getString(
135                                    request, "portletResource");
136                            String previewWidth = ParamUtil.getString(request, "previewWidth");
137    
138                            portletURL.setParameter("mvcPath", "/edit_configuration.jsp");
139                            portletURL.setParameter("returnToFullPageURL", returnToFullPageURL);
140                            portletURL.setParameter("portletResource", portletResource);
141                            portletURL.setParameter("previewWidth", previewWidth);
142                    }
143    
144                    if (Validator.isNotNull(portletMode)) {
145                            portletURL.setPortletMode(
146                                    PortletModeFactory.getPortletMode(portletMode));
147                    }
148    
149                    if (Validator.isNotNull(resourceId)) {
150                            portletURL.setResourceID(resourceId);
151                    }
152    
153                    if (!themeDisplay.isStateMaximized()) {
154                            if (Validator.isNotNull(returnToFullPageURL)) {
155                                    portletURL.setParameter(
156                                            "returnToFullPageURL", returnToFullPageURL);
157                            }
158                    }
159    
160                    portletURL.setSecure(secure);
161    
162                    if (Validator.isNotNull(windowState)) {
163                            portletURL.setWindowState(
164                                    WindowStateFactory.getWindowState(windowState));
165                    }
166    
167                    String parameterMapString = ParamUtil.getString(
168                            request, "parameterMap");
169    
170                    if (Validator.isNotNull(parameterMapString)) {
171                            Map<String, String> parameterMap =
172                                    (Map<String, String>)JSONFactoryUtil.deserialize(
173                                            parameterMapString);
174    
175                            for (Map.Entry<String, String> entry : parameterMap.entrySet()) {
176                                    String key = entry.getKey();
177                                    String value = entry.getValue();
178    
179                                    if ((key != null) && (value != null)) {
180                                            portletURL.setParameter(key, value);
181                                    }
182                            }
183                    }
184    
185                    return portletURL.toString();
186            }
187    
188    }