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.insertTilesVariables(request);
57  
58                  ThemeUtil.include(
59                      servletContext, request, stringResponse, pageContext, page,
60                      theme);
61              }
62              else {
63                  servletContext = getServletContext(servletContext, request);
64  
65                  RequestDispatcher requestDispatcher =
66                      servletContext.getRequestDispatcher(page);
67  
68                  requestDispatcher.include(request, stringResponse);
69              }
70  
71              pageContext.getOut().print(stringResponse.getString());
72  
73              return EVAL_PAGE;
74          }
75          catch (Exception e) {
76              if (request != null) {
77                  String currentURL = (String)request.getAttribute(
78                      WebKeys.CURRENT_URL);
79  
80                  _log.error(
81                      "Current URL " + currentURL + " generates exception: " +
82                          e.getMessage());
83              }
84  
85              LogUtil.log(_log, e);
86  
87              if (e instanceof JspException) {
88                  throw (JspException)e;
89              }
90  
91              return EVAL_PAGE;
92          }
93          finally {
94              clearParams();
95              clearProperties();
96          }
97      }
98  
99      public boolean isTheme() {
100         return false;
101     }
102 
103     public String getPage() {
104         if (Validator.isNull(_page)) {
105             return getDefaultPage();
106         }
107         else {
108             return _page;
109         }
110     }
111 
112     public void setPage(String page) {
113         _page = page;
114     }
115 
116     public void setPortletId(String portletId) {
117         _portletId = portletId;
118     }
119 
120     public ServletContext getServletContext() {
121         if (_servletContext != null) {
122             return _servletContext;
123         }
124         else {
125             return super.getServletContext();
126         }
127     }
128 
129     public void setServletContext(ServletContext servletContext) {
130         _servletContext = servletContext;
131     }
132 
133     protected String getDefaultPage() {
134         return null;
135     }
136 
137     protected ServletContext getServletContext(
138             ServletContext servletContext, HttpServletRequest request)
139         throws SystemException {
140 
141         if (Validator.isNull(_portletId)) {
142             return servletContext;
143         }
144 
145         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
146             WebKeys.THEME_DISPLAY);
147 
148         Portlet portlet = PortletLocalServiceUtil.getPortletById(
149             themeDisplay.getCompanyId(), _portletId);
150 
151         if (portlet == null) {
152             return servletContext;
153         }
154 
155         PortletApp portletApp = portlet.getPortletApp();
156 
157         if (!portletApp.isWARFile()) {
158             return servletContext;
159         }
160 
161         return PortalUtil.getServletContext(portlet, servletContext);
162     }
163 
164     private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
165 
166     private String _page;
167     private String _portletId;
168     private ServletContext _servletContext;
169 
170 }