1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.taglib.util;
21  
22  import com.liferay.portal.kernel.servlet.StringServletResponse;
23  import com.liferay.portal.kernel.util.HttpUtil;
24  import com.liferay.portal.model.Layout;
25  import com.liferay.portal.model.LayoutConstants;
26  import com.liferay.portal.model.Portlet;
27  import com.liferay.taglib.portlet.ActionURLTag;
28  import com.liferay.taglib.portletext.IconBackTag;
29  import com.liferay.taglib.portletext.IconCloseTag;
30  import com.liferay.taglib.portletext.IconConfigurationTag;
31  import com.liferay.taglib.portletext.IconEditDefaultsTag;
32  import com.liferay.taglib.portletext.IconEditGuestTag;
33  import com.liferay.taglib.portletext.IconEditTag;
34  import com.liferay.taglib.portletext.IconHelpTag;
35  import com.liferay.taglib.portletext.IconMaximizeTag;
36  import com.liferay.taglib.portletext.IconMinimizeTag;
37  import com.liferay.taglib.portletext.IconOptionsTag;
38  import com.liferay.taglib.portletext.IconPortletCssTag;
39  import com.liferay.taglib.portletext.IconPortletTag;
40  import com.liferay.taglib.portletext.IconPrintTag;
41  import com.liferay.taglib.portletext.IconRefreshTag;
42  import com.liferay.taglib.portletext.RuntimeTag;
43  import com.liferay.taglib.security.DoAsURLTag;
44  import com.liferay.taglib.security.PermissionsURLTag;
45  import com.liferay.taglib.theme.LayoutIconTag;
46  import com.liferay.taglib.theme.MetaTagsTag;
47  import com.liferay.taglib.theme.WrapPortletTag;
48  import com.liferay.taglib.ui.JournalContentSearchTag;
49  import com.liferay.taglib.ui.LanguageTag;
50  import com.liferay.taglib.ui.MyPlacesTag;
51  import com.liferay.taglib.ui.PngImageTag;
52  import com.liferay.taglib.ui.SearchTag;
53  import com.liferay.taglib.ui.StagingTag;
54  import com.liferay.taglib.ui.ToggleTag;
55  
56  import java.util.Map;
57  
58  import javax.portlet.PortletMode;
59  import javax.portlet.PortletRequest;
60  import javax.portlet.WindowState;
61  
62  import javax.servlet.RequestDispatcher;
63  import javax.servlet.ServletContext;
64  import javax.servlet.http.HttpServletRequest;
65  import javax.servlet.jsp.PageContext;
66  
67  /**
68   * <a href="VelocityTaglib.java.html"><b><i>View Source</i></b></a>
69   *
70   * @author Brian Wing Shun Chan
71   *
72   */
73  public class VelocityTaglib {
74  
75      public VelocityTaglib() {
76      }
77  
78      public VelocityTaglib(
79          ServletContext servletContext, HttpServletRequest request,
80          StringServletResponse stringResponse, PageContext pageContext) {
81  
82          init(servletContext, request, stringResponse, pageContext);
83      }
84  
85      public VelocityTaglib init(
86          ServletContext servletContext, HttpServletRequest request,
87          StringServletResponse stringResponse, PageContext pageContext) {
88  
89          _servletContext = servletContext;
90          _request = request;
91          _stringResponse = stringResponse;
92          _pageContext = pageContext;
93  
94          return this;
95      }
96  
97      public String actionURL(String portletName, String queryString)
98          throws Exception {
99  
100         return actionURL(
101             LayoutConstants.DEFAULT_PLID, portletName, queryString);
102     }
103 
104     public String actionURL(long plid, String portletName, String queryString)
105         throws Exception {
106 
107         String windowState = WindowState.NORMAL.toString();
108         String portletMode = PortletMode.VIEW.toString();
109 
110         return actionURL(
111             windowState, portletMode, plid, portletName, queryString);
112     }
113 
114     public String actionURL(
115             String windowState, String portletMode, String portletName,
116             String queryString)
117         throws Exception {
118 
119         return actionURL(
120             windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
121             queryString);
122     }
123 
124     public String actionURL(
125             String windowState, String portletMode, long plid,
126             String portletName, String queryString)
127         throws Exception {
128 
129         Boolean secure = null;
130         Boolean copyCurrentRenderParameters = null;
131         Boolean escapeXml = null;
132         String name = null;
133         Boolean anchor = null;
134         Boolean encrypt = null;
135         long doAsUserId = 0;
136         Boolean portletConfiguration = null;
137 
138         return actionURL(
139             windowState, portletMode, secure, copyCurrentRenderParameters,
140             escapeXml, name, plid, portletName, anchor, encrypt, doAsUserId,
141             portletConfiguration, queryString);
142     }
143 
144     public String actionURL(
145             String windowState, String portletMode, Boolean secure,
146             Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
147             long plid, String portletName, Boolean anchor, Boolean encrypt,
148             long doAsUserId, Boolean portletConfiguration, String queryString)
149         throws Exception {
150 
151         String var = null;
152         String varImpl = null;
153         String resourceID = null;
154         String cacheability = null;
155         Map<String, String[]> params = HttpUtil.parameterMapFromString(
156             queryString);
157         boolean writeOutput = false;
158 
159         return ActionURLTag.doTag(
160             PortletRequest.ACTION_PHASE, windowState, portletMode, var, varImpl,
161             secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
162             cacheability, plid, portletName, anchor, encrypt, doAsUserId,
163             portletConfiguration, params, writeOutput, _pageContext);
164     }
165 
166     public String doAsURL(long doAsUserId) throws Exception {
167         return DoAsURLTag.doTag(doAsUserId, null, false, _pageContext);
168     }
169 
170     public String iconBack() throws Exception {
171         _stringResponse.recycle();
172 
173         IconBackTag.doTag(_servletContext, _request, _stringResponse);
174 
175         return _stringResponse.getString();
176     }
177 
178     public String iconBack(String page) throws Exception {
179         _stringResponse.recycle();
180 
181         IconBackTag.doTag(page, _servletContext, _request, _stringResponse);
182 
183         return _stringResponse.getString();
184     }
185 
186     public String iconClose() throws Exception {
187         _stringResponse.recycle();
188 
189         IconCloseTag.doTag(_servletContext, _request, _stringResponse);
190 
191         return _stringResponse.getString();
192     }
193 
194     public String iconClose(String page) throws Exception {
195         _stringResponse.recycle();
196 
197         IconCloseTag.doTag(page, _servletContext, _request, _stringResponse);
198 
199         return _stringResponse.getString();
200     }
201 
202     public String iconConfiguration() throws Exception {
203         _stringResponse.recycle();
204 
205         IconConfigurationTag.doTag(_servletContext, _request, _stringResponse);
206 
207         return _stringResponse.getString();
208     }
209 
210     public String iconConfiguration(String page) throws Exception {
211         _stringResponse.recycle();
212 
213         IconConfigurationTag.doTag(
214             page, _servletContext, _request, _stringResponse);
215 
216         return _stringResponse.getString();
217     }
218 
219     public String iconEdit() throws Exception {
220         _stringResponse.recycle();
221 
222         IconEditTag.doTag(_servletContext, _request, _stringResponse);
223 
224         return _stringResponse.getString();
225     }
226 
227     public String iconEdit(String page) throws Exception {
228         _stringResponse.recycle();
229 
230         IconEditTag.doTag(page, _servletContext, _request, _stringResponse);
231 
232         return _stringResponse.getString();
233     }
234 
235     public String iconEditDefaults() throws Exception {
236         _stringResponse.recycle();
237 
238         IconEditDefaultsTag.doTag(_servletContext, _request, _stringResponse);
239 
240         return _stringResponse.getString();
241     }
242 
243     public String iconEditGuest() throws Exception {
244         _stringResponse.recycle();
245 
246         IconEditGuestTag.doTag(_servletContext, _request, _stringResponse);
247 
248         return _stringResponse.getString();
249     }
250 
251     public String iconEditGuest(String page) throws Exception {
252         _stringResponse.recycle();
253 
254         IconEditGuestTag.doTag(
255             page, _servletContext, _request, _stringResponse);
256 
257         return _stringResponse.getString();
258     }
259 
260     public String iconHelp() throws Exception {
261         _stringResponse.recycle();
262 
263         IconHelpTag.doTag(_servletContext, _request, _stringResponse);
264 
265         return _stringResponse.getString();
266     }
267 
268     public String iconHelp(String page) throws Exception {
269         _stringResponse.recycle();
270 
271         IconHelpTag.doTag(page, _servletContext, _request, _stringResponse);
272 
273         return _stringResponse.getString();
274     }
275 
276     public String iconMaximize() throws Exception {
277         _stringResponse.recycle();
278 
279         IconMaximizeTag.doTag(_servletContext, _request, _stringResponse);
280 
281         return _stringResponse.getString();
282     }
283 
284     public String iconMaximize(String page) throws Exception {
285         _stringResponse.recycle();
286 
287         IconMaximizeTag.doTag(page, _servletContext, _request, _stringResponse);
288 
289         return _stringResponse.getString();
290     }
291 
292     public String iconMinimize() throws Exception {
293         _stringResponse.recycle();
294 
295         IconMinimizeTag.doTag(_servletContext, _request, _stringResponse);
296 
297         return _stringResponse.getString();
298     }
299 
300     public String iconMinimize(String page) throws Exception {
301         _stringResponse.recycle();
302 
303         IconMinimizeTag.doTag(page, _servletContext, _request, _stringResponse);
304 
305         return _stringResponse.getString();
306     }
307 
308     public String iconOptions() throws Exception {
309         _stringResponse.recycle();
310 
311         IconOptionsTag.doTag(_servletContext, _request, _stringResponse);
312 
313         return _stringResponse.getString();
314     }
315 
316     public String iconOptions(String page) throws Exception {
317         _stringResponse.recycle();
318 
319         IconOptionsTag.doTag(page, _servletContext, _request, _stringResponse);
320 
321         return _stringResponse.getString();
322     }
323 
324     public String iconPortlet() throws Exception {
325         _stringResponse.recycle();
326 
327         IconPortletTag.doTag(_servletContext, _request, _stringResponse);
328 
329         return _stringResponse.getString();
330     }
331 
332     public String iconPortlet(String page, Portlet portlet) throws Exception {
333         _stringResponse.recycle();
334 
335         IconPortletTag.doTag(
336             page, portlet, _servletContext, _request, _stringResponse);
337 
338         return _stringResponse.getString();
339     }
340 
341     public String iconPortletCss() throws Exception {
342         _stringResponse.recycle();
343 
344         IconPortletCssTag.doTag(_servletContext, _request, _stringResponse);
345 
346         return _stringResponse.getString();
347     }
348 
349     public String iconPortletCss(String page) throws Exception {
350         _stringResponse.recycle();
351 
352         IconPortletCssTag.doTag(
353             page, _servletContext, _request, _stringResponse);
354 
355         return _stringResponse.getString();
356     }
357 
358     public String iconPrint() throws Exception {
359         _stringResponse.recycle();
360 
361         IconPrintTag.doTag(_servletContext, _request, _stringResponse);
362 
363         return _stringResponse.getString();
364     }
365 
366     public String iconPrint(String page) throws Exception {
367         _stringResponse.recycle();
368 
369         IconPrintTag.doTag(page, _servletContext, _request, _stringResponse);
370 
371         return _stringResponse.getString();
372     }
373 
374     public String iconRefresh() throws Exception {
375         _stringResponse.recycle();
376 
377         IconRefreshTag.doTag(_servletContext, _request, _stringResponse);
378 
379         return _stringResponse.getString();
380     }
381 
382     public String iconRefresh(String page) throws Exception {
383         _stringResponse.recycle();
384 
385         IconRefreshTag.doTag(page, _servletContext, _request, _stringResponse);
386 
387         return _stringResponse.getString();
388     }
389 
390     public String include(String page) throws Exception {
391         _stringResponse.recycle();
392 
393         RequestDispatcher requestDispatcher =
394             _servletContext.getRequestDispatcher(page);
395 
396         requestDispatcher.include(_request, _stringResponse);
397 
398         return _stringResponse.getString();
399     }
400 
401     public String include(ServletContext servletContext, String page)
402         throws Exception {
403 
404         _stringResponse.recycle();
405 
406         RequestDispatcher requestDispatcher =
407             servletContext.getRequestDispatcher(page);
408 
409         requestDispatcher.include(_request, _stringResponse);
410 
411         return _stringResponse.getString();
412     }
413 
414     public String journalContentSearch() throws Exception {
415         _stringResponse.recycle();
416 
417         JournalContentSearchTag.doTag(
418             _servletContext, _request, _stringResponse);
419 
420         return _stringResponse.getString();
421     }
422 
423     public String language() throws Exception {
424         _stringResponse.recycle();
425 
426         LanguageTag.doTag(_servletContext, _request, _stringResponse);
427 
428         return _stringResponse.getString();
429     }
430 
431     public String language(
432             String formName, String formAction, String name, int displayStyle)
433         throws Exception {
434 
435         _stringResponse.recycle();
436 
437         LanguageTag.doTag(
438             formName, formAction, name, null, displayStyle, _servletContext,
439             _request, _stringResponse);
440 
441         return _stringResponse.getString();
442     }
443 
444     public String language(
445             String formName, String formAction, String name,
446             String[] languageIds, int displayStyle)
447         throws Exception {
448 
449         _stringResponse.recycle();
450 
451         LanguageTag.doTag(
452             formName, formAction, name, languageIds, displayStyle,
453             _servletContext, _request, _stringResponse);
454 
455         return _stringResponse.getString();
456     }
457 
458     public String language(
459             String page, String formName, String formAction, String name,
460             int displayStyle)
461         throws Exception {
462 
463         _stringResponse.recycle();
464 
465         LanguageTag.doTag(
466             page, formName, formAction, name, null, displayStyle,
467             _servletContext, _request, _stringResponse);
468 
469         return _stringResponse.getString();
470     }
471 
472     public String language(
473             String page, String formName, String formAction, String name,
474             String[] languageIds, int displayStyle)
475         throws Exception {
476 
477         _stringResponse.recycle();
478 
479         LanguageTag.doTag(
480             page, formName, formAction, name, languageIds, displayStyle,
481             _servletContext, _request, _stringResponse);
482 
483         return _stringResponse.getString();
484     }
485 
486     public String layoutIcon(Layout layout) throws Exception {
487         _stringResponse.recycle();
488 
489         LayoutIconTag.doTag(layout, _servletContext, _request, _stringResponse);
490 
491         return _stringResponse.getString();
492     }
493 
494     public String metaTags() throws Exception {
495         _stringResponse.recycle();
496 
497         MetaTagsTag.doTag(_servletContext, _request, _stringResponse);
498 
499         return _stringResponse.getString();
500     }
501 
502     public String myPlaces() throws Exception {
503         _stringResponse.recycle();
504 
505         MyPlacesTag.doTag(_servletContext, _request, _stringResponse);
506 
507         return _stringResponse.getString();
508     }
509 
510     public String myPlaces(int max) throws Exception {
511         _stringResponse.recycle();
512 
513         MyPlacesTag.doTag(max, _servletContext, _request, _stringResponse);
514 
515         return _stringResponse.getString();
516     }
517 
518     public String permissionsURL(
519             String redirect, String modelResource,
520             String modelResourceDescription, String resourcePrimKey)
521         throws Exception {
522 
523         return PermissionsURLTag.doTag(
524             redirect, modelResource, modelResourceDescription, resourcePrimKey,
525             null, false, _pageContext);
526     }
527 
528     public String pngImage(String image, String height, String width)
529         throws Exception {
530 
531         _stringResponse.recycle();
532 
533         PngImageTag.doTag(image, height, width, _servletContext, _request,
534             _stringResponse);
535 
536         return _stringResponse.getString();
537     }
538 
539     public String renderURL(String portletName, String queryString)
540         throws Exception {
541 
542         return renderURL(
543             LayoutConstants.DEFAULT_PLID, portletName, queryString);
544     }
545 
546     public String renderURL(long plid, String portletName, String queryString)
547         throws Exception {
548 
549         String windowState = WindowState.NORMAL.toString();
550         String portletMode = PortletMode.VIEW.toString();
551 
552         return renderURL(
553             windowState, portletMode, plid, portletName, queryString);
554     }
555 
556     public String renderURL(
557             String windowState, String portletMode, String portletName,
558             String queryString)
559         throws Exception {
560 
561         return renderURL(
562             windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
563             queryString);
564     }
565 
566     public String renderURL(
567             String windowState, String portletMode, long plid,
568             String portletName, String queryString)
569         throws Exception {
570 
571         Boolean secure = null;
572         Boolean copyCurrentRenderParameters = null;
573         Boolean escapeXml = null;
574         Boolean anchor = null;
575         Boolean encrypt = null;
576         long doAsUserId = 0;
577         Boolean portletConfiguration = null;
578 
579         return renderURL(
580             windowState, portletMode, secure, copyCurrentRenderParameters,
581             escapeXml, plid, portletName, anchor, encrypt, doAsUserId,
582             portletConfiguration, queryString);
583     }
584 
585     public String renderURL(
586             String windowState, String portletMode, Boolean secure,
587             Boolean copyCurrentRenderParameters, Boolean escapeXml,
588             long plid, String portletName, Boolean anchor, Boolean encrypt,
589             long doAsUserId, Boolean portletConfiguration, String queryString)
590         throws Exception {
591 
592         String var = null;
593         String varImpl = null;
594         String name = null;
595         String resourceID = null;
596         String cacheability = null;
597         Map<String, String[]> params = HttpUtil.parameterMapFromString(
598             queryString);
599         boolean writeOutput = false;
600 
601         return ActionURLTag.doTag(
602             PortletRequest.RENDER_PHASE, windowState, portletMode, var, varImpl,
603             secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
604             cacheability, plid, portletName, anchor, encrypt, doAsUserId,
605             portletConfiguration, params, writeOutput, _pageContext);
606     }
607 
608     public String runtime(String portletName)
609         throws Exception {
610 
611         return runtime(portletName, null);
612     }
613 
614     public String runtime(String portletName, String queryString)
615         throws Exception {
616 
617         _stringResponse.recycle();
618 
619         RuntimeTag.doTag(
620             portletName, queryString, null, _servletContext, _request,
621             _stringResponse);
622 
623         return _stringResponse.getString();
624     }
625 
626     public String runtime(
627             String portletName, String queryString, String defaultPreferences)
628         throws Exception {
629 
630         _stringResponse.recycle();
631 
632         RuntimeTag.doTag(
633             portletName, queryString, defaultPreferences, null, _servletContext,
634             _request, _stringResponse);
635 
636         return _stringResponse.getString();
637     }
638 
639     public String search() throws Exception {
640         _stringResponse.recycle();
641 
642         SearchTag.doTag(_servletContext, _request, _stringResponse);
643 
644         return _stringResponse.getString();
645     }
646 
647     public String staging() throws Exception {
648         _stringResponse.recycle();
649 
650         StagingTag.doTag(_servletContext, _request, _stringResponse);
651 
652         return _stringResponse.getString();
653     }
654 
655     public String toggle(
656             String id, String showImage, String hideImage, String showMessage,
657             String hideMessage, boolean defaultShowContent)
658         throws Exception {
659 
660         _stringResponse.recycle();
661 
662         ToggleTag.doTag(
663             id, showImage, hideImage, showMessage, hideMessage,
664             defaultShowContent, null, _servletContext, _request,
665             _stringResponse);
666 
667         return _stringResponse.getString();
668     }
669 
670     public String wrapPortlet(String wrapPage, String portletPage)
671         throws Exception {
672 
673         _stringResponse.recycle();
674 
675         return WrapPortletTag.doTag(
676             wrapPage, portletPage, _servletContext, _request, _stringResponse,
677             _pageContext);
678     }
679 
680     private ServletContext _servletContext;
681     private HttpServletRequest _request;
682     private StringServletResponse _stringResponse;
683     private PageContext _pageContext;
684 
685 }