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.apache.bridges.struts;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletContext;
018    import com.liferay.portal.kernel.servlet.ServletContextProvider;
019    import com.liferay.portal.kernel.util.JavaConstants;
020    import com.liferay.portal.util.PortalUtil;
021    
022    import javax.portlet.GenericPortlet;
023    import javax.portlet.PortletContext;
024    import javax.portlet.PortletRequest;
025    import javax.portlet.PortletResponse;
026    
027    import javax.servlet.ServletContext;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author James Schopp
033     * @author Michael Young
034     * @author Deepak Gothe
035     * @author Raymond Augé
036     */
037    public class LiferayServletContextProvider implements ServletContextProvider {
038    
039            public HttpServletRequest getHttpServletRequest(
040                    GenericPortlet portlet, PortletRequest portletRequest) {
041    
042                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
043                            portletRequest);
044    
045                    return new LiferayStrutsRequestImpl(request);
046            }
047    
048            public HttpServletResponse getHttpServletResponse(
049                    GenericPortlet portlet, PortletResponse portletResponse) {
050    
051                    return PortalUtil.getHttpServletResponse(portletResponse);
052            }
053    
054            public ServletContext getServletContext(GenericPortlet portlet) {
055                    PortletContext portletContext = portlet.getPortletContext();
056    
057                    ServletContext servletContext =
058                            (ServletContext)portletContext.getAttribute(
059                                    JavaConstants.JAVAX_PORTLET_SERVLET_CONTEXT);
060    
061                    if (servletContext == null) {
062                            LiferayPortletContext liferayPortletContext =
063                                    (LiferayPortletContext)portlet.getPortletContext();
064    
065                            servletContext = liferayPortletContext.getServletContext();
066                    }
067    
068                    return getServletContext(servletContext);
069            }
070    
071            public ServletContext getServletContext(ServletContext servletContext) {
072                    return new LiferayServletContext(servletContext);
073            }
074    
075    }