diff options
Diffstat (limited to 'same/src/main/java')
4 files changed, 68 insertions, 3 deletions
diff --git a/same/src/main/java/com/orbekk/same/TjwsApp.java b/same/src/main/java/com/orbekk/same/TjwsApp.java index 0a0c30a..b06322b 100644 --- a/same/src/main/java/com/orbekk/same/TjwsApp.java +++ b/same/src/main/java/com/orbekk/same/TjwsApp.java @@ -1,10 +1,14 @@ package com.orbekk.same; +import com.orbekk.same.http.HelloServlet; +import com.orbekk.same.http.TjwsServerBuilder; import com.orbekk.same.http.TjwsServerContainer; public class TjwsApp { public static void main(String[] args) { - TjwsServerContainer server = TjwsServerContainer.create(8080); + TjwsServerContainer server = new TjwsServerBuilder(8080) + .withServlet(new HelloServlet(), "/hello") + .build(); server.start(); server.join(); } diff --git a/same/src/main/java/com/orbekk/same/http/HelloServlet.java b/same/src/main/java/com/orbekk/same/http/HelloServlet.java new file mode 100644 index 0000000..78ce6b1 --- /dev/null +++ b/same/src/main/java/com/orbekk/same/http/HelloServlet.java @@ -0,0 +1,15 @@ +package com.orbekk.same.http; + +import java.io.IOException; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class HelloServlet extends HttpServlet { + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws IOException { + response.setContentType("text/plain; charset=utf8"); + response.getWriter().println("Hello, World"); + } +} diff --git a/same/src/main/java/com/orbekk/same/http/TjwsServerBuilder.java b/same/src/main/java/com/orbekk/same/http/TjwsServerBuilder.java new file mode 100644 index 0000000..c0aed5a --- /dev/null +++ b/same/src/main/java/com/orbekk/same/http/TjwsServerBuilder.java @@ -0,0 +1,41 @@ +package com.orbekk.same.http; + +import java.util.ArrayList; + +import javax.servlet.http.HttpServlet; + +import com.googlecode.jsonrpc4j.JsonRpcServer; + +public class TjwsServerBuilder { + private int port; + private ArrayList<String> servletPaths = new ArrayList<String>(); + private ArrayList<HttpServlet> servlets = new ArrayList<HttpServlet>(); + + public TjwsServerBuilder(int port) { + this.port = port; + } + + /** Note: Does not preserve order. */ + public TjwsServerBuilder withServlet(HttpServlet servlet, + String pathSpec) { + servletPaths.add(pathSpec); + servlets.add(servlet); + return this; + } + + public <T> TjwsServerBuilder withService(T service, Class<T> clazz) { + JsonRpcServer server = new JsonRpcServer(service, clazz); + String pathSpec = "/" + clazz.getSimpleName() + ".json"; + return withServlet(new RpcServlet(server), pathSpec); + } + + public TjwsServerContainer build() { + TjwsServerContainer server = TjwsServerContainer.create(port); + for (int i = 0; i < servletPaths.size(); i++) { + String path = servletPaths.get(i); + HttpServlet servlet = servlets.get(i); + server.addServlet(path, servlet); + } + return server; + } +} diff --git a/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java b/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java index b71967c..a38cfac 100644 --- a/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java +++ b/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java @@ -1,9 +1,10 @@ package com.orbekk.same.http; -import java.util.Properties; +import javax.servlet.http.HttpServlet; -import org.slf4j.LoggerFactory; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import Acme.Serve.Serve; public class TjwsServerContainer { @@ -45,4 +46,8 @@ public class TjwsServerContainer { public void join() { server.join(); } + + public void addServlet(String pathSpec, HttpServlet servlet) { + server.addServlet(pathSpec, servlet); + } } |