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