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 doEndTag() throws JspException {
088                    try {
089                            ServletContext servletContext = getServletContext();
090                            HttpServletRequest request = getServletRequest();
091    
092                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
093                                    WebKeys.THEME_DISPLAY);
094    
095                            Theme theme = themeDisplay.getTheme();
096                            PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
097    
098                            // Portlet content
099    
100                            portletDisplay.setContent(getBodyContentAsStringBundler());
101    
102                            // Page
103    
104                            ThemeUtil.include(
105                                    servletContext, request, new PipingServletResponse(pageContext),
106                                    pageContext, getPage(), theme);
107    
108                            return EVAL_PAGE;
109                    }
110                    catch (Exception e) {
111                            throw new JspException(e);
112                    }
113                    finally {
114                            clearParams();
115                            clearProperties();
116                    }
117            }
118    
119            @Override
120            public int doStartTag() {
121                    return EVAL_BODY_BUFFERED;
122            }
123    
124            public void setPage(String page) {
125                    _page = page;
126            }
127    
128            protected String getPage() {
129                    return _page;
130            }
131    
132            private static final String _CONTENT_WRAPPER_POST = "</div>";
133    
134            private static final String _CONTENT_WRAPPER_PRE =
135                    "<div class=\"column-1\" id=\"main-content\" role=\"main\">";
136    
137            private String _page;
138    
139    }