From fb5d66c8973e493645c60be73fa233f0a96dbdd8 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 14:56:27 +0900 Subject: [PATCH 01/12] =?UTF-8?q?feat:=20=EC=8A=A4=EB=A0=88=EB=93=9C?= =?UTF-8?q?=ED=92=80=20=EC=A0=81=EC=9A=A9=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/catalina/connector/Connector.java | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/tomcat/src/main/java/org/apache/catalina/connector/Connector.java b/tomcat/src/main/java/org/apache/catalina/connector/Connector.java index 3b2c4dda7c..753d1f125e 100644 --- a/tomcat/src/main/java/org/apache/catalina/connector/Connector.java +++ b/tomcat/src/main/java/org/apache/catalina/connector/Connector.java @@ -8,6 +8,10 @@ import java.io.UncheckedIOException; import java.net.ServerSocket; import java.net.Socket; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; public class Connector implements Runnable { @@ -15,17 +19,28 @@ public class Connector implements Runnable { private static final int DEFAULT_PORT = 8080; private static final int DEFAULT_ACCEPT_COUNT = 100; + private static final int DEFAULT_MAX_THREAD = 250; private final ServerSocket serverSocket; + private final ExecutorService executorService; private boolean stopped; public Connector() { - this(DEFAULT_PORT, DEFAULT_ACCEPT_COUNT); + this(DEFAULT_PORT, DEFAULT_ACCEPT_COUNT, DEFAULT_MAX_THREAD); } - public Connector(final int port, final int acceptCount) { + public Connector(final int port, + final int acceptCount, + final int maxTread) { this.serverSocket = createServerSocket(port, acceptCount); this.stopped = false; + this.executorService = new ThreadPoolExecutor( + checkMaxThread(maxTread), + checkMaxThread(maxTread), + 0, + TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(acceptCount) + ); } private ServerSocket createServerSocket(final int port, final int acceptCount) { @@ -38,6 +53,10 @@ private ServerSocket createServerSocket(final int port, final int acceptCount) { } } + private int checkMaxThread(int maxThread) { + return Math.max(maxThread, DEFAULT_MAX_THREAD); + } + public void start() { var thread = new Thread(this); thread.setDaemon(true); @@ -67,11 +86,12 @@ private void process(final Socket connection) { return; } var processor = new Http11Processor(connection); - new Thread(processor).start(); + executorService.execute(processor); } public void stop() { stopped = true; + gracefulShutdown(); try { serverSocket.close(); } catch (IOException e) { @@ -92,4 +112,24 @@ private int checkPort(final int port) { private int checkAcceptCount(final int acceptCount) { return Math.max(acceptCount, DEFAULT_ACCEPT_COUNT); } + + private void gracefulShutdown() { + executorService.shutdown(); // Disable new tasks from being submitted + try { + // Wait a while for existing tasks to terminate + if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) { + executorService.shutdownNow(); // Cancel currently executing tasks + // Wait a while for tasks to respond to being cancelled + if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) { + log.error("Pool did not terminate"); + } + } + } catch (InterruptedException ie) { + // (Re-)Cancel if current thread also interrupted + executorService.shutdownNow(); + // Preserve interrupt status + Thread.currentThread().interrupt(); + } + } + } From ee59aff3dcba3d72852d0211f5683b5d5d4e36cd Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 15:00:01 +0900 Subject: [PATCH 02/12] =?UTF-8?q?refactor:=20=EC=84=B8=EC=85=98=EB=A9=94?= =?UTF-8?q?=EB=8B=88=EC=A0=80=20=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/apache/catalina/Manager.java | 11 +++---- .../catalina/session/SessionManager.java | 32 +++++++++++++++++++ .../coyote/http11/session/SessionManager.java | 26 --------------- 3 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 tomcat/src/main/java/org/apache/catalina/session/SessionManager.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java diff --git a/tomcat/src/main/java/org/apache/catalina/Manager.java b/tomcat/src/main/java/org/apache/catalina/Manager.java index e69410f6a9..463b901b85 100644 --- a/tomcat/src/main/java/org/apache/catalina/Manager.java +++ b/tomcat/src/main/java/org/apache/catalina/Manager.java @@ -1,8 +1,7 @@ package org.apache.catalina; -import jakarta.servlet.http.HttpSession; - import java.io.IOException; +import org.apache.catalina.session.HttpSession; /** * A Manager manages the pool of Sessions that are associated with a @@ -27,9 +26,9 @@ public interface Manager { /** * Add this Session to the set of active Sessions for this Manager. * - * @param session Session to be added + * @param httpSession Session to be added */ - void add(HttpSession session); + void add(HttpSession httpSession); /** * Return the active Session, associated with this Manager, with the @@ -50,7 +49,7 @@ public interface Manager { /** * Remove this Session from the active Sessions for this Manager. * - * @param session Session to be removed + * @param httpSession Session to be removed */ - void remove(HttpSession session); + void remove(HttpSession httpSession); } diff --git a/tomcat/src/main/java/org/apache/catalina/session/SessionManager.java b/tomcat/src/main/java/org/apache/catalina/session/SessionManager.java new file mode 100644 index 0000000000..3b4dae71b0 --- /dev/null +++ b/tomcat/src/main/java/org/apache/catalina/session/SessionManager.java @@ -0,0 +1,32 @@ +package org.apache.catalina.session; + +import org.apache.catalina.Manager; + +import java.util.HashMap; +import java.util.Map; + +public class SessionManager implements Manager { + + // static! + private static final Map sessions = new HashMap<>(); + + @Override + public void add(HttpSession httpSession) { + sessions.put(httpSession.getId(), httpSession); + } + + @Override + public HttpSession findSession(final String id) { + return sessions.get(id); + } + + @Override + public void remove(HttpSession httpSession) { + sessions.remove(httpSession.getId()); + } + + public static boolean isExist(final String id) { + return sessions.containsKey(id); + } + +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java b/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java deleted file mode 100644 index 290d79a599..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/session/SessionManager.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.apache.coyote.http11.session; - -import java.util.HashMap; -import java.util.Map; - -public class SessionManager { - - // static! - private static final Map sessions = new HashMap<>(); - - private SessionManager() { - } - - public static void add(final Session session) { - sessions.put(session.getId(), session); - } - - public static Session findSession(final String id) { - return sessions.get(id); - } - - public static boolean isExist(final String id) { - return sessions.containsKey(id); - } - -} From 13190d34e16b277e238ec0c60e63e94f7a6ee4d3 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 15:02:46 +0900 Subject: [PATCH 03/12] =?UTF-8?q?refactor:=20httpResponse=20=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coyote/http11/response/HttpResponse.java | 62 +++++++++---------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/HttpResponse.java b/tomcat/src/main/java/org/apache/coyote/http11/response/HttpResponse.java index 7a2d435398..a660dce8c9 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/HttpResponse.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/HttpResponse.java @@ -1,33 +1,19 @@ package org.apache.coyote.http11.response; -import org.apache.coyote.http11.ContentType; - -import java.util.LinkedHashMap; -import java.util.Map; import java.util.stream.Collectors; public class HttpResponse { - private final StatusCode statusCode; + private StatusCode statusCode; private final ResponseHeader header; - private final String responseBody; - - public HttpResponse(final StatusCode statusCode, - final ContentType contentType, - final String responseBody) { - this.statusCode = statusCode; - this.responseBody = responseBody; + private String responseBody = ""; - Map newHeader = new LinkedHashMap<>(); - newHeader.put("Content-Type", contentType.getContentType() + ";charset=utf-8"); - newHeader.put("Content-Length", String.valueOf(responseBody.getBytes().length)); - header = new ResponseHeader(newHeader); + public HttpResponse() { + header = new ResponseHeader(); } - public HttpResponse(final StatusCode statusCode) { - this.statusCode = statusCode; - this.responseBody = ""; - header = new ResponseHeader(new LinkedHashMap<>()); + public void setResponseBody(final String responseBody) { + this.responseBody = responseBody; } public String getResponse() { @@ -37,23 +23,35 @@ public String getResponse() { responseBody); } - public String getRedirectResponse(String redirectUrl) { - header.addHeader("Location", redirectUrl); - return String.join("\r\n", - "HTTP/1.1 " + statusCode.getCode() + " " + statusCode.getMessage() + " ", - printHeader() + "\n", - "Location: " + redirectUrl + " \r/n", - "", - ""); + public String printHeader() { + return header.getHeader().entrySet().stream() + .map(entry -> entry.getKey() + ": " + entry.getValue() + " \r") + .collect(Collectors.joining(System.lineSeparator())); + } + + public void addHeader(final String key, + final String value) { + this.header.addHeader(key, value); } public void addJSessionId(final String JSessionId) { header.addHeader("Set-Cookie", "JSESSIONID=" + JSessionId); } - public String printHeader() { - return header.getHeader().entrySet().stream() - .map(entry -> entry.getKey() + ": " + entry.getValue() + " \r") - .collect(Collectors.joining(System.lineSeparator())); + public StatusCode getStatusCode() { + return statusCode; + } + + public ResponseHeader getHeader() { + return header; } + + public String getResponseBody() { + return responseBody; + } + + public void setStatusCode(final StatusCode statusCode) { + this.statusCode = statusCode; + } + } From 7bb1b415d065ba00e03310fe91cb59979184a614 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 15:03:11 +0900 Subject: [PATCH 04/12] =?UTF-8?q?refactor:=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Session.java => catalina/session/HttpSession.java} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename tomcat/src/main/java/org/apache/{coyote/http11/session/Session.java => catalina/session/HttpSession.java} (84%) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java b/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java similarity index 84% rename from tomcat/src/main/java/org/apache/coyote/http11/session/Session.java rename to tomcat/src/main/java/org/apache/catalina/session/HttpSession.java index 7bdb9cd092..88c78ec98d 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/session/Session.java +++ b/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java @@ -1,15 +1,15 @@ -package org.apache.coyote.http11.session; +package org.apache.catalina.session; import java.util.HashMap; import java.util.Map; import java.util.UUID; -public class Session { +public class HttpSession { private final String id; private final Map values = new HashMap<>(); - public Session() { + public HttpSession() { this.id = UUID.randomUUID().toString(); } From 8cee023f66dca018161a0699843e36fb5ea18380 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 15:03:29 +0900 Subject: [PATCH 05/12] =?UTF-8?q?refactor:=20responseHeader=20=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apache/coyote/http11/response/ResponseHeader.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHeader.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHeader.java index 2516df0422..f4b6ecbe56 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHeader.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHeader.java @@ -1,13 +1,14 @@ package org.apache.coyote.http11.response; +import java.util.HashMap; import java.util.Map; public class ResponseHeader { private final Map header; - public ResponseHeader(final Map header) { - this.header = header; + public ResponseHeader() { + this.header = new HashMap<>(); } public void addHeader(String key, String value) { From f34b147e27a435ac406ff0232d3f69e9f715c5ec Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 15:05:03 +0900 Subject: [PATCH 06/12] =?UTF-8?q?refactor:=20controller=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=B0=8F=20=EC=A0=81=EC=9A=A9=20=EB=A6=AC=ED=8C=A9=ED=84=B0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++ .../apache/coyote/http11/Http11Processor.java | 14 ++++-- .../coyote/http11/RequestMappingHandler.java | 26 +++++------ .../coyote/http11/response/Controller.java | 10 +++++ .../http11/response/FileGetController.java | 20 +++++++++ .../http11/response/FileGetResponseMaker.java | 20 --------- .../http11/response/HelloController.java | 20 +++++++++ .../http11/response/HelloResponseMaker.java | 14 ------ .../http11/response/LoginGetController.java | 29 ++++++++++++ .../response/LoginGetResponseMaker.java | 25 ----------- .../http11/response/LoginPostController.java | 43 ++++++++++++++++++ .../response/LoginPostResponseMaker.java | 44 ------------------- .../response/RegisterGetController.java | 20 +++++++++ .../response/RegisterGetResponseMaker.java | 19 -------- ...Maker.java => RegisterPostController.java} | 9 ++-- .../http11/response/ResourceResolver.java | 29 ++++++++++++ .../coyote/http11/response/ResponseMaker.java | 19 -------- 17 files changed, 203 insertions(+), 162 deletions(-) create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/Controller.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/FileGetController.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/FileGetResponseMaker.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/HelloResponseMaker.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetResponseMaker.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostResponseMaker.java create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/RegisterGetController.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/RegisterGetResponseMaker.java rename tomcat/src/main/java/org/apache/coyote/http11/response/{RegisterPostResponseMaker.java => RegisterPostController.java} (70%) create mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/ResourceResolver.java delete mode 100644 tomcat/src/main/java/org/apache/coyote/http11/response/ResponseMaker.java diff --git a/README.md b/README.md index d4afd1e7c9..9c3802b3d2 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,7 @@ - [x] Session 구현하기 - [x] 로그인된 상태에서 /login 페이지에 HTTP GET method로 접근하면 이미 로그인한 상태니 index.html 페이지로 리다이렉트 처리한다. - [x] 로그인에 성공하면 Session 객체의 값으로 User 객체를 저장해보자. + +## step3 +- [x] 요청 응답의 구조를 공부하여 코드를 리팩터링 한다. +- [x] 스레드풀을 적용시킨다. 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 89d3b48564..90ccda685b 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -3,7 +3,8 @@ import nextstep.jwp.exception.UncheckedServletException; import org.apache.coyote.Processor; import org.apache.coyote.http11.request.HttpRequest; -import org.apache.coyote.http11.response.ResponseMaker; +import org.apache.coyote.http11.response.Controller; +import org.apache.coyote.http11.response.HttpResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,11 +36,12 @@ public void process(final Socket connection) { final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { final HttpRequest httpRequest = new HttpRequest(reader); + final HttpResponse httpResponse = new HttpResponse(); - final ResponseMaker responseMaker = RequestMappingHandler.findResponseMaker(httpRequest); - final String response = responseMaker.createResponse(httpRequest); + final Controller controller = RequestMappingHandler.findController(httpRequest); + controller.service(httpRequest, httpResponse); - outputStream.write(response.getBytes()); + outputStream.write(httpResponse.getResponse().getBytes()); outputStream.flush(); } catch (IOException | UncheckedServletException e) { @@ -48,4 +50,8 @@ public void process(final Socket connection) { throw new RuntimeException(e); } } + + // 쿠키값 빼내올때 문제점 + // doPost, doGet 추가수정 + // 세션 전략 세우기 } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/RequestMappingHandler.java b/tomcat/src/main/java/org/apache/coyote/http11/RequestMappingHandler.java index 4934b84c2c..1c51b5f829 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/RequestMappingHandler.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/RequestMappingHandler.java @@ -10,24 +10,24 @@ public enum RequestMappingHandler { - STRING(RequestMappingHandler::isStringGetUrl, new HelloResponseMaker()), - FILE(RequestMappingHandler::isFileGetUrl, new FileGetResponseMaker()), - LOGIN_GET(RequestMappingHandler::isLoginGetUrl, new LoginGetResponseMaker()), - LOGIN_POST(RequestMappingHandler::isLoginPostUrl, new LoginPostResponseMaker()), - REGISTER_GET(RequestMappingHandler::isRegisterGetUrl, new RegisterGetResponseMaker()), - REGISTER_POST(RequestMappingHandler::isRegisterPostUrl, new RegisterPostResponseMaker()); + STRING(RequestMappingHandler::isStringGetUrl, new HelloController()), + FILE(RequestMappingHandler::isFileGetUrl, new FileGetController()), + LOGIN_GET(RequestMappingHandler::isLoginGetUrl, new LoginGetController()), + LOGIN_POST(RequestMappingHandler::isLoginPostUrl, new LoginPostController()), + REGISTER_GET(RequestMappingHandler::isRegisterGetUrl, new RegisterGetController()), + REGISTER_POST(RequestMappingHandler::isRegisterPostUrl, new RegisterPostController()); private static final Pattern FILE_REGEX = Pattern.compile(".+\\.(html|css|js|ico)"); private final BiPredicate condition; - private final ResponseMaker responseMaker; + private final Controller controller; - RequestMappingHandler(final BiPredicate condition, final ResponseMaker responseMaker) { + RequestMappingHandler(final BiPredicate condition, final Controller controller) { this.condition = condition; - this.responseMaker = responseMaker; + this.controller = controller; } - public static ResponseMaker findResponseMaker(final HttpRequest request) { + public static Controller findController(final HttpRequest request) { String resourcePath = request.getRequestLine().getRequestUrl(); HttpMethod requestMethod = request.getRequestLine().getHttpMethod(); @@ -35,7 +35,7 @@ public static ResponseMaker findResponseMaker(final HttpRequest request) { .filter(value -> value.condition.test(resourcePath, requestMethod)) .findFirst() .orElseThrow(() -> new IllegalArgumentException("잘못된 url 요청입니다.")) - .getResponseMaker(); + .getController(); } public static boolean isFileGetUrl(final String resourcePath, final HttpMethod requestMethod) { @@ -62,8 +62,8 @@ public static boolean isRegisterPostUrl(final String requestUrl, final HttpMetho return requestUrl.startsWith("/register") && requestMethod == HttpMethod.POST; } - public ResponseMaker getResponseMaker() { - return responseMaker; + public Controller getController() { + return controller; } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/Controller.java b/tomcat/src/main/java/org/apache/coyote/http11/response/Controller.java new file mode 100644 index 0000000000..c72e2a3502 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/Controller.java @@ -0,0 +1,10 @@ +package org.apache.coyote.http11.response; + +import org.apache.coyote.http11.request.HttpRequest; + +import java.io.IOException; + +public interface Controller { + void service(final HttpRequest request, + final HttpResponse response) throws IOException; +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/FileGetController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/FileGetController.java new file mode 100644 index 0000000000..e4a5db8176 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/FileGetController.java @@ -0,0 +1,20 @@ +package org.apache.coyote.http11.response; + +import org.apache.coyote.http11.ContentType; +import org.apache.coyote.http11.request.HttpRequest; + +public class FileGetController implements Controller { + + @Override + public void service(final HttpRequest request, + final HttpResponse response) { + final String resourcePath = request.getRequestLine().getRequestUrl(); + final String responseBody = ResourceResolver.resolve(resourcePath); + + response.setStatusCode(StatusCode.OK); + response.setResponseBody(ResourceResolver.resolve(resourcePath)); + response.addHeader("Content-Type", ContentType.from(resourcePath).getContentType() + ";charset=utf-8"); + response.addHeader("Content-Length",String.valueOf(responseBody.getBytes().length)); + } + +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/FileGetResponseMaker.java b/tomcat/src/main/java/org/apache/coyote/http11/response/FileGetResponseMaker.java deleted file mode 100644 index 221c821789..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/FileGetResponseMaker.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.apache.coyote.http11.response; - -import org.apache.coyote.http11.ContentType; -import org.apache.coyote.http11.request.HttpRequest; - -import java.io.IOException; - -import static java.nio.charset.StandardCharsets.UTF_8; - -public class FileGetResponseMaker extends ResponseMaker { - - @Override - public String createResponse(final HttpRequest request) throws IOException { - final String resourcePath = request.getRequestLine().getRequestUrl(); - - final HttpResponse httpResponse = new HttpResponse(StatusCode.OK, ContentType.from(resourcePath), new String(getResponseBodyBytes(resourcePath), UTF_8)); - return httpResponse.getResponse(); - } - -} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java new file mode 100644 index 0000000000..06b4370009 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java @@ -0,0 +1,20 @@ +package org.apache.coyote.http11.response; + +import org.apache.coyote.http11.ContentType; +import org.apache.coyote.http11.request.HttpRequest; + +public class HelloController implements Controller { + + @Override + public void service(final HttpRequest request, + final HttpResponse response) { + final String resourcePath = request.getRequestLine().getRequestUrl(); + final String responseBody = "Hello world!"; + + response.setStatusCode(StatusCode.OK); + response.setResponseBody(ResourceResolver.resolve(resourcePath)); + response.addHeader("Content-Type", ContentType.from(resourcePath).getContentType() + ";charset=utf-8"); + response.addHeader("Content-Length", String.valueOf(responseBody.getBytes().length)); + } + +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/HelloResponseMaker.java b/tomcat/src/main/java/org/apache/coyote/http11/response/HelloResponseMaker.java deleted file mode 100644 index ae2e5d5bcf..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/HelloResponseMaker.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.apache.coyote.http11.response; - -import org.apache.coyote.http11.ContentType; -import org.apache.coyote.http11.request.HttpRequest; - -public class HelloResponseMaker extends ResponseMaker { - - @Override - public String createResponse(final HttpRequest request) { - final var responseBody = "Hello world!"; - final HttpResponse httpResponse = new HttpResponse(StatusCode.OK, ContentType.HTML, responseBody); - return httpResponse.getResponse(); - } -} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java new file mode 100644 index 0000000000..25bbbfac0b --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java @@ -0,0 +1,29 @@ +package org.apache.coyote.http11.response; + +import org.apache.coyote.http11.ContentType; +import org.apache.coyote.http11.request.HttpRequest; +import org.apache.catalina.session.SessionManager; + +public class LoginGetController implements Controller { + + @Override + public void service(final HttpRequest request, + final HttpResponse response) { + final String resourcePath = request.getRequestLine().getRequestUrl() + ".html"; + final String responseBody = ResourceResolver.resolve(resourcePath); + + if (request.hasJSessionId() && (SessionManager.isExist(request.getJSessionId()))) { + response.setStatusCode(StatusCode.OK); + response.setResponseBody(ResourceResolver.resolve("/index.html")); + response.addHeader("Content-Type", ContentType.from("/index.html").getContentType() + ";charset=utf-8"); + response.addHeader("Content-Length", String.valueOf(ResourceResolver.resolve("/index.html").getBytes().length)); + return; + } + + response.setStatusCode(StatusCode.OK); + response.setResponseBody(ResourceResolver.resolve(resourcePath)); + response.addHeader("Content-Type", ContentType.from(resourcePath).getContentType() + ";charset=utf-8"); + response.addHeader("Content-Length", String.valueOf(responseBody.getBytes().length)); + } + +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetResponseMaker.java b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetResponseMaker.java deleted file mode 100644 index 67960a547e..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetResponseMaker.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.apache.coyote.http11.response; - -import org.apache.coyote.http11.ContentType; -import org.apache.coyote.http11.request.HttpRequest; -import org.apache.coyote.http11.session.SessionManager; - -import java.io.IOException; - -import static java.nio.charset.StandardCharsets.UTF_8; - -public class LoginGetResponseMaker extends ResponseMaker { - - @Override - public String createResponse(final HttpRequest request) throws IOException { - if (request.hasJSessionId() && (SessionManager.isExist(request.getJSessionId()))) { - final HttpResponse httpResponse = new HttpResponse(StatusCode.OK, ContentType.from("/index.html"), new String(getResponseBodyBytes("/index.html"), UTF_8)); - return httpResponse.getResponse(); - - } - - final String resourcePath = request.getRequestLine().getRequestUrl() + ".html"; - final HttpResponse httpResponse = new HttpResponse(StatusCode.OK, ContentType.from(resourcePath), new String(getResponseBodyBytes(resourcePath), UTF_8)); - return httpResponse.getResponse(); - } -} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java new file mode 100644 index 0000000000..a20f7023f8 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java @@ -0,0 +1,43 @@ +package org.apache.coyote.http11.response; + +import nextstep.jwp.LoginHandler; +import org.apache.catalina.session.HttpSession; +import org.apache.coyote.http11.ContentType; +import org.apache.coyote.http11.request.HttpRequest; + +public class LoginPostController implements Controller { + + @Override + public void service(final HttpRequest request, + final HttpResponse response) { + final LoginHandler loginHandler = new LoginHandler(); + if (loginHandler.login(request.getRequestBody())) { + successLoginResponse(request, response); + return; + } + failLoginResponse(response); + } + + private void successLoginResponse(final HttpRequest request, + final HttpResponse response) { + response.setStatusCode(StatusCode.FOUND); + + if (!request.hasJSessionId()) { + final HttpSession httpSession = new HttpSession(); + final LoginHandler loginHandler = new LoginHandler(); + httpSession.addAttribute("user", loginHandler.getUser(request.getRequestBody())); + httpSession.addAttribute(httpSession.getId(), httpSession); + + response.addJSessionId(httpSession.getId()); + } + response.addHeader("Location", "/index.html"); + } + + private void failLoginResponse(final HttpResponse response) { + response.setStatusCode(StatusCode.UNAUTHORIZED); + response.setResponseBody(ResourceResolver.resolve("/401.html")); + response.addHeader("Content-Type", ContentType.from("/401.html").getContentType() + ";charset=utf-8"); + response.addHeader("Content-Length", String.valueOf(ResourceResolver.resolve("/401.html").getBytes().length)); + } + +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostResponseMaker.java b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostResponseMaker.java deleted file mode 100644 index 734f0aa566..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostResponseMaker.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.apache.coyote.http11.response; - -import nextstep.jwp.LoginHandler; -import org.apache.coyote.http11.ContentType; -import org.apache.coyote.http11.request.HttpRequest; -import org.apache.coyote.http11.session.Session; -import org.apache.coyote.http11.session.SessionManager; - -import java.io.IOException; - -import static java.nio.charset.StandardCharsets.UTF_8; - -public class LoginPostResponseMaker extends ResponseMaker { - - @Override - public String createResponse(final HttpRequest request) throws IOException { - final LoginHandler loginHandler = new LoginHandler(); - if (loginHandler.login(request.getRequestBody())) { - return successLoginResponse(request); - } - - return failLoginResponse(); - } - - private String successLoginResponse(final HttpRequest request) { - final HttpResponse httpResponse = new HttpResponse(StatusCode.FOUND); - if (!request.hasJSessionId()) { - final Session session = new Session(); - final LoginHandler loginHandler = new LoginHandler(); - session.addAttribute("user", loginHandler.getUser(request.getRequestBody())); - SessionManager.add(session); - - httpResponse.addJSessionId(session.getId()); - } - - return httpResponse.getRedirectResponse("/index.html"); - } - - private String failLoginResponse() throws IOException { - final HttpResponse httpResponse = new HttpResponse(StatusCode.UNAUTHORIZED, ContentType.HTML, new String(getResponseBodyBytes("/401.html"), UTF_8)); - return httpResponse.getResponse(); - } - -} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterGetController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterGetController.java new file mode 100644 index 0000000000..71c3d15127 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterGetController.java @@ -0,0 +1,20 @@ +package org.apache.coyote.http11.response; + +import org.apache.coyote.http11.ContentType; +import org.apache.coyote.http11.request.HttpRequest; + +public class RegisterGetController implements Controller { + + @Override + public void service(final HttpRequest request, + final HttpResponse response) { + final String resourcePath = request.getRequestLine().getRequestUrl() + ".html"; + final String responseBody = ResourceResolver.resolve(resourcePath); + + response.setStatusCode(StatusCode.OK); + response.setResponseBody(ResourceResolver.resolve(resourcePath)); + response.addHeader("Content-Type", ContentType.from(resourcePath).getContentType() + ";charset=utf-8"); + response.addHeader("Content-Length", String.valueOf(responseBody.getBytes().length)); + } + +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterGetResponseMaker.java b/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterGetResponseMaker.java deleted file mode 100644 index 073db96e90..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterGetResponseMaker.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.apache.coyote.http11.response; - -import org.apache.coyote.http11.ContentType; -import org.apache.coyote.http11.request.HttpRequest; - -import java.io.IOException; - -import static java.nio.charset.StandardCharsets.UTF_8; - -public class RegisterGetResponseMaker extends ResponseMaker { - - @Override - public String createResponse(final HttpRequest request) throws IOException { - final String resourcePath = request.getRequestLine().getRequestUrl() + ".html"; - final HttpResponse httpResponse = new HttpResponse(StatusCode.OK, ContentType.from(resourcePath), new String(getResponseBodyBytes(resourcePath), UTF_8)); - return httpResponse.getResponse(); - } - -} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterPostResponseMaker.java b/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterPostController.java similarity index 70% rename from tomcat/src/main/java/org/apache/coyote/http11/response/RegisterPostResponseMaker.java rename to tomcat/src/main/java/org/apache/coyote/http11/response/RegisterPostController.java index fe0c93665a..7ac7366b1a 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterPostResponseMaker.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/RegisterPostController.java @@ -5,14 +5,15 @@ import org.apache.coyote.http11.QueryParams; import org.apache.coyote.http11.request.HttpRequest; -public class RegisterPostResponseMaker extends ResponseMaker { +public class RegisterPostController implements Controller { @Override - public String createResponse(final HttpRequest request) { + public void service(final HttpRequest request, + final HttpResponse response) { saveMemberFromQuery(request); - final HttpResponse httpResponse = new HttpResponse(StatusCode.FOUND); - return httpResponse.getRedirectResponse("/index.html"); + response.setStatusCode(StatusCode.FOUND); + response.addHeader("Location", "/index.html"); } private void saveMemberFromQuery(final HttpRequest request) { diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResourceResolver.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResourceResolver.java new file mode 100644 index 0000000000..d136b63568 --- /dev/null +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResourceResolver.java @@ -0,0 +1,29 @@ +package org.apache.coyote.http11.response; + +import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.net.URL; +import java.nio.file.Files; + +public class ResourceResolver { + + private static final ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + private static final String STATIC_PATH = "static"; + + private ResourceResolver() { + } + + public static String resolve(String path) { + final URL resource = classLoader.getResource(STATIC_PATH + path); + if (resource == null) { + throw new IllegalArgumentException("해당 리소스를 찾을 수 없습니다."); + } + final File file = new File(resource.getFile()); + try { + return new String(Files.readAllBytes(file.toPath())); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +} diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseMaker.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseMaker.java deleted file mode 100644 index ae325fd1c6..0000000000 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseMaker.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.apache.coyote.http11.response; - -import org.apache.coyote.http11.request.HttpRequest; - -import java.io.IOException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Paths; - -public abstract class ResponseMaker { - - public abstract String createResponse(final HttpRequest request) throws IOException; - - protected byte[] getResponseBodyBytes(String resourcePath) throws IOException { - final URL fileUrl = this.getClass().getClassLoader().getResource("static" + resourcePath); - return Files.readAllBytes(Paths.get(fileUrl.getPath())); - } - -} From f6004556a96b666c7b99eeaf3c188be0305f7532 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 16:05:59 +0900 Subject: [PATCH 07/12] =?UTF-8?q?refactor:=20session=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/apache/catalina/session/HttpSession.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java b/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java index 88c78ec98d..e31217cf4f 100644 --- a/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java +++ b/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java @@ -7,10 +7,12 @@ public class HttpSession { private final String id; - private final Map values = new HashMap<>(); + private final Map values; - public HttpSession() { + public HttpSession(final String name, final Object value) { this.id = UUID.randomUUID().toString(); + values = new HashMap<>(); + values.put(name, value); } public String getId() { From c8588ee730527f2430ac9e35cbc3285bb0a4f2c5 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 16:07:04 +0900 Subject: [PATCH 08/12] =?UTF-8?q?refactor:=20session=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20=EA=B3=BC=EC=A0=95=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../http11/response/LoginGetController.java | 2 +- .../http11/response/LoginPostController.java | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java index 25bbbfac0b..56a9757bf2 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java @@ -12,7 +12,7 @@ public void service(final HttpRequest request, final String resourcePath = request.getRequestLine().getRequestUrl() + ".html"; final String responseBody = ResourceResolver.resolve(resourcePath); - if (request.hasJSessionId() && (SessionManager.isExist(request.getJSessionId()))) { + if (request.hasJSessionId() && SessionManager.isExist(request.getJSessionId())) { response.setStatusCode(StatusCode.OK); response.setResponseBody(ResourceResolver.resolve("/index.html")); response.addHeader("Content-Type", ContentType.from("/index.html").getContentType() + ";charset=utf-8"); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java index a20f7023f8..fbf89e651a 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java @@ -1,7 +1,9 @@ package org.apache.coyote.http11.response; import nextstep.jwp.LoginHandler; +import nextstep.jwp.model.User; import org.apache.catalina.session.HttpSession; +import org.apache.catalina.session.SessionManager; import org.apache.coyote.http11.ContentType; import org.apache.coyote.http11.request.HttpRequest; @@ -20,16 +22,17 @@ public void service(final HttpRequest request, private void successLoginResponse(final HttpRequest request, final HttpResponse response) { - response.setStatusCode(StatusCode.FOUND); - + final SessionManager sessionManager = new SessionManager(); if (!request.hasJSessionId()) { - final HttpSession httpSession = new HttpSession(); final LoginHandler loginHandler = new LoginHandler(); - httpSession.addAttribute("user", loginHandler.getUser(request.getRequestBody())); - httpSession.addAttribute(httpSession.getId(), httpSession); + final User user = loginHandler.getUser(request.getRequestBody()); + final HttpSession httpSession = new HttpSession("user", user); + sessionManager.add(httpSession); - response.addJSessionId(httpSession.getId()); + final HttpSession session = sessionManager.findSession(httpSession.getId()); + response.addJSessionId(session.getId()); } + response.setStatusCode(StatusCode.FOUND); response.addHeader("Location", "/index.html"); } From 159200d150317260bccb9395fb0f7540d8ae2fc1 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 16:22:08 +0900 Subject: [PATCH 09/12] =?UTF-8?q?refactor:=20index=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/org/apache/coyote/http11/ContentType.java | 4 ++++ .../org/apache/coyote/http11/response/HelloController.java | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/ContentType.java b/tomcat/src/main/java/org/apache/coyote/http11/ContentType.java index 2a200155a7..bccde8365a 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/ContentType.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/ContentType.java @@ -36,4 +36,8 @@ private boolean isSameExtension(String extension) { public String getContentType() { return contentType; } + + public String getExtension() { + return extension; + } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java index 06b4370009..16b5bb4e97 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java @@ -8,12 +8,11 @@ public class HelloController implements Controller { @Override public void service(final HttpRequest request, final HttpResponse response) { - final String resourcePath = request.getRequestLine().getRequestUrl(); final String responseBody = "Hello world!"; response.setStatusCode(StatusCode.OK); - response.setResponseBody(ResourceResolver.resolve(resourcePath)); - response.addHeader("Content-Type", ContentType.from(resourcePath).getContentType() + ";charset=utf-8"); + response.setResponseBody(responseBody); + response.addHeader("Content-Type", ContentType.HTML.getExtension() + ";charset=utf-8"); response.addHeader("Content-Length", String.valueOf(responseBody.getBytes().length)); } From 0ed222c36b4f0e99821f2e22592305a4e5151fbe Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Mon, 11 Sep 2023 16:26:52 +0900 Subject: [PATCH 10/12] =?UTF-8?q?refactor:=20test=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/org/apache/coyote/http11/ContentType.java | 3 --- .../org/apache/coyote/http11/response/HelloController.java | 2 +- .../org/apache/coyote/http11/response/ResponseHeader.java | 4 ++-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/ContentType.java b/tomcat/src/main/java/org/apache/coyote/http11/ContentType.java index bccde8365a..678391d8cd 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/ContentType.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/ContentType.java @@ -37,7 +37,4 @@ public String getContentType() { return contentType; } - public String getExtension() { - return extension; - } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java index 16b5bb4e97..d045b87509 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/HelloController.java @@ -12,7 +12,7 @@ public void service(final HttpRequest request, response.setStatusCode(StatusCode.OK); response.setResponseBody(responseBody); - response.addHeader("Content-Type", ContentType.HTML.getExtension() + ";charset=utf-8"); + response.addHeader("Content-Type", ContentType.HTML.getContentType() + ";charset=utf-8"); response.addHeader("Content-Length", String.valueOf(responseBody.getBytes().length)); } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHeader.java b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHeader.java index f4b6ecbe56..3df30239a2 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHeader.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/ResponseHeader.java @@ -1,6 +1,6 @@ package org.apache.coyote.http11.response; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; public class ResponseHeader { @@ -8,7 +8,7 @@ public class ResponseHeader { private final Map header; public ResponseHeader() { - this.header = new HashMap<>(); + this.header = new LinkedHashMap<>(); } public void addHeader(String key, String value) { From 93e171bd13d973bd8053e61b7d9c8502e970fc68 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Thu, 14 Sep 2023 17:45:12 +0900 Subject: [PATCH 11/12] =?UTF-8?q?refactor:=20=EB=8F=99=EC=8B=9C=EC=84=B1?= =?UTF-8?q?=20=EC=BB=AC=EB=A0=89=EC=85=98=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/apache/catalina/session/HttpSession.java | 6 +++--- .../java/org/apache/catalina/session/SessionManager.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java b/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java index e31217cf4f..043b1692b1 100644 --- a/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java +++ b/tomcat/src/main/java/org/apache/catalina/session/HttpSession.java @@ -1,8 +1,8 @@ package org.apache.catalina.session; -import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; public class HttpSession { @@ -11,8 +11,8 @@ public class HttpSession { public HttpSession(final String name, final Object value) { this.id = UUID.randomUUID().toString(); - values = new HashMap<>(); - values.put(name, value); + values = new ConcurrentHashMap<>(); + addAttribute(name, value); } public String getId() { diff --git a/tomcat/src/main/java/org/apache/catalina/session/SessionManager.java b/tomcat/src/main/java/org/apache/catalina/session/SessionManager.java index 3b4dae71b0..ea86496e75 100644 --- a/tomcat/src/main/java/org/apache/catalina/session/SessionManager.java +++ b/tomcat/src/main/java/org/apache/catalina/session/SessionManager.java @@ -2,13 +2,13 @@ import org.apache.catalina.Manager; -import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class SessionManager implements Manager { // static! - private static final Map sessions = new HashMap<>(); + private static final Map sessions = new ConcurrentHashMap<>(); @Override public void add(HttpSession httpSession) { From ac5393979f42ef6ed8f0fd433e0d2028457eac57 Mon Sep 17 00:00:00 2001 From: chaewon121 Date: Thu, 14 Sep 2023 23:49:05 +0900 Subject: [PATCH 12/12] =?UTF-8?q?fix:=20session=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RequestMappingHandler.java | 32 ++++++++++++++++--- .../apache/coyote/http11/Http11Processor.java | 4 +-- .../coyote/http11/request/HttpRequest.java | 14 ++++++++ .../coyote/http11/request/RequestHeader.java | 4 +++ .../http11/response/LoginGetController.java | 12 +++---- .../http11/response/LoginPostController.java | 15 ++------- 6 files changed, 54 insertions(+), 27 deletions(-) rename tomcat/src/main/java/org/apache/{coyote/http11 => catalina}/RequestMappingHandler.java (68%) diff --git a/tomcat/src/main/java/org/apache/coyote/http11/RequestMappingHandler.java b/tomcat/src/main/java/org/apache/catalina/RequestMappingHandler.java similarity index 68% rename from tomcat/src/main/java/org/apache/coyote/http11/RequestMappingHandler.java rename to tomcat/src/main/java/org/apache/catalina/RequestMappingHandler.java index 1c51b5f829..35c423044a 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/RequestMappingHandler.java +++ b/tomcat/src/main/java/org/apache/catalina/RequestMappingHandler.java @@ -1,5 +1,9 @@ -package org.apache.coyote.http11; +package org.apache.catalina; +import nextstep.jwp.LoginHandler; +import nextstep.jwp.model.User; +import org.apache.catalina.session.HttpSession; +import org.apache.catalina.session.SessionManager; import org.apache.coyote.http11.request.HttpMethod; import org.apache.coyote.http11.request.HttpRequest; import org.apache.coyote.http11.response.*; @@ -28,14 +32,34 @@ public enum RequestMappingHandler { } public static Controller findController(final HttpRequest request) { - String resourcePath = request.getRequestLine().getRequestUrl(); - HttpMethod requestMethod = request.getRequestLine().getHttpMethod(); + final String resourcePath = request.getRequestLine().getRequestUrl(); + final HttpMethod requestMethod = request.getRequestLine().getHttpMethod(); - return Arrays.stream(values()) + final Controller findController = Arrays.stream(values()) .filter(value -> value.condition.test(resourcePath, requestMethod)) .findFirst() .orElseThrow(() -> new IllegalArgumentException("잘못된 url 요청입니다.")) .getController(); + + if (findController instanceof LoginPostController + && (!request.hasJSessionId())) { + addSession(request); + } + if (findController instanceof LoginGetController + && request.hasJSessionId() + && SessionManager.isExist(request.getJSessionId())) { + request.setHasValidatedSessionTrue(); + } + return findController; + } + + private static void addSession(final HttpRequest request) { + final SessionManager sessionManager = new SessionManager(); + final LoginHandler loginHandler = new LoginHandler(); + final User user = loginHandler.getUser(request.getRequestBody()); + final HttpSession httpSession = new HttpSession("user", user); + sessionManager.add(httpSession); + request.addJSessionId(httpSession.getId()); } public static boolean isFileGetUrl(final String resourcePath, final HttpMethod requestMethod) { 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 90ccda685b..8523e2e433 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java @@ -1,6 +1,7 @@ package org.apache.coyote.http11; import nextstep.jwp.exception.UncheckedServletException; +import org.apache.catalina.RequestMappingHandler; import org.apache.coyote.Processor; import org.apache.coyote.http11.request.HttpRequest; import org.apache.coyote.http11.response.Controller; @@ -51,7 +52,4 @@ public void process(final Socket connection) { } } - // 쿠키값 빼내올때 문제점 - // doPost, doGet 추가수정 - // 세션 전략 세우기 } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java index 0da6c372b7..a078d29b63 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/HttpRequest.java @@ -12,6 +12,7 @@ public class HttpRequest { private final RequestLine requestLine; private final RequestHeader requestHeader; private final String requestBody; + private boolean hasValidatedSession; public HttpRequest(final BufferedReader reader) throws IOException, URISyntaxException { final List lines = readAllLines(reader); @@ -19,6 +20,7 @@ public HttpRequest(final BufferedReader reader) throws IOException, URISyntaxExc requestLine = createRequestLine(lines); requestHeader = createRequestHeader(lines); requestBody = addRequestBody(lines, reader); + hasValidatedSession = false; } private static List readAllLines(final BufferedReader reader) { @@ -70,10 +72,18 @@ private int getContentLength(List lines) { return 0; } + public void addJSessionId(final String jSessionId) { + requestHeader.addSession(jSessionId); + } + public boolean hasJSessionId() { return requestHeader.hasJSessionId(); } + public void setHasValidatedSessionTrue() { + this.hasValidatedSession = true; + } + public String getJSessionId() { return requestHeader.getJSessionId(); } @@ -89,4 +99,8 @@ public RequestHeader getRequestHeader() { public String getRequestBody() { return requestBody; } + + public boolean isHasValidatedSession() { + return hasValidatedSession; + } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHeader.java b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHeader.java index 3cbbd1e6f8..f75bb5d4a4 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHeader.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/request/RequestHeader.java @@ -42,4 +42,8 @@ public String getJSessionId() { final String[] parts = cookie.split("="); return parts[1]; } + + public void addSession(String jSessionId){ + headers.put(COOKIE, "JSessionId=" + jSessionId); + } } diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java index 56a9757bf2..081f1f2160 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginGetController.java @@ -2,7 +2,6 @@ import org.apache.coyote.http11.ContentType; import org.apache.coyote.http11.request.HttpRequest; -import org.apache.catalina.session.SessionManager; public class LoginGetController implements Controller { @@ -12,14 +11,13 @@ public void service(final HttpRequest request, final String resourcePath = request.getRequestLine().getRequestUrl() + ".html"; final String responseBody = ResourceResolver.resolve(resourcePath); - if (request.hasJSessionId() && SessionManager.isExist(request.getJSessionId())) { - response.setStatusCode(StatusCode.OK); - response.setResponseBody(ResourceResolver.resolve("/index.html")); - response.addHeader("Content-Type", ContentType.from("/index.html").getContentType() + ";charset=utf-8"); - response.addHeader("Content-Length", String.valueOf(ResourceResolver.resolve("/index.html").getBytes().length)); + if (request.isHasValidatedSession()) { + final String jSessionId = request.getJSessionId(); + response.addJSessionId(jSessionId); + response.setStatusCode(StatusCode.FOUND); + response.addHeader("Location", "/index.html"); return; } - response.setStatusCode(StatusCode.OK); response.setResponseBody(ResourceResolver.resolve(resourcePath)); response.addHeader("Content-Type", ContentType.from(resourcePath).getContentType() + ";charset=utf-8"); diff --git a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java index fbf89e651a..8d65816344 100644 --- a/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java +++ b/tomcat/src/main/java/org/apache/coyote/http11/response/LoginPostController.java @@ -1,9 +1,6 @@ package org.apache.coyote.http11.response; import nextstep.jwp.LoginHandler; -import nextstep.jwp.model.User; -import org.apache.catalina.session.HttpSession; -import org.apache.catalina.session.SessionManager; import org.apache.coyote.http11.ContentType; import org.apache.coyote.http11.request.HttpRequest; @@ -22,16 +19,8 @@ public void service(final HttpRequest request, private void successLoginResponse(final HttpRequest request, final HttpResponse response) { - final SessionManager sessionManager = new SessionManager(); - if (!request.hasJSessionId()) { - final LoginHandler loginHandler = new LoginHandler(); - final User user = loginHandler.getUser(request.getRequestBody()); - final HttpSession httpSession = new HttpSession("user", user); - sessionManager.add(httpSession); - - final HttpSession session = sessionManager.findSession(httpSession.getId()); - response.addJSessionId(session.getId()); - } + final String jSessionId = request.getJSessionId(); + response.addJSessionId(jSessionId); response.setStatusCode(StatusCode.FOUND); response.addHeader("Location", "/index.html"); }