improvements to http logic
~1.5h work
This commit is contained in:
parent
0a1f96e842
commit
2f1700ebed
@ -13,12 +13,20 @@ public class UserService implements Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response handleRequest(HttpRequest request) throws JsonProcessingException {
|
public Response handleRequest(HttpRequest request) throws JsonProcessingException {
|
||||||
UserCredentials userCredentials = new UserCredentials("test", "test");
|
try {
|
||||||
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
if (request.getPath().split("/")[1].equals("sessions")) {
|
||||||
String json = ow.writeValueAsString(userCredentials);
|
// response = login()
|
||||||
|
return new Response(HttpStatus.NOT_IMPLEMENTED);
|
||||||
System.out.println(json);
|
}
|
||||||
|
return switch (request.getMethod()) {
|
||||||
return new Response(HttpStatus.OK, "application/json", "");
|
case GET -> new Response(HttpStatus.NO_CONTENT);
|
||||||
|
case POST -> new Response(HttpStatus.NOT_IMPLEMENTED);
|
||||||
|
case PUT -> new Response(HttpStatus.NOT_IMPLEMENTED);
|
||||||
|
default -> new Response(HttpStatus.INTERNAL);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
return new Response(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,11 @@
|
|||||||
|
package at.nanopenguin.mtcg.application.service.schemas;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
@JsonSerialize
|
||||||
|
@JsonDeserialize
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public record Card(String id, String name, Float damage) {
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package at.nanopenguin.mtcg.application.service.schemas;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
@JsonSerialize
|
||||||
|
@JsonDeserialize
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public record TradingDeal(String id, String cardToTrade, String type, Float minimumDamage) {
|
||||||
|
}
|
||||||
@ -1,7 +1,11 @@
|
|||||||
package at.nanopenguin.mtcg.application.service.schemas;
|
package at.nanopenguin.mtcg.application.service.schemas;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
@JsonSerialize
|
@JsonSerialize
|
||||||
|
@JsonDeserialize
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public record UserCredentials(String username, String password) {
|
public record UserCredentials(String username, String password) {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,12 @@
|
|||||||
|
package at.nanopenguin.mtcg.application.service.schemas;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
@JsonSerialize
|
||||||
|
@JsonDeserialize
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public record UserData(String name, String bio, String image) {
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package at.nanopenguin.mtcg.application.service.schemas;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
@JsonSerialize
|
||||||
|
@JsonDeserialize
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public record UserStats(String name, Integer elo, Integer wins, Integer losses) {
|
||||||
|
}
|
||||||
@ -1,15 +1,21 @@
|
|||||||
package at.nanopenguin.mtcg.http;
|
package at.nanopenguin.mtcg.http;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class HttpRequest {
|
public class HttpRequest {
|
||||||
|
@Getter
|
||||||
private final HttpMethod method;
|
private final HttpMethod method;
|
||||||
|
@Getter
|
||||||
private final String path;
|
private final String path;
|
||||||
|
@Getter
|
||||||
private final String version;
|
private final String version;
|
||||||
private final Map<String, String> httpHeaders = new HashMap<>();
|
private final Map<String, String> httpHeaders = new HashMap<>();
|
||||||
|
@Getter
|
||||||
private final String body;
|
private final String body;
|
||||||
|
|
||||||
public HttpRequest(BufferedReader br) throws IOException {
|
public HttpRequest(BufferedReader br) throws IOException {
|
||||||
@ -26,7 +32,10 @@ public class HttpRequest {
|
|||||||
this.httpHeaders.put(headerEntry[0], headerEntry[1]);
|
this.httpHeaders.put(headerEntry[0], headerEntry[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.body = this.httpHeaders.containsKey("Content-Length") ? new String(new char[Integer.parseInt(this.httpHeaders.get("Content-Length"))]) : null;
|
// this.body = this.httpHeaders.containsKey("Content-Length") ? new String(new char[Integer.parseInt(this.httpHeaders.get("Content-Length"))]) : null;
|
||||||
|
int contentLength = Integer.parseInt(this.httpHeaders.get("Content-Length"));
|
||||||
|
char[] charBuffer = new char[contentLength];
|
||||||
|
this.body = br.read(charBuffer, 0, contentLength) > 0 ? new String(charBuffer) : null;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -37,23 +46,8 @@ public class HttpRequest {
|
|||||||
this.body = null;
|
this.body = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpMethod getMethod() {
|
|
||||||
return method;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPath() {
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getVersion() {
|
|
||||||
return version;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getHttpHeader(String header) {
|
public String getHttpHeader(String header) {
|
||||||
return httpHeaders.get(header);
|
return httpHeaders.get(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBody() {
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package at.nanopenguin.mtcg.http;
|
package at.nanopenguin.mtcg.http;
|
||||||
|
|
||||||
import at.nanopenguin.mtcg.application.service.Service;
|
import at.nanopenguin.mtcg.application.service.Service;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -40,7 +41,12 @@ public class RequestHandler implements Runnable {
|
|||||||
break responseBuilder;
|
break responseBuilder;
|
||||||
}
|
}
|
||||||
System.out.println("creating response");
|
System.out.println("creating response");
|
||||||
response = service.handleRequest(httpRequest);
|
try {
|
||||||
|
response = service.handleRequest(httpRequest);
|
||||||
|
}
|
||||||
|
catch (JsonProcessingException e) {
|
||||||
|
response = new Response(HttpStatus.BAD_REQUEST, "application/json", "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputStream out = this.serviceSocket.getOutputStream();
|
OutputStream out = this.serviceSocket.getOutputStream();
|
||||||
|
|||||||
@ -11,19 +11,40 @@ public class Response {
|
|||||||
|
|
||||||
public Response(HttpStatus httpStatus, String contentType, String content) {
|
public Response(HttpStatus httpStatus, String contentType, String content) {
|
||||||
this.httpStatus = httpStatus;
|
this.httpStatus = httpStatus;
|
||||||
|
|
||||||
|
if (httpStatus == HttpStatus.NO_CONTENT) {
|
||||||
|
this.contentType = null;
|
||||||
|
this.content = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.contentType = contentType;
|
this.contentType = contentType;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Response(HttpStatus httpStatus) {
|
||||||
|
this.httpStatus = httpStatus;
|
||||||
|
|
||||||
|
if (httpStatus == HttpStatus.NO_CONTENT) {
|
||||||
|
this.contentType = null;
|
||||||
|
this.content = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.contentType = "";
|
||||||
|
this.content = "";
|
||||||
|
}
|
||||||
|
|
||||||
public String get() {
|
public String get() {
|
||||||
|
|
||||||
String localDatetime = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneId.of("UTC")));
|
String localDatetime = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneId.of("UTC")));
|
||||||
return "HTTP/1.1 " + this.httpStatus.statusCode + " " + this.httpStatus.statusMessage + "\r\n" +
|
return "HTTP/1.1 " + this.httpStatus.statusCode + " " + this.httpStatus.statusMessage + "\r\n" +
|
||||||
"Connection: close\r\n" +
|
"Connection: close\r\n" +
|
||||||
"Date: " + localDatetime + "\r\n" +
|
"Date: " + localDatetime + "\r\n" +
|
||||||
"Content-Type: " + this.contentType + "\r\n" +
|
(this.content == null ? "\r\n\r\n" :
|
||||||
"Content-Length: " + this.content.length() + "\r\n" +
|
((this.contentType == null || this.contentType.isEmpty()) ? "" : "Content-Type: " + this.contentType + "\r\n") +
|
||||||
"\r\n" +
|
"Content-Length: " + this.content.length() + "\r\n" +
|
||||||
this.content;
|
"\r\n" +
|
||||||
|
this.content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,15 +33,13 @@ public class Router {
|
|||||||
System.out.println("resolving route " + route);
|
System.out.println("resolving route " + route);
|
||||||
String[] routeComponents = route.split("/");
|
String[] routeComponents = route.split("/");
|
||||||
|
|
||||||
String pathVariable = null; // might be useful later, idk how services
|
|
||||||
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
|
|
||||||
Route component = this.routeMap.get(method).get("/" + routeComponents[i]);
|
Route component = this.routeMap.get(method).get("/" + routeComponents[i]);
|
||||||
|
|
||||||
for (String search = "/" + routeComponents[i]; component != null && (component.service() == null || routeComponents.length - 1 > i) && routeComponents.length - 1 >= i; component = this.routeMap.get(method).get(search = routeComponents.length - 1 > i++ ? String.join("/", search, routeComponents[i]) : search)) {
|
for (String search = "/" + routeComponents[i]; component != null && (component.service() == null || routeComponents.length - 1 > i) && routeComponents.length - 1 >= i; component = this.routeMap.get(method).get(search = routeComponents.length - 1 > i++ ? String.join("/", search, routeComponents[i]) : search)) {
|
||||||
if (component.hasPathVariable() && routeComponents.length - 1 > i) {
|
if (component.hasPathVariable() && routeComponents.length - 1 > i) {
|
||||||
pathVariable = routeComponents[++i];
|
++i;
|
||||||
search = String.join("/", search, "{var}");
|
search = String.join("/", search, "{var}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user