diff --git a/tomcat/build.gradle b/tomcat/build.gradle index 56275ceb94..5e2a76a777 100644 --- a/tomcat/build.gradle +++ b/tomcat/build.gradle @@ -3,8 +3,8 @@ plugins { id "jacoco" } -sourceCompatibility = JavaVersion.VERSION_17 -targetCompatibility = JavaVersion.VERSION_17 +sourceCompatibility = JavaVersion.VERSION_11 +targetCompatibility = JavaVersion.VERSION_11 repositories { mavenCentral() diff --git a/tomcat/src/main/java/nextstep/jwp/controller/LoginController.java b/tomcat/src/main/java/nextstep/jwp/controller/LoginController.java index 79be79f205..4b19988c84 100644 --- a/tomcat/src/main/java/nextstep/jwp/controller/LoginController.java +++ b/tomcat/src/main/java/nextstep/jwp/controller/LoginController.java @@ -15,7 +15,7 @@ public class LoginController extends AbstractController { @Override protected void doPost(final HttpRequest request, final HttpResponse response) throws Exception { - final Body body = request.body(); + final Body body = request.getBody(); final String account = body.getValue("account"); final String password = body.getValue("password"); final User loginUser = InMemoryUserRepository.findByAccount(account) @@ -30,7 +30,7 @@ protected void doPost(final HttpRequest request, final HttpResponse response) th @Override protected void doGet(final HttpRequest request, final HttpResponse response) throws Exception { - if (request.headers().getCookies().containsKey("JSESSIONID")) { + if (request.getHeaders().getCookies().containsKey("JSESSIONID")) { response.addStatus(HttpStatus.FOUND) .addLocation("/index.html"); return; diff --git a/tomcat/src/main/java/nextstep/jwp/controller/RegisterController.java b/tomcat/src/main/java/nextstep/jwp/controller/RegisterController.java index 515f78508b..c4078628e2 100644 --- a/tomcat/src/main/java/nextstep/jwp/controller/RegisterController.java +++ b/tomcat/src/main/java/nextstep/jwp/controller/RegisterController.java @@ -14,7 +14,7 @@ public class RegisterController extends AbstractController { @Override protected void doPost(final HttpRequest request, final HttpResponse response) throws Exception { - final var body = request.body(); + final var body = request.getBody(); final String account = body.getValue("account"); InMemoryUserRepository.findByAccount(account) .ifPresent(user -> { diff --git a/tomcat/src/main/java/nextstep/jwp/controller/ResourceController.java b/tomcat/src/main/java/nextstep/jwp/controller/ResourceController.java index 04fb2e598d..6acd742e07 100644 --- a/tomcat/src/main/java/nextstep/jwp/controller/ResourceController.java +++ b/tomcat/src/main/java/nextstep/jwp/controller/ResourceController.java @@ -17,7 +17,7 @@ public class ResourceController extends AbstractController { @Override protected void doGet(final HttpRequest request, final HttpResponse response) throws Exception { - final String resourcePath = request.requestLine().uri().getPath(); + final String resourcePath = request.getRequestLine().getUri().getPath(); final String extension = resourcePath.substring(resourcePath.lastIndexOf(".") + 1); final ContentType contentType = ContentType.from(extension); final String content = getResourceContent(resourcePath); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java index 8a50be55a7..4f4e713058 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -3,6 +3,7 @@ import nextstep.jwp.Controller; import org.apache.coyote.Processor; import org.apache.coyote.RequestMapping; +import org.apache.coyote.http11.header.RequestLine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,11 +35,12 @@ public void process(final Socket connection) { final var outputStream = connection.getOutputStream(); final var bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { final HttpRequest request = HttpRequest.parse(bufferedReader); - log.info("{} {}", request.requestLine().method(), request.requestLine().uri()); + final RequestLine requestLine = request.getRequestLine(); + log.info("{} {}", requestLine.getMethod(), requestLine.getUri()); final Controller controller = RequestMapping.getController(request); final HttpResponse response = new HttpResponse(); controller.service(request, response); - log.info("{} {} {}", response.getStatus(), request.requestLine().method(), request.getPath()); + log.info("{} {} {}", response.getStatus(), requestLine.getMethod(), request.getPath()); writeResponse(outputStream, response.build()); } catch (Exception e) { log.error(e.getMessage(), e); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/HttpRequest.java b/tomcat/src/main/java/org/apache/coyote/http11/HttpRequest.java index cb81e3cb53..9967d091d1 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/HttpRequest.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/HttpRequest.java @@ -8,12 +8,19 @@ import java.io.BufferedReader; import java.io.IOException; import java.util.List; +import java.util.stream.Collectors; + +public class HttpRequest { + private final RequestLine requestLine; + private final Headers headers; + private final Body body; + + public HttpRequest(final RequestLine requestLine, final Headers headers, final Body body) { + this.requestLine = requestLine; + this.headers = headers; + this.body = body; + } -public record HttpRequest( - RequestLine requestLine, - Headers headers, - Body body -) { public static HttpRequest parse(final BufferedReader bufferedReader) throws IOException { final var lines = read(bufferedReader); final var requestLine = RequestLine.parse(lines.get(0)); @@ -25,18 +32,30 @@ public static HttpRequest parse(final BufferedReader bufferedReader) throws IOEx private static List read(final BufferedReader bufferedReader) { return bufferedReader.lines() .takeWhile(line -> !line.isBlank()) - .toList(); + .collect(Collectors.toList()); + } + + public RequestLine getRequestLine() { + return requestLine; + } + + public Headers getHeaders() { + return headers; + } + + public Body getBody() { + return body; } public boolean isGet() { - return requestLine.method() == HttpMethod.GET; + return requestLine.getMethod() == HttpMethod.GET; } public boolean isPost() { - return requestLine.method() == HttpMethod.POST; + return requestLine.getMethod() == HttpMethod.POST; } public String getPath() { - return requestLine.uri().getPath(); + return requestLine.getUri().getPath(); } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/body/Body.java b/tomcat/src/main/java/org/apache/coyote/http11/body/Body.java index 06c3e79ed3..1b97414486 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/body/Body.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/body/Body.java @@ -9,11 +9,14 @@ public interface Body { static Body parse(int contentLength, ContentType contentType, final BufferedReader bufferedReader) throws IOException { final char[] body = new char[contentLength]; bufferedReader.read(body, 0, contentLength); - return switch (contentType) { - case FORM_URLENCODED -> FormUrlEncodedBody.parse(String.valueOf(body)); - case NONE -> new EmptyBody(); - default -> throw new IllegalArgumentException("지원하지 않는 Content-Type 입니다."); - }; + switch (contentType) { + case FORM_URLENCODED: + return FormUrlEncodedBody.parse(String.valueOf(body)); + case NONE: + return new EmptyBody(); + default: + throw new IllegalArgumentException("지원하지 않는 Content-Type 입니다."); + } } String getValue(String key); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/header/Headers.java b/tomcat/src/main/java/org/apache/coyote/http11/header/Headers.java index a3cdf0d5ff..4f6b9c3004 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/header/Headers.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/header/Headers.java @@ -4,9 +4,13 @@ import java.util.List; import java.util.Map; -public record Headers( - Map headers -) { +public class Headers { + private final Map headers; + + public Headers(final Map headers) { + this.headers = headers; + } + public static Headers parse(final List headerLines) { final var headers = new EnumMap(HeaderType.class); headerLines.stream() diff --git a/tomcat/src/main/java/org/apache/coyote/http11/header/RequestLine.java b/tomcat/src/main/java/org/apache/coyote/http11/header/RequestLine.java index 179bd1b987..190a406b6b 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/header/RequestLine.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/header/RequestLine.java @@ -2,11 +2,17 @@ import java.net.URI; -public record RequestLine( - HttpMethod method, - URI uri, - HttpVersion version -) { +public class RequestLine { + private final HttpMethod method; + private final URI uri; + private final HttpVersion version; + + public RequestLine(final HttpMethod method, final URI uri, final HttpVersion version) { + this.method = method; + this.uri = uri; + this.version = version; + } + public static RequestLine parse(final String requestLine) { final var tokens = requestLine.split(" "); return new RequestLine( @@ -15,4 +21,16 @@ public static RequestLine parse(final String requestLine) { HttpVersion.of(tokens[2]) ); } + + public HttpMethod getMethod() { + return method; + } + + public URI getUri() { + return uri; + } + + public HttpVersion getVersion() { + return version; + } }