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