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.util;
016    
017    import com.liferay.portal.kernel.servlet.DirectRequestDispatcherFactoryUtil;
018    import com.liferay.portal.kernel.util.WebKeys;
019    import com.liferay.taglib.servlet.PipingServletResponse;
020    
021    import java.io.IOException;
022    
023    import javax.servlet.RequestDispatcher;
024    import javax.servlet.ServletContext;
025    import javax.servlet.ServletException;
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    import javax.servlet.jsp.PageContext;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Shuyang Zhou
033     */
034    public class PortalIncludeUtil {
035    
036            public static void include(
037                            PageContext pageContext, HTMLRenderer htmlRenderer)
038                    throws IOException, ServletException {
039    
040                    HttpServletRequest request =
041                            (HttpServletRequest)pageContext.getRequest();
042                    HttpServletResponse response =
043                            (HttpServletResponse)pageContext.getResponse();
044    
045                    htmlRenderer.renderHTML(
046                            request, new PipingServletResponse(response, pageContext.getOut()));
047            }
048    
049            public static void include(PageContext pageContext, String path)
050                    throws IOException, ServletException {
051    
052                    HttpServletRequest request =
053                            (HttpServletRequest)pageContext.getRequest();
054                    HttpServletResponse response =
055                            (HttpServletResponse)pageContext.getResponse();
056    
057                    ServletContext servletContext = (ServletContext)request.getAttribute(
058                            WebKeys.CTX);
059    
060                    RequestDispatcher requestDispatcher =
061                            DirectRequestDispatcherFactoryUtil.getRequestDispatcher(
062                                    servletContext, path);
063    
064                    requestDispatcher.include(
065                            request, new PipingServletResponse(response, pageContext.getOut()));
066            }
067    
068            public interface HTMLRenderer {
069    
070                    public void renderHTML(
071                                    HttpServletRequest request, HttpServletResponse response)
072                            throws IOException, ServletException;
073    
074            }
075    
076    }