001    /**
002     * Copyright (c) 2000-2011 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            /**
063             * @deprecated {@link #doTag(String, String, boolean, String,
064             *                         ServletContext, HttpServletRequest, HttpServletResponse)}
065             */
066            public static void doTag(
067                            String portletName, String queryString,
068                            String width, ServletContext servletContext,
069                            HttpServletRequest request, HttpServletResponse response)
070                    throws Exception {
071    
072                    doTag(
073                            portletName, queryString, false, width, servletContext, request,
074                            response);
075            }
076    
077            /**
078             * @deprecated {@link #doTag(String, String, String, boolean, String,
079             *                         ServletContext, HttpServletRequest, HttpServletResponse)}
080             */
081            public static void doTag(
082                            String page, String portletName, String queryString, String width,
083                            ServletContext servletContext, HttpServletRequest request,
084                            HttpServletResponse response)
085                    throws Exception {
086    
087                    doTag(
088                            page, portletName, queryString, false, width, servletContext,
089                            request, response);
090            }
091    
092            public int doEndTag() throws JspException {
093                    try {
094                            ServletContext servletContext = getServletContext();
095                            HttpServletRequest request = getServletRequest();
096    
097                            doTag(
098                                    getPage(), _portletName, _queryString, _showBorders, _width,
099                                    servletContext, request,
100                                    new PipingServletResponse(pageContext));
101    
102                            return EVAL_PAGE;
103                    }
104                    catch (Exception e) {
105                            throw new JspException(e);
106                    }
107            }
108    
109            public void setPortletName(String portletName) {
110                    _portletName = portletName;
111            }
112    
113            public void setQueryString(String queryString) {
114                    _queryString = queryString;
115            }
116    
117            public void setShowBorders(boolean showBorders) {
118                    _showBorders = showBorders;
119            }
120    
121            public void setWidth(String width) {
122                    _width = width;
123            }
124    
125            protected String getPage() {
126                    return _PAGE;
127            }
128    
129            private static final String _PAGE = "/html/taglib/portlet/preview/page.jsp";
130    
131            private String _portletName;
132            private String _queryString;
133            private boolean _showBorders;
134            private String _width;
135    
136    }