1   /**
2    * Copyright (c) 2000-2008 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.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ReleaseInfo;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.model.PortletApp;
30  
31  import java.io.InputStream;
32  
33  import java.net.MalformedURLException;
34  import java.net.URL;
35  
36  import java.util.Enumeration;
37  import java.util.Set;
38  
39  import javax.portlet.PortletContext;
40  import javax.portlet.PortletRequestDispatcher;
41  
42  import javax.servlet.RequestDispatcher;
43  import javax.servlet.ServletContext;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
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 that returns the default request
146         // dispatcher instead of null for an invalid path
147 
148         if ((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 = LogFactory.getLog(PortletContextImpl.class);
237 
238     private Portlet _portlet;
239     private ServletContext _servletContext;
240     private String _servletContextName;
241 
242 }