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.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.LiferayPortletContext;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.ReleaseInfo;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.model.PortletApp;
025    import com.liferay.portal.security.lang.DoPrivilegedUtil;
026    
027    import java.io.InputStream;
028    
029    import java.net.MalformedURLException;
030    import java.net.URL;
031    
032    import java.util.Enumeration;
033    import java.util.Set;
034    
035    import javax.portlet.PortletRequestDispatcher;
036    
037    import javax.servlet.RequestDispatcher;
038    import javax.servlet.ServletContext;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Brett Randall
043     */
044    public class PortletContextImpl implements LiferayPortletContext {
045    
046            public PortletContextImpl(Portlet portlet, ServletContext servletContext) {
047                    _portlet = portlet;
048                    _servletContext = servletContext;
049                    _servletContextName = GetterUtil.getString(
050                            _servletContext.getServletContextName());
051            }
052    
053            public Object getAttribute(String name) {
054                    if (name == null) {
055                            throw new IllegalArgumentException();
056                    }
057    
058                    return _servletContext.getAttribute(name);
059            }
060    
061            public Enumeration<String> getAttributeNames() {
062                    return _servletContext.getAttributeNames();
063            }
064    
065            public Enumeration<String> getContainerRuntimeOptions() {
066                    return null;
067            }
068    
069            public String getInitParameter(String name) {
070                    if (name == null) {
071                            throw new IllegalArgumentException();
072                    }
073    
074                    return _servletContext.getInitParameter(name);
075            }
076    
077            public Enumeration<String> getInitParameterNames() {
078                    return _servletContext.getInitParameterNames();
079            }
080    
081            public int getMajorVersion() {
082                    return _MAJOR_VERSION;
083            }
084    
085            public String getMimeType(String file) {
086                    return _servletContext.getMimeType(file);
087            }
088    
089            public int getMinorVersion() {
090                    return _MINOR_VERSION;
091            }
092    
093            public PortletRequestDispatcher getNamedDispatcher(String name) {
094                    RequestDispatcher requestDispatcher = null;
095    
096                    try {
097                            requestDispatcher = _servletContext.getNamedDispatcher(name);
098                    }
099                    catch (IllegalArgumentException iae) {
100                            return null;
101                    }
102    
103                    if (requestDispatcher != null) {
104                            return DoPrivilegedUtil.wrap(
105                                    new PortletRequestDispatcherImpl(
106                                            requestDispatcher, true, this), true);
107                    }
108                    else {
109                            return null;
110                    }
111            }
112    
113            public Portlet getPortlet() {
114                    return _portlet;
115            }
116    
117            public String getPortletContextName() {
118                    return _servletContextName;
119            }
120    
121            public String getRealPath(String path) {
122                    return _servletContext.getRealPath(path);
123            }
124    
125            public PortletRequestDispatcher getRequestDispatcher(String path) {
126                    RequestDispatcher requestDispatcher = null;
127    
128                    try {
129                            requestDispatcher = _servletContext.getRequestDispatcher(path);
130                    }
131                    catch (IllegalArgumentException iae) {
132                            return null;
133                    }
134    
135                    if (requestDispatcher != null) {
136                            return DoPrivilegedUtil.wrap(
137                                    new PortletRequestDispatcherImpl(
138                                            requestDispatcher, false, this, path), true);
139                    }
140                    else {
141                            return null;
142                    }
143            }
144    
145            public URL getResource(String path) throws MalformedURLException {
146                    if ((path == null) || !path.startsWith(StringPool.SLASH)) {
147                            throw new MalformedURLException();
148                    }
149    
150                    return _servletContext.getResource(path);
151            }
152    
153            public InputStream getResourceAsStream(String path) {
154                    return _servletContext.getResourceAsStream(path);
155            }
156    
157            public Set<String> getResourcePaths(String path) {
158                    return _servletContext.getResourcePaths(path);
159            }
160    
161            public String getServerInfo() {
162                    return ReleaseInfo.getServerInfo();
163            }
164    
165            public ServletContext getServletContext() {
166                    return _servletContext;
167            }
168    
169            public boolean isWARFile() {
170                    PortletApp portletApp = _portlet.getPortletApp();
171    
172                    return portletApp.isWARFile();
173            }
174    
175            public void log(String msg) {
176                    if (_log.isInfoEnabled()) {
177                            _log.info(msg);
178                    }
179            }
180    
181            public void log(String msg, Throwable throwable) {
182                    if (_log.isInfoEnabled()) {
183                            _log.info(msg, throwable);
184                    }
185            }
186    
187            public void removeAttribute(String name) {
188                    if (name == null) {
189                            throw new IllegalArgumentException();
190                    }
191    
192                    _servletContext.removeAttribute(name);
193            }
194    
195            public void setAttribute(String name, Object obj) {
196                    if (name == null) {
197                            throw new IllegalArgumentException();
198                    }
199    
200                    _servletContext.setAttribute(name, obj);
201            }
202    
203            private static final int _MAJOR_VERSION = 2;
204    
205            private static final int _MINOR_VERSION = 0;
206    
207            private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
208    
209            private Portlet _portlet;
210            private ServletContext _servletContext;
211            private String _servletContextName;
212    
213    }