So I couldn't figure it out on my own but thanks to initial response here and then feeding the code to chat GPT it gave some results although it looks quite different now but now it actually works both ways reading from json and html file respectively.
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HttpServer implements Runnable {
private final int port;
// private final ExecutorService executor = Executors.newFixedThreadPool(5);
public HttpServer(final int port) {
this.port = port;
}
public void run() {
try (var serverSocket = new ServerSocket(port)) {
System.out.println("Server started on port " + port);
/* while (true) {
var socket = serverSocket.accept();
executor.submit(() -> handleClient(socket));
}*/
var socket = serverSocket.accept();
handleClient(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleClient(Socket socket) {
try (socket;
var inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
var outputStream = new DataOutputStream(socket.getOutputStream())) {
String line;
int contentLength = 0;
while (!(line = inputStream.readLine()).isBlank()) {
System.out.println("Header: " + line);
if (line.toLowerCase().startsWith("content-length:")) {
contentLength = Integer.parseInt(line.split(":")[1].trim());
}
}
if (contentLength > 0) {
char[] buffer = new char[contentLength];
inputStream.read(buffer, 0, contentLength);
System.out.println("Received body: " + new String(buffer));
}
Path filePath = Path.of("src/main/resources/site.html");
byte[] body = Files.readAllBytes(filePath);
outputStream.write((
"HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: " + body.length + "\r\n" +
"\r\n"
).getBytes(StandardCharsets.UTF_8));
outputStream.write(body);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Thread(new HttpServer(8082)).start();
}
}
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import static java.net.http.HttpRequest.BodyPublishers.ofFile;
public class HttpClientRunner {
public static void main(final String[] args) throws IOException, InterruptedException {
var httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
Path jsonFilePath = Path.of("src/main/resources/example.json");
var request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8082"))
.header("Content-Type", "application/json")
.POST(ofFile(jsonFilePath))
.build();
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Response headers: " + response.headers());
System.out.println("Response body: " + response.body());
}
}