Return to Snippet

Revision: 22057
at December 30, 2009 09:43 by narkisr


Initial Code
import org.simpleframework.http.Request
import org.simpleframework.http.Response
import org.simpleframework.http.core.Container
import org.simpleframework.transport.connect.Connection
import org.simpleframework.transport.connect.SocketConnection

class HttpContainer implements Container {

	Connection connection

	def setup(){
		connection = new SocketConnection(this);
		SocketAddress address = new InetSocketAddress(13080);
		connection.connect(address);
		this
	}

	public void handle(Request request, Response response) {
		PrintStream body = response.getPrintStream();
		long time = System.currentTimeMillis();
		response.set("Content-Type", "text/xml;charset=utf-8");
		response.set("Server", "Apache-Coyote/1.1");
		response.setDate("Date", time);

		def text = simpleRoute(request)
		body.println(text);
		body.close();
	}

	def simpleRoute(request){
		switch(request.target){
			case "/some/url/file":
			return new File('src/test/resources/file').text

			case "/some/url/hello":
			return 'hello'

			default: throw new RuntimeException("no route found")
		}
	}

	def close(){
		connection.close()
	}
}

Initial URL

                                

Initial Description
This is excellent for testing (mocking a SOAP server for example), we use an in memory http server in order to answer client requests.

Initial Title
Starting an in memory http server with custom routes

Initial Tags
http, server, simple

Initial Language
Groovy