-
-
-
- """);
- }
- }
-
- public void updateUserDataById(Socket clientSocket, BufferedReader in, String requestLine) throws IOException {
- try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
-
- String requestBody = HttpUtils.getRequestBody(in);
- int userId = UserUtils.getUserId(requestLine);
-
- String newName = HttpUtils.extractQueryParams(requestBody).get("new-name");
- String newJob = HttpUtils.extractQueryParams(requestBody).get("new-job");
-
- if (!newName.equals("") && newName != null) {
- data.getUserDataByIndex(userId - 1).setName(newName);
- }
-
- if (!newJob.equals("") && newJob != null) {
- data.getUserDataByIndex(userId - 1).setJob(newJob);
- }
-
-
- // HTTP response header
- out.println("HTTP/1.1 200 OK");
- out.println("Content-Type: application/json");
- out.println();
-
- // HTTP response body
- out.println(
- """
- {
- "status": "success",
- "message": "Data updated successfully"
- }
- """);
- }
- }
-
- public void deleteUserDataById(Socket clientSocket, String requestLine) throws IOException {
- System.out.println("delete bang!");
- try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
-
- int userId = UserUtils.getUserId(requestLine);
-
- data.removeData(userId - 1);
-
- // HTTP response header
- out.println("HTTP/1.1 200 OK");
- out.println("Content-Type: application/json");
- out.println();
-
- // HTTP response body
- out.println(
- """
- {
- "status": "success",
- "message": "Data deleted successfully"
- }
- """);
- }
- }
-}
\ No newline at end of file
diff --git a/src/main/java/com/server/Main.java b/src/main/java/com/server/Main.java
new file mode 100644
index 0000000..616a950
--- /dev/null
+++ b/src/main/java/com/server/Main.java
@@ -0,0 +1,70 @@
+package com.server;
+
+import com.server.controller.RootController;
+import com.server.controller.UsersController;
+import com.server.model.Users;
+import com.server.utils.HttpUtils;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class Main {
+ private static Users data = new Users();
+ private static RootController rootController = new RootController(data);
+ private static UsersController usersController = new UsersController(data);
+ private static final ExecutorService threadPool = Executors.newFixedThreadPool(10);
+
+ public static void main(String[] args) throws Exception {
+ try (ServerSocket serverSocket = new ServerSocket(8000)) {
+ System.out.println("Server is running on http://localhost:" + serverSocket.getLocalPort());
+
+ while (true) {
+ try {
+ Socket clientSocket = serverSocket.accept();
+ threadPool.submit(() -> handleRequest(clientSocket));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void handleRequest(Socket clientSocket) {
+ try (clientSocket;
+ BufferedReader in =
+ new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) {
+ String requestLine = in.readLine();
+
+ // client HTTP request line log
+ if (requestLine == null) return;
+ System.out.println(requestLine);
+
+ // Routing
+ if (requestLine.startsWith("GET / ")) {
+ rootController.handleGetRequest(clientSocket);
+ } else if (requestLine.startsWith("GET /users ")) {
+ usersController.getAllUsers(clientSocket);
+ } else if (requestLine.startsWith("POST /users ")) {
+ usersController.postUserData(clientSocket, in);
+ } else if (requestLine.startsWith("GET /users/")) {
+ usersController.getUserDataById(clientSocket, requestLine);
+ } else if (requestLine.startsWith("PATCH /users/")) {
+ usersController.updateUserDataById(clientSocket, in, requestLine);
+ } else if (requestLine.startsWith("DELETE /users/")) {
+ usersController.deleteUserDataById(clientSocket, requestLine);
+ } else if (requestLine.startsWith("GET /search?")) {
+ usersController.getUserDataByQuery(clientSocket, requestLine);
+ } else {
+ HttpUtils.handle404ErrorResponse(clientSocket);
+ }
+ } catch (Exception e) {
+ HttpUtils.handleServerErrorResponse(clientSocket, e);
+ }
+ }
+}
diff --git a/src/main/java/com/server/controller/RootController.java b/src/main/java/com/server/controller/RootController.java
new file mode 100644
index 0000000..adcb3a0
--- /dev/null
+++ b/src/main/java/com/server/controller/RootController.java
@@ -0,0 +1,48 @@
+package com.server.controller;
+
+import com.google.gson.Gson;
+import com.server.model.Users;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Map;
+
+/** Controller for handling requests to the root endpoint. */
+public class RootController {
+ private Users data;
+ private Gson gson = new Gson();
+
+ /**
+ * Constructs the root controller.
+ *
+ * @param data The user data model.
+ */
+ public RootController(Users data) {
+ this.data = data;
+ }
+
+ /**
+ * Handles GET requests to the root endpoint.
+ *
+ * @param clientSocket The client socket.
+ * @throws IOException If an I/O error occurs.
+ */
+ public void handleGetRequest(Socket clientSocket) throws IOException {
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+
+ Map responseBody = new HashMap<>();
+ responseBody.put("status", "UP");
+ responseBody.put("message", "Welcome to Basic Web Server API");
+ responseBody.put("totalUsers", data.getArrayLength());
+
+ // HTTP response header
+ out.println("HTTP/1.1 200 OK");
+ out.println("Content-Type: application/json");
+ out.println();
+
+ // HTTP response body
+ out.println(gson.toJson(responseBody));
+ }
+ }
+}
diff --git a/src/main/java/com/server/controller/UsersController.java b/src/main/java/com/server/controller/UsersController.java
new file mode 100644
index 0000000..39e0b1f
--- /dev/null
+++ b/src/main/java/com/server/controller/UsersController.java
@@ -0,0 +1,143 @@
+package com.server.controller;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import com.server.model.User;
+import com.server.model.Users;
+import com.server.utils.HttpUtils;
+import com.server.utils.UserUtils;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Map;
+
+/** Controller for user endpoints, refactored to REST API (JSON). */
+public class UsersController {
+ private Users data;
+ private Gson gson = new Gson();
+
+ public UsersController(Users data) {
+ this.data = data;
+ }
+
+ public void getAllUsers(Socket clientSocket) throws IOException {
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+ ArrayList dataResult = data.getAllData();
+
+ out.println("HTTP/1.1 200 OK");
+ out.println("Content-Type: application/json");
+ out.println();
+ out.println(gson.toJson(dataResult));
+ }
+ }
+
+ public void getUserDataById(Socket clientSocket, String requestLine) throws IOException {
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+ int userId = UserUtils.getUserId(requestLine);
+
+ if (userId != -1 && userId <= data.getArrayLength()) {
+ User user = data.getUserDataByIndex(userId - 1);
+ out.println("HTTP/1.1 200 OK");
+ out.println("Content-Type: application/json");
+ out.println();
+ out.println(gson.toJson(user));
+ } else {
+ HttpUtils.handle404ErrorResponse(clientSocket);
+ }
+ }
+ }
+
+ public void getUserDataByQuery(Socket clientSocket, String requestLine)
+ throws IOException, URISyntaxException {
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+
+ String[] arrayRequestLine = requestLine.split(" ");
+ URI uri = new URI(arrayRequestLine[1]);
+
+ Map queryParams = HttpUtils.extractQueryParams(uri.getQuery());
+ ArrayList dataResult = data.getUserDataByQuery(queryParams);
+
+ out.println("HTTP/1.1 200 OK");
+ out.println("Content-Type: application/json");
+ out.println();
+ out.println(gson.toJson(dataResult));
+ }
+ }
+
+ public void postUserData(Socket clientSocket, BufferedReader in) throws IOException {
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+ String requestBody = HttpUtils.getRequestBody(in);
+
+ User newUser = gson.fromJson(requestBody, User.class);
+ data.addNewData(newUser);
+
+ JsonObject response = new JsonObject();
+ response.addProperty("status", "success");
+ response.addProperty("message", "User added successfully");
+
+ out.println("HTTP/1.1 201 Created");
+ out.println("Content-Type: application/json");
+ out.println();
+ out.println(gson.toJson(response));
+ }
+ }
+
+ public void updateUserDataById(Socket clientSocket, BufferedReader in, String requestLine)
+ throws IOException {
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+ String requestBody = HttpUtils.getRequestBody(in);
+ int userId = UserUtils.getUserId(requestLine);
+
+ if (userId == -1 || userId > data.getArrayLength()) {
+ HttpUtils.handle404ErrorResponse(clientSocket);
+ return;
+ }
+
+ User updates = gson.fromJson(requestBody, User.class);
+ User existingUser = data.getUserDataByIndex(userId - 1);
+
+ if (updates.getName() != null && !updates.getName().trim().isEmpty()) {
+ existingUser.setName(updates.getName());
+ }
+ if (updates.getJob() != null && !updates.getJob().trim().isEmpty()) {
+ existingUser.setJob(updates.getJob());
+ }
+
+ JsonObject response = new JsonObject();
+ response.addProperty("status", "success");
+ response.addProperty("message", "Data updated successfully");
+
+ out.println("HTTP/1.1 200 OK");
+ out.println("Content-Type: application/json");
+ out.println();
+ out.println(gson.toJson(response));
+ }
+ }
+
+ public void deleteUserDataById(Socket clientSocket, String requestLine) throws IOException {
+ System.out.println("delete request received!");
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+ int userId = UserUtils.getUserId(requestLine);
+
+ if (userId == -1 || userId > data.getArrayLength()) {
+ HttpUtils.handle404ErrorResponse(clientSocket);
+ return;
+ }
+
+ data.removeData(userId - 1);
+
+ JsonObject response = new JsonObject();
+ response.addProperty("status", "success");
+ response.addProperty("message", "Data deleted successfully");
+
+ out.println("HTTP/1.1 200 OK");
+ out.println("Content-Type: application/json");
+ out.println();
+ out.println(gson.toJson(response));
+ }
+ }
+}
diff --git a/src/main/java/com/server/model/User.java b/src/main/java/com/server/model/User.java
new file mode 100644
index 0000000..017aa0c
--- /dev/null
+++ b/src/main/java/com/server/model/User.java
@@ -0,0 +1,26 @@
+package com.server.model;
+
+public class User {
+ private String name, job;
+
+ public User(String name, String job) {
+ this.name = name;
+ this.job = job;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getJob() {
+ return job;
+ }
+
+ public void setJob(String job) {
+ this.job = job;
+ }
+}
diff --git a/src/main/java/com/server/model/Users.java b/src/main/java/com/server/model/Users.java
new file mode 100644
index 0000000..799e2a5
--- /dev/null
+++ b/src/main/java/com/server/model/Users.java
@@ -0,0 +1,73 @@
+package com.server.model;
+
+import java.util.ArrayList;
+import java.util.Map;
+
+public class Users {
+ private ArrayList data;
+
+ public Users() {
+ data = new ArrayList<>();
+
+ data.add(new User("Alex", "Data Analyst"));
+ data.add(new User("Budi", "Frontend Web Developer"));
+ data.add(new User("Cahya", "Computer Vision Engineer"));
+ data.add(new User("Dono", "Data Analyst"));
+ data.add(new User("Cakra", "Frontend Web Developer"));
+ data.add(new User("Zara", "Frontend Web Developer"));
+ data.add(new User("Jojo", "Machine Learning Engineer"));
+ data.add(new User("Pepe", "Data Analyst"));
+ }
+
+ public ArrayList getAllData() {
+ return data;
+ }
+
+ public int getArrayLength() {
+ return data.size();
+ }
+
+ public User getUserDataByIndex(int index) {
+ return data.get(index);
+ }
+
+ public ArrayList getUserDataByQuery(Map queryParams) {
+ String nameWanted = queryParams.get("name");
+ String jobWanted = queryParams.get("job");
+
+ ArrayList result = new ArrayList<>();
+
+ if (!nameWanted.equals("") && !jobWanted.equals("")) {
+ for (User datum : data) {
+ if (datum.getName().equalsIgnoreCase(nameWanted)
+ && datum.getJob().equalsIgnoreCase(jobWanted)) {
+ result.add(datum);
+ }
+ }
+ } else if (!nameWanted.equals("")) {
+ for (User datum : data) {
+ if (datum.getName().equalsIgnoreCase(nameWanted)) {
+ result.add(datum);
+ }
+ }
+ } else if (!jobWanted.equals("")) {
+ for (User datum : data) {
+ if (datum.getJob().equalsIgnoreCase(jobWanted)) {
+ result.add(datum);
+ }
+ }
+ } else {
+ result = getAllData();
+ }
+
+ return result;
+ }
+
+ public void addNewData(User newData) {
+ data.add(newData);
+ }
+
+ public void removeData(int index) {
+ data.remove(index);
+ }
+}
diff --git a/src/main/java/com/server/utils/HttpUtils.java b/src/main/java/com/server/utils/HttpUtils.java
new file mode 100644
index 0000000..57ff863
--- /dev/null
+++ b/src/main/java/com/server/utils/HttpUtils.java
@@ -0,0 +1,106 @@
+package com.server.utils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Map;
+
+public class HttpUtils {
+
+ /**
+ * Sends a 404 Not Found response to the client.
+ *
+ * @param clientSocket the socket connection to the client
+ * @throws IOException if an I/O error occurs while sending the response
+ */
+ public static void handle404ErrorResponse(Socket clientSocket) throws IOException {
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+ out.println("HTTP/1.1 404 Not Found");
+ out.println("Content-Type: application/json");
+ out.println();
+ out.println("{\"error\": \"404 Not Found\"}");
+ }
+ }
+
+ /**
+ * Sends a 500 Internal Server Error response to the client.
+ *
+ * @param clientSocket the socket connection to the client
+ * @param e the exception that occurred, which will be included in the response message
+ */
+ public static void handleServerErrorResponse(Socket clientSocket, Exception e) {
+ try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
+ out.println("HTTP/1.1 500 Internal Server Error");
+ out.println("Content-Type: application/json");
+ out.println();
+
+ // We escape double quotes just in case, though for a simple server it is basic
+ String safeMsg =
+ e.getMessage() != null ? e.getMessage().replace("\"", "\\\"") : "Unknown Error";
+ out.println("{\"error\": \"500 Internal Server Error\", \"details\": \"" + safeMsg + "\"}");
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ /**
+ * Retrieves the entire HTTP request body from the given BufferedReader.
+ *
+ * @param in the BufferedReader from which the HTTP request body is read
+ * @return a String containing the entire request body
+ */
+ public static String getRequestBody(BufferedReader in) throws IOException {
+ String requestBody = null;
+ int contentLength = getContentLength(in);
+
+ // Read the body content if not empty
+ if (contentLength > 0) {
+ char[] body = new char[contentLength];
+ try {
+ in.read(body, 0, contentLength);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ requestBody = new String(body);
+ }
+
+ return requestBody;
+ }
+
+ private static int getContentLength(BufferedReader in) throws IOException {
+ String inputLine;
+ int contentLength = 0;
+ while ((inputLine = in.readLine()) != null && !inputLine.isEmpty()) {
+ if (inputLine.startsWith("Content-Length:")) {
+ contentLength = Integer.parseInt(inputLine.split(":")[1].trim());
+ }
+ }
+
+ return contentLength;
+ }
+
+ /**
+ * Extracts raw query parameters from a URL string and returns them as a map.
+ *
+ * @param queryParam the URL query parameter string (e.g., "key1=value1&key2=value2")
+ * @return a Map containing the extracted query parameters, where each key is a parameter name and
+ * the corresponding value is the parameter value
+ */
+ public static Map extractQueryParams(String queryParam) {
+ Map result = new HashMap<>();
+ String[] pairs = queryParam.split("&");
+
+ for (String pair : pairs) {
+ String[] keyValue = pair.split("=");
+ if (keyValue.length == 2) {
+ result.put(keyValue[0], keyValue[1]);
+ } else if (keyValue.length == 1) {
+ result.put(keyValue[0], "");
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/src/main/java/com/server/utils/UserUtils.java b/src/main/java/com/server/utils/UserUtils.java
new file mode 100644
index 0000000..eda46ef
--- /dev/null
+++ b/src/main/java/com/server/utils/UserUtils.java
@@ -0,0 +1,23 @@
+package com.server.utils;
+
+public class UserUtils {
+
+ /**
+ * Retrieves the user ID from the specified URL request line.
+ *
+ * @param requestLine the HTTP request line containing the URL with the user ID parameter
+ * @return the user ID extracted from the request line
+ */
+ public static int getUserId(String requestLine) {
+ String[] arrayRequestLine = requestLine.split(" ");
+ int userId = -1;
+
+ try {
+ userId = Integer.parseInt(arrayRequestLine[1].substring("/users/".length()));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return userId;
+ }
+}
diff --git a/src/model/User.java b/src/model/User.java
deleted file mode 100644
index 548c4fa..0000000
--- a/src/model/User.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package model;
-
-public class User {
- private String name, job;
-
- public User(String name, String job) {
- this.name = name;
- this.job = job;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getJob() {
- return job;
- }
-
- public void setJob(String job) {
- this.job = job;
- }
-}
diff --git a/src/model/Users.java b/src/model/Users.java
deleted file mode 100644
index 086ec63..0000000
--- a/src/model/Users.java
+++ /dev/null
@@ -1,73 +0,0 @@
-package model;
-
-import java.util.ArrayList;
-import java.util.Map;
-
-public class Users {
- private ArrayList data;
-
- public Users() {
- data = new ArrayList<>();
-
- data.add(new User("Alex", "Data Analyst"));
- data.add(new User("Budi", "Frontend Web Developer"));
- data.add(new User("Cahya", "Computer Vision Engineer"));
- data.add(new User("Dono", "Data Analyst"));
- data.add(new User("Cakra", "Frontend Web Developer"));
- data.add(new User("Zara", "Frontend Web Developer"));
- data.add(new User("Jojo", "Machine Learning Engineer"));
- data.add(new User("Pepe", "Data Analyst"));
- }
-
- public ArrayList getAllData() {
- return data;
- }
-
- public int getArrayLength() {
- return data.size();
- }
-
- public User getUserDataByIndex(int index) {
- return data.get(index);
- }
-
- public ArrayList getUserDataByQuery(Map queryParams) {
- String nameWanted = queryParams.get("name");
- String jobWanted = queryParams.get("job");
-
- ArrayList result = new ArrayList<>();
-
- if (!nameWanted.equals("") && !jobWanted.equals("")) {
- for (User datum : data) {
- if (datum.getName().equalsIgnoreCase(nameWanted) &&
- datum.getJob().equalsIgnoreCase(jobWanted)) {
- result.add(datum);
- }
- }
- } else if (!nameWanted.equals("")) {
- for (User datum : data) {
- if (datum.getName().equalsIgnoreCase(nameWanted)) {
- result.add(datum);
- }
- }
- } else if (!jobWanted.equals("")) {
- for (User datum : data) {
- if (datum.getJob().equalsIgnoreCase(jobWanted)) {
- result.add(datum);
- }
- }
- } else {
- result = getAllData();
- }
-
- return result;
- }
-
- public void addNewData(User newData) {
- data.add(newData);
- }
-
- public void removeData(int index) {
- data.remove(index);
- }
-}
\ No newline at end of file
diff --git a/src/test/java/com/server/model/UsersTest.java b/src/test/java/com/server/model/UsersTest.java
new file mode 100644
index 0000000..0e12e84
--- /dev/null
+++ b/src/test/java/com/server/model/UsersTest.java
@@ -0,0 +1,33 @@
+package com.server.model;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+public class UsersTest {
+
+ @Test
+ public void testAddNewData() {
+ Users users = new Users();
+ int initialSize = users.getArrayLength();
+
+ User newUser = new User("Test User", "Tester");
+ users.addNewData(newUser);
+
+ assertEquals(initialSize + 1, users.getArrayLength(), "Users array size should increase by 1");
+ assertEquals(
+ "Test User", users.getUserDataByIndex(initialSize).getName(), "New user name should match");
+ assertEquals(
+ "Tester", users.getUserDataByIndex(initialSize).getJob(), "New user job should match");
+ }
+
+ @Test
+ public void testRemoveData() {
+ Users users = new Users();
+ int initialSize = users.getArrayLength();
+
+ users.removeData(0); // Remove the first user
+
+ assertEquals(initialSize - 1, users.getArrayLength(), "Users array size should decrease by 1");
+ }
+}
diff --git a/src/utils/HttpUtils.java b/src/utils/HttpUtils.java
deleted file mode 100644
index 4382d47..0000000
--- a/src/utils/HttpUtils.java
+++ /dev/null
@@ -1,103 +0,0 @@
-package utils;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.net.Socket;
-import java.util.HashMap;
-import java.util.Map;
-
-public class HttpUtils {
-
- /**
- * Sends a 404 Not Found response to the client.
- *
- * @param clientSocket the socket connection to the client
- * @throws IOException if an I/O error occurs while sending the response
- */
- public static void handle404ErrorResponse(Socket clientSocket) throws IOException {
- try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
- out.println("HTTP/1.1 404 Not Found");
- out.println("Content-Type: text/html");
- out.println();
- out.println("
404 Not Found!
");
- }
- }
-
- /**
- * Sends a 500 Internal Server Error response to the client.
- *
- * @param clientSocket the socket connection to the client
- * @param e the exception that occurred, which will be included in the response message
- */
- public static void handleServerErrorResponse(Socket clientSocket, Exception e) {
- try (PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
- out.println("HTTP/1.1 500 Internal Server Error");
- out.println("Content-Type: text/html");
- out.println();
- out.println("Error occured: " + e.getMessage());
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
-
- /**
- * Retrieves the entire HTTP request body from the given BufferedReader.
- *
- * @param in the BufferedReader from which the HTTP request body is read
- * @return a String containing the entire request body
- */
- public static String getRequestBody(BufferedReader in) throws IOException {
- String requestBody = null;
- int contentLength = getContentLength(in);
-
- // Read the body content if not empty
- if (contentLength > 0) {
- char[] body = new char[contentLength];
- try {
- in.read(body, 0, contentLength);
- } catch (IOException e) {
- e.printStackTrace();
- }
- requestBody = new String(body);
- }
-
- return requestBody;
- }
-
- private static int getContentLength(BufferedReader in) throws IOException {
- String inputLine;
- int contentLength = 0;
- while ((inputLine = in.readLine()) != null && !inputLine.isEmpty()) {
- if (inputLine.startsWith("Content-Length:")) {
- contentLength = Integer.parseInt(inputLine.split(":")[1].trim());
- }
- }
-
- return contentLength;
- }
-
- /**
- * Extracts raw query parameters from a URL string and returns them as a map.
- *
- * @param queryParam the URL query parameter string (e.g., "key1=value1&key2=value2")
- * @return a Map containing the extracted query parameters, where each key is a parameter name
- * and the corresponding value is the parameter value
- */
- public static Map extractQueryParams(String queryParam) {
- Map result = new HashMap<>();
- String[] pairs = queryParam.split("&");
-
- for (String pair : pairs) {
- String[] keyValue = pair.split("=");
- if (keyValue.length == 2) {
- result.put(keyValue[0], keyValue[1]);
- } else if (keyValue.length == 1) {
- result.put(keyValue[0], "");
- }
- }
-
- return result;
- }
-
-}
diff --git a/src/utils/UserUtils.java b/src/utils/UserUtils.java
deleted file mode 100644
index 999617d..0000000
--- a/src/utils/UserUtils.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package utils;
-
-public class UserUtils {
-
- /**
- * Retrieves the user ID from the specified URL request line.
- *
- * @param requestLine the HTTP request line containing the URL with the user ID parameter
- * @return the user ID extracted from the request line
- */
- public static int getUserId(String requestLine) {
- String[] arrayRequestLine = requestLine.split(" ");
- int userId = -1;
-
- try {
- userId = Integer.parseInt(arrayRequestLine[1].substring("/users/".length()));
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return userId;
- }
-}