diff --git a/src/main/java/io/github/lumijiez/core/http/HttpFileItem.java b/src/main/java/io/github/lumijiez/core/http/HttpFileItem.java index 0df09ed..86f13e4 100644 --- a/src/main/java/io/github/lumijiez/core/http/HttpFileItem.java +++ b/src/main/java/io/github/lumijiez/core/http/HttpFileItem.java @@ -4,28 +4,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -public class HttpFileItem { - private final String fileName; - private final String contentType; - private final byte[] content; - - public HttpFileItem(String fileName, String contentType, byte[] content) { - this.fileName = fileName; - this.contentType = contentType; - this.content = content; - } - - public String getFileName() { - return fileName; - } - - public String getContentType() { - return contentType; - } - - public byte[] getContent() { - return content; - } +public record HttpFileItem(String fileName, String contentType, byte[] content) { public void saveTo(File destination) throws IOException { try (FileOutputStream fos = new FileOutputStream(destination)) { diff --git a/src/main/java/io/github/lumijiez/core/http/HttpRequest.java b/src/main/java/io/github/lumijiez/core/http/HttpRequest.java index 27196f3..8e25375 100644 --- a/src/main/java/io/github/lumijiez/core/http/HttpRequest.java +++ b/src/main/java/io/github/lumijiez/core/http/HttpRequest.java @@ -74,7 +74,7 @@ public class HttpRequest { this.path = URLDecoder.decode(this.path, StandardCharsets.UTF_8); } - private void parseQueryString(String queryString) throws IOException { + private void parseQueryString(String queryString) { String[] pairs = queryString.split("&"); for (String pair : pairs) { int idx = pair.indexOf("="); @@ -82,7 +82,7 @@ public class HttpRequest { String key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8); String value = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8); - queryParams.computeIfAbsent(key, k -> new ArrayList<>()).add(value); + queryParams.computeIfAbsent(key, _ -> new ArrayList<>()).add(value); Logger.debug("HTTP", "Query param: " + key + " = " + value); } } @@ -150,6 +150,7 @@ public class HttpRequest { } } + @SuppressWarnings("StatementWithEmptyBody") private void parseChunkedBody(BufferedReader in) throws IOException { StringBuilder content = new StringBuilder(); while (true) { @@ -248,7 +249,7 @@ public class HttpRequest { public String getQueryParam(String name) { List values = queryParams.get(name); - return values != null && !values.isEmpty() ? values.get(0) : null; + return values != null && !values.isEmpty() ? values.getFirst() : null; } public List getQueryParams(String name) { diff --git a/src/main/java/io/github/lumijiez/core/http/HttpRequestBody.java b/src/main/java/io/github/lumijiez/core/http/HttpRequestBody.java index 2374103..720ddab 100644 --- a/src/main/java/io/github/lumijiez/core/http/HttpRequestBody.java +++ b/src/main/java/io/github/lumijiez/core/http/HttpRequestBody.java @@ -5,17 +5,10 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper; import java.io.IOException; -public class HttpRequestBody { - private final String rawContent; - private final HttpContentType contentType; +public record HttpRequestBody(String rawContent, HttpContentType contentType) { private static final ObjectMapper jsonMapper = new ObjectMapper(); private static final XmlMapper xmlMapper = new XmlMapper(); - public HttpRequestBody(String rawContent, HttpContentType contentType) { - this.rawContent = rawContent; - this.contentType = contentType; - } - public T parseAs(Class clazz) throws IOException { return switch (contentType) { case APPLICATION_JSON -> jsonMapper.readValue(rawContent, clazz); @@ -23,12 +16,4 @@ public class HttpRequestBody { default -> throw new IOException("Unsupported content type for parsing: " + contentType); }; } - - public String getRawContent() { - return rawContent; - } - - public HttpContentType getContentType() { - return contentType; - } } diff --git a/src/main/java/io/github/lumijiez/example/Main.java b/src/main/java/io/github/lumijiez/example/Main.java index 97a6f6a..7ada27f 100644 --- a/src/main/java/io/github/lumijiez/example/Main.java +++ b/src/main/java/io/github/lumijiez/example/Main.java @@ -45,9 +45,9 @@ public class Main { HttpFileItem uploadedFile = multipartData.getFile("file"); if (uploadedFile != null) { - String fileName = uploadedFile.getFileName(); - String contentType = uploadedFile.getContentType(); - byte[] fileContent = uploadedFile.getContent(); + String fileName = uploadedFile.fileName(); + String contentType = uploadedFile.contentType(); + byte[] fileContent = uploadedFile.content(); File uploadDir = new File("uploads"); if (!uploadDir.exists()) {