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            @Override
054            public Object getAttribute(String name) {
055                    if (name == null) {
056                            throw new IllegalArgumentException();
057                    }
058    
059                    return _servletContext.getAttribute(name);
060            }
061    
062            @Override
063            public Enumeration<String> getAttributeNames() {
064                    return _servletContext.getAttributeNames();
065            }
066    
067            @Override
068            public Enumeration<String> getContainerRuntimeOptions() {
069                    return null;
070            }
071    
072            @Override
073            public String getInitParameter(String name) {
074                    if (name == null) {
075                            throw new IllegalArgumentException();
076                    }
077    
078                    return _servletContext.getInitParameter(name);
079            }
080    
081            @Override
082            public Enumeration<String> getInitParameterNames() {
083                    return _servletContext.getInitParameterNames();
084            }
085    
086            @Override
087            public int getMajorVersion() {
088                    return _MAJOR_VERSION;
089            }
090    
091            @Override
092            public String getMimeType(String file) {
093                    return _servletContext.getMimeType(file);
094            }
095    
096            @Override
097            public int getMinorVersion() {
098                    return _MINOR_VERSION;
099            }
100    
101            @Override
102            public PortletRequestDispatcher getNamedDispatcher(String name) {
103                    RequestDispatcher requestDispatcher = null;
104    
105                    try {
106                            requestDispatcher = _servletContext.getNamedDispatcher(name);
107                    }
108                    catch (IllegalArgumentException iae) {
109                            return null;
110                    }
111    
112                    if (requestDispatcher != null) {
113                            return DoPrivilegedUtil.wrapWhenActive(
114                                    new PortletRequestDispatcherImpl(
115                                            requestDispatcher, true, this));
116                    }
117                    else {
118                            return null;
119                    }
120            }
121    
122            @Override
123            public Portlet getPortlet() {
124                    return _portlet;
125            }
126    
127            @Override
128            public String getPortletContextName() {
129                    return _servletContextName;
130            }
131    
132            @Override
133            public String getRealPath(String path) {
134                    return _servletContext.getRealPath(path);
135            }
136    
137            @Override
138            public PortletRequestDispatcher getRequestDispatcher(String path) {
139                    RequestDispatcher requestDispatcher = null;
140    
141                    try {
142                            requestDispatcher = _servletContext.getRequestDispatcher(path);
143                    }
144                    catch (IllegalArgumentException iae) {
145                            return null;
146                    }
147    
148                    if (requestDispatcher != null) {
149                            return DoPrivilegedUtil.wrapWhenActive(
150                                    new PortletRequestDispatcherImpl(
151                                            requestDispatcher, false, this, path));
152                    }
153                    else {
154                            return null;
155                    }
156            }
157    
158            @Override
159            public URL getResource(String path) throws MalformedURLException {
160                    if ((path == null) || !path.startsWith(StringPool.SLASH)) {
161                            throw new MalformedURLException();
162                    }
163    
164                    return _servletContext.getResource(path);
165            }
166    
167            @Override
168            public InputStream getResourceAsStream(String path) {
169                    return _servletContext.getResourceAsStream(path);
170            }
171    
172            @Override
173            public Set<String> getResourcePaths(String path) {
174                    return _servletContext.getResourcePaths(path);
175            }
176    
177            @Override
178            public String getServerInfo() {
179                    return ReleaseInfo.getServerInfo();
180            }
181    
182            @Override
183            public ServletContext getServletContext() {
184                    return _servletContext;
185            }
186    
187            public boolean isWARFile() {
188                    PortletApp portletApp = _portlet.getPortletApp();
189    
190                    return portletApp.isWARFile();
191            }
192    
193            @Override
194            public void log(String msg) {
195                    if (_log.isInfoEnabled()) {
196                            _log.info(msg);
197                    }
198            }
199    
200            @Override
201            public void log(String msg, Throwable throwable) {
202                    if (_log.isInfoEnabled()) {
203                            _log.info(msg, throwable);
204                    }
205            }
206    
207            @Override
208            public void removeAttribute(String name) {
209                    if (name == null) {
210                            throw new IllegalArgumentException();
211                    }
212    
213                    _servletContext.removeAttribute(name);
214            }
215    
216            @Override
217            public void setAttribute(String name, Object obj) {
218                    if (name == null) {
219                            throw new IllegalArgumentException();
220                    }
221    
222                    _servletContext.setAttribute(name, obj);
223            }
224    
225            private static final int _MAJOR_VERSION = 2;
226    
227            private static final int _MINOR_VERSION = 0;
228    
229            private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
230    
231            private Portlet _portlet;
232            private ServletContext _servletContext;
233            private String _servletContextName;
234    
235    }