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