001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.ReleaseInfo;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.model.Portlet;
027:
028: import java.io.InputStream;
029:
030: import java.net.MalformedURLException;
031: import java.net.URL;
032:
033: import java.util.Enumeration;
034: import java.util.Set;
035:
036: import javax.portlet.PortletContext;
037: import javax.portlet.PortletRequestDispatcher;
038:
039: import javax.servlet.RequestDispatcher;
040: import javax.servlet.ServletContext;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: /**
046: * <a href="PortletContextImpl.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: * @author Brett Randall
050: *
051: */
052: public class PortletContextImpl implements PortletContext {
053:
054: public PortletContextImpl(Portlet portlet, ServletContext ctx) {
055: _portlet = portlet;
056: _ctx = ctx;
057: _ctxName = GetterUtil.getString(_ctx.getServletContextName());
058: }
059:
060: public String getServerInfo() {
061: return ReleaseInfo.getServerInfo();
062: }
063:
064: public int getMajorVersion() {
065: return _MAJOR_VERSION;
066: }
067:
068: public int getMinorVersion() {
069: return _MINOR_VERSION;
070: }
071:
072: public String getPortletContextName() {
073: return _ctxName;
074: }
075:
076: public PortletRequestDispatcher getRequestDispatcher(String path) {
077: RequestDispatcher rd = null;
078:
079: try {
080: rd = _ctx.getRequestDispatcher(path);
081: } catch (IllegalArgumentException iae) {
082: return null;
083: }
084:
085: // Workaround for bug in Jetty that returns the default request
086: // dispatcher instead of null for an invalid path
087:
088: if ((rd != null)
089: && (rd.getClass().getName()
090: .equals("org.mortbay.jetty.servlet.Dispatcher"))) {
091:
092: // Dispatcher[/,default[org.mortbay.jetty.servlet.Default]]
093:
094: String rdToString = rd.toString();
095:
096: String rdPath = rdToString.substring(11, rdToString
097: .indexOf(","));
098:
099: if (rdPath.equals(StringPool.SLASH)
100: && !path.equals(StringPool.SLASH)) {
101:
102: rd = null;
103: }
104: }
105:
106: if (rd != null) {
107: return new PortletRequestDispatcherImpl(rd, this , path);
108: } else {
109: return null;
110: }
111: }
112:
113: public PortletRequestDispatcher getNamedDispatcher(String name) {
114: RequestDispatcher rd = null;
115:
116: try {
117: rd = _ctx.getNamedDispatcher(name);
118: } catch (IllegalArgumentException iae) {
119: return null;
120: }
121:
122: if (rd != null) {
123: return new PortletRequestDispatcherImpl(rd, this );
124: } else {
125: return null;
126: }
127: }
128:
129: public String getMimeType(String file) {
130: return _ctx.getMimeType(file);
131: }
132:
133: public String getRealPath(String path) {
134: return _ctx.getRealPath(path);
135: }
136:
137: public Set getResourcePaths(String path) {
138: return _ctx.getResourcePaths(path);
139: }
140:
141: public URL getResource(String path) throws MalformedURLException {
142: if ((path == null) || (!path.startsWith(StringPool.SLASH))) {
143: throw new MalformedURLException();
144: }
145:
146: return _ctx.getResource(path);
147: }
148:
149: public InputStream getResourceAsStream(String path) {
150: return _ctx.getResourceAsStream(path);
151: }
152:
153: public Object getAttribute(String name) {
154: if (name == null) {
155: throw new IllegalArgumentException();
156: }
157:
158: return _ctx.getAttribute(name);
159: }
160:
161: public void removeAttribute(String name) {
162: if (name == null) {
163: throw new IllegalArgumentException();
164: }
165:
166: _ctx.removeAttribute(name);
167: }
168:
169: public void setAttribute(String name, Object obj) {
170: if (name == null) {
171: throw new IllegalArgumentException();
172: }
173:
174: _ctx.setAttribute(name, obj);
175: }
176:
177: public Enumeration getAttributeNames() {
178: return _ctx.getAttributeNames();
179: }
180:
181: public String getInitParameter(String name) {
182: if (name == null) {
183: throw new IllegalArgumentException();
184: }
185:
186: return _ctx.getInitParameter(name);
187: }
188:
189: public Enumeration getInitParameterNames() {
190: return _ctx.getInitParameterNames();
191: }
192:
193: public Portlet getPortlet() {
194: return _portlet;
195: }
196:
197: public ServletContext getServletContext() {
198: return _ctx;
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: private static int _MAJOR_VERSION = 1;
214:
215: private static int _MINOR_VERSION = 0;
216:
217: private static Log _log = LogFactory
218: .getLog(PortletContextImpl.class);
219:
220: private Portlet _portlet;
221: private ServletContext _ctx;
222: private String _ctxName = null;
223:
224: }
|