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.PipingServletResponse;
22  import com.liferay.portal.kernel.servlet.TrackedServletRequest;
23  import com.liferay.portal.kernel.util.ServerDetector;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.util.WebKeys;
26  import com.liferay.portal.model.Portlet;
27  import com.liferay.portal.model.PortletApp;
28  import com.liferay.portal.service.PortletLocalServiceUtil;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import javax.servlet.RequestDispatcher;
36  import javax.servlet.ServletContext;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.jsp.JspException;
39  import javax.servlet.jsp.tagext.DynamicAttributes;
40  
41  /**
42   * <a href="IncludeTag.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class IncludeTag
47      extends ParamAndPropertyAncestorTagImpl implements DynamicAttributes {
48  
49      public int doEndTag() throws JspException {
50          try {
51              String page = getPage();
52  
53              if (Validator.isNull(page)) {
54                  page = getEndPage();
55              }
56  
57              _callSetAttributes();
58  
59              if (Validator.isNotNull(page)) {
60                  _doInclude(page);
61              }
62  
63              return EVAL_PAGE;
64          }
65          finally {
66              _dynamicAttributes.clear();
67  
68              clearParams();
69              clearProperties();
70  
71              _cleanUpSetAttributes();
72  
73              if (!ServerDetector.isResin()) {
74                  _page = null;
75  
76                  cleanUp();
77              }
78          }
79      }
80  
81      public int doStartTag() throws JspException {
82          String page = getStartPage();
83  
84          if (Validator.isNull(page)) {
85              return EVAL_BODY_BUFFERED;
86          }
87  
88          _callSetAttributes();
89  
90          _doInclude(page);
91  
92          return EVAL_BODY_INCLUDE;
93      }
94  
95      public void setDynamicAttribute(
96          String uri, String localName, Object value) {
97  
98          _dynamicAttributes.put(localName, value);
99      }
100 
101     public void setPage(String page) {
102         _page = page;
103     }
104 
105     public void setPortletId(String portletId) {
106         _portletId = portletId;
107     }
108 
109     public void runEndTag() throws JspException {
110         doStartTag();
111     }
112 
113     public void runStartTag() throws JspException {
114         doStartTag();
115     }
116 
117     public void runTag() throws JspException {
118         doStartTag();
119         doEndTag();
120     }
121 
122     protected void cleanUp() {
123     }
124 
125     protected Map<String, Object> getDynamicAttributes() {
126         return _dynamicAttributes;
127     }
128 
129     protected String getEndPage() {
130         return null;
131     }
132 
133     protected String getPage() {
134         return _page;
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     protected String getStartPage() {
165         return null;
166     }
167 
168     protected void include(String page) throws Exception {
169         ServletContext servletContext = getServletContext();
170         HttpServletRequest request = getServletRequest();
171 
172         servletContext = getServletContext(servletContext, request);
173 
174         RequestDispatcher requestDispatcher =
175             servletContext.getRequestDispatcher(page);
176 
177         requestDispatcher.include(
178             request, new PipingServletResponse(pageContext));
179     }
180 
181     protected boolean isCleanUpSetAttributes() {
182         return _CLEAN_UP_SET_ATTRIBUTES;
183     }
184 
185     protected void setAttributes(HttpServletRequest request) {
186     }
187 
188     private void _callSetAttributes() {
189         if (_calledSetAttributes) {
190             return;
191         }
192 
193         _calledSetAttributes = true;
194 
195         HttpServletRequest request =
196             (HttpServletRequest)pageContext.getRequest();
197 
198         if (isCleanUpSetAttributes()) {
199             _trackedRequest = new TrackedServletRequest(request);
200 
201             request = _trackedRequest;
202         }
203 
204         setAttributes(request);
205     }
206 
207     private void _cleanUpSetAttributes() {
208         _calledSetAttributes = false;
209 
210         if (isCleanUpSetAttributes()) {
211             for (String name : _trackedRequest.getSetAttributes()) {
212                 _trackedRequest.removeAttribute(name);
213             }
214 
215             _trackedRequest = null;
216         }
217     }
218 
219     private void _doInclude(String page) throws JspException {
220         try {
221             include(page);
222         }
223         catch (Exception e) {
224             HttpServletRequest request = getServletRequest();
225 
226             String currentURL = (String)request.getAttribute(
227                 WebKeys.CURRENT_URL);
228 
229             _log.error(
230                 "Current URL " + currentURL + " generates exception: " +
231                     e.getMessage());
232 
233             LogUtil.log(_log, e);
234 
235             if (e instanceof JspException) {
236                 throw (JspException)e;
237             }
238         }
239     }
240 
241     private static final boolean _CLEAN_UP_SET_ATTRIBUTES = false;
242 
243     private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
244 
245     private boolean _calledSetAttributes;
246     private Map<String, Object> _dynamicAttributes =
247         new HashMap<String, Object>();
248     private String _page;
249     private String _portletId;
250     private TrackedServletRequest _trackedRequest;
251 
252 }