1 17 package org.apache.servicemix.http; 18 19 import java.io.IOException ; 20 import java.io.OutputStream ; 21 import java.net.URI ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.mortbay.jetty.MimeTypes; 32 import org.mortbay.util.ByteArrayISO8859Writer; 33 import org.mortbay.util.StringUtil; 34 35 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 36 37 public class ManagedContextManager implements ContextManager { 38 39 private static final Log logger = LogFactory.getLog(ManagedContextManager.class); 40 41 private HttpConfiguration configuration; 42 private Map managedContexts; 43 44 public void init() throws Exception { 45 if (configuration == null) { 46 configuration = new HttpConfiguration(); 47 } 48 managedContexts = new ConcurrentHashMap(); 49 } 50 51 public void shutDown() throws Exception { 52 stop(); 53 } 54 55 public void start() throws Exception { 56 } 57 58 public void stop() throws Exception { 59 } 60 61 public synchronized Object createContext(String strUrl, 62 HttpProcessor processor) throws Exception { 63 URI uri = new URI (strUrl); 64 String path = uri.getPath(); 65 if (!path.startsWith("/")) { 66 path = path + "/"; 67 } 68 if (!path.endsWith("/")) { 69 path = path + "/"; 70 } 71 managedContexts.put(path, processor); 72 return path; 73 } 74 75 public synchronized void remove(Object context) throws Exception { 76 managedContexts.remove(context); 77 } 78 79 public HttpConfiguration getConfiguration() { 80 return configuration; 81 } 82 83 public void setConfiguration(HttpConfiguration configuration) { 84 this.configuration = configuration; 85 } 86 87 public HttpProcessor getMainProcessor() { 88 if (!configuration.isManaged()) { 89 throw new IllegalStateException ("ServerManager is not managed"); 90 } 91 return new MainProcessor(); 92 } 93 94 protected class MainProcessor implements HttpProcessor { 95 private String mapping; 96 public MainProcessor() { 97 this.mapping = configuration.getMapping(); 98 } 99 public String getAuthMethod() { 100 return null; 101 } 102 public SslParameters getSsl() { 103 return null; 104 } 105 public void process(HttpServletRequest request, HttpServletResponse response) throws Exception { 106 String uri = request.getRequestURI(); 107 if ("/".equals(uri) && "GET".equals(request.getMethod())) { 108 displayServices(request, response); 109 return; 110 } 111 Set urls = managedContexts.keySet(); 112 for (Iterator iter = urls.iterator(); iter.hasNext();) { 113 String url = (String ) iter.next(); 114 if (uri.startsWith(request.getContextPath() + mapping + url)) { 115 HttpProcessor proc = (HttpProcessor) managedContexts.get(url); 116 proc.process(request, response); 117 return; 118 } 119 } 120 displayServices(request, response); 121 } 122 public void displayServices(HttpServletRequest request, HttpServletResponse response) throws IOException { 123 response.setStatus(404); 124 response.setContentType(MimeTypes.TEXT_HTML); 125 126 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500); 127 128 String uri = request.getRequestURI(); 129 uri = StringUtil.replace(uri, "<", "<"); 130 uri = StringUtil.replace(uri, ">", ">"); 131 132 writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found"); 133 writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n"); 134 writer.write("No service matched or handled this request.<BR>"); 135 writer.write("Known services are: <ul>"); 136 137 Set servers = ManagedContextManager.this.managedContexts.keySet(); 138 for (Iterator iter = servers.iterator(); iter.hasNext();) { 139 String context = (String ) iter.next(); 140 if (!context.endsWith("/")) { 141 context += "/"; 142 } 143 String protocol = request.isSecure() ? "https" : "http"; 144 context = protocol + "://" + request.getLocalName() + ":" + request.getLocalPort() + request.getContextPath() + mapping + context; 145 writer.write("<li><a HREF=\""); 146 writer.write(context); 147 writer.write("?wsdl\">"); 148 writer.write(context); 149 writer.write("</a></li>\n"); 150 } 151 152 for (int i=0; i < 10; i++) { 153 writer.write("\n<!-- Padding for IE -->"); 154 } 155 156 writer.write("\n</BODY>\n</HTML>\n"); 157 writer.flush(); 158 response.setContentLength(writer.size()); 159 OutputStream out = response.getOutputStream(); 160 writer.writeTo(out); 161 out.close(); 162 } 163 } 164 165 } 166 | Popular Tags |