1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.ReleaseInfo;
29  import com.liferay.portal.kernel.util.ServerDetector;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.model.PortletApp;
33  
34  import java.io.InputStream;
35  
36  import java.net.MalformedURLException;
37  import java.net.URL;
38  
39  import java.util.Enumeration;
40  import java.util.Set;
41  
42  import javax.portlet.PortletContext;
43  import javax.portlet.PortletRequestDispatcher;
44  
45  import javax.servlet.RequestDispatcher;
46  import javax.servlet.ServletContext;
47  
48  /**
49   * <a href="PortletContextImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Brett Randall
53   */
54  public class PortletContextImpl implements PortletContext {
55  
56      public PortletContextImpl(Portlet portlet, ServletContext servletContext) {
57          _portlet = portlet;
58          _servletContext = servletContext;
59          _servletContextName = GetterUtil.getString(
60              _servletContext.getServletContextName());
61      }
62  
63      public Object getAttribute(String name) {
64          if (name == null) {
65              throw new IllegalArgumentException();
66          }
67  
68          return _servletContext.getAttribute(name);
69      }
70  
71      public Enumeration<String> getAttributeNames() {
72          return _servletContext.getAttributeNames();
73      }
74  
75      public Enumeration<String> getContainerRuntimeOptions() {
76          return null;
77      }
78  
79      public String getInitParameter(String name) {
80          if (name == null) {
81              throw new IllegalArgumentException();
82          }
83  
84          return _servletContext.getInitParameter(name);
85      }
86  
87      public Enumeration<String> getInitParameterNames() {
88          return _servletContext.getInitParameterNames();
89      }
90  
91      public int getMajorVersion() {
92          return _MAJOR_VERSION;
93      }
94  
95      public String getMimeType(String file) {
96          return _servletContext.getMimeType(file);
97      }
98  
99      public int getMinorVersion() {
100         return _MINOR_VERSION;
101     }
102 
103     public PortletRequestDispatcher getNamedDispatcher(String name) {
104         RequestDispatcher requestDispatcher = null;
105 
106         try {
107             requestDispatcher = _servletContext.getNamedDispatcher(name);
108         }
109         catch (IllegalArgumentException iae) {
110             return null;
111         }
112 
113         if (requestDispatcher != null) {
114             return new PortletRequestDispatcherImpl(
115                 requestDispatcher, true, this);
116         }
117         else {
118             return null;
119         }
120     }
121 
122     public Portlet getPortlet() {
123         return _portlet;
124     }
125 
126     public String getPortletContextName() {
127         return _servletContextName;
128     }
129 
130     public String getRealPath(String path) {
131         return _servletContext.getRealPath(path);
132     }
133 
134     public PortletRequestDispatcher getRequestDispatcher(String path) {
135         RequestDispatcher requestDispatcher = null;
136 
137         try {
138             requestDispatcher = _servletContext.getRequestDispatcher(path);
139         }
140         catch (IllegalArgumentException iae) {
141             return null;
142         }
143 
144         // Workaround for bug in Jetty 5 that returns the default request
145         // dispatcher instead of null for an invalid path
146 
147         if (ServerDetector.isJOnAS() && ServerDetector.isJetty() &&
148             (requestDispatcher != null) &&
149             (requestDispatcher.getClass().getName().equals(
150                 "org.mortbay.jetty.servlet.Dispatcher"))) {
151 
152             // Dispatcher[/,default[org.mortbay.jetty.servlet.Default]]
153 
154             String rdToString = requestDispatcher.toString();
155 
156             String rdPath = rdToString.substring(11, rdToString.indexOf(","));
157 
158             if (rdPath.equals(StringPool.SLASH) &&
159                 !path.equals(StringPool.SLASH)) {
160 
161                 requestDispatcher = null;
162             }
163         }
164 
165         if (requestDispatcher != null) {
166             return new PortletRequestDispatcherImpl(
167                 requestDispatcher, false, this, path);
168         }
169         else {
170             return null;
171         }
172     }
173 
174     public URL getResource(String path) throws MalformedURLException {
175         if ((path == null) || (!path.startsWith(StringPool.SLASH))) {
176             throw new MalformedURLException();
177         }
178 
179         return _servletContext.getResource(path);
180     }
181 
182     public InputStream getResourceAsStream(String path) {
183         return _servletContext.getResourceAsStream(path);
184     }
185 
186     public Set<String> getResourcePaths(String path) {
187         return _servletContext.getResourcePaths(path);
188     }
189 
190     public String getServerInfo() {
191         return ReleaseInfo.getServerInfo();
192     }
193 
194     public ServletContext getServletContext() {
195         return _servletContext;
196     }
197 
198     public boolean isWARFile() {
199         PortletApp portletApp = _portlet.getPortletApp();
200 
201         return portletApp.isWARFile();
202     }
203 
204     public void log(String msg) {
205         if (_log.isInfoEnabled()) {
206             _log.info(msg);
207         }
208     }
209 
210     public void log(String msg, Throwable throwable) {
211         if (_log.isInfoEnabled()) {
212             _log.info(msg, throwable);
213         }
214     }
215 
216     public void removeAttribute(String name) {
217         if (name == null) {
218             throw new IllegalArgumentException();
219         }
220 
221         _servletContext.removeAttribute(name);
222     }
223 
224     public void setAttribute(String name, Object obj) {
225         if (name == null) {
226             throw new IllegalArgumentException();
227         }
228 
229         _servletContext.setAttribute(name, obj);
230     }
231 
232     private static int _MAJOR_VERSION = 2;
233 
234     private static int _MINOR_VERSION = 0;
235 
236     private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
237 
238     private Portlet _portlet;
239     private ServletContext _servletContext;
240     private String _servletContextName;
241 
242 }