added REST features not needed for the project
~0.75h work
This commit is contained in:
parent
a72c8f1caf
commit
28eb5cf44d
@ -8,33 +8,33 @@ import java.io.IOException;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
Router router = new Router();
|
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 */
|
/* users */
|
||||||
router.addRoute(HttpMethod.POST, "/users", new TestService(), 0);
|
router.addRoute(HttpMethod.POST, "/users", new TestService(), new int[]{});
|
||||||
router.addRoute(HttpMethod.GET, "/users/{username}", new TestService(), 2);
|
router.addRoute(HttpMethod.GET, "/users/{username}", new TestService(), new int[]{2});
|
||||||
router.addRoute(HttpMethod.PUT, "/users/{username}", new TestService(), 2);
|
router.addRoute(HttpMethod.PUT, "/users/{username}", new TestService(), new int[]{2});
|
||||||
router.addRoute(HttpMethod.POST, "/sessions", new TestService(), 0);
|
router.addRoute(HttpMethod.POST, "/sessions", new TestService(), new int[]{});
|
||||||
|
|
||||||
/* packages */
|
/* packages */
|
||||||
router.addRoute(HttpMethod.POST, "/packages", new TestService(), 0);
|
router.addRoute(HttpMethod.POST, "/packages", new TestService(), new int[]{});
|
||||||
router.addRoute(HttpMethod.POST, "/transaction/packages", new TestService(), 0);
|
router.addRoute(HttpMethod.POST, "/transaction/packages", new TestService(), new int[]{});
|
||||||
|
|
||||||
/* cards */
|
/* cards */
|
||||||
router.addRoute(HttpMethod.GET, "/cards", new TestService(), 0);
|
router.addRoute(HttpMethod.GET, "/cards", new TestService(), new int[]{});
|
||||||
router.addRoute(HttpMethod.GET, "/deck", new TestService(), 0);
|
router.addRoute(HttpMethod.GET, "/deck", new TestService(), new int[]{});
|
||||||
router.addRoute(HttpMethod.PUT, "/deck", new TestService(), 0);
|
router.addRoute(HttpMethod.PUT, "/deck", new TestService(), new int[]{});
|
||||||
|
|
||||||
/* game */
|
/* game */
|
||||||
router.addRoute(HttpMethod.GET, "/stats", new TestService(), 0);
|
router.addRoute(HttpMethod.GET, "/stats", new TestService(), new int[]{});
|
||||||
router.addRoute(HttpMethod.GET, "/scoreboard", new TestService(), 0);
|
router.addRoute(HttpMethod.GET, "/scoreboard", new TestService(), new int[]{});
|
||||||
router.addRoute(HttpMethod.POST, "/battles", new TestService(), 0);
|
router.addRoute(HttpMethod.POST, "/battles", new TestService(), new int[]{});
|
||||||
|
|
||||||
/* trading */
|
/* trading */
|
||||||
router.addRoute(HttpMethod.GET, "/tradings", new TestService(), 0);
|
router.addRoute(HttpMethod.GET, "/tradings", new TestService(), new int[]{});
|
||||||
router.addRoute(HttpMethod.POST, "/tradings", new TestService(), 0);
|
router.addRoute(HttpMethod.POST, "/tradings", new TestService(), new int[]{});
|
||||||
router.addRoute(HttpMethod.DELETE, "/tradings/{tradingdealid}", new TestService(), 2);
|
router.addRoute(HttpMethod.DELETE, "/tradings/{tradingdealid}", new TestService(), new int[]{2});
|
||||||
router.addRoute(HttpMethod.POST, "/tradings/{tradingdealid}", new TestService(), 2);
|
router.addRoute(HttpMethod.POST, "/tradings/{tradingdealid}", new TestService(), new int[]{2});
|
||||||
|
|
||||||
Server server = new Server(10001, 10, router);
|
Server server = new Server(10001, 10, router);
|
||||||
server.start();
|
server.start();
|
||||||
|
|||||||
@ -1,18 +1,13 @@
|
|||||||
package at.nanopenguin.mtcg.application;
|
package at.nanopenguin.mtcg.application;
|
||||||
|
|
||||||
import at.nanopenguin.mtcg.http.HttpStatus;
|
import at.nanopenguin.mtcg.http.HttpStatus;
|
||||||
|
import at.nanopenguin.mtcg.http.HttpRequest;
|
||||||
import at.nanopenguin.mtcg.http.Response;
|
import at.nanopenguin.mtcg.http.Response;
|
||||||
|
|
||||||
public class InternalErrorService implements Service {
|
public class InternalErrorService implements Service {
|
||||||
private String pathVariable = null;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPathVariable(String var) {
|
public Response handleRequest(HttpRequest request) {
|
||||||
this.pathVariable = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Response handleRequest(String request) {
|
|
||||||
return new Response(HttpStatus.INTERNAL, "application/json", "");
|
return new Response(HttpStatus.INTERNAL, "application/json", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,9 @@
|
|||||||
package at.nanopenguin.mtcg.application;
|
package at.nanopenguin.mtcg.application;
|
||||||
|
|
||||||
|
import at.nanopenguin.mtcg.http.HttpRequest;
|
||||||
import at.nanopenguin.mtcg.http.Response;
|
import at.nanopenguin.mtcg.http.Response;
|
||||||
|
|
||||||
public interface Service {
|
public interface Service {
|
||||||
String pathVariable = null;
|
|
||||||
|
|
||||||
void setPathVariable(String var);
|
Response handleRequest(HttpRequest request);
|
||||||
|
|
||||||
Response handleRequest(String request);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,18 +1,13 @@
|
|||||||
package at.nanopenguin.mtcg.application;
|
package at.nanopenguin.mtcg.application;
|
||||||
|
|
||||||
|
import at.nanopenguin.mtcg.http.HttpRequest;
|
||||||
import at.nanopenguin.mtcg.http.HttpStatus;
|
import at.nanopenguin.mtcg.http.HttpStatus;
|
||||||
import at.nanopenguin.mtcg.http.Response;
|
import at.nanopenguin.mtcg.http.Response;
|
||||||
|
|
||||||
public class TestService implements Service {
|
public class TestService implements Service {
|
||||||
private String pathVariable = null;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPathVariable(String var) {
|
public Response handleRequest(HttpRequest request) {
|
||||||
this.pathVariable = var;
|
return new Response(HttpStatus.OK, "application/json", "");
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Response handleRequest(String request) {
|
|
||||||
return new Response(HttpStatus.OK, "application/json", "{\"var\":\"" + this.pathVariable + "\"}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,7 +40,7 @@ public class RequestHandler implements Runnable {
|
|||||||
break responseBuilder;
|
break responseBuilder;
|
||||||
}
|
}
|
||||||
System.out.println("creating response");
|
System.out.println("creating response");
|
||||||
response = service.handleRequest(httpRequest.getBody());
|
response = service.handleRequest(httpRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputStream out = this.serviceSocket.getOutputStream();
|
OutputStream out = this.serviceSocket.getOutputStream();
|
||||||
|
|||||||
@ -4,25 +4,27 @@ import at.nanopenguin.mtcg.application.InternalErrorService;
|
|||||||
import at.nanopenguin.mtcg.application.Service;
|
import at.nanopenguin.mtcg.application.Service;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class Router {
|
public class Router {
|
||||||
private final Map<HttpMethod, Map<String, Route>> routeMap = new HashMap<>();
|
private final Map<HttpMethod, Map<String, Route>> 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<String, Route> map = this.routeMap.get(method);
|
Map<String, Route> map = this.routeMap.get(method);
|
||||||
if (method != null && map == null) {
|
if (method != null && map == null) {
|
||||||
this.routeMap.put(method, (map = new HashMap<>()));
|
this.routeMap.put(method, (map = new HashMap<>()));
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> routeComponents = new ArrayList<>(Arrays.asList(route.split("/")));
|
List<String> routeComponents = new ArrayList<>(Arrays.asList(route.split("/")));
|
||||||
if (pathVarPos > 0) {
|
for ( Integer pathVarPos : pathVars) {
|
||||||
routeComponents.set(pathVarPos, "{var}");
|
routeComponents.set(pathVarPos, "{var}");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < routeComponents.size(); i++) {
|
for (int i = 0; i < routeComponents.size(); i++) {
|
||||||
String path = String.join("/", routeComponents.subList(0, i+1));
|
String path = String.join("/", routeComponents.subList(0, i+1));
|
||||||
Route existingRoute = map.get(path);
|
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);
|
map.put(path, routeComponent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,22 +40,17 @@ public class Router {
|
|||||||
|
|
||||||
Route component = this.routeMap.get(method).get("/" + routeComponents[i]);
|
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) {
|
if (component.hasPathVariable() && routeComponents.length - 1 > i) {
|
||||||
pathVariable = routeComponents[++i];
|
pathVariable = routeComponents[++i];
|
||||||
search = String.join("/", search, "{var}");
|
search = String.join("/", search, "{var}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (component == null) {
|
if (component == null || component.service() == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (component.service() == null) {
|
|
||||||
return new InternalErrorService();
|
|
||||||
}
|
|
||||||
|
|
||||||
component.service().setPathVariable(pathVariable);
|
|
||||||
return component.service();
|
return component.service();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user