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