001    /**
002     * Copyright (c) 2000-2012 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    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.PortletRequest;
036    import javax.portlet.PortletResponse;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.jsp.JspException;
040    import javax.servlet.jsp.JspWriter;
041    import javax.servlet.jsp.PageContext;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class ActionURLTag extends ParamAndPropertyAncestorTagImpl {
047    
048            public static void doTag(
049                            String lifecycle, String windowState, String portletMode,
050                            String var, String varImpl, Boolean secure,
051                            Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
052                            String resourceID, String cacheability, long plid, long refererPlid,
053                            String portletName, Boolean anchor, Boolean encrypt,
054                            long doAsGroupId, long doAsUserId, Boolean portletConfiguration,
055                            Map<String, String[]> params, PageContext pageContext)
056                    throws Exception {
057    
058                    HttpServletRequest request =
059                            (HttpServletRequest)pageContext.getRequest();
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;
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 (params != null) {
154                            MapUtil.merge(liferayPortletURL.getParameterMap(), params);
155    
156                            liferayPortletURL.setParameters(params);
157                    }
158    
159                    String portletURLToString = liferayPortletURL.toString();
160    
161                    if (Validator.isNotNull(var)) {
162                            pageContext.setAttribute(var, portletURLToString);
163                    }
164                    else if (Validator.isNotNull(varImpl)) {
165                            pageContext.setAttribute(varImpl, liferayPortletURL);
166                    }
167                    else {
168                            JspWriter jspWriter = pageContext.getOut();
169    
170                            jspWriter.write(portletURLToString);
171                    }
172            }
173    
174            @Override
175            public int doEndTag() throws JspException {
176                    try {
177                            doTag(
178                                    getLifecycle(), _windowState, _portletMode, _var, _varImpl,
179                                    _secure, _copyCurrentRenderParameters, _escapeXml, _name,
180                                    _resourceID, _cacheability, _plid, _refererPlid, _portletName,
181                                    _anchor, _encrypt, _doAsGroupId, _doAsUserId,
182                                    _portletConfiguration, getParams(), pageContext);
183    
184                            return EVAL_PAGE;
185                    }
186                    catch (Exception e) {
187                            throw new JspException(e);
188                    }
189                    finally {
190                            clearParams();
191                            clearProperties();
192    
193                            _plid = LayoutConstants.DEFAULT_PLID;
194                    }
195            }
196    
197            @Override
198            public int doStartTag() {
199                    return EVAL_BODY_INCLUDE;
200            }
201    
202            public String getLifecycle() {
203                    return PortletRequest.ACTION_PHASE;
204            }
205    
206            public void setAnchor(boolean anchor) {
207                    _anchor = Boolean.valueOf(anchor);
208            }
209    
210            public void setCacheability(String cacheability) {
211                    _cacheability = cacheability;
212            }
213    
214            public void setCopyCurrentRenderParameters(
215                    boolean copyCurrentRenderParameters) {
216    
217                    _copyCurrentRenderParameters = Boolean.valueOf(
218                            copyCurrentRenderParameters);
219            }
220    
221            public void setDoAsGroupId(long doAsGroupId) {
222                    _doAsGroupId = doAsGroupId;
223            }
224    
225            public void setDoAsUserId(long doAsUserId) {
226                    _doAsUserId = doAsUserId;
227            }
228    
229            public void setEncrypt(boolean encrypt) {
230                    _encrypt = Boolean.valueOf(encrypt);
231            }
232    
233            public void setEscapeXml(boolean escapeXml) {
234                    _escapeXml = Boolean.valueOf(escapeXml);
235            }
236    
237            public void setId(String resourceID) {
238                    _resourceID = resourceID;
239            }
240    
241            public void setName(String name) {
242                    _name = name;
243            }
244    
245            public void setPlid(long plid) {
246                    _plid = plid;
247            }
248    
249            public void setPortletConfiguration(boolean portletConfiguration) {
250                    _portletConfiguration = Boolean.valueOf(portletConfiguration);
251            }
252    
253            public void setPortletMode(String portletMode) {
254                    _portletMode = portletMode;
255            }
256    
257            public void setPortletName(String portletName) {
258                    _portletName = portletName;
259            }
260    
261            public void setRefererPlid(long refererPlid) {
262                    _refererPlid = refererPlid;
263            }
264    
265            public void setSecure(boolean secure) {
266                    _secure = Boolean.valueOf(secure);
267            }
268    
269            public void setVar(String var) {
270                    _var = var;
271            }
272    
273            public void setVarImpl(String varImpl) {
274                    _varImpl = varImpl;
275            }
276    
277            public void setWindowState(String windowState) {
278                    _windowState = windowState;
279            }
280    
281            private static LiferayPortletURL _getLiferayPortletURL(
282                    HttpServletRequest request, long plid, String portletName,
283                    String lifecycle) {
284    
285                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
286                            JavaConstants.JAVAX_PORTLET_REQUEST);
287    
288                    if (portletRequest == null) {
289                            return null;
290                    }
291    
292                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
293                            JavaConstants.JAVAX_PORTLET_RESPONSE);
294    
295                    LiferayPortletResponse liferayPortletResponse =
296                            PortalUtil.getLiferayPortletResponse(portletResponse);
297    
298                    return liferayPortletResponse.createLiferayPortletURL(
299                            plid, portletName, lifecycle);
300            }
301    
302            private static String _getPortletName(HttpServletRequest request) {
303                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
304                            JavaConstants.JAVAX_PORTLET_REQUEST);
305    
306                    if (portletRequest == null) {
307                            return null;
308                    }
309    
310                    LiferayPortletConfig liferayPortletConfig =
311                            (LiferayPortletConfig)request.getAttribute(
312                                    JavaConstants.JAVAX_PORTLET_CONFIG);
313    
314                    return liferayPortletConfig.getPortletId();
315            }
316    
317            private static Log _log = LogFactoryUtil.getLog(ActionURLTag.class);
318    
319            private Boolean _anchor;
320            private String _cacheability;
321            private Boolean _copyCurrentRenderParameters;
322            private long _doAsGroupId;
323            private long _doAsUserId;
324            private Boolean _encrypt;
325            private Boolean _escapeXml;
326            private String _name;
327            private long _plid = LayoutConstants.DEFAULT_PLID;
328            private Boolean _portletConfiguration;
329            private String _portletMode;
330            private String _portletName;
331            private long _refererPlid = LayoutConstants.DEFAULT_PLID;
332            private String _resourceID;
333            private Boolean _secure;
334            private String _var;
335            private String _varImpl;
336            private String _windowState;
337    
338    }