Add request interceptor
This commit is contained in:
@@ -1,94 +1,38 @@
|
|||||||
package httpsServer.httpServer.src;
|
package httpsServer.httpServer.src;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
|
||||||
import com.sun.net.httpserver.HttpServer;
|
import com.sun.net.httpserver.HttpServer;
|
||||||
import common.common.src.html.HtmlManager;
|
|
||||||
import common.common.src.logger.Logger;
|
import common.common.src.logger.Logger;
|
||||||
import httpsServer.httpServer.src.authorization.AuthorizedClients;
|
import httpsServer.httpServer.src.handlers.IRequestHandler;
|
||||||
import httpsServer.httpServer.src.authorization.Client;
|
import httpsServer.httpServer.src.handlers.RequestHandler;
|
||||||
|
import httpsServer.httpServer.src.interceptors.RequestInterceptor;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
final int port = 8043;
|
final int port = 8043;
|
||||||
final AuthorizedClients authorizedClients = new AuthorizedClients();
|
|
||||||
|
|
||||||
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
|
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
|
||||||
|
|
||||||
server.createContext("/", exchange -> {
|
IRequestHandler requestHandler = new RequestHandler();
|
||||||
Logger.displayReceived("/ request");
|
|
||||||
respondToGet(exchange, "./assets/pages/index.html");
|
|
||||||
});
|
|
||||||
|
|
||||||
server.createContext("/payment", exchange -> {
|
IRequestHandler proxy = (IRequestHandler) Proxy.newProxyInstance(
|
||||||
Logger.displayReceived("/payment request");
|
requestHandler.getClass().getClassLoader(),
|
||||||
respondToGet(exchange, "./assets/pages/payment.html");
|
new Class[]{ IRequestHandler.class },
|
||||||
});
|
new RequestInterceptor(requestHandler)
|
||||||
|
);
|
||||||
|
|
||||||
server.createContext("/login", exchange -> {
|
server.createContext("/", proxy::handleRoot);
|
||||||
if(isUnauthorizedVerb(exchange, "POST")){
|
|
||||||
exchange.sendResponseHeaders(405, -1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
InputStream is = exchange.getRequestBody();
|
server.createContext("/payment", proxy::handlePayment);
|
||||||
String body = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))
|
|
||||||
.lines()
|
|
||||||
.reduce("", (acc, line) -> acc + line + "\n");
|
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
server.createContext("/login", proxy::handleLogin);
|
||||||
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.start();
|
server.start();
|
||||||
Logger.displayInfo("Server started on port " + port);
|
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