Implement payment page

This commit is contained in:
Laurent
2025-12-04 08:48:09 +01:00
parent c99d8068f9
commit dd7a63c22f
5 changed files with 82 additions and 32 deletions

View File

@@ -1,17 +1,14 @@
package common.common.src.html;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
public class HtmlManager {
public String serveFile(String path){
public String serveFile(String path) throws IOException {
return readFile(path);
}
private String readFile(String path){
private String readFile(String path) throws IOException {
File file = new File(path);
try(BufferedReader fileReader = new BufferedReader(new FileReader(file))){
@@ -21,8 +18,6 @@ public class HtmlManager {
responseBody.append(line);
}
return responseBody.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -16,23 +16,18 @@ public class Main {
public static void main(String[] args) throws IOException {
final int port = 8043;
final HtmlManager htmlManager = new HtmlManager();
final AuthorizedClients authorizedClients = new AuthorizedClients();
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
server.createContext("/", exchange -> {
if(isUnauthorizedVerb(exchange, "GET")){
exchange.sendResponseHeaders(405, -1);
return;
}
Logger.displayReceived("/ request");
respondToGet(exchange, "./assets/pages/index.html");
});
String content = htmlManager.serveFile("./assets/pages/index.html");
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);
server.createContext("/payment", exchange -> {
Logger.displayReceived("/payment request");
respondToGet(exchange, "./assets/pages/payment.html");
});
server.createContext("/login", exchange -> {
@@ -58,12 +53,37 @@ public class Main {
exchange.sendResponseHeaders(200, response.getBytes().length);
send(exchange, response.getBytes());
exchange.getResponseBody().close();
});
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) {
ioe.printStackTrace();
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());
}

View File

@@ -18,8 +18,8 @@ public class AuthorizedClients {
registerClient("Aude Vaiselle", "password1");
registerClient("Tony Truand", "password2");
registerClient("Jean Porte", "password3");
registerClient("Ruby Niole", "password4");
registerClient("Nat Assion", "password5");
registerClient("Ruby Gnaule", "password4");
registerClient("Nat Action", "password5");
registerClient("hepl", "hepl");
}