1 19 20 package com.sslexplorer.server.jetty; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.mortbay.http.HttpContext; 31 import org.mortbay.http.HttpServer; 32 import org.mortbay.http.ResourceCache; 33 import org.mortbay.util.Resource; 34 35 import com.sslexplorer.boot.ContextHolder; 36 import com.sslexplorer.boot.RequestHandler; 37 import com.sslexplorer.server.Main; 38 39 49 public class CustomHttpContext extends HttpContext { 50 51 final static Log log = LogFactory.getLog(CustomHttpContext.class); 53 54 private static final long serialVersionUID = -4556775842230104865L; 56 57 59 private List resourceCaches = new ArrayList (); 60 private RequestHandlerAdapter requestHandlerAdapater; 61 62 68 public CustomHttpContext(HttpServer server, String path, boolean useDevConfig) throws Exception { 69 super(server, path); 70 addHandler(requestHandlerAdapater = new RequestHandlerAdapter()); 71 if (useDevConfig) { 72 setClassLoader(Main.class.getClassLoader()); 73 setClassLoaderJava2Compliant(true); 74 } 75 setTempDirectory(ContextHolder.getContext().getTempDirectory()); 76 setWelcomeFiles(new String [] { "showHome.do" }); 77 resourceCaches = new ArrayList (); 78 } 79 80 92 public void registerRequestHandler(RequestHandler requestHandler) { 93 requestHandlerAdapater.registerRequestHandler(requestHandler); 94 } 95 96 108 public void deregisterRequestHandler(RequestHandler requestHandler) { 109 requestHandlerAdapater.deregisterRequestHandler(requestHandler); 110 } 111 112 122 public void addResourceCache(ResourceCache cache) { 123 resourceCaches.add(cache); 124 } 125 126 135 public void removeResourceCache(ResourceCache cache) { 136 resourceCaches.remove(cache); 137 } 138 139 142 public Resource getResource(String pathInContext) throws IOException { 143 if (log.isDebugEnabled()) 144 log.debug("Getting resource " + pathInContext + ", checking in plugins"); 145 for (Iterator i = resourceCaches.iterator(); i.hasNext();) { 147 ResourceCache cache = (ResourceCache) i.next(); 148 Resource r = cache.getResource(pathInContext); 149 if (r != null && r.exists() && !r.isDirectory()) { 150 if (log.isDebugEnabled()) 151 log.debug("Found in " + cache.getBaseResource().toString()); 152 return r; 153 } 154 } 155 if (log.isDebugEnabled()) 156 log.debug("Checking for alias"); 157 158 if (log.isDebugEnabled()) 160 log.debug("Passsing to main webapp"); 161 return super.getResource(pathInContext); 162 } 163 } 164 | Popular Tags |