-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[톰캣 구현하기 - 3, 4단계] 땡칠(박성철) 미션 제출합니다. (#471)
* refactor(Http11Processor): HTTP Version 관리를 Response로 위임 * refactor(HttpRequest): Request Line을 객체로 분리 * feat: GET, POST를 담당하는 핸들러 * feat(Connector): Thread Pool 적용 * feat(SessionManager): 발생할 수 있는 동시성 문제를 해결한다 * refactor: Handler 인터페이스를 request, response 로 수정한다 * refactor: 쿠키, 세션 조회에 Optional 사용 * refactor(Headers): 메서드 변수명 수정 * refactor(HttpResponse): 바디 길이 0일때, Content-Length 헤더 제외 * refactor: HttpResponse가 응답을 생성한다 * refactor: Handler를 Controller로 재명명 * refactor: 컨트롤러 매핑 RequestMapping으로 분리, TargetPath 객체 분리 * fix: 실패하는 테스트 보완 * refactor: RequestMapping 상수화 * refactor: 핸들러 목록의 자료형을 Set으로 변경 * typo(HttpResponse): 변수명 headers2 -> headers
- Loading branch information
Showing
25 changed files
with
577 additions
and
309 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
tomcat/src/main/java/nextstep/jwp/handler/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nextstep.jwp.handler; | ||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.TargetPath; | ||
import org.apache.coyote.http11.controller.Controller; | ||
import org.apache.coyote.http11.header.ContentType; | ||
|
||
public class HelloController implements Controller { | ||
|
||
private static final TargetPath SUPPORTED_PATH = new TargetPath("/"); | ||
|
||
@Override | ||
public boolean supports(HttpRequest httpRequest) { | ||
return httpRequest.getTarget().getPath().equals(SUPPORTED_PATH) && httpRequest.getMethod().isGet(); | ||
} | ||
|
||
@Override | ||
public void handle(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
httpResponse.setContentType(new ContentType("text/html")); | ||
httpResponse.setBody("Hello world!"); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
tomcat/src/main/java/nextstep/jwp/handler/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package nextstep.jwp.handler; | ||
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.TargetPath; | ||
import org.apache.coyote.http11.body.FormData; | ||
import org.apache.coyote.http11.controller.FileController; | ||
import org.apache.coyote.http11.controller.GetAndPostController; | ||
import org.apache.coyote.http11.header.Cookies; | ||
import org.apache.coyote.http11.session.Session; | ||
import org.apache.coyote.http11.session.SessionManager; | ||
|
||
import java.util.Optional; | ||
|
||
public class LoginController extends GetAndPostController { | ||
|
||
private static final String SESSION_KEY = "JSESSIONID"; | ||
private static final String SESSION_USER_KEY = "user"; | ||
|
||
private static final String UNAUTHORIZED_LOCATION = "/401"; | ||
private static final String MAIN_LOCATION = "/index"; | ||
|
||
private static final TargetPath SUPPORTED_PATH = new TargetPath("/login").autoComplete(); | ||
|
||
private final FileController fileHandler = new FileController(); | ||
|
||
@Override | ||
public boolean supports(HttpRequest httpRequest) { | ||
if (httpRequest.getMethod().isGet() || httpRequest.getMethod().isPost()) { | ||
return httpRequest.getTarget().getPath().autoComplete().equals(SUPPORTED_PATH); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
boolean isSignedIn = httpRequest.getSession(SESSION_KEY) | ||
.map(session -> session.hasAttribute(SESSION_USER_KEY)) | ||
.isPresent(); | ||
if (isSignedIn) { | ||
httpResponse.redirectTo(MAIN_LOCATION); | ||
} | ||
fileHandler.handle(httpRequest, httpResponse); | ||
} | ||
|
||
@Override | ||
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
FormData formData = FormData.of(httpRequest.getBody()); | ||
String account = formData.get("account"); | ||
String password = formData.get("password"); | ||
|
||
Optional<User> found = InMemoryUserRepository.findByAccount(account); | ||
if (found.isPresent()) { | ||
User user = found.get(); | ||
signIn(httpResponse, user, password); | ||
return; | ||
} | ||
httpResponse.redirectTo(UNAUTHORIZED_LOCATION); | ||
} | ||
|
||
private void signIn(final HttpResponse httpResponse, final User user, final String password) { | ||
if (user.checkPassword(password)) { | ||
Session session = new Session(); | ||
session.addAttribute(SESSION_USER_KEY, user); | ||
SessionManager.add(session); | ||
|
||
Cookies cookies = new Cookies(); | ||
cookies.add(SESSION_KEY, session.getId()); | ||
|
||
httpResponse.redirectTo(MAIN_LOCATION); | ||
httpResponse.setCookies(cookies); | ||
return; | ||
} | ||
httpResponse.redirectTo(UNAUTHORIZED_LOCATION); | ||
} | ||
} |
69 changes: 0 additions & 69 deletions
69
tomcat/src/main/java/nextstep/jwp/handler/LoginHandler.java
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
tomcat/src/main/java/nextstep/jwp/handler/RegisterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package nextstep.jwp.handler; | ||
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.TargetPath; | ||
import org.apache.coyote.http11.body.FormData; | ||
import org.apache.coyote.http11.controller.FileController; | ||
import org.apache.coyote.http11.controller.GetAndPostController; | ||
|
||
public class RegisterController extends GetAndPostController { | ||
|
||
private static final String MAIN_LOCATION = "/index"; | ||
private static final TargetPath SUPPORTED_PATH = new TargetPath("/register").autoComplete(); | ||
|
||
private final FileController fileHandler = new FileController(); | ||
|
||
@Override | ||
public boolean supports(HttpRequest httpRequest) { | ||
if (httpRequest.getMethod().isGet() || httpRequest.getMethod().isPost()) { | ||
return httpRequest.getTarget().getPath().autoComplete().equals(SUPPORTED_PATH); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
fileHandler.handle(httpRequest, httpResponse); | ||
} | ||
|
||
@Override | ||
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
FormData formData = FormData.of(httpRequest.getBody()); | ||
|
||
String account = formData.get("account"); | ||
String password = formData.get("password"); | ||
String email = formData.get("email"); | ||
|
||
InMemoryUserRepository.save(new User(account, password, email)); | ||
httpResponse.redirectTo(MAIN_LOCATION); | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
tomcat/src/main/java/nextstep/jwp/handler/RegisterHandler.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.