1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
46  public class WrapPortletTag extends ParamAndPropertyAncestorTagImpl {
47  
48      public static String doTag(
49              String wrapPage, String portletPage, ServletContext servletContext,
50              HttpServletRequest request, StringServletResponse stringResponse,
51              PageContext pageContext)
52          throws Exception {
53  
54          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
55              WebKeys.THEME_DISPLAY);
56  
57          Theme theme = themeDisplay.getTheme();
58          PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
59  
60          // Portlet content
61  
62          RequestDispatcher requestDispatcher =
63              servletContext.getRequestDispatcher(portletPage);
64  
65          requestDispatcher.include(request, stringResponse);
66  
67          portletDisplay.setContent(stringResponse.getString());
68  
69          // Page
70  
71          stringResponse.recycle();
72  
73          String content = ThemeUtil.includeVM(
74              servletContext, request, pageContext, wrapPage, theme, false);
75  
76          StringBuilder sb = new StringBuilder(
77              content.length() + _CONTENT_WRAPPER_LENGTH);
78  
79          sb.append(_CONTENT_WRAPPER_PRE);
80          sb.append(content);
81          sb.append(_CONTENT_WRAPPER_POST);
82  
83          return sb.toString();
84      }
85  
86      public int doStartTag() {
87          return EVAL_BODY_BUFFERED;
88      }
89  
90      public int doAfterBody() {
91          _bodyContentString = getBodyContent().getString();
92  
93          return SKIP_BODY;
94      }
95  
96      public int doEndTag() throws JspException {
97          try {
98              ServletContext servletContext = getServletContext();
99              HttpServletRequest request = getServletRequest();
100             StringServletResponse stringResponse = getServletResponse();
101 
102             ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
103                 WebKeys.THEME_DISPLAY);
104 
105             Theme theme = themeDisplay.getTheme();
106             PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
107 
108             // Portlet content
109 
110             portletDisplay.setContent(_bodyContentString);
111 
112             // Page
113 
114             ThemeUtil.include(
115                 servletContext, request, stringResponse, pageContext, getPage(),
116                 theme);
117 
118             pageContext.getOut().print(stringResponse.getString());
119 
120             return EVAL_PAGE;
121         }
122         catch (Exception e) {
123             throw new JspException(e);
124         }
125         finally {
126             clearParams();
127             clearProperties();
128         }
129     }
130 
131     public String getPage() {
132         return _page;
133     }
134 
135     public void setPage(String page) {
136         _page = page;
137     }
138 
139     private static final String _CONTENT_WRAPPER_PRE =
140         "<div id=\"content-wrapper\" class=\"column-1\">";
141 
142     private static final String _CONTENT_WRAPPER_POST = "</div>";
143 
144     private static final int _CONTENT_WRAPPER_LENGTH =
145         _CONTENT_WRAPPER_PRE.length() + _CONTENT_WRAPPER_POST.length();
146 
147     private String _page;
148     private String _bodyContentString = StringPool.BLANK;
149 
150 }