diff --git a/src/Main.java b/src/Main.java index d88b560..52f1bac 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,33 +8,33 @@ import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { Router router = new Router(); - router.addRoute(HttpMethod.GET, "/test/{var}/service", new TestService(), 2); + router.addRoute(HttpMethod.GET, "/test/{var}/service", new TestService(), new int[]{2}); /* users */ - router.addRoute(HttpMethod.POST, "/users", new TestService(), 0); - router.addRoute(HttpMethod.GET, "/users/{username}", new TestService(), 2); - router.addRoute(HttpMethod.PUT, "/users/{username}", new TestService(), 2); - router.addRoute(HttpMethod.POST, "/sessions", new TestService(), 0); + router.addRoute(HttpMethod.POST, "/users", new TestService(), new int[]{}); + router.addRoute(HttpMethod.GET, "/users/{username}", new TestService(), new int[]{2}); + router.addRoute(HttpMethod.PUT, "/users/{username}", new TestService(), new int[]{2}); + router.addRoute(HttpMethod.POST, "/sessions", new TestService(), new int[]{}); /* packages */ - router.addRoute(HttpMethod.POST, "/packages", new TestService(), 0); - router.addRoute(HttpMethod.POST, "/transaction/packages", new TestService(), 0); + router.addRoute(HttpMethod.POST, "/packages", new TestService(), new int[]{}); + router.addRoute(HttpMethod.POST, "/transaction/packages", new TestService(), new int[]{}); /* cards */ - router.addRoute(HttpMethod.GET, "/cards", new TestService(), 0); - router.addRoute(HttpMethod.GET, "/deck", new TestService(), 0); - router.addRoute(HttpMethod.PUT, "/deck", new TestService(), 0); + router.addRoute(HttpMethod.GET, "/cards", new TestService(), new int[]{}); + router.addRoute(HttpMethod.GET, "/deck", new TestService(), new int[]{}); + router.addRoute(HttpMethod.PUT, "/deck", new TestService(), new int[]{}); /* game */ - router.addRoute(HttpMethod.GET, "/stats", new TestService(), 0); - router.addRoute(HttpMethod.GET, "/scoreboard", new TestService(), 0); - router.addRoute(HttpMethod.POST, "/battles", new TestService(), 0); + router.addRoute(HttpMethod.GET, "/stats", new TestService(), new int[]{}); + router.addRoute(HttpMethod.GET, "/scoreboard", new TestService(), new int[]{}); + router.addRoute(HttpMethod.POST, "/battles", new TestService(), new int[]{}); /* trading */ - router.addRoute(HttpMethod.GET, "/tradings", new TestService(), 0); - router.addRoute(HttpMethod.POST, "/tradings", new TestService(), 0); - router.addRoute(HttpMethod.DELETE, "/tradings/{tradingdealid}", new TestService(), 2); - router.addRoute(HttpMethod.POST, "/tradings/{tradingdealid}", new TestService(), 2); + router.addRoute(HttpMethod.GET, "/tradings", new TestService(), new int[]{}); + router.addRoute(HttpMethod.POST, "/tradings", new TestService(), new int[]{}); + router.addRoute(HttpMethod.DELETE, "/tradings/{tradingdealid}", new TestService(), new int[]{2}); + router.addRoute(HttpMethod.POST, "/tradings/{tradingdealid}", new TestService(), new int[]{2}); Server server = new Server(10001, 10, router); server.start(); diff --git a/src/at/nanopenguin/mtcg/application/InternalErrorService.java b/src/at/nanopenguin/mtcg/application/InternalErrorService.java index d9a72c0..9d55ee9 100644 --- a/src/at/nanopenguin/mtcg/application/InternalErrorService.java +++ b/src/at/nanopenguin/mtcg/application/InternalErrorService.java @@ -1,18 +1,13 @@ package at.nanopenguin.mtcg.application; import at.nanopenguin.mtcg.http.HttpStatus; +import at.nanopenguin.mtcg.http.HttpRequest; import at.nanopenguin.mtcg.http.Response; public class InternalErrorService implements Service { - private String pathVariable = null; @Override - public void setPathVariable(String var) { - this.pathVariable = null; - } - - @Override - public Response handleRequest(String request) { + public Response handleRequest(HttpRequest request) { return new Response(HttpStatus.INTERNAL, "application/json", ""); } } diff --git a/src/at/nanopenguin/mtcg/application/Service.java b/src/at/nanopenguin/mtcg/application/Service.java index f2c9d96..41485a5 100644 --- a/src/at/nanopenguin/mtcg/application/Service.java +++ b/src/at/nanopenguin/mtcg/application/Service.java @@ -1,11 +1,9 @@ package at.nanopenguin.mtcg.application; +import at.nanopenguin.mtcg.http.HttpRequest; import at.nanopenguin.mtcg.http.Response; public interface Service { - String pathVariable = null; - void setPathVariable(String var); - - Response handleRequest(String request); + Response handleRequest(HttpRequest request); } diff --git a/src/at/nanopenguin/mtcg/application/TestService.java b/src/at/nanopenguin/mtcg/application/TestService.java index 1f7686b..46ccb79 100644 --- a/src/at/nanopenguin/mtcg/application/TestService.java +++ b/src/at/nanopenguin/mtcg/application/TestService.java @@ -1,18 +1,13 @@ package at.nanopenguin.mtcg.application; +import at.nanopenguin.mtcg.http.HttpRequest; import at.nanopenguin.mtcg.http.HttpStatus; import at.nanopenguin.mtcg.http.Response; public class TestService implements Service { - private String pathVariable = null; @Override - public void setPathVariable(String var) { - this.pathVariable = var; - } - - @Override - public Response handleRequest(String request) { - return new Response(HttpStatus.OK, "application/json", "{\"var\":\"" + this.pathVariable + "\"}"); + public Response handleRequest(HttpRequest request) { + return new Response(HttpStatus.OK, "application/json", ""); } } diff --git a/src/at/nanopenguin/mtcg/http/RequestHandler.java b/src/at/nanopenguin/mtcg/http/RequestHandler.java index 0b32314..8483775 100644 --- a/src/at/nanopenguin/mtcg/http/RequestHandler.java +++ b/src/at/nanopenguin/mtcg/http/RequestHandler.java @@ -40,7 +40,7 @@ public class RequestHandler implements Runnable { break responseBuilder; } System.out.println("creating response"); - response = service.handleRequest(httpRequest.getBody()); + response = service.handleRequest(httpRequest); } OutputStream out = this.serviceSocket.getOutputStream(); diff --git a/src/at/nanopenguin/mtcg/http/Router.java b/src/at/nanopenguin/mtcg/http/Router.java index cccefae..3496843 100644 --- a/src/at/nanopenguin/mtcg/http/Router.java +++ b/src/at/nanopenguin/mtcg/http/Router.java @@ -4,25 +4,27 @@ import at.nanopenguin.mtcg.application.InternalErrorService; import at.nanopenguin.mtcg.application.Service; import java.util.*; +import java.util.stream.IntStream; public class Router { private final Map> routeMap = new HashMap<>(); - public void addRoute(final HttpMethod method, final String route, final Service service, final int pathVarPos) { + public void addRoute(final HttpMethod method, final String route, final Service service, final int[] pathVars) { Map map = this.routeMap.get(method); if (method != null && map == null) { this.routeMap.put(method, (map = new HashMap<>())); } List routeComponents = new ArrayList<>(Arrays.asList(route.split("/"))); - if (pathVarPos > 0) { + for ( Integer pathVarPos : pathVars) { routeComponents.set(pathVarPos, "{var}"); } for (int i = 0; i < routeComponents.size(); i++) { String path = String.join("/", routeComponents.subList(0, i+1)); Route existingRoute = map.get(path); - Route routeComponent = new Route(i == routeComponents.size() - 1 ? service : existingRoute != null ? existingRoute.service() : null, i == pathVarPos - 1 || (existingRoute != null && existingRoute.hasPathVariable())); + int finalI = i; + Route routeComponent = new Route(i == routeComponents.size() - 1 ? service : existingRoute != null ? existingRoute.service() : null, IntStream.of(pathVars).anyMatch(x -> x == (finalI + 1)) || (existingRoute != null && existingRoute.hasPathVariable())); map.put(path, routeComponent); } @@ -38,22 +40,17 @@ public class Router { Route component = this.routeMap.get(method).get("/" + routeComponents[i]); - for (String search = "/" + routeComponents[i]; component != null && (component.service() == null || routeComponents.length - 1 > i); component = this.routeMap.get(method).get(search = routeComponents.length - 1 > i ? String.join("/", search, routeComponents[++i]) : search)) { + for (String search = "/" + routeComponents[i]; component != null && (component.service() == null || routeComponents.length - 1 > i) && routeComponents.length - 1 >= i; component = this.routeMap.get(method).get(search = routeComponents.length - 1 > i++ ? String.join("/", search, routeComponents[i]) : search)) { if (component.hasPathVariable() && routeComponents.length - 1 > i) { pathVariable = routeComponents[++i]; search = String.join("/", search, "{var}"); } } - if (component == null) { + if (component == null || component.service() == null) { return null; } - if (component.service() == null) { - return new InternalErrorService(); - } - - component.service().setPathVariable(pathVariable); return component.service(); }