1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.util;
16  
17  import com.liferay.portal.kernel.servlet.PipingPageContext;
18  import com.liferay.portal.kernel.util.HttpUtil;
19  import com.liferay.portal.kernel.util.WebKeys;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.model.LayoutConstants;
22  import com.liferay.portal.model.Portlet;
23  import com.liferay.portal.model.Theme;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.taglib.portlet.ActionURLTag;
26  import com.liferay.taglib.portletext.IconBackTag;
27  import com.liferay.taglib.portletext.IconCloseTag;
28  import com.liferay.taglib.portletext.IconConfigurationTag;
29  import com.liferay.taglib.portletext.IconEditDefaultsTag;
30  import com.liferay.taglib.portletext.IconEditGuestTag;
31  import com.liferay.taglib.portletext.IconEditTag;
32  import com.liferay.taglib.portletext.IconHelpTag;
33  import com.liferay.taglib.portletext.IconMaximizeTag;
34  import com.liferay.taglib.portletext.IconMinimizeTag;
35  import com.liferay.taglib.portletext.IconOptionsTag;
36  import com.liferay.taglib.portletext.IconPortletCssTag;
37  import com.liferay.taglib.portletext.IconPortletTag;
38  import com.liferay.taglib.portletext.IconPrintTag;
39  import com.liferay.taglib.portletext.IconRefreshTag;
40  import com.liferay.taglib.portletext.RuntimeTag;
41  import com.liferay.taglib.security.DoAsURLTag;
42  import com.liferay.taglib.security.PermissionsURLTag;
43  import com.liferay.taglib.theme.LayoutIconTag;
44  import com.liferay.taglib.theme.MetaTagsTag;
45  import com.liferay.taglib.theme.WrapPortletTag;
46  import com.liferay.taglib.ui.BreadcrumbTag;
47  import com.liferay.taglib.ui.JournalContentSearchTag;
48  import com.liferay.taglib.ui.LanguageTag;
49  import com.liferay.taglib.ui.MyPlacesTag;
50  import com.liferay.taglib.ui.PngImageTag;
51  import com.liferay.taglib.ui.SearchTag;
52  import com.liferay.taglib.ui.StagingTag;
53  import com.liferay.taglib.ui.ToggleTag;
54  
55  import java.util.Map;
56  
57  import javax.portlet.PortletMode;
58  import javax.portlet.PortletRequest;
59  import javax.portlet.WindowState;
60  
61  import javax.servlet.RequestDispatcher;
62  import javax.servlet.ServletContext;
63  import javax.servlet.http.HttpServletRequest;
64  import javax.servlet.http.HttpServletResponse;
65  import javax.servlet.jsp.PageContext;
66  import javax.servlet.jsp.tagext.TagSupport;
67  
68  /**
69   * <a href="VelocityTaglib.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Brian Wing Shun Chan
72   * @author Shuyang Zhou
73   */
74  public class VelocityTaglib {
75  
76      public VelocityTaglib() {
77      }
78  
79      public VelocityTaglib(
80          ServletContext servletContext, HttpServletRequest request,
81          HttpServletResponse response, PageContext pageContext) {
82  
83          init(servletContext, request, response, pageContext);
84      }
85  
86      public void actionURL(long plid, String portletName, String queryString)
87          throws Exception {
88  
89          String windowState = WindowState.NORMAL.toString();
90          String portletMode = PortletMode.VIEW.toString();
91  
92          actionURL(windowState, portletMode, plid, portletName, queryString);
93      }
94  
95      public void actionURL(String portletName, String queryString)
96          throws Exception {
97  
98          actionURL(LayoutConstants.DEFAULT_PLID, portletName, queryString);
99      }
100 
101     public void actionURL(
102             String windowState, String portletMode, Boolean secure,
103             Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
104             long plid, String portletName, Boolean anchor, Boolean encrypt,
105             long doAsUserId, Boolean portletConfiguration, String queryString)
106         throws Exception {
107 
108         String var = null;
109         String varImpl = null;
110         String resourceID = null;
111         String cacheability = null;
112         Map<String, String[]> params = HttpUtil.parameterMapFromString(
113             queryString);
114 
115         ActionURLTag.doTag(
116             PortletRequest.ACTION_PHASE, windowState, portletMode, var, varImpl,
117             secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
118             cacheability, plid, portletName, anchor, encrypt, doAsUserId,
119             portletConfiguration, params, _pageContext);
120     }
121 
122     public void actionURL(
123             String windowState, String portletMode, long plid,
124             String portletName, String queryString)
125         throws Exception {
126 
127         Boolean secure = null;
128         Boolean copyCurrentRenderParameters = null;
129         Boolean escapeXml = null;
130         String name = null;
131         Boolean anchor = null;
132         Boolean encrypt = null;
133         long doAsUserId = 0;
134         Boolean portletConfiguration = null;
135 
136         actionURL(
137             windowState, portletMode, secure, copyCurrentRenderParameters,
138             escapeXml, name, plid, portletName, anchor, encrypt, doAsUserId,
139             portletConfiguration, queryString);
140     }
141 
142     public void actionURL(
143             String windowState, String portletMode, String portletName,
144             String queryString)
145         throws Exception {
146 
147         actionURL(
148             windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
149             queryString);
150     }
151 
152     public void breadcrumb() throws Exception {
153         BreadcrumbTag breadcrumbTag = new BreadcrumbTag();
154 
155         setUp(breadcrumbTag);
156 
157         breadcrumbTag.runTag();
158     }
159 
160     public void breadcrumb(
161             int displayStyle, boolean showGuestGroup, boolean showParentGroups,
162             boolean showLayout, boolean showPortletBreadcrumb)
163         throws Exception {
164 
165         BreadcrumbTag breadcrumbTag = new BreadcrumbTag();
166 
167         setUp(breadcrumbTag);
168 
169         breadcrumbTag.setDisplayStyle(displayStyle);
170         breadcrumbTag.setShowGuestGroup(showGuestGroup);
171         breadcrumbTag.setShowLayout(showLayout);
172         breadcrumbTag.setShowParentGroups(showParentGroups);
173         breadcrumbTag.setShowPortletBreadcrumb(showPortletBreadcrumb);
174 
175         breadcrumbTag.runTag();
176     }
177 
178     public void doAsURL(long doAsUserId) throws Exception {
179         DoAsURLTag.doTag(doAsUserId, null, _pageContext);
180     }
181 
182     public BreadcrumbTag getBreadcrumbTag() throws Exception {
183         BreadcrumbTag breadcrumbTag = new BreadcrumbTag();
184 
185         setUp(breadcrumbTag);
186 
187         return breadcrumbTag;
188     }
189 
190     public MyPlacesTag getMyPlacesTag() throws Exception {
191         MyPlacesTag myPlacesTag = new MyPlacesTag();
192 
193         setUp(myPlacesTag);
194 
195         return myPlacesTag;
196     }
197 
198     public PngImageTag getPngImageTag() throws Exception {
199         PngImageTag pngImageTag = new PngImageTag();
200 
201         setUp(pngImageTag);
202 
203         return pngImageTag;
204     }
205 
206     public String getSetting(String name) {
207         ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
208             WebKeys.THEME_DISPLAY);
209 
210         Theme theme = themeDisplay.getTheme();
211 
212         return theme.getSetting(name);
213     }
214 
215     public void iconBack() throws Exception {
216         IconBackTag iconBackTag = new IconBackTag();
217 
218         setUp(iconBackTag);
219 
220         iconBackTag.runTag();
221     }
222 
223     public void iconClose() throws Exception {
224         IconCloseTag iconCloseTag = new IconCloseTag();
225 
226         setUp(iconCloseTag);
227 
228         iconCloseTag.runTag();
229     }
230 
231     public void iconConfiguration() throws Exception {
232         IconConfigurationTag iconConfigurationTag = new IconConfigurationTag();
233 
234         setUp(iconConfigurationTag);
235 
236         iconConfigurationTag.runTag();
237     }
238 
239     public void iconEdit() throws Exception {
240         IconEditTag iconEditTag = new IconEditTag();
241 
242         setUp(iconEditTag);
243 
244         iconEditTag.runTag();
245     }
246 
247     public void iconEditDefaults() throws Exception {
248         IconEditDefaultsTag iconEditDefaultsTag = new IconEditDefaultsTag();
249 
250         setUp(iconEditDefaultsTag);
251 
252         iconEditDefaultsTag.runTag();
253     }
254 
255     public void iconEditGuest() throws Exception {
256         IconEditGuestTag iconEditGuestTag = new IconEditGuestTag();
257 
258         setUp(iconEditGuestTag);
259 
260         iconEditGuestTag.runTag();
261     }
262 
263     public void iconHelp() throws Exception {
264         IconHelpTag iconHelpTag = new IconHelpTag();
265 
266         setUp(iconHelpTag);
267 
268         iconHelpTag.runTag();
269     }
270 
271     public void iconMaximize() throws Exception {
272         IconMaximizeTag iconMaximizeTag = new IconMaximizeTag();
273 
274         setUp(iconMaximizeTag);
275 
276         iconMaximizeTag.runTag();
277     }
278 
279     public void iconMinimize() throws Exception {
280         IconMinimizeTag iconMinimizeTag = new IconMinimizeTag();
281 
282         setUp(iconMinimizeTag);
283 
284         iconMinimizeTag.runTag();
285     }
286 
287     public void iconOptions() throws Exception {
288         IconOptionsTag iconOptionsTag = new IconOptionsTag();
289 
290         setUp(iconOptionsTag);
291 
292         iconOptionsTag.runTag();
293     }
294 
295     public void iconPortlet() throws Exception {
296         IconPortletTag iconPortletTag = new IconPortletTag();
297 
298         setUp(iconPortletTag);
299 
300         iconPortletTag.runTag();
301     }
302 
303     public void iconPortlet(Portlet portlet) throws Exception {
304         IconPortletTag iconPortletTag = new IconPortletTag();
305 
306         setUp(iconPortletTag);
307 
308         iconPortletTag.setPortlet(portlet);
309 
310         iconPortletTag.runTag();
311     }
312 
313     public void iconPortletCss() throws Exception {
314         IconPortletCssTag iconPortletCssTag = new IconPortletCssTag();
315 
316         setUp(iconPortletCssTag);
317 
318         iconPortletCssTag.runTag();
319     }
320 
321     public void iconPrint() throws Exception {
322         IconPrintTag iconPrintTag = new IconPrintTag();
323 
324         setUp(iconPrintTag);
325 
326         iconPrintTag.runTag();
327     }
328 
329     public void iconRefresh() throws Exception {
330         IconRefreshTag iconRefreshTag = new IconRefreshTag();
331 
332         setUp(iconRefreshTag);
333 
334         iconRefreshTag.runTag();
335     }
336 
337     public void include(ServletContext servletContext, String page)
338         throws Exception {
339 
340         RequestDispatcher requestDispatcher =
341             servletContext.getRequestDispatcher(page);
342 
343         requestDispatcher.include(_request, _response);
344     }
345 
346     public void include(String page) throws Exception {
347         RequestDispatcher requestDispatcher =
348             _servletContext.getRequestDispatcher(page);
349 
350         requestDispatcher.include(_request, _response);
351     }
352 
353     public VelocityTaglib init(
354         ServletContext servletContext, HttpServletRequest request,
355         HttpServletResponse response, PageContext pageContext) {
356 
357         _servletContext = servletContext;
358         _request = request;
359         _response = response;
360         _pageContext = pageContext;
361 
362         return this;
363     }
364 
365     public void journalContentSearch() throws Exception {
366         JournalContentSearchTag journalContentSearchTag =
367             new JournalContentSearchTag();
368 
369         setUp(journalContentSearchTag);
370 
371         journalContentSearchTag.runTag();
372     }
373 
374     public void language() throws Exception {
375         LanguageTag languageTag = new LanguageTag();
376 
377         setUp(languageTag);
378 
379         languageTag.runTag();
380     }
381 
382     public void language(
383             String formName, String formAction, String name, int displayStyle)
384         throws Exception {
385 
386         LanguageTag languageTag = new LanguageTag();
387 
388         setUp(languageTag);
389 
390         languageTag.setDisplayStyle(displayStyle);
391         languageTag.setFormAction(formAction);
392         languageTag.setFormName(formName);
393         languageTag.setName(name);
394 
395         languageTag.runTag();
396     }
397 
398     public void language(
399             String formName, String formAction, String name,
400             String[] languageIds, int displayStyle)
401         throws Exception {
402 
403         LanguageTag languageTag = new LanguageTag();
404 
405         setUp(languageTag);
406 
407         languageTag.setDisplayStyle(displayStyle);
408         languageTag.setFormAction(formAction);
409         languageTag.setFormName(formName);
410         languageTag.setLanguageIds(languageIds);
411         languageTag.setName(name);
412 
413         languageTag.runTag();
414     }
415 
416     public void layoutIcon(Layout layout) throws Exception {
417         LayoutIconTag.doTag(layout, _servletContext, _request, _response);
418     }
419 
420     public void metaTags() throws Exception {
421         MetaTagsTag.doTag(_servletContext, _request, _response);
422     }
423 
424     public void myPlaces() throws Exception {
425         MyPlacesTag myPlacesTag = new MyPlacesTag();
426 
427         setUp(myPlacesTag);
428 
429         myPlacesTag.runTag();
430     }
431 
432     public void myPlaces(int max) throws Exception {
433         MyPlacesTag myPlacesTag = new MyPlacesTag();
434 
435         setUp(myPlacesTag);
436 
437         myPlacesTag.setMax(max);
438 
439         myPlacesTag.runTag();
440     }
441 
442     public void permissionsURL(
443             String redirect, String modelResource,
444             String modelResourceDescription, String resourcePrimKey)
445         throws Exception {
446 
447         PermissionsURLTag.doTag(
448             redirect, modelResource, modelResourceDescription, resourcePrimKey,
449             null, _pageContext);
450     }
451 
452     public void renderURL(long plid, String portletName, String queryString)
453         throws Exception {
454 
455         String windowState = WindowState.NORMAL.toString();
456         String portletMode = PortletMode.VIEW.toString();
457 
458         renderURL(windowState, portletMode, plid, portletName, queryString);
459     }
460 
461     public void renderURL(String portletName, String queryString)
462         throws Exception {
463 
464         renderURL(LayoutConstants.DEFAULT_PLID, portletName, queryString);
465     }
466 
467     public void renderURL(
468             String windowState, String portletMode, Boolean secure,
469             Boolean copyCurrentRenderParameters, Boolean escapeXml,
470             long plid, String portletName, Boolean anchor, Boolean encrypt,
471             long doAsUserId, Boolean portletConfiguration, String queryString)
472         throws Exception {
473 
474         String var = null;
475         String varImpl = null;
476         String name = null;
477         String resourceID = null;
478         String cacheability = null;
479         Map<String, String[]> params = HttpUtil.parameterMapFromString(
480             queryString);
481 
482         ActionURLTag.doTag(
483             PortletRequest.RENDER_PHASE, windowState, portletMode, var, varImpl,
484             secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
485             cacheability, plid, portletName, anchor, encrypt, doAsUserId,
486             portletConfiguration, params, _pageContext);
487     }
488 
489     public void renderURL(
490             String windowState, String portletMode, long plid,
491             String portletName, String queryString)
492         throws Exception {
493 
494         Boolean secure = null;
495         Boolean copyCurrentRenderParameters = null;
496         Boolean escapeXml = null;
497         Boolean anchor = null;
498         Boolean encrypt = null;
499         long doAsUserId = 0;
500         Boolean portletConfiguration = null;
501 
502         renderURL(
503             windowState, portletMode, secure, copyCurrentRenderParameters,
504             escapeXml, plid, portletName, anchor, encrypt, doAsUserId,
505             portletConfiguration, queryString);
506     }
507 
508     public void renderURL(
509             String windowState, String portletMode, String portletName,
510             String queryString)
511         throws Exception {
512 
513         renderURL(
514             windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
515             queryString);
516     }
517 
518     public void runtime(String portletName)
519         throws Exception {
520 
521         runtime(portletName, null);
522     }
523 
524     public void runtime(String portletName, String queryString)
525         throws Exception {
526 
527         RuntimeTag.doTag(
528             portletName, queryString, null, _servletContext, _request,
529             _response);
530     }
531 
532     public void runtime(
533             String portletName, String queryString, String defaultPreferences)
534         throws Exception {
535 
536         RuntimeTag.doTag(
537             portletName, queryString, defaultPreferences, null, _servletContext,
538             _request, _response);
539     }
540 
541     public void search() throws Exception {
542         SearchTag searchTag = new SearchTag();
543 
544         setUp(searchTag);
545 
546         searchTag.runTag();
547     }
548 
549     public void staging() throws Exception {
550         StagingTag stagingTag = new StagingTag();
551 
552         setUp(stagingTag);
553 
554         stagingTag.runTag();
555     }
556 
557     public void toggle(
558             String id, String showImage, String hideImage, String showMessage,
559             String hideMessage, boolean defaultShowContent)
560         throws Exception {
561 
562         ToggleTag.doTag(
563             id, showImage, hideImage, showMessage, hideMessage,
564             defaultShowContent, null, _servletContext, _request, _response);
565     }
566 
567     public String wrapPortlet(String wrapPage, String portletPage)
568         throws Exception {
569 
570         return WrapPortletTag.doTag(
571             wrapPage, portletPage, _servletContext, _request, _response,
572             _pageContext);
573     }
574 
575     protected void setUp(TagSupport tagSupport) throws Exception {
576         tagSupport.setPageContext(
577             new PipingPageContext(_pageContext, _response.getWriter()));
578     }
579 
580     private PageContext _pageContext;
581     private HttpServletRequest _request;
582     private HttpServletResponse _response;
583     private ServletContext _servletContext;
584 
585 }