Add request interceptor
This commit is contained in:
@@ -1,94 +1,38 @@
|
||||
package httpsServer.httpServer.src;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import common.common.src.html.HtmlManager;
|
||||
import common.common.src.logger.Logger;
|
||||
import httpsServer.httpServer.src.authorization.AuthorizedClients;
|
||||
import httpsServer.httpServer.src.authorization.Client;
|
||||
import httpsServer.httpServer.src.handlers.IRequestHandler;
|
||||
import httpsServer.httpServer.src.handlers.RequestHandler;
|
||||
import httpsServer.httpServer.src.interceptors.RequestInterceptor;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
final int port = 8043;
|
||||
final AuthorizedClients authorizedClients = new AuthorizedClients();
|
||||
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
|
||||
|
||||
server.createContext("/", exchange -> {
|
||||
Logger.displayReceived("/ request");
|
||||
respondToGet(exchange, "./assets/pages/index.html");
|
||||
});
|
||||
IRequestHandler requestHandler = new RequestHandler();
|
||||
|
||||
server.createContext("/payment", exchange -> {
|
||||
Logger.displayReceived("/payment request");
|
||||
respondToGet(exchange, "./assets/pages/payment.html");
|
||||
});
|
||||
IRequestHandler proxy = (IRequestHandler) Proxy.newProxyInstance(
|
||||
requestHandler.getClass().getClassLoader(),
|
||||
new Class[]{ IRequestHandler.class },
|
||||
new RequestInterceptor(requestHandler)
|
||||
);
|
||||
|
||||
server.createContext("/login", exchange -> {
|
||||
if(isUnauthorizedVerb(exchange, "POST")){
|
||||
exchange.sendResponseHeaders(405, -1);
|
||||
return;
|
||||
}
|
||||
server.createContext("/", proxy::handleRoot);
|
||||
|
||||
InputStream is = exchange.getRequestBody();
|
||||
String body = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))
|
||||
.lines()
|
||||
.reduce("", (acc, line) -> acc + line + "\n");
|
||||
server.createContext("/payment", proxy::handlePayment);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Client client = mapper.readValue(body, Client.class);
|
||||
|
||||
System.out.println("Received POST data:\n" + body);
|
||||
|
||||
if(authorizedClients.isAuthorized(client)){
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
} else {
|
||||
exchange.sendResponseHeaders(401, 0);
|
||||
}
|
||||
|
||||
exchange.getResponseBody().close();
|
||||
});
|
||||
server.createContext("/login", proxy::handleLogin);
|
||||
|
||||
server.start();
|
||||
Logger.displayInfo("Server started on port " + port);
|
||||
}
|
||||
|
||||
private static void respondToGet(HttpExchange exchange, String pagePath) throws IOException {
|
||||
if(isUnauthorizedVerb(exchange, "GET")){
|
||||
exchange.sendResponseHeaders(405, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
try{
|
||||
final HtmlManager htmlManager = new HtmlManager();
|
||||
String content = htmlManager.serveFile(pagePath);
|
||||
byte[] data = content.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
|
||||
exchange.sendResponseHeaders(200, data.length);
|
||||
send(exchange, data);
|
||||
} catch (IOException ioe) {
|
||||
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
|
||||
exchange.sendResponseHeaders(404, 0);
|
||||
}
|
||||
finally {
|
||||
exchange.getResponseBody().close();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isUnauthorizedVerb(HttpExchange exchange, String verb) throws IOException {
|
||||
return !verb.equalsIgnoreCase(exchange.getRequestMethod());
|
||||
}
|
||||
|
||||
private static void send(HttpExchange exchange, byte[] data) throws IOException {
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
os.write(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package httpsServer.httpServer.src.annotations;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface Intercept {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package httpsServer.httpServer.src.handlers;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
public interface IRequestHandler {
|
||||
void handleRoot(HttpExchange exchange);
|
||||
void handlePayment(HttpExchange exchange);
|
||||
void handleLogin(HttpExchange exchange);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package httpsServer.httpServer.src.handlers;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import common.common.src.html.HtmlManager;
|
||||
import common.common.src.logger.Logger;
|
||||
import httpsServer.httpServer.src.annotations.Intercept;
|
||||
import httpsServer.httpServer.src.authorization.AuthorizedClients;
|
||||
import httpsServer.httpServer.src.authorization.Client;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class RequestHandler implements IRequestHandler {
|
||||
|
||||
final AuthorizedClients authorizedClients = new AuthorizedClients();
|
||||
|
||||
@Intercept
|
||||
public void handleRoot(HttpExchange exchange) {
|
||||
Logger.displayReceived("/ request");
|
||||
try{
|
||||
respondToGet(exchange, "./assets/pages/index.html");
|
||||
} catch(Exception e){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Intercept
|
||||
public void handlePayment(HttpExchange exchange) {
|
||||
Logger.displayReceived("/payment request");
|
||||
try{
|
||||
respondToGet(exchange, "./assets/pages/payment.html");
|
||||
} catch(Exception e){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Intercept
|
||||
public void handleLogin(HttpExchange exchange) {
|
||||
try {
|
||||
if (isUnauthorizedVerb(exchange, "POST")) {
|
||||
exchange.sendResponseHeaders(405, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
InputStream is = exchange.getRequestBody();
|
||||
String body = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))
|
||||
.lines()
|
||||
.reduce("", (acc, line) -> acc + line + "\n");
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Client client = mapper.readValue(body, Client.class);
|
||||
|
||||
System.out.println("Received POST data:\n" + body);
|
||||
|
||||
if (authorizedClients.isAuthorized(client)) {
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
} else {
|
||||
exchange.sendResponseHeaders(401, 0);
|
||||
}
|
||||
|
||||
exchange.getResponseBody().close();
|
||||
} catch (Exception e){
|
||||
|
||||
}
|
||||
}
|
||||
private void respondToGet(HttpExchange exchange, String pagePath) throws IOException {
|
||||
if(isUnauthorizedVerb(exchange, "GET")){
|
||||
exchange.sendResponseHeaders(405, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
try{
|
||||
final HtmlManager htmlManager = new HtmlManager();
|
||||
String content = htmlManager.serveFile(pagePath);
|
||||
byte[] data = content.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
|
||||
exchange.sendResponseHeaders(200, data.length);
|
||||
send(exchange, data);
|
||||
} catch (IOException ioe) {
|
||||
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
|
||||
exchange.sendResponseHeaders(404, 0);
|
||||
}
|
||||
finally {
|
||||
exchange.getResponseBody().close();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isUnauthorizedVerb(HttpExchange exchange, String verb) throws IOException {
|
||||
return !verb.equalsIgnoreCase(exchange.getRequestMethod());
|
||||
}
|
||||
|
||||
private void send(HttpExchange exchange, byte[] data) throws IOException {
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
os.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package httpsServer.httpServer.src.interceptors;
|
||||
|
||||
import httpsServer.httpServer.src.annotations.Intercept;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
|
||||
public class RequestInterceptor implements InvocationHandler {
|
||||
|
||||
private final Object target;
|
||||
|
||||
public RequestInterceptor(Object target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
|
||||
Method realMethod = target.getClass().getMethod(method.getName(), method.getParameterTypes());
|
||||
if (realMethod.isAnnotationPresent(Intercept.class)) {
|
||||
System.out.println(">>> Intercepted call to " + method.getName());
|
||||
}
|
||||
|
||||
return method.invoke(target, args); // call original method
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user