001    /**
002     * Copyright (c) 2000-present 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.portal.kernel.trash;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.util.WebKeys;
021    
022    import java.io.IOException;
023    
024    import javax.servlet.RequestDispatcher;
025    import javax.servlet.ServletContext;
026    import javax.servlet.ServletException;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Alexander Chow
032     */
033    public abstract class BaseJSPTrashRenderer extends BaseTrashRenderer {
034    
035            public abstract String getJspPath(
036                    HttpServletRequest request, String template);
037    
038            @Override
039            public boolean include(
040                            HttpServletRequest request, HttpServletResponse response,
041                            String template)
042                    throws Exception {
043    
044                    ServletContext servletContext = getServletContext(request);
045    
046                    String jspPath = getJspPath(request, template);
047    
048                    if (Validator.isNull(jspPath)) {
049                            return false;
050                    }
051    
052                    RequestDispatcher requestDispatcher =
053                            servletContext.getRequestDispatcher(jspPath);
054    
055                    try {
056                            requestDispatcher.include(request, response);
057    
058                            return true;
059                    }
060                    catch (ServletException se) {
061                            _log.error("Unable to include JSP " + jspPath, se);
062    
063                            throw new IOException("Unable to include " + jspPath, se);
064                    }
065            }
066    
067            public void setServletContext(ServletContext servletContext) {
068                    _servletContext = servletContext;
069            }
070    
071            protected ServletContext getServletContext(HttpServletRequest request) {
072                    if (_servletContext != null) {
073                            return _servletContext;
074                    }
075    
076                    return (ServletContext)request.getAttribute(WebKeys.CTX);
077            }
078    
079            private static final Log _log = LogFactoryUtil.getLog(
080                    BaseJSPTrashRenderer.class);
081    
082            private ServletContext _servletContext;
083    
084    }