1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.ReleaseInfo;
21  import com.liferay.portal.kernel.util.ServerDetector;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.model.Portlet;
24  import com.liferay.portal.model.PortletApp;
25  
26  import java.io.InputStream;
27  
28  import java.net.MalformedURLException;
29  import java.net.URL;
30  
31  import java.util.Enumeration;
32  import java.util.Set;
33  
34  import javax.portlet.PortletContext;
35  import javax.portlet.PortletRequestDispatcher;
36  
37  import javax.servlet.RequestDispatcher;
38  import javax.servlet.ServletContext;
39  
40  /**
41   * <a href="PortletContextImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Brett Randall
45   */
46  public class PortletContextImpl implements PortletContext {
47  
48      public PortletContextImpl(Portlet portlet, ServletContext servletContext) {
49          _portlet = portlet;
50          _servletContext = servletContext;
51          _servletContextName = GetterUtil.getString(
52              _servletContext.getServletContextName());
53      }
54  
55      public Object getAttribute(String name) {
56          if (name == null) {
57              throw new IllegalArgumentException();
58          }
59  
60          return _servletContext.getAttribute(name);
61      }
62  
63      public Enumeration<String> getAttributeNames() {
64          return _servletContext.getAttributeNames();
65      }
66  
67      public Enumeration<String> getContainerRuntimeOptions() {
68          return null;
69      }
70  
71      public String getInitParameter(String name) {
72          if (name == null) {
73              throw new IllegalArgumentException();
74          }
75  
76          return _servletContext.getInitParameter(name);
77      }
78  
79      public Enumeration<String> getInitParameterNames() {
80          return _servletContext.getInitParameterNames();
81      }
82  
83      public int getMajorVersion() {
84          return _MAJOR_VERSION;
85      }
86  
87      public String getMimeType(String file) {
88          return _servletContext.getMimeType(file);
89      }
90  
91      public int getMinorVersion() {
92          return _MINOR_VERSION;
93      }
94  
95      public PortletRequestDispatcher getNamedDispatcher(String name) {
96          RequestDispatcher requestDispatcher = null;
97  
98          try {
99              requestDispatcher = _servletContext.getNamedDispatcher(name);
100         }
101         catch (IllegalArgumentException iae) {
102             return null;
103         }
104 
105         if (requestDispatcher != null) {
106             return new PortletRequestDispatcherImpl(
107                 requestDispatcher, true, this);
108         }
109         else {
110             return null;
111         }
112     }
113 
114     public Portlet getPortlet() {
115         return _portlet;
116     }
117 
118     public String getPortletContextName() {
119         return _servletContextName;
120     }
121 
122     public String getRealPath(String path) {
123         return _servletContext.getRealPath(path);
124     }
125 
126     public PortletRequestDispatcher getRequestDispatcher(String path) {
127         RequestDispatcher requestDispatcher = null;
128 
129         try {
130             requestDispatcher = _servletContext.getRequestDispatcher(path);
131         }
132         catch (IllegalArgumentException iae) {
133             return null;
134         }
135 
136         // Workaround for bug in Jetty 5 that returns the default request
137         // dispatcher instead of null for an invalid path
138 
139         if (ServerDetector.isJOnAS() && ServerDetector.isJetty() &&
140             (requestDispatcher != null) &&
141             (requestDispatcher.getClass().getName().equals(
142                 "org.mortbay.jetty.servlet.Dispatcher"))) {
143 
144             // Dispatcher[/,default[org.mortbay.jetty.servlet.Default]]
145 
146             String rdToString = requestDispatcher.toString();
147 
148             String rdPath = rdToString.substring(11, rdToString.indexOf(","));
149 
150             if (rdPath.equals(StringPool.SLASH) &&
151                 !path.equals(StringPool.SLASH)) {
152 
153                 requestDispatcher = null;
154             }
155         }
156 
157         if (requestDispatcher != null) {
158             return new PortletRequestDispatcherImpl(
159                 requestDispatcher, false, this, path);
160         }
161         else {
162             return null;
163         }
164     }
165 
166     public URL getResource(String path) throws MalformedURLException {
167         if ((path == null) || (!path.startsWith(StringPool.SLASH))) {
168             throw new MalformedURLException();
169         }
170 
171         return _servletContext.getResource(path);
172     }
173 
174     public InputStream getResourceAsStream(String path) {
175         return _servletContext.getResourceAsStream(path);
176     }
177 
178     public Set<String> getResourcePaths(String path) {
179         return _servletContext.getResourcePaths(path);
180     }
181 
182     public String getServerInfo() {
183         return ReleaseInfo.getServerInfo();
184     }
185 
186     public ServletContext getServletContext() {
187         return _servletContext;
188     }
189 
190     public boolean isWARFile() {
191         PortletApp portletApp = _portlet.getPortletApp();
192 
193         return portletApp.isWARFile();
194     }
195 
196     public void log(String msg) {
197         if (_log.isInfoEnabled()) {
198             _log.info(msg);
199         }
200     }
201 
202     public void log(String msg, Throwable throwable) {
203         if (_log.isInfoEnabled()) {
204             _log.info(msg, throwable);
205         }
206     }
207 
208     public void removeAttribute(String name) {
209         if (name == null) {
210             throw new IllegalArgumentException();
211         }
212 
213         _servletContext.removeAttribute(name);
214     }
215 
216     public void setAttribute(String name, Object obj) {
217         if (name == null) {
218             throw new IllegalArgumentException();
219         }
220 
221         _servletContext.setAttribute(name, obj);
222     }
223 
224     private static int _MAJOR_VERSION = 2;
225 
226     private static int _MINOR_VERSION = 0;
227 
228     private static Log _log = LogFactoryUtil.getLog(PortletContextImpl.class);
229 
230     private Portlet _portlet;
231     private ServletContext _servletContext;
232     private String _servletContextName;
233 
234 }