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.util.bridges.struts;
016    
017    import com.liferay.portal.kernel.servlet.ServletContextProvider;
018    
019    import javax.portlet.GenericPortlet;
020    import javax.portlet.PortletContext;
021    import javax.portlet.PortletRequest;
022    import javax.portlet.PortletResponse;
023    
024    import javax.servlet.ServletContext;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    /**
029     * @author Michael Young
030     */
031    public class LiferayServletContextProviderWrapper
032            implements org.apache.portals.bridges.common.ServletContextProvider {
033    
034            @Override
035            public HttpServletRequest getHttpServletRequest(
036                    GenericPortlet portlet, PortletRequest portletRequest) {
037    
038                    ServletContextProvider provider = _getProvider(portlet);
039    
040                    return provider.getHttpServletRequest(portlet, portletRequest);
041            }
042    
043            @Override
044            public HttpServletResponse getHttpServletResponse(
045                    GenericPortlet portlet, PortletResponse portletResponse) {
046    
047                    ServletContextProvider provider = _getProvider(portlet);
048    
049                    return provider.getHttpServletResponse(portlet, portletResponse);
050            }
051    
052            @Override
053            public ServletContext getServletContext(GenericPortlet portlet) {
054                    ServletContextProvider provider = _getProvider(portlet);
055    
056                    return provider.getServletContext(portlet);
057            }
058    
059            private ServletContextProvider _getProvider(GenericPortlet portlet) {
060                    PortletContext portletContext = portlet.getPortletContext();
061    
062                    if (_provider == null) {
063                            _provider = (ServletContextProvider)portletContext.getAttribute(
064                                    ServletContextProvider.STRUTS_BRIDGES_CONTEXT_PROVIDER);
065                    }
066    
067                    return _provider;
068            }
069    
070            private ServletContextProvider _provider;
071    
072    }