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.portletext;
016    
017    import com.liferay.portal.kernel.servlet.PipingServletResponse;
018    import com.liferay.taglib.util.IncludeTag;
019    
020    import javax.servlet.RequestDispatcher;
021    import javax.servlet.ServletContext;
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    import javax.servlet.jsp.JspException;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class PreviewTag extends IncludeTag {
030    
031            public static void doTag(
032                            String portletName, String queryString, boolean showBorders,
033                            String width, ServletContext servletContext,
034                            HttpServletRequest request, HttpServletResponse response)
035                    throws Exception {
036    
037                    doTag(
038                            _PAGE, portletName, queryString, showBorders, width, servletContext,
039                            request, response);
040            }
041    
042            public static void doTag(
043                            String page, String portletName, String queryString,
044                            boolean showBorders, String width, ServletContext servletContext,
045                            HttpServletRequest request, HttpServletResponse response)
046                    throws Exception {
047    
048                    request.setAttribute(
049                            "liferay-portlet:preview:portletName", portletName);
050                    request.setAttribute(
051                            "liferay-portlet:preview:queryString", queryString);
052                    request.setAttribute(
053                            "liferay-portlet:preview:showBorders", String.valueOf(showBorders));
054                    request.setAttribute("liferay-portlet:preview:width", width);
055    
056                    RequestDispatcher requestDispatcher =
057                            servletContext.getRequestDispatcher(page);
058    
059                    requestDispatcher.include(request, response);
060            }
061    
062            @Override
063            public int doEndTag() throws JspException {
064                    try {
065                            doTag(
066                                    getPage(), _portletName, _queryString, _showBorders, _width,
067                                    servletContext, request,
068                                    new PipingServletResponse(pageContext));
069    
070                            return EVAL_PAGE;
071                    }
072                    catch (Exception e) {
073                            throw new JspException(e);
074                    }
075            }
076    
077            public void setPortletName(String portletName) {
078                    _portletName = portletName;
079            }
080    
081            public void setQueryString(String queryString) {
082                    _queryString = queryString;
083            }
084    
085            public void setShowBorders(boolean showBorders) {
086                    _showBorders = showBorders;
087            }
088    
089            public void setWidth(String width) {
090                    _width = width;
091            }
092    
093            @Override
094            protected String getPage() {
095                    return _PAGE;
096            }
097    
098            private static final String _PAGE = "/html/taglib/portlet/preview/page.jsp";
099    
100            private String _portletName;
101            private String _queryString;
102            private boolean _showBorders;
103            private String _width;
104    
105    }