001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.theme;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018    import com.liferay.portal.kernel.servlet.PipingServletResponse;
019    import com.liferay.portal.kernel.util.WebKeys;
020    import com.liferay.portal.model.Theme;
021    import com.liferay.portal.theme.PortletDisplay;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
024    import com.liferay.taglib.util.ThemeUtil;
025    
026    import javax.servlet.RequestDispatcher;
027    import javax.servlet.ServletContext;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    import javax.servlet.jsp.JspException;
031    import javax.servlet.jsp.PageContext;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class WrapPortletTag extends ParamAndPropertyAncestorTagImpl {
037    
038            public static String doTag(
039                            String wrapPage, String portletPage, ServletContext servletContext,
040                            HttpServletRequest request, HttpServletResponse response,
041                            PageContext pageContext)
042                    throws Exception {
043    
044                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
045                            WebKeys.THEME_DISPLAY);
046    
047                    Theme theme = themeDisplay.getTheme();
048                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
049    
050                    // Portlet content
051    
052                    RequestDispatcher requestDispatcher =
053                            servletContext.getRequestDispatcher(portletPage);
054    
055                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
056    
057                    PipingServletResponse pipingServletResponse = new PipingServletResponse(
058                            response, unsyncStringWriter);
059    
060                    requestDispatcher.include(request, pipingServletResponse);
061    
062                    portletDisplay.setContent(unsyncStringWriter.getStringBundler());
063    
064                    // Page
065    
066                    String content = ThemeUtil.includeVM(
067                            servletContext, request, pageContext, wrapPage, theme, false);
068    
069                    return _CONTENT_WRAPPER_PRE.concat(content).concat(
070                            _CONTENT_WRAPPER_POST);
071            }
072    
073            public int doStartTag() {
074                    return EVAL_BODY_BUFFERED;
075            }
076    
077            public int doEndTag() throws JspException {
078                    try {
079                            ServletContext servletContext = getServletContext();
080                            HttpServletRequest request = getServletRequest();
081    
082                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
083                                    WebKeys.THEME_DISPLAY);
084    
085                            Theme theme = themeDisplay.getTheme();
086                            PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
087    
088                            // Portlet content
089    
090                            portletDisplay.setContent(getBodyContentAsStringBundler());
091    
092                            // Page
093    
094                            ThemeUtil.include(
095                                    servletContext, request, new PipingServletResponse(pageContext),
096                                    pageContext, getPage(), theme);
097    
098                            return EVAL_PAGE;
099                    }
100                    catch (Exception e) {
101                            throw new JspException(e);
102                    }
103                    finally {
104                            clearParams();
105                            clearProperties();
106                    }
107            }
108    
109            protected String getPage() {
110                    return _page;
111            }
112    
113            public void setPage(String page) {
114                    _page = page;
115            }
116    
117            private static final String _CONTENT_WRAPPER_PRE =
118                    "<div class=\"column-1\" id=\"main-content\" role=\"main\">";
119    
120            private static final String _CONTENT_WRAPPER_POST = "</div>";
121    
122            private String _page;
123    
124    }