1 17 package org.apache.servicemix.http; 18 19 import java.util.EventListener ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import junit.framework.TestCase; 24 25 import org.apache.commons.httpclient.HttpClient; 26 import org.apache.commons.httpclient.methods.PostMethod; 27 import org.apache.commons.httpclient.methods.StringRequestEntity; 28 import org.apache.servicemix.components.http.InvalidStatusResponseException; 29 import org.apache.xbean.spring.context.XmlWebApplicationContext; 30 import org.mortbay.jetty.Connector; 31 import org.mortbay.jetty.Handler; 32 import org.mortbay.jetty.Server; 33 import org.mortbay.jetty.handler.ContextHandler; 34 import org.mortbay.jetty.handler.ContextHandlerCollection; 35 import org.mortbay.jetty.handler.HandlerCollection; 36 import org.mortbay.jetty.nio.SelectChannelConnector; 37 import org.mortbay.jetty.servlet.ServletHandler; 38 import org.mortbay.jetty.servlet.ServletHolder; 39 import org.mortbay.jetty.servlet.ServletMapping; 40 import org.springframework.web.context.ContextLoaderListener; 41 42 public class HttpManagedTest extends TestCase { 43 44 public void test() throws Exception { 45 ContextHandler context = new ContextHandler(); 46 context.setContextPath("/test"); 47 context.setEventListeners(new EventListener [] { new ContextLoaderListener() }); 48 Map initParams = new HashMap (); 49 initParams.put("contextConfigLocation", "classpath:org/apache/servicemix/http/spring-web.xml"); 50 initParams.put("contextClass", XmlWebApplicationContext.class.getName()); 51 context.setInitParams(initParams); 52 ServletHolder holder = new ServletHolder(); 53 holder.setName("jbiServlet"); 54 holder.setClassName(HttpManagedServlet.class.getName()); 55 ServletHandler handler = new ServletHandler(); 56 handler.setServlets(new ServletHolder[] { holder }); 57 ServletMapping mapping = new ServletMapping(); 58 mapping.setServletName("jbiServlet"); 59 mapping.setPathSpec("/*"); 60 handler.setServletMappings(new ServletMapping[] { mapping }); 61 context.setHandler(handler); 62 63 ContextHandlerCollection contexts = new ContextHandlerCollection(); 64 HandlerCollection handlers = new HandlerCollection(); 65 handlers.setHandlers(new Handler[] { contexts }); 66 contexts.addHandler(context); 67 68 SelectChannelConnector connector = new SelectChannelConnector(); 69 connector.setHost("localhost"); 70 connector.setPort(8080); 71 72 Server server = new Server(); 73 server.setConnectors(new Connector[] { connector }); 74 server.setHandler(handlers); 75 server.start(); 76 77 System.err.println("Started"); 78 79 PostMethod post = new PostMethod("http://localhost:8080/test/jbi/Service/"); 80 post.setRequestEntity(new StringRequestEntity("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><hello>world</hello></soap:Body></soap:Envelope>")); 81 new HttpClient().executeMethod(post); 82 if (post.getStatusCode() != 200) { 83 throw new InvalidStatusResponseException(post.getStatusCode()); 84 } 85 System.err.println(post.getResponseBodyAsString()); 86 87 } 88 } 89 | Popular Tags |