001    /**
002     * Copyright (c) 2000-2012 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.ThemeHelper;
020    import com.liferay.portal.kernel.util.WebKeys;
021    import com.liferay.portal.model.Theme;
022    import com.liferay.portal.theme.PortletDisplay;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
025    import com.liferay.taglib.util.ThemeUtil;
026    
027    import javax.servlet.RequestDispatcher;
028    import javax.servlet.ServletContext;
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    import javax.servlet.jsp.JspException;
032    import javax.servlet.jsp.PageContext;
033    import javax.servlet.jsp.tagext.BodyTag;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class WrapPortletTag
039            extends ParamAndPropertyAncestorTagImpl implements BodyTag {
040    
041            public static String doTag(
042                            String wrapPage, String portletPage, ServletContext servletContext,
043                            HttpServletRequest request, HttpServletResponse response,
044                            PageContext pageContext)
045                    throws Exception {
046    
047                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
048                            WebKeys.THEME_DISPLAY);
049    
050                    Theme theme = themeDisplay.getTheme();
051                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
052    
053                    // Portlet content
054    
055                    RequestDispatcher requestDispatcher =
056                            servletContext.getRequestDispatcher(portletPage);
057    
058                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
059    
060                    PipingServletResponse pipingServletResponse = new PipingServletResponse(
061                            response, unsyncStringWriter);
062    
063                    requestDispatcher.include(request, pipingServletResponse);
064    
065                    portletDisplay.setContent(unsyncStringWriter.getStringBundler());
066    
067                    // Page
068    
069                    String content = null;
070    
071                    String extension = theme.getTemplateExtension();
072    
073                    if (extension.equals(ThemeHelper.TEMPLATE_EXTENSION_FTL)) {
074                            content = ThemeUtil.includeFTL(
075                                    servletContext, request, pageContext, wrapPage, theme, false);
076                    }
077                    else if (extension.equals(ThemeHelper.TEMPLATE_EXTENSION_VM)) {
078                            content = ThemeUtil.includeVM(
079                                    servletContext, request, pageContext, wrapPage, theme, false);
080                    }
081    
082                    return _CONTENT_WRAPPER_PRE.concat(content).concat(
083                            _CONTENT_WRAPPER_POST);
084            }
085    
086            @Override
087            public int doStartTag() {
088                    return EVAL_BODY_BUFFERED;
089            }
090    
091            @Override
092            public int doEndTag() throws JspException {
093                    try {
094                            ServletContext servletContext = getServletContext();
095                            HttpServletRequest request = getServletRequest();
096    
097                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
098                                    WebKeys.THEME_DISPLAY);
099    
100                            Theme theme = themeDisplay.getTheme();
101                            PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
102    
103                            // Portlet content
104    
105                            portletDisplay.setContent(getBodyContentAsStringBundler());
106    
107                            // Page
108    
109                            ThemeUtil.include(
110                                    servletContext, request, new PipingServletResponse(pageContext),
111                                    pageContext, getPage(), theme);
112    
113                            return EVAL_PAGE;
114                    }
115                    catch (Exception e) {
116                            throw new JspException(e);
117                    }
118                    finally {
119                            clearParams();
120                            clearProperties();
121                    }
122            }
123    
124            protected String getPage() {
125                    return _page;
126            }
127    
128            public void setPage(String page) {
129                    _page = page;
130            }
131    
132            private static final String _CONTENT_WRAPPER_PRE =
133                    "<div class=\"column-1\" id=\"main-content\" role=\"main\">";
134    
135            private static final String _CONTENT_WRAPPER_POST = "</div>";
136    
137            private String _page;
138    
139    }