1 12 package org.eclipse.equinox.http.servlet.internal; 13 14 import java.io.*; 15 import java.lang.reflect.Method ; 16 import java.net.URL ; 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 44 public Set getResourcePaths(String name) { 45 if (name == null || !name.startsWith("/")) return null; 47 try { 48 Method getResourcePathsMethod = httpContext.getClass().getMethod("getResourcePaths", new Class [] {String .class}); if (!getResourcePathsMethod.isAccessible()) 50 getResourcePathsMethod.setAccessible(true); 51 return (Set) getResourcePathsMethod.invoke(httpContext, new Object [] {name}); 52 } catch (Exception e) { 53 } 55 return null; 56 } 57 58 public Object getAttribute(String 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 attributeName, Object attributeValue) { 69 Dictionary attributes = proxyContext.getContextAttributes(httpContext); 70 attributes.put(attributeName, attributeValue); 71 } 72 73 public void removeAttribute(String attributeName) { 74 Dictionary attributes = proxyContext.getContextAttributes(httpContext); 75 attributes.remove(attributeName); 76 } 77 78 public String getMimeType(String name) { 79 String mimeType = httpContext.getMimeType(name); 80 return (mimeType != null) ? mimeType : servletContext.getMimeType(name); 81 } 82 83 public URL getResource(final String name) { 84 try { 85 return (URL ) AccessController.doPrivileged(new PrivilegedExceptionAction() { 86 public Object run() throws Exception { 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 name) { 97 URL 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); } 104 } 105 return null; 106 } 107 108 public ServletContext getContext(String arg0) { 109 return servletContext.getContext(arg0); 110 } 111 112 public String getInitParameter(String 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 arg0) { 129 return new RequestDispatcherAdaptor(servletContext.getNamedDispatcher(arg0)); 130 } 131 132 public String getRealPath(String arg0) { 133 return servletContext.getRealPath(arg0); 134 } 135 136 public RequestDispatcher getRequestDispatcher(String arg0) { 137 return new RequestDispatcherAdaptor(servletContext.getRequestDispatcher(proxyContext.getServletPath() + arg0)); 138 } 139 140 public String getServerInfo() { 141 return servletContext.getServerInfo(); 142 } 143 144 145 public Servlet getServlet(String arg0) throws ServletException { 146 return servletContext.getServlet(arg0); 147 } 148 149 public String getServletContextName() { 150 return servletContext.getServletContextName(); 151 } 152 153 154 public Enumeration getServletNames() { 155 return servletContext.getServletNames(); 156 } 157 158 159 public Enumeration getServlets() { 160 return servletContext.getServlets(); 161 } 162 163 164 public void log(Exception arg0, String arg1) { 165 servletContext.log(arg0, arg1); 166 } 167 168 public void log(String arg0, Throwable arg1) { 169 servletContext.log(arg0, arg1); 170 } 171 172 public void log(String arg0) { 173 servletContext.log(arg0); 174 } 175 176 public String getContextPath() { 178 try { 179 Method getContextPathMethod = servletContext.getClass().getMethod("getContextPath", null); return (String ) getContextPathMethod.invoke(servletContext, null) + proxyContext.getServletPath(); 181 } catch (Exception e) { 182 } 184 return null; 185 } 186 } 187 | Popular Tags |