1   /**
2    * Copyright (c) 2000-2007 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.ParamAncestorTagImpl;
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 ParamAncestorTagImpl {
47  
48      public static String doTag(
49              String wrapPage, String portletPage, ServletContext ctx,
50              HttpServletRequest req, StringServletResponse res,
51              PageContext pageContext)
52          throws Exception {
53  
54          ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
55              WebKeys.THEME_DISPLAY);
56  
57          Theme theme = themeDisplay.getTheme();
58          PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
59  
60          // Portlet content
61  
62          RequestDispatcher rd = ctx.getRequestDispatcher(portletPage);
63  
64          rd.include(req, res);
65  
66          portletDisplay.setContent(res.getString());
67  
68          // Page
69  
70          res.recycle();
71  
72          return ThemeUtil.includeVM(
73              ctx, req, pageContext, wrapPage, theme, false);
74      }
75  
76      public int doStartTag() {
77          return EVAL_BODY_BUFFERED;
78      }
79  
80      public int doAfterBody() {
81          _bodyContentString = getBodyContent().getString();
82  
83          return SKIP_BODY;
84      }
85  
86      public int doEndTag() throws JspException {
87          try {
88              ServletContext ctx = getServletContext();
89              HttpServletRequest req = getServletRequest();
90              StringServletResponse res = getServletResponse();
91  
92              ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
93                  WebKeys.THEME_DISPLAY);
94  
95              Theme theme = themeDisplay.getTheme();
96              PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
97  
98              // Portlet content
99  
100             portletDisplay.setContent(_bodyContentString);
101 
102             // Page
103 
104             ThemeUtil.include(ctx, req, res, pageContext, getPage(), theme);
105 
106             pageContext.getOut().print(res.getString());
107 
108             return EVAL_PAGE;
109         }
110         catch (Exception e) {
111             throw new JspException(e);
112         }
113         finally {
114             clearParams();
115         }
116     }
117 
118     public String getPage() {
119         return _page;
120     }
121 
122     public void setPage(String page) {
123         _page = page;
124     }
125 
126     private String _page;
127     private String _bodyContentString = StringPool.BLANK;
128 
129 }