1 12 package org.eclipse.equinox.http.servlet.internal; 13 14 import java.io.IOException ; 15 import java.security.AccessController ; 16 import java.util.*; 17 import javax.servlet.*; 18 import javax.servlet.http.*; 19 import org.osgi.framework.Bundle; 20 import org.osgi.service.http.HttpContext; 21 import org.osgi.service.http.NamespaceException; 22 23 29 public class ProxyServlet extends HttpServlet { 30 31 private static final long serialVersionUID = 4117456123807468871L; 32 private Map registrations = new HashMap(); private Set servlets = new HashSet(); private ProxyContext proxyContext; 35 36 public void init(ServletConfig config) throws ServletException { 37 super.init(config); 38 proxyContext = new ProxyContext(config.getServletContext()); 39 Activator.addProxyServlet(this); 40 } 41 42 public void destroy() { 43 Activator.removeProxyServlet(this); 44 proxyContext.destroy(); 45 proxyContext = null; 46 super.destroy(); 47 } 48 49 52 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 53 proxyContext.initializeServletPath(req); 54 String alias = HttpServletRequestAdaptor.getDispatchPathInfo(req); 55 if (alias == null) 56 alias = "/"; 58 if (processAlias(req, resp, alias, null)) 60 return; 61 62 String extensionAlias = findExtensionAlias(alias); 63 alias = alias.substring(0, alias.lastIndexOf('/')); 64 65 while (alias.length() != 0) { 67 if (processAlias(req, resp, alias, extensionAlias)) 68 return; 69 alias = alias.substring(0, alias.lastIndexOf('/')); 70 } 71 72 if (extensionAlias != null) 74 extensionAlias = extensionAlias.substring(1); if (processAlias(req, resp, "/", extensionAlias)) return; 77 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "ProxyServlet: " + req.getRequestURI()); } 79 80 private String findExtensionAlias(String alias) { 81 String lastSegment = alias.substring(alias.lastIndexOf('/') + 1); 82 int dot = lastSegment.indexOf('.'); 83 if (dot == -1) 84 return null; 85 String extension = lastSegment.substring(dot + 1); 86 if (extension.length() == 0) 87 return null; 88 return "/*." + extension; } 90 91 private boolean processAlias(HttpServletRequest req, HttpServletResponse resp, String alias, String extensionAlias) throws ServletException, IOException { 92 Registration registration = null; 93 synchronized (this) { 94 if (extensionAlias == null) 95 registration = (Registration) registrations.get(alias); 96 else { 97 registration = (Registration) registrations.get(alias + extensionAlias); 98 if (registration != null) { 99 if (registration instanceof ServletRegistration) 101 alias = HttpServletRequestAdaptor.getDispatchPathInfo(req); 102 } else 103 registration = (Registration) registrations.get(alias); 104 } 105 106 if (registration != null) 107 registration.addReference(); 108 } 109 if (registration != null) { 110 try { 111 if (registration.handleRequest(req, resp, alias)) 112 return true; 113 } finally { 114 registration.removeReference(); 115 } 116 } 117 return false; 118 } 119 120 synchronized void unregister(String alias, boolean destroy) { 122 Registration removedRegistration = (Registration) registrations.remove(alias); 123 if (removedRegistration != null) { 124 if (destroy) 125 removedRegistration.destroy(); 126 removedRegistration.close(); 127 } 128 } 129 130 synchronized void registerServlet(String alias, Servlet servlet, Dictionary initparams, HttpContext context, Bundle bundle) throws ServletException, NamespaceException { 132 checkAlias(alias); 133 if (servlet == null) 134 throw new IllegalArgumentException ("Servlet cannot be null"); 136 ServletRegistration registration = new ServletRegistration(servlet, proxyContext, context, bundle, servlets); 137 registration.checkServletRegistration(); 138 139 ServletContext wrappedServletContext = new ServletContextAdaptor(proxyContext, getServletContext(), context, AccessController.getContext()); 140 ServletConfig servletConfig = new ServletConfigImpl(servlet, initparams, wrappedServletContext); 141 142 registration.init(servletConfig); 143 registrations.put(alias, registration); 144 } 145 146 synchronized void registerResources(String alias, String name, HttpContext context) throws NamespaceException { 148 checkAlias(alias); 149 checkName(name); 150 registrations.put(alias, new ResourceRegistration(name, context, getServletContext(), AccessController.getContext())); 151 } 152 153 private void checkName(String name) { 154 if (name == null) 155 throw new IllegalArgumentException ("Name cannot be null"); 157 if (name.endsWith("/") && !name.equals("/")) throw new IllegalArgumentException ("Invalid Name '" + name + "'"); } 160 161 private void checkAlias(String alias) throws NamespaceException { 162 if (alias == null) 163 throw new IllegalArgumentException ("Alias cannot be null"); 165 if (!alias.startsWith("/") || (alias.endsWith("/") && !alias.equals("/"))) throw new IllegalArgumentException ("Invalid alias '" + alias + "'"); 168 if (registrations.containsKey(alias)) 169 throw new NamespaceException("The alias '" + alias + "' is already in use."); } 171 } 172 | Popular Tags |