KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > servlet > internal > ServletContextAdaptor


1 /*******************************************************************************
2  * Copyright (c) 2005-2007 Cognos Incorporated, IBM Corporation and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Cognos Incorporated - initial API and implementation
10  * IBM Corporation - bug fixes and enhancements
11  *******************************************************************************/

12 package org.eclipse.equinox.http.servlet.internal;
13
14 import java.io.*;
15 import java.lang.reflect.Method JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.security.*;
18 import java.util.*;
19 import javax.servlet.*;
20 import org.osgi.service.http.HttpContext;
21
22 public class ServletContextAdaptor implements ServletContext {
23
24     private ServletContext servletContext;
25     HttpContext httpContext;
26     private AccessControlContext acc;
27     private ProxyContext proxyContext;
28
29     public ServletContextAdaptor(ProxyContext proxyContext, ServletContext servletContext, HttpContext httpContext, AccessControlContext acc) {
30         this.servletContext = servletContext;
31         this.httpContext = httpContext;
32         this.acc = acc;
33         this.proxyContext = proxyContext;
34     }
35
36     /**
37      * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
38      *
39      * This method was added in the Servlet 2.3 API however the OSGi HttpService currently does not provide
40      * support for this method in the HttpContext interface. To support "getResourcePaths(...) this
41      * implementation uses reflection to check for and then call the associated HttpContext.getResourcePaths(...)
42      * method opportunistically. Null is returned if the method is not present or fails.
43      */

44     public Set getResourcePaths(String JavaDoc name) {
45         if (name == null || !name.startsWith("/")) //$NON-NLS-1$
46
return null;
47         try {
48             Method JavaDoc getResourcePathsMethod = httpContext.getClass().getMethod("getResourcePaths", new Class JavaDoc[] {String JavaDoc.class}); //$NON-NLS-1$
49
if (!getResourcePathsMethod.isAccessible())
50                 getResourcePathsMethod.setAccessible(true);
51             return (Set) getResourcePathsMethod.invoke(httpContext, new Object JavaDoc[] {name});
52         } catch (Exception JavaDoc e) {
53             // ignore
54
}
55         return null;
56     }
57
58     public Object JavaDoc getAttribute(String JavaDoc attributeName) {
59         Dictionary attributes = proxyContext.getContextAttributes(httpContext);
60         return attributes.get(attributeName);
61     }
62
63     public Enumeration getAttributeNames() {
64         Dictionary attributes = proxyContext.getContextAttributes(httpContext);
65         return attributes.keys();
66     }
67
68     public void setAttribute(String JavaDoc attributeName, Object JavaDoc attributeValue) {
69         Dictionary attributes = proxyContext.getContextAttributes(httpContext);
70         attributes.put(attributeName, attributeValue);
71     }
72
73     public void removeAttribute(String JavaDoc attributeName) {
74         Dictionary attributes = proxyContext.getContextAttributes(httpContext);
75         attributes.remove(attributeName);
76     }
77
78     public String JavaDoc getMimeType(String JavaDoc name) {
79         String JavaDoc mimeType = httpContext.getMimeType(name);
80         return (mimeType != null) ? mimeType : servletContext.getMimeType(name);
81     }
82
83     public URL JavaDoc getResource(final String JavaDoc name) {
84         try {
85             return (URL JavaDoc) AccessController.doPrivileged(new PrivilegedExceptionAction() {
86                 public Object JavaDoc run() throws Exception JavaDoc {
87                     return httpContext.getResource(name);
88                 }
89             }, acc);
90         } catch (PrivilegedActionException e) {
91             log(e.getException().getMessage(), e.getException());
92         }
93         return null;
94     }
95
96     public InputStream getResourceAsStream(String JavaDoc name) {
97         URL JavaDoc url = getResource(name);
98         if (url != null) {
99             try {
100                 return url.openStream();
101             } catch (IOException e) {
102                 log("Error opening stream for resource '" + name + "'", e); //$NON-NLS-1$ //$NON-NLS-2$
103
}
104         }
105         return null;
106     }
107
108     public ServletContext getContext(String JavaDoc arg0) {
109         return servletContext.getContext(arg0);
110     }
111
112     public String JavaDoc getInitParameter(String JavaDoc arg0) {
113         return servletContext.getInitParameter(arg0);
114     }
115
116     public Enumeration getInitParameterNames() {
117         return servletContext.getInitParameterNames();
118     }
119
120     public int getMajorVersion() {
121         return servletContext.getMajorVersion();
122     }
123
124     public int getMinorVersion() {
125         return servletContext.getMinorVersion();
126     }
127
128     public RequestDispatcher getNamedDispatcher(String JavaDoc arg0) {
129         return new RequestDispatcherAdaptor(servletContext.getNamedDispatcher(arg0));
130     }
131
132     public String JavaDoc getRealPath(String JavaDoc arg0) {
133         return servletContext.getRealPath(arg0);
134     }
135
136     public RequestDispatcher getRequestDispatcher(String JavaDoc arg0) {
137         return new RequestDispatcherAdaptor(servletContext.getRequestDispatcher(proxyContext.getServletPath() + arg0));
138     }
139
140     public String JavaDoc getServerInfo() {
141         return servletContext.getServerInfo();
142     }
143
144     /**@deprecated*/
145     public Servlet getServlet(String JavaDoc arg0) throws ServletException {
146         return servletContext.getServlet(arg0);
147     }
148
149     public String JavaDoc getServletContextName() {
150         return servletContext.getServletContextName();
151     }
152
153     /**@deprecated*/
154     public Enumeration getServletNames() {
155         return servletContext.getServletNames();
156     }
157
158     /**@deprecated*/
159     public Enumeration getServlets() {
160         return servletContext.getServlets();
161     }
162
163     /**@deprecated*/
164     public void log(Exception JavaDoc arg0, String JavaDoc arg1) {
165         servletContext.log(arg0, arg1);
166     }
167
168     public void log(String JavaDoc arg0, Throwable JavaDoc arg1) {
169         servletContext.log(arg0, arg1);
170     }
171
172     public void log(String JavaDoc arg0) {
173         servletContext.log(arg0);
174     }
175
176     // Added in Servlet 2.5
177
public String JavaDoc getContextPath() {
178         try {
179             Method JavaDoc getContextPathMethod = servletContext.getClass().getMethod("getContextPath", null); //$NON-NLS-1$
180
return (String JavaDoc) getContextPathMethod.invoke(servletContext, null) + proxyContext.getServletPath();
181         } catch (Exception JavaDoc e) {
182             // ignore
183
}
184         return null;
185     }
186 }
187
Popular Tags