1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.taglib.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.log.LogUtil;
25  import com.liferay.portal.kernel.servlet.StringServletResponse;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.Theme;
28  import com.liferay.portal.util.WebKeys;
29  
30  import javax.servlet.RequestDispatcher;
31  import javax.servlet.ServletContext;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.jsp.JspException;
34  
35  /**
36   * <a href="IncludeTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
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                  RequestDispatcher requestDispatcher =
62                      servletContext.getRequestDispatcher(page);
63  
64                  requestDispatcher.include(request, stringResponse);
65              }
66  
67              pageContext.getOut().print(stringResponse.getString());
68  
69              return EVAL_PAGE;
70          }
71          catch (Exception e) {
72              if (request != null) {
73                  String currentURL = (String)request.getAttribute(
74                      WebKeys.CURRENT_URL);
75  
76                  _log.error(
77                      "Current URL " + currentURL + " generates exception: " +
78                          e.getMessage());
79              }
80  
81              LogUtil.log(_log, e);
82  
83              if (e instanceof JspException) {
84                  throw (JspException)e;
85              }
86  
87              return EVAL_PAGE;
88          }
89          finally {
90              clearParams();
91              clearProperties();
92          }
93      }
94  
95      public boolean isTheme() {
96          return false;
97      }
98  
99      public String getPage() {
100         if (Validator.isNull(_page)) {
101             return getDefaultPage();
102         }
103         else {
104             return _page;
105         }
106     }
107 
108     public void setPage(String page) {
109         _page = page;
110     }
111 
112     public ServletContext getServletContext() {
113         if (_servletContext != null) {
114             return _servletContext;
115         }
116         else {
117             return super.getServletContext();
118         }
119     }
120 
121     public void setServletContext(ServletContext servletContext) {
122         _servletContext = servletContext;
123     }
124 
125     protected String getDefaultPage() {
126         return null;
127     }
128 
129     private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
130 
131     private String _page;
132     private ServletContext _servletContext;
133 
134 }