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