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.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  public class PortletContextImpl implements PortletContext {
54  
55      public PortletContextImpl(Portlet portlet, ServletContext servletContext) {
56          _portlet = portlet;
57          _servletContext = servletContext;
58          _servletContextName = GetterUtil.getString(
59              _servletContext.getServletContextName());
60      }
61  
62      public Object getAttribute(String name) {
63          if (name == null) {
64              throw new IllegalArgumentException();
65          }
66  
67          return _servletContext.getAttribute(name);
68      }
69  
70      public Enumeration<String> getAttributeNames() {
71          return _servletContext.getAttributeNames();
72      }
73  
74      public Enumeration<String> getContainerRuntimeOptions() {
75          return null;
76      }
77  
78      public String getInitParameter(String name) {
79          if (name == null) {
80              throw new IllegalArgumentException();
81          }
82  
83          return _servletContext.getInitParameter(name);
84      }
85  
86      public Enumeration<String> getInitParameterNames() {
87          return _servletContext.getInitParameterNames();
88      }
89  
90      public int getMajorVersion() {
91          return _MAJOR_VERSION;
92      }
93  
94      public String getMimeType(String file) {
95          return _servletContext.getMimeType(file);
96      }
97  
98      public int getMinorVersion() {
99          return _MINOR_VERSION;
100     }
101 
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 new PortletRequestDispatcherImpl(
114                 requestDispatcher, true, this);
115         }
116         else {
117             return null;
118         }
119     }
120 
121     public Portlet getPortlet() {
122         return _portlet;
123     }
124 
125     public String getPortletContextName() {
126         return _servletContextName;
127     }
128 
129     public String getRealPath(String path) {
130         return _servletContext.getRealPath(path);
131     }
132 
133     public PortletRequestDispatcher getRequestDispatcher(String path) {
134         RequestDispatcher requestDispatcher = null;
135 
136         try {
137             requestDispatcher = _servletContext.getRequestDispatcher(path);
138         }
139         catch (IllegalArgumentException iae) {
140             return null;
141         }
142 
143         // Workaround for bug in Jetty that returns the default request
144         // dispatcher instead of null for an invalid path
145 
146         if ((requestDispatcher != null) &&
147             (requestDispatcher.getClass().getName().equals(
148                 "org.mortbay.jetty.servlet.Dispatcher"))) {
149 
150             // Dispatcher[/,default[org.mortbay.jetty.servlet.Default]]
151 
152             String rdToString = requestDispatcher.toString();
153 
154             String rdPath = rdToString.substring(11, rdToString.indexOf(","));
155 
156             if (rdPath.equals(StringPool.SLASH) &&
157                 !path.equals(StringPool.SLASH)) {
158 
159                 requestDispatcher = null;
160             }
161         }
162 
163         if (requestDispatcher != null) {
164             return new PortletRequestDispatcherImpl(
165                 requestDispatcher, false, this, path);
166         }
167         else {
168             return null;
169         }
170     }
171 
172     public URL getResource(String path) throws MalformedURLException {
173         if ((path == null) || (!path.startsWith(StringPool.SLASH))) {
174             throw new MalformedURLException();
175         }
176 
177         return _servletContext.getResource(path);
178     }
179 
180     public InputStream getResourceAsStream(String path) {
181         return _servletContext.getResourceAsStream(path);
182     }
183 
184     public Set<String> getResourcePaths(String path) {
185         return _servletContext.getResourcePaths(path);
186     }
187 
188     public String getServerInfo() {
189         return ReleaseInfo.getServerInfo();
190     }
191 
192     public ServletContext getServletContext() {
193         return _servletContext;
194     }
195 
196     public boolean isWARFile() {
197         PortletApp portletApp = _portlet.getPortletApp();
198 
199         return portletApp.isWARFile();
200     }
201 
202     public void log(String msg) {
203         if (_log.isInfoEnabled()) {
204             _log.info(msg);
205         }
206     }
207 
208     public void log(String msg, Throwable throwable) {
209         if (_log.isInfoEnabled()) {
210             _log.info(msg, throwable);
211         }
212     }
213 
214     public void removeAttribute(String name) {
215         if (name == null) {
216             throw new IllegalArgumentException();
217         }
218 
219         _servletContext.removeAttribute(name);
220     }
221 
222     public void setAttribute(String name, Object obj) {
223         if (name == null) {
224             throw new IllegalArgumentException();
225         }
226 
227         _servletContext.setAttribute(name, obj);
228     }
229 
230     private static int _MAJOR_VERSION = 2;
231 
232     private static int _MINOR_VERSION = 0;
233 
234     private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
235 
236     private Portlet _portlet;
237     private ServletContext _servletContext;
238     private String _servletContextName;
239 
240 }