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, isTrimNewLines()));
177            }
178    
179            protected boolean isCleanUpSetAttributes() {
180                    return _CLEAN_UP_SET_ATTRIBUTES;
181            }
182    
183            protected boolean isTrimNewLines() {
184                    return _TRIM_NEW_LINES;
185            }
186    
187            protected void setAttributes(HttpServletRequest request) {
188            }
189    
190            private void _callSetAttributes() {
191                    if (_calledSetAttributes) {
192                            return;
193                    }
194    
195                    _calledSetAttributes = true;
196    
197                    HttpServletRequest request =
198                            (HttpServletRequest)pageContext.getRequest();
199    
200                    if (isCleanUpSetAttributes()) {
201                            _trackedRequest = new TrackedServletRequest(request);
202    
203                            request = _trackedRequest;
204                    }
205    
206                    setAttributes(request);
207            }
208    
209            private void _cleanUpSetAttributes() {
210                    _calledSetAttributes = false;
211    
212                    if (isCleanUpSetAttributes()) {
213                            for (String name : _trackedRequest.getSetAttributes()) {
214                                    _trackedRequest.removeAttribute(name);
215                            }
216    
217                            _trackedRequest = null;
218                    }
219            }
220    
221            private void _doInclude(String page) throws JspException {
222                    try {
223                            include(page);
224                    }
225                    catch (Exception e) {
226                            HttpServletRequest request = getServletRequest();
227    
228                            String currentURL = (String)request.getAttribute(
229                                    WebKeys.CURRENT_URL);
230    
231                            _log.error(
232                                    "Current URL " + currentURL + " generates exception: " +
233                                            e.getMessage());
234    
235                            LogUtil.log(_log, e);
236    
237                            if (e instanceof JspException) {
238                                    throw (JspException)e;
239                            }
240                    }
241            }
242    
243            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = false;
244    
245            private static final boolean _TRIM_NEW_LINES = false;
246    
247            private static Log _log = LogFactoryUtil.getLog(IncludeTag.class);
248    
249            private boolean _calledSetAttributes;
250            private Map<String, Object> _dynamicAttributes =
251                    new HashMap<String, Object>();
252            private String _page;
253            private String _portletId;
254            private TrackedServletRequest _trackedRequest;
255    
256    }