cleanup, turn into record classes

This commit is contained in:
Daniel
2024-11-03 23:33:04 +02:00
parent bfefcd0718
commit 80640ebeab
4 changed files with 9 additions and 44 deletions

View File

@@ -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)) {

View File

@@ -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<String> values = queryParams.get(name);
return values != null && !values.isEmpty() ? values.get(0) : null;
return values != null && !values.isEmpty() ? values.getFirst() : null;
}
public List<String> getQueryParams(String name) {

View File

@@ -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> T parseAs(Class<T> 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;
}
}

View File

@@ -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()) {