001    /**
002     * Copyright (c) 2000-2013 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.servlet;
016    
017    import javax.portlet.PortletConfig;
018    import javax.portlet.PortletRequest;
019    import javax.portlet.PortletResponse;
020    
021    import javax.servlet.ServletConfig;
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    /**
026     * @author Deepak Gothe
027     */
028    public class PortletServletObjectsFactory implements ServletObjectsFactory {
029    
030            @Override
031            public ServletConfig getServletConfig(
032                    PortletConfig portletConfig, PortletRequest portletRequest) {
033    
034                    Object servletConfig = portletConfig.getPortletContext().getAttribute(
035                            _PORTLET_CONTAINER_SERVLET_CONFIG);
036    
037                    if (servletConfig == null) {
038                            servletConfig = portletRequest.getAttribute(
039                                    PortletServlet.PORTLET_SERVLET_CONFIG);
040                    }
041    
042                    return (ServletConfig)servletConfig;
043            }
044    
045            @Override
046            public HttpServletRequest getServletRequest(PortletRequest portletRequest) {
047                    Object request = portletRequest.getAttribute(
048                            _PORTLET_CONTAINER_SERVLET_REQUEST);
049    
050                    if (request == null) {
051                            request = portletRequest.getAttribute(
052                                    PortletServlet.PORTLET_SERVLET_REQUEST);
053                    }
054    
055                    return (HttpServletRequest)request;
056            }
057    
058            @Override
059            public HttpServletResponse getServletResponse(
060                    PortletRequest portletRequest, PortletResponse portletResponse) {
061    
062                    Object response = portletRequest.getAttribute(
063                            _PORTLET_CONTAINER_SERVLET_RESPONSE);
064    
065                    if (response == null) {
066                            response = portletRequest.getAttribute(
067                                    PortletServlet.PORTLET_SERVLET_RESPONSE);
068                    }
069    
070                    return (HttpServletResponse)response;
071            }
072    
073            private static final String _PORTLET_CONTAINER_SERVLET_CONFIG =
074                    "javax.portlet.portletc.servletConfig";
075    
076            private static final String _PORTLET_CONTAINER_SERVLET_REQUEST =
077                    "javax.portlet.portletc.httpServletRequest";
078    
079            private static final String _PORTLET_CONTAINER_SERVLET_RESPONSE =
080                    "javax.portlet.portletc.httpServletResponse";
081    
082    }