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