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.taglib.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.DummyPortletURL;
020    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
021    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
022    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
023    import com.liferay.portal.kernel.portlet.PortletModeFactory;
024    import com.liferay.portal.kernel.portlet.WindowStateFactory;
025    import com.liferay.portal.kernel.util.JavaConstants;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.LayoutConstants;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.PortletPreferencesFactoryConstants;
031    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
032    
033    import java.util.Map;
034    import java.util.Map.Entry;
035    import java.util.Set;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.PortletRequest;
039    import javax.portlet.PortletResponse;
040    import javax.portlet.PortletURL;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.jsp.JspException;
044    import javax.servlet.jsp.JspWriter;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class ActionURLTag extends ParamAndPropertyAncestorTagImpl {
050    
051            public static PortletURL doTag(
052                            String lifecycle, String windowState, String portletMode,
053                            Boolean secure, Boolean copyCurrentRenderParameters,
054                            Boolean escapeXml, String name, String resourceID,
055                            String cacheability, long plid, long refererPlid,
056                            String portletName, Boolean anchor, Boolean encrypt,
057                            long doAsGroupId, long doAsUserId, Boolean portletConfiguration,
058                            Map<String, String[]> parameterMap,
059                            Set<String> removedParameterNames, HttpServletRequest request)
060                    throws Exception {
061    
062                    if (portletName == null) {
063                            portletName = _getPortletName(request);
064                    }
065    
066                    LiferayPortletURL liferayPortletURL = _getLiferayPortletURL(
067                            request, plid, portletName, lifecycle);
068    
069                    if (liferayPortletURL == null) {
070                            _log.error(
071                                    "Render response is null because this tag is not being " +
072                                            "called within the context of a portlet");
073    
074                            return DummyPortletURL.getInstance();
075                    }
076    
077                    if (Validator.isNotNull(windowState)) {
078                            liferayPortletURL.setWindowState(
079                                    WindowStateFactory.getWindowState(windowState));
080                    }
081    
082                    if (Validator.isNotNull(portletMode)) {
083                            liferayPortletURL.setPortletMode(
084                                    PortletModeFactory.getPortletMode(portletMode));
085                    }
086    
087                    if (secure != null) {
088                            liferayPortletURL.setSecure(secure.booleanValue());
089                    }
090                    else {
091                            liferayPortletURL.setSecure(PortalUtil.isSecure(request));
092                    }
093    
094                    if (copyCurrentRenderParameters != null) {
095                            liferayPortletURL.setCopyCurrentRenderParameters(
096                                    copyCurrentRenderParameters.booleanValue());
097                    }
098    
099                    if (escapeXml != null) {
100                            liferayPortletURL.setEscapeXml(escapeXml.booleanValue());
101                    }
102    
103                    if (lifecycle.equals(PortletRequest.ACTION_PHASE) &&
104                            Validator.isNotNull(name)) {
105    
106                            liferayPortletURL.setParameter(ActionRequest.ACTION_NAME, name);
107                    }
108    
109                    if (resourceID != null) {
110                            liferayPortletURL.setResourceID(resourceID);
111                    }
112    
113                    if (cacheability != null) {
114                            liferayPortletURL.setCacheability(cacheability);
115                    }
116    
117                    if (refererPlid > LayoutConstants.DEFAULT_PLID) {
118                            liferayPortletURL.setRefererPlid(refererPlid);
119                    }
120    
121                    if (anchor != null) {
122                            liferayPortletURL.setAnchor(anchor.booleanValue());
123                    }
124    
125                    if (encrypt != null) {
126                            liferayPortletURL.setEncrypt(encrypt.booleanValue());
127                    }
128    
129                    if (doAsGroupId > 0) {
130                            liferayPortletURL.setDoAsGroupId(doAsGroupId);
131                    }
132    
133                    if (doAsUserId > 0) {
134                            liferayPortletURL.setDoAsUserId(doAsUserId);
135                    }
136    
137                    String settingsScope = null;
138    
139                    if ((portletConfiguration != null) &&
140                            portletConfiguration.booleanValue()) {
141    
142                            String returnToFullPageURL = ParamUtil.getString(
143                                    request, "returnToFullPageURL");
144                            String portletResource = ParamUtil.getString(
145                                    request, "portletResource");
146                            String previewWidth = ParamUtil.getString(request, "previewWidth");
147                            settingsScope = ParamUtil.getString(
148                                    request, "settingsScope",
149                                    PortletPreferencesFactoryConstants.
150                                            SETTINGS_SCOPE_PORTLET_INSTANCE);
151    
152                            if (Validator.isNull(name)) {
153                                    liferayPortletURL.setParameter(
154                                            ActionRequest.ACTION_NAME, "editConfiguration");
155                            }
156    
157                            liferayPortletURL.setParameter(
158                                    "mvcPath", "/edit_configuration.jsp");
159                            liferayPortletURL.setParameter(
160                                    "returnToFullPageURL", returnToFullPageURL);
161                            liferayPortletURL.setParameter(
162                                    "portletConfiguration", Boolean.TRUE.toString());
163                            liferayPortletURL.setParameter("portletResource", portletResource);
164                            liferayPortletURL.setParameter("previewWidth", previewWidth);
165                    }
166    
167                    if (parameterMap != null) {
168                            for (Entry<String, String[]> entry : parameterMap.entrySet()) {
169                                    liferayPortletURL.setParameter(
170                                            entry.getKey(), entry.getValue(), false);
171                            }
172                    }
173    
174                    if ((settingsScope != null) &&
175                            ((parameterMap == null) ||
176                             !parameterMap.containsKey("settingsScope"))) {
177    
178                            liferayPortletURL.setParameter("settingsScope", settingsScope);
179                    }
180    
181                    liferayPortletURL.setRemovedParameterNames(removedParameterNames);
182    
183                    return liferayPortletURL;
184            }
185    
186            @Override
187            public int doEndTag() throws JspException {
188                    try {
189                            PortletURL portletURL = doTag(
190                                    getLifecycle(), _windowState, _portletMode, _secure,
191                                    _copyCurrentRenderParameters, _escapeXml, _name, _resourceID,
192                                    _cacheability, _plid, _refererPlid, _portletName, _anchor,
193                                    _encrypt, _doAsGroupId, _doAsUserId, _portletConfiguration,
194                                    getParams(), getRemovedParameterNames(),
195                                    (HttpServletRequest)pageContext.getRequest());
196    
197                            if (Validator.isNotNull(_var)) {
198                                    pageContext.setAttribute(_var, portletURL.toString());
199                            }
200                            else if (Validator.isNotNull(_varImpl)) {
201                                    pageContext.setAttribute(_varImpl, portletURL);
202                            }
203                            else {
204                                    JspWriter jspWriter = pageContext.getOut();
205    
206                                    jspWriter.write(portletURL.toString());
207                            }
208    
209                            return EVAL_PAGE;
210                    }
211                    catch (Exception e) {
212                            throw new JspException(e);
213                    }
214                    finally {
215                            clearParams();
216                            clearProperties();
217    
218                            _plid = LayoutConstants.DEFAULT_PLID;
219                    }
220            }
221    
222            @Override
223            public int doStartTag() {
224                    return EVAL_BODY_INCLUDE;
225            }
226    
227            public String getLifecycle() {
228                    return PortletRequest.ACTION_PHASE;
229            }
230    
231            public void setAnchor(boolean anchor) {
232                    _anchor = Boolean.valueOf(anchor);
233            }
234    
235            public void setCacheability(String cacheability) {
236                    _cacheability = cacheability;
237            }
238    
239            public void setCopyCurrentRenderParameters(
240                    boolean copyCurrentRenderParameters) {
241    
242                    _copyCurrentRenderParameters = Boolean.valueOf(
243                            copyCurrentRenderParameters);
244            }
245    
246            public void setDoAsGroupId(long doAsGroupId) {
247                    _doAsGroupId = doAsGroupId;
248            }
249    
250            public void setDoAsUserId(long doAsUserId) {
251                    _doAsUserId = doAsUserId;
252            }
253    
254            public void setEncrypt(boolean encrypt) {
255                    _encrypt = Boolean.valueOf(encrypt);
256            }
257    
258            public void setEscapeXml(boolean escapeXml) {
259                    _escapeXml = Boolean.valueOf(escapeXml);
260            }
261    
262            public void setId(String resourceID) {
263                    _resourceID = resourceID;
264            }
265    
266            public void setName(String name) {
267                    _name = name;
268            }
269    
270            public void setPlid(long plid) {
271                    _plid = plid;
272            }
273    
274            public void setPortletConfiguration(boolean portletConfiguration) {
275                    _portletConfiguration = Boolean.valueOf(portletConfiguration);
276            }
277    
278            public void setPortletMode(String portletMode) {
279                    _portletMode = portletMode;
280            }
281    
282            public void setPortletName(String portletName) {
283                    _portletName = portletName;
284            }
285    
286            public void setRefererPlid(long refererPlid) {
287                    _refererPlid = refererPlid;
288            }
289    
290            public void setSecure(boolean secure) {
291                    _secure = Boolean.valueOf(secure);
292            }
293    
294            public void setVar(String var) {
295                    _var = var;
296            }
297    
298            public void setVarImpl(String varImpl) {
299                    _varImpl = varImpl;
300            }
301    
302            public void setWindowState(String windowState) {
303                    _windowState = windowState;
304            }
305    
306            private static LiferayPortletURL _getLiferayPortletURL(
307                    HttpServletRequest request, long plid, String portletName,
308                    String lifecycle) {
309    
310                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
311                            JavaConstants.JAVAX_PORTLET_REQUEST);
312    
313                    if (portletRequest == null) {
314                            return null;
315                    }
316    
317                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
318                            JavaConstants.JAVAX_PORTLET_RESPONSE);
319    
320                    LiferayPortletResponse liferayPortletResponse =
321                            PortalUtil.getLiferayPortletResponse(portletResponse);
322    
323                    return liferayPortletResponse.createLiferayPortletURL(
324                            plid, portletName, lifecycle);
325            }
326    
327            private static String _getPortletName(HttpServletRequest request) {
328                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
329                            JavaConstants.JAVAX_PORTLET_REQUEST);
330    
331                    if (portletRequest == null) {
332                            return null;
333                    }
334    
335                    LiferayPortletConfig liferayPortletConfig =
336                            (LiferayPortletConfig)request.getAttribute(
337                                    JavaConstants.JAVAX_PORTLET_CONFIG);
338    
339                    return liferayPortletConfig.getPortletId();
340            }
341    
342            private static final Log _log = LogFactoryUtil.getLog(ActionURLTag.class);
343    
344            private Boolean _anchor;
345            private String _cacheability;
346            private Boolean _copyCurrentRenderParameters;
347            private long _doAsGroupId;
348            private long _doAsUserId;
349            private Boolean _encrypt;
350            private Boolean _escapeXml;
351            private String _name;
352            private long _plid = LayoutConstants.DEFAULT_PLID;
353            private Boolean _portletConfiguration;
354            private String _portletMode;
355            private String _portletName;
356            private long _refererPlid = LayoutConstants.DEFAULT_PLID;
357            private String _resourceID;
358            private Boolean _secure;
359            private String _var;
360            private String _varImpl;
361            private String _windowState;
362    
363    }