1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.taglib.util;
16  
17  import com.liferay.portal.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.portal.util.PortalUtil;
30  
31  import javax.servlet.RequestDispatcher;
32  import javax.servlet.ServletContext;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.jsp.JspException;
35  
36  /**
37   * <a href="IncludeTag.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class IncludeTag extends ParamAndPropertyAncestorTagImpl {
42  
43      public int doEndTag() throws JspException {
44          HttpServletRequest request = null;
45  
46          try {
47              ServletContext servletContext = getServletContext();
48              request = getServletRequest();
49              StringServletResponse stringResponse = getServletResponse();
50  
51              Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
52  
53              String page = getPage();
54  
55              if (isTheme()) {
56                  ThemeUtil.include(
57                      servletContext, request, stringResponse, pageContext, page,
58                      theme);
59              }
60              else {
61                  servletContext = getServletContext(servletContext, request);
62  
63                  RequestDispatcher requestDispatcher =
64                      servletContext.getRequestDispatcher(page);
65  
66                  requestDispatcher.include(request, stringResponse);
67              }
68  
69              pageContext.getOut().print(stringResponse.getString());
70  
71              return EVAL_PAGE;
72          }
73          catch (Exception e) {
74              if (request != null) {
75                  String currentURL = (String)request.getAttribute(
76                      WebKeys.CURRENT_URL);
77  
78                  _log.error(
79                      "Current URL " + currentURL + " generates exception: " +
80                          e.getMessage());
81              }
82  
83              LogUtil.log(_log, e);
84  
85              if (e instanceof JspException) {
86                  throw (JspException)e;
87              }
88  
89              return EVAL_PAGE;
90          }
91          finally {
92              clearParams();
93              clearProperties();
94          }
95      }
96  
97      public boolean isTheme() {
98          return false;
99      }
100 
101     public String getPage() {
102         if (Validator.isNull(_page)) {
103             return getDefaultPage();
104         }
105         else {
106             return _page;
107         }
108     }
109 
110     public void setPage(String page) {
111         _page = page;
112     }
113 
114     public void setPortletId(String portletId) {
115         _portletId = portletId;
116     }
117 
118     public ServletContext getServletContext() {
119         if (_servletContext != null) {
120             return _servletContext;
121         }
122         else {
123             return super.getServletContext();
124         }
125     }
126 
127     public void setServletContext(ServletContext servletContext) {
128         _servletContext = servletContext;
129     }
130 
131     protected String getDefaultPage() {
132         return null;
133     }
134 
135     protected ServletContext getServletContext(
136             ServletContext servletContext, HttpServletRequest request)
137         throws SystemException {
138 
139         if (Validator.isNull(_portletId)) {
140             return servletContext;
141         }
142 
143         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
144             WebKeys.THEME_DISPLAY);
145 
146         Portlet portlet = PortletLocalServiceUtil.getPortletById(
147             themeDisplay.getCompanyId(), _portletId);
148 
149         if (portlet == null) {
150             return servletContext;
151         }
152 
153         PortletApp portletApp = portlet.getPortletApp();
154 
155         if (!portletApp.isWARFile()) {
156             return servletContext;
157         }
158 
159         return PortalUtil.getServletContext(portlet, servletContext);
160     }
161 
162     private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
163 
164     private String _page;
165     private String _portletId;
166     private ServletContext _servletContext;
167 
168 }