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.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.log.LogUtil;
28  import com.liferay.portal.kernel.servlet.StringServletResponse;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Theme;
31  import com.liferay.portal.util.WebKeys;
32  
33  import javax.servlet.RequestDispatcher;
34  import javax.servlet.ServletContext;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.jsp.JspException;
37  
38  /**
39   * <a href="IncludeTag.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class IncludeTag extends ParamAndPropertyAncestorTagImpl {
44  
45      public int doEndTag() throws JspException {
46          HttpServletRequest request = null;
47  
48          try {
49              ServletContext servletContext = getServletContext();
50              request = getServletRequest();
51              StringServletResponse stringResponse = getServletResponse();
52  
53              Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
54  
55              String page = getPage();
56  
57              if (isTheme()) {
58                  ThemeUtil.include(
59                      servletContext, request, stringResponse, pageContext, page,
60                      theme);
61              }
62              else {
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 ServletContext getServletContext() {
115         if (_servletContext != null) {
116             return _servletContext;
117         }
118         else {
119             return super.getServletContext();
120         }
121     }
122 
123     public void setServletContext(ServletContext servletContext) {
124         _servletContext = servletContext;
125     }
126 
127     protected String getDefaultPage() {
128         return null;
129     }
130 
131     private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
132 
133     private String _page;
134     private ServletContext _servletContext;
135 
136 }