Skip to content

Commit

Permalink
refactor: downgrade jdk version to 11
Browse files Browse the repository at this point in the history
  • Loading branch information
echo724 committed Sep 11, 2023
1 parent 0516102 commit 158a7a1
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 30 deletions.
4 changes: 2 additions & 2 deletions tomcat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
37 changes: 28 additions & 9 deletions tomcat/src/main/java/org/apache/coyote/http11/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -25,18 +32,30 @@ public static HttpRequest parse(final BufferedReader bufferedReader) throws IOEx
private static List<String> 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();
}
}
13 changes: 8 additions & 5 deletions tomcat/src/main/java/org/apache/coyote/http11/body/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import java.util.List;
import java.util.Map;

public record Headers(
Map<HeaderType, String> headers
) {
public class Headers {
private final Map<HeaderType, String> headers;

public Headers(final Map<HeaderType, String> headers) {
this.headers = headers;
}

public static Headers parse(final List<String> headerLines) {
final var headers = new EnumMap<HeaderType, String>(HeaderType.class);
headerLines.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
}

0 comments on commit 158a7a1

Please sign in to comment.