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