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