cleanup, turn into record classes
This commit is contained in:
@@ -4,28 +4,7 @@ import java.io.File;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class HttpFileItem {
|
public record HttpFileItem(String fileName, String contentType, byte[] content) {
|
||||||
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 void saveTo(File destination) throws IOException {
|
public void saveTo(File destination) throws IOException {
|
||||||
try (FileOutputStream fos = new FileOutputStream(destination)) {
|
try (FileOutputStream fos = new FileOutputStream(destination)) {
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public class HttpRequest {
|
|||||||
this.path = URLDecoder.decode(this.path, StandardCharsets.UTF_8);
|
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("&");
|
String[] pairs = queryString.split("&");
|
||||||
for (String pair : pairs) {
|
for (String pair : pairs) {
|
||||||
int idx = pair.indexOf("=");
|
int idx = pair.indexOf("=");
|
||||||
@@ -82,7 +82,7 @@ public class HttpRequest {
|
|||||||
String key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8);
|
String key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8);
|
||||||
String value = URLDecoder.decode(pair.substring(idx + 1), 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);
|
Logger.debug("HTTP", "Query param: " + key + " = " + value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,6 +150,7 @@ public class HttpRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("StatementWithEmptyBody")
|
||||||
private void parseChunkedBody(BufferedReader in) throws IOException {
|
private void parseChunkedBody(BufferedReader in) throws IOException {
|
||||||
StringBuilder content = new StringBuilder();
|
StringBuilder content = new StringBuilder();
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -248,7 +249,7 @@ public class HttpRequest {
|
|||||||
|
|
||||||
public String getQueryParam(String name) {
|
public String getQueryParam(String name) {
|
||||||
List<String> values = queryParams.get(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) {
|
public List<String> getQueryParams(String name) {
|
||||||
|
|||||||
@@ -5,17 +5,10 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class HttpRequestBody {
|
public record HttpRequestBody(String rawContent, HttpContentType contentType) {
|
||||||
private final String rawContent;
|
|
||||||
private final HttpContentType contentType;
|
|
||||||
private static final ObjectMapper jsonMapper = new ObjectMapper();
|
private static final ObjectMapper jsonMapper = new ObjectMapper();
|
||||||
private static final XmlMapper xmlMapper = new XmlMapper();
|
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 {
|
public <T> T parseAs(Class<T> clazz) throws IOException {
|
||||||
return switch (contentType) {
|
return switch (contentType) {
|
||||||
case APPLICATION_JSON -> jsonMapper.readValue(rawContent, clazz);
|
case APPLICATION_JSON -> jsonMapper.readValue(rawContent, clazz);
|
||||||
@@ -23,12 +16,4 @@ public class HttpRequestBody {
|
|||||||
default -> throw new IOException("Unsupported content type for parsing: " + contentType);
|
default -> throw new IOException("Unsupported content type for parsing: " + contentType);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRawContent() {
|
|
||||||
return rawContent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpContentType getContentType() {
|
|
||||||
return contentType;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,9 +45,9 @@ public class Main {
|
|||||||
|
|
||||||
HttpFileItem uploadedFile = multipartData.getFile("file");
|
HttpFileItem uploadedFile = multipartData.getFile("file");
|
||||||
if (uploadedFile != null) {
|
if (uploadedFile != null) {
|
||||||
String fileName = uploadedFile.getFileName();
|
String fileName = uploadedFile.fileName();
|
||||||
String contentType = uploadedFile.getContentType();
|
String contentType = uploadedFile.contentType();
|
||||||
byte[] fileContent = uploadedFile.getContent();
|
byte[] fileContent = uploadedFile.content();
|
||||||
|
|
||||||
File uploadDir = new File("uploads");
|
File uploadDir = new File("uploads");
|
||||||
if (!uploadDir.exists()) {
|
if (!uploadDir.exists()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user