001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.util;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.log.LogUtil;
021    import com.liferay.portal.kernel.servlet.PipingServletResponse;
022    import com.liferay.portal.kernel.servlet.TrackedServletRequest;
023    import com.liferay.portal.kernel.util.ServerDetector;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.Portlet;
027    import com.liferay.portal.model.PortletApp;
028    import com.liferay.portal.service.PortletLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    
032    import java.util.HashMap;
033    import java.util.Map;
034    
035    import javax.servlet.RequestDispatcher;
036    import javax.servlet.ServletContext;
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.jsp.JspException;
039    import javax.servlet.jsp.tagext.DynamicAttributes;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class IncludeTag
045            extends ParamAndPropertyAncestorTagImpl implements DynamicAttributes {
046    
047            public int doEndTag() throws JspException {
048                    try {
049                            String page = getPage();
050    
051                            if (Validator.isNull(page)) {
052                                    page = getEndPage();
053                            }
054    
055                            _callSetAttributes();
056    
057                            if (Validator.isNotNull(page)) {
058                                    _doInclude(page);
059                            }
060    
061                            return EVAL_PAGE;
062                    }
063                    finally {
064                            _dynamicAttributes.clear();
065    
066                            clearParams();
067                            clearProperties();
068    
069                            _cleanUpSetAttributes();
070    
071                            if (!ServerDetector.isResin()) {
072                                    _page = null;
073    
074                                    cleanUp();
075                            }
076                    }
077            }
078    
079            public int doStartTag() throws JspException {
080                    String page = getStartPage();
081    
082                    if (Validator.isNull(page)) {
083                            return EVAL_BODY_BUFFERED;
084                    }
085    
086                    _callSetAttributes();
087    
088                    _doInclude(page);
089    
090                    return EVAL_BODY_INCLUDE;
091            }
092    
093            public void setDynamicAttribute(
094                    String uri, String localName, Object value) {
095    
096                    _dynamicAttributes.put(localName, value);
097            }
098    
099            public void setPage(String page) {
100                    _page = page;
101            }
102    
103            public void setPortletId(String portletId) {
104                    _portletId = portletId;
105            }
106    
107            public void runEndTag() throws JspException {
108                    doStartTag();
109            }
110    
111            public void runStartTag() throws JspException {
112                    doStartTag();
113            }
114    
115            public void runTag() throws JspException {
116                    doStartTag();
117                    doEndTag();
118            }
119    
120            protected void cleanUp() {
121            }
122    
123            protected Map<String, Object> getDynamicAttributes() {
124                    return _dynamicAttributes;
125            }
126    
127            protected String getEndPage() {
128                    return null;
129            }
130    
131            protected String getPage() {
132                    return _page;
133            }
134    
135            protected ServletContext getServletContext(
136                            ServletContext servletContext, HttpServletRequest request)
137                    throws SystemException {
138    
139                    if (Validator.isNull(_portletId)) {
140                            return servletContext;
141                    }
142    
143                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
144                            WebKeys.THEME_DISPLAY);
145    
146                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
147                            themeDisplay.getCompanyId(), _portletId);
148    
149                    if (portlet == null) {
150                            return servletContext;
151                    }
152    
153                    PortletApp portletApp = portlet.getPortletApp();
154    
155                    if (!portletApp.isWARFile()) {
156                            return servletContext;
157                    }
158    
159                    return PortalUtil.getServletContext(portlet, servletContext);
160            }
161    
162            protected String getStartPage() {
163                    return null;
164            }
165    
166            protected void include(String page) throws Exception {
167                    ServletContext servletContext = getServletContext();
168                    HttpServletRequest request = getServletRequest();
169    
170                    servletContext = getServletContext(servletContext, request);
171    
172                    RequestDispatcher requestDispatcher =
173                            servletContext.getRequestDispatcher(page);
174    
175                    requestDispatcher.include(
176                            request, new PipingServletResponse(pageContext));
177            }
178    
179            protected boolean isCleanUpSetAttributes() {
180                    return _CLEAN_UP_SET_ATTRIBUTES;
181            }
182    
183            protected void setAttributes(HttpServletRequest request) {
184            }
185    
186            private void _callSetAttributes() {
187                    if (_calledSetAttributes) {
188                            return;
189                    }
190    
191                    _calledSetAttributes = true;
192    
193                    HttpServletRequest request =
194                            (HttpServletRequest)pageContext.getRequest();
195    
196                    if (isCleanUpSetAttributes()) {
197                            _trackedRequest = new TrackedServletRequest(request);
198    
199                            request = _trackedRequest;
200                    }
201    
202                    setAttributes(request);
203            }
204    
205            private void _cleanUpSetAttributes() {
206                    _calledSetAttributes = false;
207    
208                    if (isCleanUpSetAttributes()) {
209                            for (String name : _trackedRequest.getSetAttributes()) {
210                                    _trackedRequest.removeAttribute(name);
211                            }
212    
213                            _trackedRequest = null;
214                    }
215            }
216    
217            private void _doInclude(String page) throws JspException {
218                    try {
219                            include(page);
220                    }
221                    catch (Exception e) {
222                            HttpServletRequest request = getServletRequest();
223    
224                            String currentURL = (String)request.getAttribute(
225                                    WebKeys.CURRENT_URL);
226    
227                            _log.error(
228                                    "Current URL " + currentURL + " generates exception: " +
229                                            e.getMessage());
230    
231                            LogUtil.log(_log, e);
232    
233                            if (e instanceof JspException) {
234                                    throw (JspException)e;
235                            }
236                    }
237            }
238    
239            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = false;
240    
241            private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
242    
243            private boolean _calledSetAttributes;
244            private Map<String, Object> _dynamicAttributes =
245                    new HashMap<String, Object>();
246            private String _page;
247            private String _portletId;
248            private TrackedServletRequest _trackedRequest;
249    
250    }