001    /**
002     * Copyright (c) 2000-present 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.DirectRequestDispatcherFactoryUtil;
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.servlet.PipingServletResponse;
025    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
026    import com.liferay.taglib.util.ThemeUtil;
027    
028    import javax.servlet.RequestDispatcher;
029    import javax.servlet.ServletContext;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    import javax.servlet.jsp.JspException;
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                    throws Exception {
045    
046                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
047                            WebKeys.THEME_DISPLAY);
048    
049                    Theme theme = themeDisplay.getTheme();
050                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
051    
052                    // Portlet content
053    
054                    RequestDispatcher requestDispatcher =
055                            DirectRequestDispatcherFactoryUtil.getRequestDispatcher(
056                                    servletContext, 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, response, wrapPage, theme, false);
076                    }
077                    else if (extension.equals(ThemeHelper.TEMPLATE_EXTENSION_VM)) {
078                            content = ThemeUtil.includeVM(
079                                    servletContext, request, response, 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                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
090                                    WebKeys.THEME_DISPLAY);
091    
092                            Theme theme = themeDisplay.getTheme();
093                            PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
094    
095                            // Portlet content
096    
097                            portletDisplay.setContent(getBodyContentAsStringBundler());
098    
099                            // Page
100    
101                            ThemeUtil.include(
102                                    servletContext, request, new PipingServletResponse(pageContext),
103                                    getPage(), theme);
104    
105                            return EVAL_PAGE;
106                    }
107                    catch (Exception e) {
108                            throw new JspException(e);
109                    }
110                    finally {
111                            clearParams();
112                            clearProperties();
113                    }
114            }
115    
116            @Override
117            public int doStartTag() {
118                    return EVAL_BODY_BUFFERED;
119            }
120    
121            public void setPage(String page) {
122                    _page = page;
123            }
124    
125            protected String getPage() {
126                    return _page;
127            }
128    
129            private static final String _CONTENT_WRAPPER_POST = "</div>";
130    
131            private static final String _CONTENT_WRAPPER_PRE =
132                    "<div class=\"column-1\" id=\"main-content\" role=\"main\">";
133    
134            private String _page;
135    
136    }