1   /**
2    * Copyright (c) 2000-2008 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.util.BooleanWrapper;
26  import com.liferay.portal.kernel.util.LongWrapper;
27  import com.liferay.portal.kernel.util.MethodInvoker;
28  import com.liferay.portal.kernel.util.MethodWrapper;
29  import com.liferay.portal.kernel.util.NullWrapper;
30  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
33  
34  import java.util.Map;
35  
36  import javax.portlet.PortletRequest;
37  
38  import javax.servlet.jsp.JspException;
39  import javax.servlet.jsp.PageContext;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
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, String portletName,
57              Boolean anchor, Boolean encrypt, long doAsUserId,
58              Boolean portletConfiguration, Map<String, String[]> params,
59              boolean writeOutput, PageContext pageContext)
60          throws Exception {
61  
62          Object returnObj = null;
63  
64          Thread currentThread = Thread.currentThread();
65  
66          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
67  
68          try {
69              currentThread.setContextClassLoader(
70                  PortalClassLoaderUtil.getClassLoader());
71  
72              Object windowStateWrapper = windowState;
73  
74              if (windowStateWrapper == null) {
75                  windowStateWrapper = new NullWrapper(String.class.getName());
76              }
77  
78              Object portletModeWrapper = portletMode;
79  
80              if (portletModeWrapper == null) {
81                  portletModeWrapper = new NullWrapper(String.class.getName());
82              }
83  
84              Object varWrapper = var;
85  
86              if (varWrapper == null) {
87                  varWrapper = new NullWrapper(String.class.getName());
88              }
89  
90              Object varImplWrapper = varImpl;
91  
92              if (varImplWrapper == null) {
93                  varImplWrapper = new NullWrapper(String.class.getName());
94              }
95  
96              Object secureWrapper = secure;
97  
98              if (secureWrapper == null) {
99                  secureWrapper = new NullWrapper(Boolean.class.getName());
100             }
101 
102             Object copyCurrentRenderParametersWrapper =
103                 copyCurrentRenderParameters;
104 
105             if (copyCurrentRenderParametersWrapper == null) {
106                 copyCurrentRenderParametersWrapper = new NullWrapper(
107                     Boolean.class.getName());
108             }
109 
110             Object escapeXmlWrapper = escapeXml;
111 
112             if (escapeXmlWrapper == null) {
113                 escapeXmlWrapper = new NullWrapper(Boolean.class.getName());
114             }
115 
116             Object nameWrapper = name;
117 
118             if (nameWrapper == null) {
119                 nameWrapper = new NullWrapper(String.class.getName());
120             }
121 
122             Object resourceIDWrapper = resourceID;
123 
124             if (resourceIDWrapper == null) {
125                 resourceIDWrapper = new NullWrapper(String.class.getName());
126             }
127 
128             Object cacheabilityWrapper = cacheability;
129 
130             if (cacheabilityWrapper == null) {
131                 cacheabilityWrapper = new NullWrapper(String.class.getName());
132             }
133 
134             Object portletNameWrapper = portletName;
135 
136             if (portletNameWrapper == null) {
137                 portletNameWrapper = new NullWrapper(String.class.getName());
138             }
139 
140             Object anchorWrapper = anchor;
141 
142             if (anchorWrapper == null) {
143                 anchorWrapper = new NullWrapper(Boolean.class.getName());
144             }
145 
146             Object encryptWrapper = encrypt;
147 
148             if (encryptWrapper == null) {
149                 encryptWrapper = new NullWrapper(Boolean.class.getName());
150             }
151 
152             Object portletConfigurationWrapper = portletConfiguration;
153 
154             if (portletConfigurationWrapper == null) {
155                 portletConfigurationWrapper = new NullWrapper(
156                     Boolean.class.getName());
157             }
158 
159             Object paramsWrapper = params;
160 
161             if (paramsWrapper == null) {
162                 paramsWrapper = new NullWrapper(Map.class.getName());
163             }
164 
165             MethodWrapper methodWrapper = new MethodWrapper(
166                 _TAG_CLASS, _TAG_DO_END_METHOD,
167                 new Object[] {
168                     lifecycle, windowStateWrapper, portletModeWrapper,
169                     varWrapper, varImplWrapper, secureWrapper,
170                     copyCurrentRenderParametersWrapper, escapeXmlWrapper,
171                     nameWrapper, resourceIDWrapper, cacheabilityWrapper,
172                     portletNameWrapper, anchorWrapper, encryptWrapper,
173                     new LongWrapper(doAsUserId), portletConfigurationWrapper,
174                     paramsWrapper, new BooleanWrapper(writeOutput), pageContext
175                 });
176 
177             returnObj = MethodInvoker.invoke(methodWrapper);
178         }
179         catch (Exception e) {
180             _log.error(e, e);
181 
182             throw e;
183         }
184         finally {
185             currentThread.setContextClassLoader(contextClassLoader);
186         }
187 
188         if (returnObj != null) {
189             return returnObj.toString();
190         }
191         else {
192             return StringPool.BLANK;
193         }
194     }
195 
196     public int doEndTag() throws JspException {
197         try {
198             doTag(
199                 getLifecycle(), _windowState, _portletMode, _var, _varImpl,
200                 _secure, _copyCurrentRenderParameters, _escapeXml, _name,
201                 _resourceID, _cacheability, _portletName, _anchor, _encrypt,
202                 _doAsUserId, _portletConfiguration, getParams(), true,
203                 pageContext);
204         }
205         catch (Exception e) {
206             if (e instanceof JspException) {
207                 throw (JspException)e;
208             }
209             else {
210                 throw new JspException(e);
211             }
212         }
213         finally {
214             clearParams();
215             clearProperties();
216         }
217 
218         return EVAL_PAGE;
219     }
220 
221     public String getLifecycle() {
222         return PortletRequest.ACTION_PHASE;
223     }
224 
225     public void setWindowState(String windowState) {
226         _windowState = windowState;
227     }
228 
229     public void setPortletMode(String portletMode) {
230         _portletMode = portletMode;
231     }
232 
233     public void setVar(String var) {
234         _var = var;
235     }
236 
237     public void setVarImpl(String varImpl) {
238         _varImpl = varImpl;
239     }
240 
241     public void setSecure(boolean secure) {
242         _secure = Boolean.valueOf(secure);
243     }
244 
245     public void setCopyCurrentRenderParameters(
246         boolean copyCurrentRenderParameters) {
247 
248         _copyCurrentRenderParameters = Boolean.valueOf(
249             copyCurrentRenderParameters);
250     }
251 
252     public void setEscapeXml(boolean escapeXml) {
253         _escapeXml = Boolean.valueOf(escapeXml);
254     }
255 
256     public void setName(String name) {
257         _name = name;
258     }
259 
260     public void setId(String resourceID) {
261         _resourceID = resourceID;
262     }
263 
264     public void setCacheability(String cacheability) {
265         _cacheability = cacheability;
266     }
267 
268     public void setPortletName(String portletName) {
269         _portletName = portletName;
270     }
271 
272     public void setAnchor(boolean anchor) {
273         _anchor = Boolean.valueOf(anchor);
274     }
275 
276     public void setEncrypt(boolean encrypt) {
277         _encrypt = Boolean.valueOf(encrypt);
278     }
279 
280     public void setDoAsUserId(long doAsUserId) {
281         _doAsUserId = doAsUserId;
282     }
283 
284     public void setPortletConfiguration(boolean portletConfiguration) {
285         _portletConfiguration = Boolean.valueOf(portletConfiguration);
286     }
287 
288     private static final String _TAG_CLASS =
289         "com.liferay.portal.servlet.taglib.portlet.ActionURLTagUtil";
290 
291     private static final String _TAG_DO_END_METHOD = "doEndTag";
292 
293     private static Log _log = LogFactory.getLog(ActionURLTag.class);
294 
295     private String _windowState;
296     private String _portletMode;
297     private String _var;
298     private String _varImpl;
299     private Boolean _secure;
300     private Boolean _copyCurrentRenderParameters;
301     private Boolean _escapeXml;
302     private String  _name;
303     private String _resourceID;
304     private String _cacheability;
305     private String _portletName;
306     private Boolean _anchor;
307     private Boolean _encrypt;
308     private long _doAsUserId;
309     private Boolean _portletConfiguration;
310 
311 }