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