1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.servlet;
16  
17  import com.liferay.portal.kernel.util.WebKeys;
18  
19  import java.io.IOException;
20  
21  import javax.servlet.RequestDispatcher;
22  import javax.servlet.ServletContext;
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.jsp.PageContext;
27  
28  /**
29   * <a href="PortalIncludeUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class PortalIncludeUtil {
34  
35      public static void include(PageContext pageContext, String path)
36          throws IOException, ServletException {
37  
38          String s = toString(pageContext, path);
39  
40          pageContext.getOut().print(s);
41      }
42  
43      public static String toString(PageContext pageContext, String path)
44          throws IOException, ServletException {
45  
46          HttpServletRequest request =
47              (HttpServletRequest)pageContext.getRequest();
48          HttpServletResponse response =
49              (HttpServletResponse)pageContext.getResponse();
50  
51          ServletContext servletContext = (ServletContext)request.getAttribute(
52              WebKeys.CTX);
53  
54          StringServletResponse stringResponse = new StringServletResponse(
55              response);
56  
57          RequestDispatcher requestDispatcher =
58              servletContext.getRequestDispatcher(path);
59  
60          requestDispatcher.include(request, stringResponse);
61  
62          return stringResponse.getString();
63      }
64  
65  }