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