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