status/method enums, middleware base grounds
This commit is contained in:
13
src/main/java/org/lumijiez/core/http/HttpMethod.java
Normal file
13
src/main/java/org/lumijiez/core/http/HttpMethod.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package org.lumijiez.core.http;
|
||||
|
||||
public enum HttpMethod {
|
||||
GET, POST, PUT, DELETE, HEAD, OPTIONS;
|
||||
|
||||
public static HttpMethod fromString(String method) {
|
||||
try {
|
||||
return valueOf(method.toUpperCase());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new UnsupportedOperationException("Unsupported HTTP method: " + method);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,6 +131,7 @@ public class HttpServer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
running = false;
|
||||
if (serverSocket != null) {
|
||||
|
||||
22
src/main/java/org/lumijiez/core/http/HttpStatus.java
Normal file
22
src/main/java/org/lumijiez/core/http/HttpStatus.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package org.lumijiez.core.http;
|
||||
|
||||
public enum HttpStatus {
|
||||
OK(200, "OK"),
|
||||
CREATED(201, "Created"),
|
||||
NO_CONTENT(204, "No Content"),
|
||||
BAD_REQUEST(400, "Bad Request"),
|
||||
NOT_FOUND(404, "Not Found"),
|
||||
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
|
||||
INTERNAL_SERVER_ERROR(500, "Internal Server Error");
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
HttpStatus(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getCode() { return code; }
|
||||
public String getMessage() { return message; }
|
||||
}
|
||||
10
src/main/java/org/lumijiez/core/middleware/Chain.java
Normal file
10
src/main/java/org/lumijiez/core/middleware/Chain.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package org.lumijiez.core.middleware;
|
||||
|
||||
import org.lumijiez.core.http.HttpRequest;
|
||||
import org.lumijiez.core.http.HttpResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Chain {
|
||||
void next(HttpRequest request, HttpResponse response) throws IOException;
|
||||
}
|
||||
11
src/main/java/org/lumijiez/core/middleware/Middleware.java
Normal file
11
src/main/java/org/lumijiez/core/middleware/Middleware.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package org.lumijiez.core.middleware;
|
||||
|
||||
import com.sun.net.httpserver.Filter;
|
||||
import org.lumijiez.core.http.HttpRequest;
|
||||
import org.lumijiez.core.http.HttpResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Middleware {
|
||||
void process(HttpRequest request, HttpResponse response, Chain chain) throws IOException;
|
||||
}
|
||||
15
src/main/java/org/lumijiez/core/routing/Route.java
Normal file
15
src/main/java/org/lumijiez/core/routing/Route.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package org.lumijiez.core.routing;
|
||||
|
||||
public class Route {
|
||||
private final HttpMethod method;
|
||||
private final String path;
|
||||
private final HttpHandler handler;
|
||||
|
||||
public Route(HttpMethod method, String path, HttpHandler handler) {
|
||||
this.method = method;
|
||||
this.path = path;
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
// Add getters...
|
||||
}
|
||||
@@ -3,27 +3,56 @@ package org.lumijiez.core.routing;
|
||||
import org.lumijiez.core.http.HttpHandler;
|
||||
import org.lumijiez.core.http.HttpRequest;
|
||||
import org.lumijiez.core.http.HttpResponse;
|
||||
import org.lumijiez.core.middleware.Chain;
|
||||
import org.lumijiez.core.middleware.Middleware;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Router {
|
||||
private final Map<String, HttpHandler> routes = new HashMap<>();
|
||||
private final Map<String, Route> routes = new HashMap<>();
|
||||
private final List<Middleware> middleware = new ArrayList<>();
|
||||
|
||||
public void addRoute(String method, String path, HttpHandler handler) {
|
||||
String key = method.toUpperCase() + ":" + path;
|
||||
routes.put(key, handler);
|
||||
public void addMiddleware(Middleware middleware) {
|
||||
this.middleware.add(middleware);
|
||||
}
|
||||
|
||||
public void addRoute(HttpMethod method, String path, HttpHandler handler) {
|
||||
String key = method.name() + ":" + path;
|
||||
routes.put(key, new Route(method, path, handler));
|
||||
}
|
||||
|
||||
public void handleRequest(HttpRequest request, HttpResponse response) throws IOException {
|
||||
String key = request.getMethod().toUpperCase() + ":" + request.getPath();
|
||||
HttpHandler handler = routes.get(key);
|
||||
// Create middleware chain
|
||||
Chain chain = new Chain() {
|
||||
private int index = 0;
|
||||
|
||||
if (handler != null) {
|
||||
handler.handle(request, response);
|
||||
@Override
|
||||
public void next(HttpRequest request, HttpResponse response) throws IOException {
|
||||
if (index < middleware.size()) {
|
||||
middleware.get(index++).process(request, response, this);
|
||||
} else {
|
||||
response.sendResponse(404, "Not Found");
|
||||
executeHandler(request, response);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Start middleware chain
|
||||
chain.next(request, response);
|
||||
}
|
||||
|
||||
private void executeHandler(HttpRequest request, HttpResponse response) throws IOException {
|
||||
String key = request.getMethod() + ":" + request.getPath();
|
||||
Route route = routes.get(key);
|
||||
|
||||
if (route != null) {
|
||||
route.getHandler().handle(request, response);
|
||||
} else {
|
||||
response.sendResponse(HttpStatus.NOT_FOUND, "Not Found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user