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