-
Notifications
You must be signed in to change notification settings - Fork 388
[2단계 - Tomcat 구현하기] 짱수(장혁수) 미션 제출합니다. #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
caae46a
fed02f6
7e91356
04921cb
a7f5de9
24c9787
d4b32a0
0742e5a
1e0aba2
400605f
c93ad34
7cff4d7
002f558
0d23bab
708b765
42adfa3
82885c1
5b141fc
917529e
fabed9b
a84d977
6197946
264a24e
a8f1971
554a8b0
fc085e7
bd88159
f93b392
5213bd4
6ad41a6
b385ea7
7431593
857470c
439599d
c711c0e
165077f
39d24d2
291d24f
97af4f1
7079e5d
c5265b3
6cb90a6
8f55340
d4589cd
f0d694a
95283ac
7392038
71b373c
ca76404
0bab601
24d9b85
ec3bf84
93bfcd9
37ba30a
f97d75d
3523cda
9e86016
b7fa7e1
e7f61e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.apache.coyote.http11.exception; | ||
|
|
||
| public class CanNotHandleRequest extends RuntimeException { | ||
| public CanNotHandleRequest(String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.apache.coyote.http11.exception; | ||
|
|
||
| public class NoSuchUserException extends RuntimeException { | ||
| public NoSuchUserException(String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.apache.coyote.http11.exception; | ||
|
|
||
| public class NotCompleteResponseException extends RuntimeException { | ||
| public NotCompleteResponseException(String message) { | ||
| super(message); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package org.apache.coyote.http11.handler; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| import org.apache.coyote.http11.RequestHandler; | ||
| import org.apache.coyote.http11.exception.CanNotHandleRequest; | ||
| import org.apache.coyote.http11.exception.NoSuchUserException; | ||
| import org.apache.coyote.http11.httpmessage.request.Request; | ||
| import org.apache.coyote.http11.httpmessage.response.Response; | ||
| import org.apache.coyote.http11.httpmessage.response.StaticResource; | ||
| import org.apache.coyote.http11.session.Session; | ||
| import org.apache.coyote.http11.session.SessionManager; | ||
|
|
||
| import com.techcourse.db.InMemoryUserRepository; | ||
| import com.techcourse.model.User; | ||
|
|
||
| public class DefaultResourceHandler implements RequestHandler { | ||
|
|
||
| @Override | ||
| public String handle(Request request) throws IOException { | ||
| if (request.isStaticResourceRequest()) { | ||
| return Response.builder() | ||
| .versionOf(request.getHttpVersion()) | ||
| .ofStaticResource(new StaticResource(request.getTarget())) | ||
| .toHttpMessage(); | ||
| } | ||
| if (request.getTarget().equals("/")) { | ||
| return Response.builder() | ||
| .versionOf(request.getHttpVersion()) | ||
| .ofStaticResource(new StaticResource("/index.html")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. root 요청이 여기오기도전에 에러터지네요. 한번 확인 해봐야할 것 같아요
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static resource 요청을 판단할 때 문제가 있었네용..ㅎㅎ |
||
| .toHttpMessage(); | ||
| } | ||
| if (request.getTarget().equals("/login")) { | ||
| return loginResponse(request); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. contains로 분기해도 괜찮을까요?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오! 이젠 로그인 요청이 POST 로 변경되어서 쿼리 파라미터가 포함된 요청이 존재하지 않는군요! |
||
| } | ||
| if (request.getTarget().contains("register")) { | ||
| return registerResponse(request); | ||
| } | ||
| throw new CanNotHandleRequest("처리할 수 없는 요청입니다. : " + request.getTarget()); | ||
| } | ||
|
|
||
| private String loginResponse(Request request) throws IOException { | ||
| if (request.isPost()) { | ||
| return login(request).toHttpMessage(); | ||
| } | ||
| if (isLoggedIn(request)) { | ||
| return Response.builder() | ||
| .versionOf(request.getHttpVersion()) | ||
| .found("/index.html") | ||
| .toHttpMessage(); | ||
| } | ||
| return Response.builder() | ||
| .versionOf(request.getHttpVersion()) | ||
| .ofStaticResource(new StaticResource("/login.html")) | ||
| .toHttpMessage(); | ||
| } | ||
|
|
||
| private boolean isLoggedIn(Request request) { | ||
| return Objects.nonNull(request.getSession(false)); | ||
| } | ||
|
|
||
| private Response login(Request request) throws NoSuchUserException { | ||
| RequestParameters requestParams = RequestParameters.parseFrom(request.getBody()); | ||
| String account = requestParams.getParam("account"); | ||
| String password = requestParams.getParam("password"); | ||
| User user = InMemoryUserRepository.fetchByAccount(account); | ||
| if (user.checkPassword(password)) { | ||
| Session session = request.getSession(true); | ||
| session.setAttribute("user", user); | ||
| SessionManager.getInstance().add(session); | ||
| return Response.builder() | ||
| .versionOf(request.getHttpVersion()) | ||
| .addCookie("JSESSIONID", session.getId()) | ||
| .found("/index.html"); | ||
| } | ||
|
|
||
| return Response.builder() | ||
| .versionOf(request.getHttpVersion()) | ||
| .found("/401.html"); | ||
| } | ||
|
|
||
| private String registerResponse(Request request) throws IOException { | ||
| if (request.isPost()) { | ||
| RequestParameters methodRequest = RequestParameters.parseFrom(request.getBody()); | ||
| User user = register(methodRequest); | ||
| Session session = request.getSession(true); | ||
| session.setAttribute("user", user); | ||
| SessionManager.getInstance().add(session); | ||
| return Response.builder() | ||
| .versionOf(request.getHttpVersion()) | ||
| .addCookie("JSESSIONID", session.getId()) | ||
| .found("/index.html") | ||
| .toHttpMessage(); | ||
| } | ||
|
|
||
| return Response.builder() | ||
| .versionOf(request.getHttpVersion()) | ||
| .ofStaticResource(new StaticResource("/register.html")) | ||
| .toHttpMessage(); | ||
| } | ||
|
|
||
| private User register(RequestParameters requestParams) { | ||
| String account = requestParams.getParam("account"); | ||
| User user = new User( | ||
| account, | ||
| requestParams.getParam("password"), | ||
| requestParams.getParam("email") | ||
| ); | ||
| InMemoryUserRepository.save(user); | ||
| return InMemoryUserRepository.fetchByAccount(account); | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
클래스명이 되게 상세한데, 이렇게 여러 Exception을 분리한 이유, 기준이 궁금합니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception 을 하나로 두지 않고 지금처럼 여러개의 Exception 을 둔 것은 단순한 취향이었던 것 같아요.
아직까지 필요한 Exception 이 많지 않기 때문에 하나의 Exception 을 두고 예외 메시지로 구분하는 것 보다 각각의 명확한 Exception 을 사용하고 싶었어요.