1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.BooleanWrapper;
28  import com.liferay.portal.kernel.util.LongWrapper;
29  import com.liferay.portal.kernel.util.MethodInvoker;
30  import com.liferay.portal.kernel.util.MethodWrapper;
31  import com.liferay.portal.kernel.util.NullWrapper;
32  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.model.LayoutConstants;
35  import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
36  
37  import java.util.Map;
38  
39  import javax.portlet.PortletRequest;
40  
41  import javax.servlet.jsp.JspException;
42  import javax.servlet.jsp.PageContext;
43  
44  /**
45   * <a href="ActionURLTag.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ActionURLTag extends ParamAndPropertyAncestorTagImpl {
51  
52      public static String doTag(
53              String lifecycle, String windowState, String portletMode,
54              String var, String varImpl, Boolean secure,
55              Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
56              String resourceID, String cacheability, long plid,
57              String portletName, Boolean anchor, Boolean encrypt,
58              long doAsUserId, Boolean portletConfiguration,
59              Map<String, String[]> params, boolean writeOutput,
60              PageContext pageContext)
61          throws Exception {
62  
63          Object returnObj = null;
64  
65          Thread currentThread = Thread.currentThread();
66  
67          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
68  
69          try {
70              currentThread.setContextClassLoader(
71                  PortalClassLoaderUtil.getClassLoader());
72  
73              Object windowStateWrapper = windowState;
74  
75              if (windowStateWrapper == null) {
76                  windowStateWrapper = new NullWrapper(String.class.getName());
77              }
78  
79              Object portletModeWrapper = portletMode;
80  
81              if (portletModeWrapper == null) {
82                  portletModeWrapper = new NullWrapper(String.class.getName());
83              }
84  
85              Object varWrapper = var;
86  
87              if (varWrapper == null) {
88                  varWrapper = new NullWrapper(String.class.getName());
89              }
90  
91              Object varImplWrapper = varImpl;
92  
93              if (varImplWrapper == null) {
94                  varImplWrapper = new NullWrapper(String.class.getName());
95              }
96  
97              Object secureWrapper = secure;
98  
99              if (secureWrapper == null) {
100                 secureWrapper = new NullWrapper(Boolean.class.getName());
101             }
102 
103             Object copyCurrentRenderParametersWrapper =
104                 copyCurrentRenderParameters;
105 
106             if (copyCurrentRenderParametersWrapper == null) {
107                 copyCurrentRenderParametersWrapper = new NullWrapper(
108                     Boolean.class.getName());
109             }
110 
111             Object escapeXmlWrapper = escapeXml;
112 
113             if (escapeXmlWrapper == null) {
114                 escapeXmlWrapper = new NullWrapper(Boolean.class.getName());
115             }
116 
117             Object nameWrapper = name;
118 
119             if (nameWrapper == null) {
120                 nameWrapper = new NullWrapper(String.class.getName());
121             }
122 
123             Object resourceIDWrapper = resourceID;
124 
125             if (resourceIDWrapper == null) {
126                 resourceIDWrapper = new NullWrapper(String.class.getName());
127             }
128 
129             Object cacheabilityWrapper = cacheability;
130 
131             if (cacheabilityWrapper == null) {
132                 cacheabilityWrapper = new NullWrapper(String.class.getName());
133             }
134 
135             Object portletNameWrapper = portletName;
136 
137             if (portletNameWrapper == null) {
138                 portletNameWrapper = new NullWrapper(String.class.getName());
139             }
140 
141             Object anchorWrapper = anchor;
142 
143             if (anchorWrapper == null) {
144                 anchorWrapper = new NullWrapper(Boolean.class.getName());
145             }
146 
147             Object encryptWrapper = encrypt;
148 
149             if (encryptWrapper == null) {
150                 encryptWrapper = new NullWrapper(Boolean.class.getName());
151             }
152 
153             Object portletConfigurationWrapper = portletConfiguration;
154 
155             if (portletConfigurationWrapper == null) {
156                 portletConfigurationWrapper = new NullWrapper(
157                     Boolean.class.getName());
158             }
159 
160             Object paramsWrapper = params;
161 
162             if (paramsWrapper == null) {
163                 paramsWrapper = new NullWrapper(Map.class.getName());
164             }
165 
166             MethodWrapper methodWrapper = new MethodWrapper(
167                 _TAG_CLASS, _TAG_DO_END_METHOD,
168                 new Object[] {
169                     lifecycle, windowStateWrapper, portletModeWrapper,
170                     varWrapper, varImplWrapper, secureWrapper,
171                     copyCurrentRenderParametersWrapper, escapeXmlWrapper,
172                     nameWrapper, resourceIDWrapper, cacheabilityWrapper,
173                     new LongWrapper(plid), portletNameWrapper, anchorWrapper,
174                     encryptWrapper, new LongWrapper(doAsUserId),
175                     portletConfigurationWrapper, paramsWrapper,
176                     new BooleanWrapper(writeOutput), pageContext
177                 });
178 
179             returnObj = MethodInvoker.invoke(methodWrapper);
180         }
181         catch (Exception e) {
182             _log.error(e, e);
183 
184             throw e;
185         }
186         finally {
187             currentThread.setContextClassLoader(contextClassLoader);
188         }
189 
190         if (returnObj != null) {
191             return returnObj.toString();
192         }
193         else {
194             return StringPool.BLANK;
195         }
196     }
197 
198     public int doEndTag() throws JspException {
199         try {
200             doTag(
201                 getLifecycle(), _windowState, _portletMode, _var, _varImpl,
202                 _secure, _copyCurrentRenderParameters, _escapeXml, _name,
203                 _resourceID, _cacheability, _plid, _portletName, _anchor,
204                 _encrypt, _doAsUserId, _portletConfiguration, getParams(), true,
205                 pageContext);
206         }
207         catch (Exception e) {
208             if (e instanceof JspException) {
209                 throw (JspException)e;
210             }
211             else {
212                 throw new JspException(e);
213             }
214         }
215         finally {
216             clearParams();
217             clearProperties();
218 
219             _plid = LayoutConstants.DEFAULT_PLID;
220         }
221 
222         return EVAL_PAGE;
223     }
224 
225     public String getLifecycle() {
226         return PortletRequest.ACTION_PHASE;
227     }
228 
229     public void setWindowState(String windowState) {
230         _windowState = windowState;
231     }
232 
233     public void setPortletMode(String portletMode) {
234         _portletMode = portletMode;
235     }
236 
237     public void setVar(String var) {
238         _var = var;
239     }
240 
241     public void setVarImpl(String varImpl) {
242         _varImpl = varImpl;
243     }
244 
245     public void setSecure(boolean secure) {
246         _secure = Boolean.valueOf(secure);
247     }
248 
249     public void setCopyCurrentRenderParameters(
250         boolean copyCurrentRenderParameters) {
251 
252         _copyCurrentRenderParameters = Boolean.valueOf(
253             copyCurrentRenderParameters);
254     }
255 
256     public void setEscapeXml(boolean escapeXml) {
257         _escapeXml = Boolean.valueOf(escapeXml);
258     }
259 
260     public void setName(String name) {
261         _name = name;
262     }
263 
264     public void setId(String resourceID) {
265         _resourceID = resourceID;
266     }
267 
268     public void setCacheability(String cacheability) {
269         _cacheability = cacheability;
270     }
271 
272     public void setPlid(long plid) {
273         _plid = plid;
274     }
275 
276     public void setPortletName(String portletName) {
277         _portletName = portletName;
278     }
279 
280     public void setAnchor(boolean anchor) {
281         _anchor = Boolean.valueOf(anchor);
282     }
283 
284     public void setEncrypt(boolean encrypt) {
285         _encrypt = Boolean.valueOf(encrypt);
286     }
287 
288     public void setDoAsUserId(long doAsUserId) {
289         _doAsUserId = doAsUserId;
290     }
291 
292     public void setPortletConfiguration(boolean portletConfiguration) {
293         _portletConfiguration = Boolean.valueOf(portletConfiguration);
294     }
295 
296     private static final String _TAG_CLASS =
297         "com.liferay.portal.servlet.taglib.portlet.ActionURLTagUtil";
298 
299     private static final String _TAG_DO_END_METHOD = "doEndTag";
300 
301     private static Log _log = LogFactoryUtil.getLog(ActionURLTag.class);
302 
303     private String _windowState;
304     private String _portletMode;
305     private String _var;
306     private String _varImpl;
307     private Boolean _secure;
308     private Boolean _copyCurrentRenderParameters;
309     private Boolean _escapeXml;
310     private String  _name;
311     private String _resourceID;
312     private String _cacheability;
313     private long _plid = LayoutConstants.DEFAULT_PLID;
314     private String _portletName;
315     private Boolean _anchor;
316     private Boolean _encrypt;
317     private long _doAsUserId;
318     private Boolean _portletConfiguration;
319 
320 }