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