1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.StringPool;
30  import com.liferay.portal.model.Portlet;
31  import com.liferay.portal.model.PortletApp;
32  
33  import java.io.InputStream;
34  
35  import java.net.MalformedURLException;
36  import java.net.URL;
37  
38  import java.util.Enumeration;
39  import java.util.Set;
40  
41  import javax.portlet.PortletContext;
42  import javax.portlet.PortletRequestDispatcher;
43  
44  import javax.servlet.RequestDispatcher;
45  import javax.servlet.ServletContext;
46  
47  /**
48   * <a href="PortletContextImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Brett Randall
52   *
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 that returns the default request
145         // dispatcher instead of null for an invalid path
146 
147         if ((requestDispatcher != null) &&
148             (requestDispatcher.getClass().getName().equals(
149                 "org.mortbay.jetty.servlet.Dispatcher"))) {
150 
151             // Dispatcher[/,default[org.mortbay.jetty.servlet.Default]]
152 
153             String rdToString = requestDispatcher.toString();
154 
155             String rdPath = rdToString.substring(11, rdToString.indexOf(","));
156 
157             if (rdPath.equals(StringPool.SLASH) &&
158                 !path.equals(StringPool.SLASH)) {
159 
160                 requestDispatcher = null;
161             }
162         }
163 
164         if (requestDispatcher != null) {
165             return new PortletRequestDispatcherImpl(
166                 requestDispatcher, false, this, path);
167         }
168         else {
169             return null;
170         }
171     }
172 
173     public URL getResource(String path) throws MalformedURLException {
174         if ((path == null) || (!path.startsWith(StringPool.SLASH))) {
175             throw new MalformedURLException();
176         }
177 
178         return _servletContext.getResource(path);
179     }
180 
181     public InputStream getResourceAsStream(String path) {
182         return _servletContext.getResourceAsStream(path);
183     }
184 
185     public Set<String> getResourcePaths(String path) {
186         return _servletContext.getResourcePaths(path);
187     }
188 
189     public String getServerInfo() {
190         return ReleaseInfo.getServerInfo();
191     }
192 
193     public ServletContext getServletContext() {
194         return _servletContext;
195     }
196 
197     public boolean isWARFile() {
198         PortletApp portletApp = _portlet.getPortletApp();
199 
200         return portletApp.isWARFile();
201     }
202 
203     public void log(String msg) {
204         if (_log.isInfoEnabled()) {
205             _log.info(msg);
206         }
207     }
208 
209     public void log(String msg, Throwable throwable) {
210         if (_log.isInfoEnabled()) {
211             _log.info(msg, throwable);
212         }
213     }
214 
215     public void removeAttribute(String name) {
216         if (name == null) {
217             throw new IllegalArgumentException();
218         }
219 
220         _servletContext.removeAttribute(name);
221     }
222 
223     public void setAttribute(String name, Object obj) {
224         if (name == null) {
225             throw new IllegalArgumentException();
226         }
227 
228         _servletContext.setAttribute(name, obj);
229     }
230 
231     private static int _MAJOR_VERSION = 2;
232 
233     private static int _MINOR_VERSION = 0;
234 
235     private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
236 
237     private Portlet _portlet;
238     private ServletContext _servletContext;
239     private String _servletContextName;
240 
241 }