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.theme;
16  
17  import com.liferay.portal.kernel.servlet.PipingServletResponse;
18  import com.liferay.portal.kernel.servlet.StringServletResponse;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.WebKeys;
21  import com.liferay.portal.model.Theme;
22  import com.liferay.portal.theme.PortletDisplay;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
25  import com.liferay.taglib.util.ThemeUtil;
26  
27  import javax.servlet.RequestDispatcher;
28  import javax.servlet.ServletContext;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.PageContext;
33  
34  /**
35   * <a href="WrapPortletTag.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class WrapPortletTag extends ParamAndPropertyAncestorTagImpl {
40  
41      public static String doTag(
42              String wrapPage, String portletPage, ServletContext servletContext,
43              HttpServletRequest request, HttpServletResponse response,
44              PageContext pageContext)
45          throws Exception {
46  
47          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
48              WebKeys.THEME_DISPLAY);
49  
50          Theme theme = themeDisplay.getTheme();
51          PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
52  
53          // Portlet content
54  
55          RequestDispatcher requestDispatcher =
56              servletContext.getRequestDispatcher(portletPage);
57  
58          StringServletResponse stringResponse =
59              new StringServletResponse(response);
60  
61          requestDispatcher.include(request, stringResponse);
62  
63          portletDisplay.setContent(stringResponse.getString());
64  
65          // Page
66  
67          String content = ThemeUtil.includeVM(
68              servletContext, request, pageContext, wrapPage, theme, false);
69  
70          return _CONTENT_WRAPPER_PRE.concat(content).concat(
71              _CONTENT_WRAPPER_POST);
72      }
73  
74      public int doStartTag() {
75          return EVAL_BODY_BUFFERED;
76      }
77  
78      public int doEndTag() throws JspException {
79          try {
80              ServletContext servletContext = getServletContext();
81              HttpServletRequest request = getServletRequest();
82  
83              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
84                  WebKeys.THEME_DISPLAY);
85  
86              Theme theme = themeDisplay.getTheme();
87              PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
88  
89              // Portlet content
90  
91              StringBundler bodyContentSB = getBodyContentAsStringBundler();
92  
93              portletDisplay.setContent(bodyContentSB.toString());
94  
95              // Page
96  
97              ThemeUtil.include(
98                  servletContext, request, new PipingServletResponse(pageContext),
99                  pageContext, getPage(), theme);
100 
101             return EVAL_PAGE;
102         }
103         catch (Exception e) {
104             throw new JspException(e);
105         }
106         finally {
107             clearParams();
108             clearProperties();
109         }
110     }
111 
112     protected String getPage() {
113         return _page;
114     }
115 
116     public void setPage(String page) {
117         _page = page;
118     }
119 
120     private static final String _CONTENT_WRAPPER_PRE =
121         "<div class=\"column-1\" id=\"main-content\" role=\"main\">";
122 
123     private static final String _CONTENT_WRAPPER_POST = "</div>";
124 
125     private String _page;
126 
127 }