1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.util;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.log.LogUtil;
29  import com.liferay.portal.kernel.servlet.StringServletResponse;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.model.PortletApp;
33  import com.liferay.portal.model.Theme;
34  import com.liferay.portal.service.PortletLocalServiceUtil;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.PortletConfigFactory;
38  import com.liferay.portlet.PortletContextImpl;
39  
40  import javax.portlet.PortletConfig;
41  
42  import javax.servlet.RequestDispatcher;
43  import javax.servlet.ServletContext;
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.jsp.JspException;
46  
47  /**
48   * <a href="IncludeTag.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class IncludeTag extends ParamAndPropertyAncestorTagImpl {
53  
54      public int doEndTag() throws JspException {
55          HttpServletRequest request = null;
56  
57          try {
58              ServletContext servletContext = getServletContext();
59              request = getServletRequest();
60              StringServletResponse stringResponse = getServletResponse();
61  
62              Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
63  
64              String page = getPage();
65  
66              if (isTheme()) {
67                  ThemeUtil.include(
68                      servletContext, request, stringResponse, pageContext, page,
69                      theme);
70              }
71              else {
72                  servletContext = getServletContext(servletContext, request);
73  
74                  RequestDispatcher requestDispatcher =
75                      servletContext.getRequestDispatcher(page);
76  
77                  requestDispatcher.include(request, stringResponse);
78              }
79  
80              pageContext.getOut().print(stringResponse.getString());
81  
82              return EVAL_PAGE;
83          }
84          catch (Exception e) {
85              if (request != null) {
86                  String currentURL = (String)request.getAttribute(
87                      WebKeys.CURRENT_URL);
88  
89                  _log.error(
90                      "Current URL " + currentURL + " generates exception: " +
91                          e.getMessage());
92              }
93  
94              LogUtil.log(_log, e);
95  
96              if (e instanceof JspException) {
97                  throw (JspException)e;
98              }
99  
100             return EVAL_PAGE;
101         }
102         finally {
103             clearParams();
104             clearProperties();
105         }
106     }
107 
108     public boolean isTheme() {
109         return false;
110     }
111 
112     public String getPage() {
113         if (Validator.isNull(_page)) {
114             return getDefaultPage();
115         }
116         else {
117             return _page;
118         }
119     }
120 
121     public void setPage(String page) {
122         _page = page;
123     }
124 
125     public void setPortletId(String portletId) {
126         _portletId = portletId;
127     }
128 
129     public ServletContext getServletContext() {
130         if (_servletContext != null) {
131             return _servletContext;
132         }
133         else {
134             return super.getServletContext();
135         }
136     }
137 
138     public void setServletContext(ServletContext servletContext) {
139         _servletContext = servletContext;
140     }
141 
142     protected String getDefaultPage() {
143         return null;
144     }
145 
146     protected ServletContext getServletContext(
147             ServletContext servletContext, HttpServletRequest request)
148         throws SystemException {
149 
150         if (Validator.isNull(_portletId)) {
151             return servletContext;
152         }
153 
154         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
155             WebKeys.THEME_DISPLAY);
156 
157         Portlet portlet = PortletLocalServiceUtil.getPortletById(
158             themeDisplay.getCompanyId(), _portletId);
159 
160         if (portlet == null) {
161             return servletContext;
162         }
163 
164         PortletApp portletApp = portlet.getPortletApp();
165 
166         if (!portletApp.isWARFile()) {
167             return servletContext;
168         }
169 
170         PortletConfig portletConfig = PortletConfigFactory.create(
171                 portlet, servletContext);
172         PortletContextImpl portletContextImpl =
173             (PortletContextImpl)portletConfig.getPortletContext();
174 
175         return portletContextImpl.getServletContext();
176     }
177 
178     private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
179 
180     private String _page;
181     private String _portletId;
182     private ServletContext _servletContext;
183 
184 }