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.taglib.util;
16  
17  import com.liferay.portal.kernel.exception.SystemException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.log.LogUtil;
21  import com.liferay.portal.kernel.servlet.StringServletResponse;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.util.WebKeys;
24  import com.liferay.portal.model.Portlet;
25  import com.liferay.portal.model.PortletApp;
26  import com.liferay.portal.model.Theme;
27  import com.liferay.portal.service.PortletLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portlet.PortletConfigFactory;
30  import com.liferay.portlet.PortletContextImpl;
31  
32  import javax.portlet.PortletConfig;
33  
34  import javax.servlet.RequestDispatcher;
35  import javax.servlet.ServletContext;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.jsp.JspException;
38  
39  /**
40   * <a href="IncludeTag.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class IncludeTag extends ParamAndPropertyAncestorTagImpl {
45  
46      public int doEndTag() throws JspException {
47          HttpServletRequest request = null;
48  
49          try {
50              ServletContext servletContext = getServletContext();
51              request = getServletRequest();
52              StringServletResponse stringResponse = getServletResponse();
53  
54              Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
55  
56              String page = getPage();
57  
58              if (isTheme()) {
59                  ThemeUtil.include(
60                      servletContext, request, stringResponse, pageContext, page,
61                      theme);
62              }
63              else {
64                  servletContext = getServletContext(servletContext, request);
65  
66                  RequestDispatcher requestDispatcher =
67                      servletContext.getRequestDispatcher(page);
68  
69                  requestDispatcher.include(request, stringResponse);
70              }
71  
72              pageContext.getOut().print(stringResponse.getString());
73  
74              return EVAL_PAGE;
75          }
76          catch (Exception e) {
77              if (request != null) {
78                  String currentURL = (String)request.getAttribute(
79                      WebKeys.CURRENT_URL);
80  
81                  _log.error(
82                      "Current URL " + currentURL + " generates exception: " +
83                          e.getMessage());
84              }
85  
86              LogUtil.log(_log, e);
87  
88              if (e instanceof JspException) {
89                  throw (JspException)e;
90              }
91  
92              return EVAL_PAGE;
93          }
94          finally {
95              clearParams();
96              clearProperties();
97          }
98      }
99  
100     public boolean isTheme() {
101         return false;
102     }
103 
104     public String getPage() {
105         if (Validator.isNull(_page)) {
106             return getDefaultPage();
107         }
108         else {
109             return _page;
110         }
111     }
112 
113     public void setPage(String page) {
114         _page = page;
115     }
116 
117     public void setPortletId(String portletId) {
118         _portletId = portletId;
119     }
120 
121     public ServletContext getServletContext() {
122         if (_servletContext != null) {
123             return _servletContext;
124         }
125         else {
126             return super.getServletContext();
127         }
128     }
129 
130     public void setServletContext(ServletContext servletContext) {
131         _servletContext = servletContext;
132     }
133 
134     protected String getDefaultPage() {
135         return null;
136     }
137 
138     protected ServletContext getServletContext(
139             ServletContext servletContext, HttpServletRequest request)
140         throws SystemException {
141 
142         if (Validator.isNull(_portletId)) {
143             return servletContext;
144         }
145 
146         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
147             WebKeys.THEME_DISPLAY);
148 
149         Portlet portlet = PortletLocalServiceUtil.getPortletById(
150             themeDisplay.getCompanyId(), _portletId);
151 
152         if (portlet == null) {
153             return servletContext;
154         }
155 
156         PortletApp portletApp = portlet.getPortletApp();
157 
158         if (!portletApp.isWARFile()) {
159             return servletContext;
160         }
161 
162         PortletConfig portletConfig = PortletConfigFactory.create(
163             portlet, servletContext);
164         PortletContextImpl portletContextImpl =
165             (PortletContextImpl)portletConfig.getPortletContext();
166 
167         return portletContextImpl.getServletContext();
168     }
169 
170     private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
171 
172     private String _page;
173     private String _portletId;
174     private ServletContext _servletContext;
175 
176 }