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.util;
016    
017    import com.liferay.portal.kernel.servlet.DirectServletContext;
018    import com.liferay.portal.kernel.servlet.PipingPageContext;
019    import com.liferay.portal.kernel.servlet.taglib.TagSupport;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.PropsUtil;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.LayoutConstants;
027    import com.liferay.portal.model.Portlet;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.taglib.portlet.ActionURLTag;
030    import com.liferay.taglib.portletext.IconBackTag;
031    import com.liferay.taglib.portletext.IconCloseTag;
032    import com.liferay.taglib.portletext.IconConfigurationTag;
033    import com.liferay.taglib.portletext.IconEditDefaultsTag;
034    import com.liferay.taglib.portletext.IconEditGuestTag;
035    import com.liferay.taglib.portletext.IconEditTag;
036    import com.liferay.taglib.portletext.IconHelpTag;
037    import com.liferay.taglib.portletext.IconMaximizeTag;
038    import com.liferay.taglib.portletext.IconMinimizeTag;
039    import com.liferay.taglib.portletext.IconOptionsTag;
040    import com.liferay.taglib.portletext.IconPortletCssTag;
041    import com.liferay.taglib.portletext.IconPortletTag;
042    import com.liferay.taglib.portletext.IconPrintTag;
043    import com.liferay.taglib.portletext.IconRefreshTag;
044    import com.liferay.taglib.portletext.RuntimeTag;
045    import com.liferay.taglib.security.DoAsURLTag;
046    import com.liferay.taglib.security.PermissionsURLTag;
047    import com.liferay.taglib.theme.LayoutIconTag;
048    import com.liferay.taglib.theme.MetaTagsTag;
049    import com.liferay.taglib.theme.WrapPortletTag;
050    import com.liferay.taglib.ui.BreadcrumbTag;
051    import com.liferay.taglib.ui.JournalContentSearchTag;
052    import com.liferay.taglib.ui.LanguageTag;
053    import com.liferay.taglib.ui.MySitesTag;
054    import com.liferay.taglib.ui.PngImageTag;
055    import com.liferay.taglib.ui.SearchTag;
056    import com.liferay.taglib.ui.StagingTag;
057    import com.liferay.taglib.ui.ToggleTag;
058    
059    import java.util.Map;
060    
061    import javax.portlet.PortletMode;
062    import javax.portlet.PortletRequest;
063    import javax.portlet.WindowState;
064    
065    import javax.servlet.RequestDispatcher;
066    import javax.servlet.ServletContext;
067    import javax.servlet.http.HttpServletRequest;
068    import javax.servlet.http.HttpServletResponse;
069    import javax.servlet.jsp.PageContext;
070    
071    /**
072     * @author Brian Wing Shun Chan
073     * @author Shuyang Zhou
074     */
075    public class VelocityTaglib {
076    
077            public VelocityTaglib() {
078            }
079    
080            public VelocityTaglib(
081                    ServletContext servletContext, HttpServletRequest request,
082                    HttpServletResponse response, PageContext pageContext) {
083    
084                    init(servletContext, request, response, pageContext);
085            }
086    
087            public void actionURL(long plid, String portletName, String queryString)
088                    throws Exception {
089    
090                    String windowState = WindowState.NORMAL.toString();
091                    String portletMode = PortletMode.VIEW.toString();
092    
093                    actionURL(windowState, portletMode, plid, portletName, queryString);
094            }
095    
096            public void actionURL(String portletName, String queryString)
097                    throws Exception {
098    
099                    actionURL(LayoutConstants.DEFAULT_PLID, portletName, queryString);
100            }
101    
102            /**
103             * @deprecated {@link #actionURL(String, String, Boolean, Boolean, Boolean,
104             *             String, long, long, String, Boolean, Boolean, long, long,
105             *             Boolean, String)}
106             */
107            public void actionURL(
108                            String windowState, String portletMode, Boolean secure,
109                            Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
110                            long plid, long refererPlid, String portletName, Boolean anchor,
111                            Boolean encrypt, long doAsUserId, Boolean portletConfiguration,
112                            String queryString)
113                    throws Exception {
114    
115                    actionURL(
116                            windowState, portletMode, secure, copyCurrentRenderParameters,
117                            escapeXml, name, plid, refererPlid, portletName, anchor, encrypt, 0,
118                            doAsUserId, portletConfiguration, queryString);
119            }
120    
121            public void actionURL(
122                            String windowState, String portletMode, Boolean secure,
123                            Boolean copyCurrentRenderParameters, Boolean escapeXml, String name,
124                            long plid, long refererPlid, String portletName, Boolean anchor,
125                            Boolean encrypt, long doAsGroupId, long doAsUserId,
126                            Boolean portletConfiguration, String queryString)
127                    throws Exception {
128    
129                    String var = null;
130                    String varImpl = null;
131                    String resourceID = null;
132                    String cacheability = null;
133                    Map<String, String[]> params = HttpUtil.parameterMapFromString(
134                            queryString);
135    
136                    ActionURLTag.doTag(
137                            PortletRequest.ACTION_PHASE, windowState, portletMode, var, varImpl,
138                            secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
139                            cacheability, plid, refererPlid, portletName, anchor, encrypt,
140                            doAsGroupId, doAsUserId, portletConfiguration, params,
141                            _pageContext);
142            }
143    
144            public void actionURL(
145                            String windowState, String portletMode, long plid,
146                            String portletName, String queryString)
147                    throws Exception {
148    
149                    Boolean secure = null;
150                    Boolean copyCurrentRenderParameters = null;
151                    Boolean escapeXml = null;
152                    long refererPlid = LayoutConstants.DEFAULT_PLID;
153                    String name = null;
154                    Boolean anchor = null;
155                    Boolean encrypt = null;
156                    long doAsGroupId = 0;
157                    long doAsUserId = 0;
158                    Boolean portletConfiguration = null;
159    
160                    actionURL(
161                            windowState, portletMode, secure, copyCurrentRenderParameters,
162                            escapeXml, name, plid, refererPlid, portletName, anchor, encrypt,
163                            doAsGroupId, doAsUserId, portletConfiguration, queryString);
164            }
165    
166            public void actionURL(
167                            String windowState, String portletMode, String portletName,
168                            String queryString)
169                    throws Exception {
170    
171                    actionURL(
172                            windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
173                            queryString);
174            }
175    
176            public void breadcrumb() throws Exception {
177                    BreadcrumbTag breadcrumbTag = new BreadcrumbTag();
178    
179                    setUp(breadcrumbTag);
180    
181                    breadcrumbTag.runTag();
182            }
183    
184            public void breadcrumb(
185                            String displayStyle, boolean showGuestGroup,
186                            boolean showParentGroups, boolean showLayout,
187                            boolean showPortletBreadcrumb)
188                    throws Exception {
189    
190                    BreadcrumbTag breadcrumbTag = new BreadcrumbTag();
191    
192                    setUp(breadcrumbTag);
193    
194                    breadcrumbTag.setDisplayStyle(displayStyle);
195                    breadcrumbTag.setShowGuestGroup(showGuestGroup);
196                    breadcrumbTag.setShowLayout(showLayout);
197                    breadcrumbTag.setShowParentGroups(showParentGroups);
198                    breadcrumbTag.setShowPortletBreadcrumb(showPortletBreadcrumb);
199    
200                    breadcrumbTag.runTag();
201            }
202    
203            public void doAsURL(long doAsUserId) throws Exception {
204                    DoAsURLTag.doTag(doAsUserId, null, _pageContext);
205            }
206    
207            public BreadcrumbTag getBreadcrumbTag() throws Exception {
208                    BreadcrumbTag breadcrumbTag = new BreadcrumbTag();
209    
210                    setUp(breadcrumbTag);
211    
212                    return breadcrumbTag;
213            }
214    
215            public MySitesTag getMySitesTag() throws Exception {
216                    MySitesTag mySitesTag = new MySitesTag();
217    
218                    setUp(mySitesTag);
219    
220                    return mySitesTag;
221            }
222    
223            public PngImageTag getPngImageTag() throws Exception {
224                    PngImageTag pngImageTag = new PngImageTag();
225    
226                    setUp(pngImageTag);
227    
228                    return pngImageTag;
229            }
230    
231            public String getSetting(String name) {
232                    ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
233                            WebKeys.THEME_DISPLAY);
234    
235                    return themeDisplay.getThemeSetting(name);
236            }
237    
238            public void iconBack() throws Exception {
239                    IconBackTag iconBackTag = new IconBackTag();
240    
241                    setUp(iconBackTag);
242    
243                    iconBackTag.runTag();
244            }
245    
246            public void iconClose() throws Exception {
247                    IconCloseTag iconCloseTag = new IconCloseTag();
248    
249                    setUp(iconCloseTag);
250    
251                    iconCloseTag.runTag();
252            }
253    
254            public void iconConfiguration() throws Exception {
255                    IconConfigurationTag iconConfigurationTag = new IconConfigurationTag();
256    
257                    setUp(iconConfigurationTag);
258    
259                    iconConfigurationTag.runTag();
260            }
261    
262            public void iconEdit() throws Exception {
263                    IconEditTag iconEditTag = new IconEditTag();
264    
265                    setUp(iconEditTag);
266    
267                    iconEditTag.runTag();
268            }
269    
270            public void iconEditDefaults() throws Exception {
271                    IconEditDefaultsTag iconEditDefaultsTag = new IconEditDefaultsTag();
272    
273                    setUp(iconEditDefaultsTag);
274    
275                    iconEditDefaultsTag.runTag();
276            }
277    
278            public void iconEditGuest() throws Exception {
279                    IconEditGuestTag iconEditGuestTag = new IconEditGuestTag();
280    
281                    setUp(iconEditGuestTag);
282    
283                    iconEditGuestTag.runTag();
284            }
285    
286            public void iconHelp() throws Exception {
287                    IconHelpTag iconHelpTag = new IconHelpTag();
288    
289                    setUp(iconHelpTag);
290    
291                    iconHelpTag.runTag();
292            }
293    
294            public void iconMaximize() throws Exception {
295                    IconMaximizeTag iconMaximizeTag = new IconMaximizeTag();
296    
297                    setUp(iconMaximizeTag);
298    
299                    iconMaximizeTag.runTag();
300            }
301    
302            public void iconMinimize() throws Exception {
303                    IconMinimizeTag iconMinimizeTag = new IconMinimizeTag();
304    
305                    setUp(iconMinimizeTag);
306    
307                    iconMinimizeTag.runTag();
308            }
309    
310            public void iconOptions() throws Exception {
311                    IconOptionsTag iconOptionsTag = new IconOptionsTag();
312    
313                    setUp(iconOptionsTag);
314    
315                    iconOptionsTag.runTag();
316            }
317    
318            public void iconPortlet() throws Exception {
319                    IconPortletTag iconPortletTag = new IconPortletTag();
320    
321                    setUp(iconPortletTag);
322    
323                    iconPortletTag.runTag();
324            }
325    
326            public void iconPortlet(Portlet portlet) throws Exception {
327                    IconPortletTag iconPortletTag = new IconPortletTag();
328    
329                    setUp(iconPortletTag);
330    
331                    iconPortletTag.setPortlet(portlet);
332    
333                    iconPortletTag.runTag();
334            }
335    
336            public void iconPortletCss() throws Exception {
337                    IconPortletCssTag iconPortletCssTag = new IconPortletCssTag();
338    
339                    setUp(iconPortletCssTag);
340    
341                    iconPortletCssTag.runTag();
342            }
343    
344            public void iconPrint() throws Exception {
345                    IconPrintTag iconPrintTag = new IconPrintTag();
346    
347                    setUp(iconPrintTag);
348    
349                    iconPrintTag.runTag();
350            }
351    
352            public void iconRefresh() throws Exception {
353                    IconRefreshTag iconRefreshTag = new IconRefreshTag();
354    
355                    setUp(iconRefreshTag);
356    
357                    iconRefreshTag.runTag();
358            }
359    
360            public void include(ServletContext servletContext, String page)
361                    throws Exception {
362    
363                    RequestDispatcher requestDispatcher =
364                            servletContext.getRequestDispatcher(page);
365    
366                    requestDispatcher.include(_request, _response);
367            }
368    
369            public void include(String page) throws Exception {
370                    if (_DIRECT_SERVLET_CONTEXT_ENABLED) {
371                            _request.setAttribute(WebKeys.SERVLET_PATH, page);
372                    }
373    
374                    RequestDispatcher requestDispatcher =
375                            _servletContext.getRequestDispatcher(page);
376    
377                    requestDispatcher.include(_request, _response);
378            }
379    
380            public VelocityTaglib init(
381                    ServletContext servletContext, HttpServletRequest request,
382                    HttpServletResponse response, PageContext pageContext) {
383    
384                    if (_DIRECT_SERVLET_CONTEXT_ENABLED) {
385                            servletContext = new DirectServletContext(servletContext);
386                    }
387    
388                    _servletContext = servletContext;
389                    _request = request;
390                    _response = response;
391                    _pageContext = pageContext;
392    
393                    return this;
394            }
395    
396            public void journalContentSearch() throws Exception {
397                    JournalContentSearchTag journalContentSearchTag =
398                            new JournalContentSearchTag();
399    
400                    setUp(journalContentSearchTag);
401    
402                    journalContentSearchTag.runTag();
403            }
404    
405            public void language() throws Exception {
406                    LanguageTag languageTag = new LanguageTag();
407    
408                    setUp(languageTag);
409    
410                    languageTag.runTag();
411            }
412    
413            public void language(
414                            String formName, String formAction, String name, int displayStyle)
415                    throws Exception {
416    
417                    LanguageTag languageTag = new LanguageTag();
418    
419                    setUp(languageTag);
420    
421                    languageTag.setDisplayStyle(displayStyle);
422                    languageTag.setFormAction(formAction);
423                    languageTag.setFormName(formName);
424                    languageTag.setName(name);
425    
426                    languageTag.runTag();
427            }
428    
429            public void language(
430                            String formName, String formAction, String name,
431                            String[] languageIds, int displayStyle)
432                    throws Exception {
433    
434                    LanguageTag languageTag = new LanguageTag();
435    
436                    setUp(languageTag);
437    
438                    languageTag.setDisplayStyle(displayStyle);
439                    languageTag.setFormAction(formAction);
440                    languageTag.setFormName(formName);
441                    languageTag.setLanguageIds(languageIds);
442                    languageTag.setName(name);
443    
444                    languageTag.runTag();
445            }
446    
447            public void layoutIcon(Layout layout) throws Exception {
448                    LayoutIconTag.doTag(layout, _servletContext, _request, _response);
449            }
450    
451            public void metaTags() throws Exception {
452                    MetaTagsTag.doTag(_servletContext, _request, _response);
453            }
454    
455            /**
456             * @deprecated {@link #mySites}
457             */
458            public void myPlaces() throws Exception {
459                    mySites();
460            }
461    
462            /**
463             * @deprecated {@link #mySites(int)}
464             */
465            public void myPlaces(int max) throws Exception {
466                    mySites(max);
467            }
468    
469            public void mySites() throws Exception {
470                    MySitesTag mySitesTag = new MySitesTag();
471    
472                    setUp(mySitesTag);
473    
474                    mySitesTag.runTag();
475            }
476    
477            public void mySites(int max) throws Exception {
478                    MySitesTag mySitesTag = new MySitesTag();
479    
480                    setUp(mySitesTag);
481    
482                    mySitesTag.setMax(max);
483    
484                    mySitesTag.runTag();
485            }
486    
487            public void permissionsURL(
488                            String redirect, String modelResource,
489                            String modelResourceDescription, String resourcePrimKey,
490                            String windowState, int[] roleTypes)
491                    throws Exception {
492    
493                    PermissionsURLTag.doTag(
494                            redirect, modelResource, modelResourceDescription, resourcePrimKey,
495                            windowState, null, roleTypes, _pageContext);
496            }
497    
498            public void renderURL(long plid, String portletName, String queryString)
499                    throws Exception {
500    
501                    String windowState = WindowState.NORMAL.toString();
502                    String portletMode = PortletMode.VIEW.toString();
503    
504                    renderURL(windowState, portletMode, plid, portletName, queryString);
505            }
506    
507            public void renderURL(String portletName, String queryString)
508                    throws Exception {
509    
510                    renderURL(LayoutConstants.DEFAULT_PLID, portletName, queryString);
511            }
512    
513            public void renderURL(
514                            String windowState, String portletMode, Boolean secure,
515                            Boolean copyCurrentRenderParameters, Boolean escapeXml, long plid,
516                            long refererPlid, String portletName, Boolean anchor,
517                            Boolean encrypt, long doAsGroupId, long doAsUserId,
518                            Boolean portletConfiguration, String queryString)
519                    throws Exception {
520    
521                    String var = null;
522                    String varImpl = null;
523                    String name = null;
524                    String resourceID = null;
525                    String cacheability = null;
526                    Map<String, String[]> params = HttpUtil.parameterMapFromString(
527                            queryString);
528    
529                    ActionURLTag.doTag(
530                            PortletRequest.RENDER_PHASE, windowState, portletMode, var, varImpl,
531                            secure, copyCurrentRenderParameters, escapeXml, name, resourceID,
532                            cacheability, plid, refererPlid, portletName, anchor, encrypt,
533                            doAsGroupId, doAsUserId, portletConfiguration, params,
534                            _pageContext);
535            }
536    
537            /**
538             * @deprecated {@link #renderURL(String, String, Boolean, Boolean, Boolean,
539             *             long, long, String, Boolean, Boolean, long, long, Boolean,
540             *             String)}
541             */
542            public void renderURL(
543                            String windowState, String portletMode, Boolean secure,
544                            Boolean copyCurrentRenderParameters, Boolean escapeXml, long plid,
545                            String portletName, Boolean anchor, Boolean encrypt,
546                            long doAsUserId, Boolean portletConfiguration, String queryString)
547                    throws Exception {
548    
549                    long refererPlid = LayoutConstants.DEFAULT_PLID;
550    
551                    renderURL(
552                            windowState, portletMode, secure, copyCurrentRenderParameters,
553                            escapeXml, plid, refererPlid, portletName, anchor, encrypt, 0,
554                            doAsUserId, portletConfiguration, queryString);
555            }
556    
557            public void renderURL(
558                            String windowState, String portletMode, long plid,
559                            String portletName, String queryString)
560                    throws Exception {
561    
562                    Boolean secure = null;
563                    Boolean copyCurrentRenderParameters = null;
564                    Boolean escapeXml = null;
565                    long referPlid = LayoutConstants.DEFAULT_PLID;
566                    Boolean anchor = null;
567                    Boolean encrypt = null;
568                    long doAsGroupId = 0;
569                    long doAsUserId = 0;
570                    Boolean portletConfiguration = null;
571    
572                    renderURL(
573                            windowState, portletMode, secure, copyCurrentRenderParameters,
574                            escapeXml, plid, referPlid, portletName, anchor, encrypt,
575                            doAsGroupId, doAsUserId, portletConfiguration, queryString);
576            }
577    
578            public void renderURL(
579                            String windowState, String portletMode, String portletName,
580                            String queryString)
581                    throws Exception {
582    
583                    renderURL(
584                            windowState, portletMode, LayoutConstants.DEFAULT_PLID, portletName,
585                            queryString);
586            }
587    
588            public void runtime(String portletName)
589                    throws Exception {
590    
591                    runtime(portletName, null);
592            }
593    
594            public void runtime(String portletName, String queryString)
595                    throws Exception {
596    
597                    RuntimeTag.doTag(
598                            portletName, queryString, null, _servletContext, _request,
599                            _response);
600            }
601    
602            public void runtime(
603                            String portletName, String queryString, String defaultPreferences)
604                    throws Exception {
605    
606                    RuntimeTag.doTag(
607                            portletName, queryString, defaultPreferences, null, _servletContext,
608                            _request, _response);
609            }
610    
611            public void search() throws Exception {
612                    SearchTag searchTag = new SearchTag();
613    
614                    setUp(searchTag);
615    
616                    searchTag.runTag();
617            }
618    
619            public void staging() throws Exception {
620                    StagingTag stagingTag = new StagingTag();
621    
622                    setUp(stagingTag);
623    
624                    stagingTag.runTag();
625            }
626    
627            public void toggle(
628                            String id, String showImage, String hideImage, String showMessage,
629                            String hideMessage, boolean defaultShowContent)
630                    throws Exception {
631    
632                    ToggleTag.doTag(
633                            id, showImage, hideImage, showMessage, hideMessage,
634                            defaultShowContent, null, _servletContext, _request, _response);
635            }
636    
637            public String wrapPortlet(String wrapPage, String portletPage)
638                    throws Exception {
639    
640                    return WrapPortletTag.doTag(
641                            wrapPage, portletPage, _servletContext, _request, _response,
642                            _pageContext);
643            }
644    
645            protected void setUp(TagSupport tagSupport) throws Exception {
646                    tagSupport.setPageContext(
647                            new PipingPageContext(_pageContext, _response.getWriter()));
648            }
649    
650            private static final boolean _DIRECT_SERVLET_CONTEXT_ENABLED =
651                    GetterUtil.getBoolean(
652                            PropsUtil.get(PropsKeys.DIRECT_SERVLET_CONTEXT_ENABLED));
653    
654            private PageContext _pageContext;
655            private HttpServletRequest _request;
656            private HttpServletResponse _response;
657            private ServletContext _servletContext;
658    
659    }