-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1 -2단계 - Tomcat 구현하기] 아톰(이하늘) 미션 제출합니다. (#531)
* fix: remove implementation logback-classic on gradle (#501) * fix: add threads min-spare configuration on properties (#502) * feat: index.html 응답 구현 * refactor: 응답값과 contentType 자료구조로 포장 * feat: css, javascript 파일 지원 * feat: login.html 응답 구현 * docs: todo list 작성 * refactor: query parameter 책임 분리 * refactor: process 메서드 가독성 개선 * fix: javascript contentType 변경 * test: 누락된 쿼리 파라미터 테스트 추가 * refactor: header 책임 분리 * refactor: 패키지 이동 * fix: header pair 구분자 변경 * refactor: 정적 파일 핸들링 책임 분리 * refactor: login 책임 분리 * refactor: handler 추상화 * refactor: 추상화된 핸들러 사용하도록 변경 * fix: */* 타입 처리하도록 변경 * fix: hello handler hello.html 반환하도록 변경 * refactor: 불필요한 로그 제거 * chore: import 최적화 * refactor: response 내부로 메시지 생성 책임 위임 * feat: 로그인 redirection 처리 * refactor: 메서드 이름 변경 * refactor: /login get post 분리 * feat: register 페이지 조회 기능 구현 * feat: not found 핸들러 구현 * refactor: content-type 분리 * refactor: forward 결과 포장 * feat: 회원가입 기능 구현 * refactor: http header 상수화 * feat: header 추가 메서드 구현 * fix: null 검증 제거 * chore: import문 최적화 * refactor: forward 내부에서 status, header 다룰 수 있도록 변경 * feat: cookie 클래스 구현 * refactor: 패키지 구조 변경 * feat: session 구현 * feat: 로그인 세션 적용 * style: enum 개행 제거 * refactor: 불필요한 throws 제거 * chore: 학습 테스트 흔적 제거 * chore: p 태그 제거 * refactor: content-type 내부로 content-type 결정 책임 이동 * refactor: header.empty 정적 팩터리 메서드로 변경 * refactor: http version 상수화 * refactor: http request body를 char[]로 변경 * fix: from 메서드에서 case 무시하지 않도록 변경 --------- Co-authored-by: Gyeongho Yang <[email protected]>
- Loading branch information
Showing
44 changed files
with
1,375 additions
and
30 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ server: | |
accept-count: 1 | ||
max-connections: 1 | ||
threads: | ||
min-spare: 2 | ||
max: 2 |
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
25 changes: 25 additions & 0 deletions
25
tomcat/src/main/java/com/techcourse/handler/GetLoginHandler.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,25 @@ | ||
package com.techcourse.handler; | ||
|
||
import org.apache.catalina.Manager; | ||
import org.apache.coyote.http11.AbstractHandler; | ||
import org.apache.coyote.http11.ForwardResult; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpStatus; | ||
|
||
import java.net.URI; | ||
|
||
public class GetLoginHandler extends AbstractHandler { | ||
|
||
@Override | ||
public boolean canHandle(HttpRequest httpRequest) { | ||
URI uri = httpRequest.getUri(); | ||
String path = uri.getPath(); | ||
|
||
return "/login".equals(path) && httpRequest.getMethod().isGet(); | ||
} | ||
|
||
@Override | ||
protected ForwardResult forward(HttpRequest httpRequest, Manager sessionManager) { | ||
return new ForwardResult("login.html", HttpStatus.OK); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tomcat/src/main/java/com/techcourse/handler/GetRegisterHandler.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,25 @@ | ||
package com.techcourse.handler; | ||
|
||
import org.apache.catalina.Manager; | ||
import org.apache.coyote.http11.AbstractHandler; | ||
import org.apache.coyote.http11.ForwardResult; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpStatus; | ||
|
||
import java.net.URI; | ||
|
||
public class GetRegisterHandler extends AbstractHandler { | ||
|
||
@Override | ||
public boolean canHandle(HttpRequest httpRequest) { | ||
URI uri = httpRequest.getUri(); | ||
String path = uri.getPath(); | ||
|
||
return "/register".equals(path) && httpRequest.getMethod().isGet(); | ||
} | ||
|
||
@Override | ||
protected ForwardResult forward(HttpRequest httpRequest, Manager sessionManager) { | ||
return new ForwardResult("register.html", HttpStatus.OK); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tomcat/src/main/java/com/techcourse/handler/HelloHandler.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,25 @@ | ||
package com.techcourse.handler; | ||
|
||
import org.apache.catalina.Manager; | ||
import org.apache.coyote.http11.AbstractHandler; | ||
import org.apache.coyote.http11.ForwardResult; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpStatus; | ||
|
||
import java.net.URI; | ||
|
||
public class HelloHandler extends AbstractHandler { | ||
|
||
@Override | ||
public boolean canHandle(HttpRequest httpRequest) { | ||
URI uri = httpRequest.getUri(); | ||
String path = uri.getPath(); | ||
|
||
return "/".equals(path); | ||
} | ||
|
||
@Override | ||
protected ForwardResult forward(HttpRequest httpRequest, Manager sessionManager) { | ||
return new ForwardResult("hello.html", HttpStatus.OK); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tomcat/src/main/java/com/techcourse/handler/NotFoundHandler.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,20 @@ | ||
package com.techcourse.handler; | ||
|
||
import org.apache.catalina.Manager; | ||
import org.apache.coyote.http11.AbstractHandler; | ||
import org.apache.coyote.http11.ForwardResult; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpStatus; | ||
|
||
public class NotFoundHandler extends AbstractHandler { | ||
|
||
@Override | ||
public boolean canHandle(HttpRequest httpRequest) { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected ForwardResult forward(HttpRequest httpRequest, Manager sessionManager) { | ||
return new ForwardResult("404.html", HttpStatus.NOT_FOUND); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tomcat/src/main/java/com/techcourse/handler/PostLoginHandler.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,61 @@ | ||
package com.techcourse.handler; | ||
|
||
import com.techcourse.db.InMemoryUserRepository; | ||
import com.techcourse.model.User; | ||
import jakarta.servlet.http.HttpSession; | ||
import org.apache.catalina.Manager; | ||
import org.apache.coyote.http11.AbstractHandler; | ||
import org.apache.coyote.http11.ForwardResult; | ||
import org.apache.coyote.http11.Header; | ||
import org.apache.coyote.http11.HttpHeaderKey; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpStatus; | ||
import org.apache.coyote.http11.QueryParameter; | ||
|
||
import java.net.URI; | ||
|
||
public class PostLoginHandler extends AbstractHandler { | ||
|
||
@Override | ||
public boolean canHandle(HttpRequest httpRequest) { | ||
URI uri = httpRequest.getUri(); | ||
String path = uri.getPath(); | ||
|
||
return "/login".equals(path) && httpRequest.getMethod().isPost(); | ||
} | ||
|
||
@Override | ||
protected ForwardResult forward(HttpRequest httpRequest, Manager sessionManager) { | ||
if (httpRequest.hasNotApplicationXW3FormUrlEncodedBody()) { | ||
throw new RuntimeException(); | ||
} | ||
|
||
QueryParameter queryParameter = new QueryParameter(httpRequest.body()); | ||
Header header = Header.empty(); | ||
String redirectionPath = "401.html"; | ||
|
||
if (isLoggedIn(queryParameter)) { | ||
HttpSession session = findSessionOrCreate(sessionManager, createCookie(httpRequest)); | ||
session.setAttribute("user", getUser(queryParameter)); | ||
header.append(HttpHeaderKey.SET_COOKIE, getSessionKey() + "=" + session.getId()); | ||
redirectionPath = "index.html"; | ||
} | ||
|
||
header.append(HttpHeaderKey.LOCATION, redirectionPath); | ||
return new ForwardResult(HttpStatus.FOUND, header); | ||
} | ||
|
||
private boolean isLoggedIn(QueryParameter queryParameter) { | ||
String password = queryParameter.get("password").orElse(""); | ||
|
||
return queryParameter.get("account") | ||
.flatMap(InMemoryUserRepository::findByAccount) | ||
.map(it -> it.checkPassword(password)) | ||
.orElse(false); | ||
} | ||
|
||
private User getUser(QueryParameter queryParameter) { | ||
String account = queryParameter.get("account").orElseThrow(); | ||
return InMemoryUserRepository.findByAccount(account).orElseThrow(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tomcat/src/main/java/com/techcourse/handler/PostRegisterHandler.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 com.techcourse.handler; | ||
|
||
import com.techcourse.db.InMemoryUserRepository; | ||
import com.techcourse.model.User; | ||
import org.apache.catalina.Manager; | ||
import org.apache.coyote.http11.AbstractHandler; | ||
import org.apache.coyote.http11.ForwardResult; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpStatus; | ||
import org.apache.coyote.http11.QueryParameter; | ||
|
||
import java.net.URI; | ||
|
||
public class PostRegisterHandler extends AbstractHandler { | ||
|
||
@Override | ||
public boolean canHandle(HttpRequest httpRequest) { | ||
URI uri = httpRequest.getUri(); | ||
String path = uri.getPath(); | ||
|
||
return "/register".equals(path) && httpRequest.getMethod().isPost(); | ||
} | ||
|
||
@Override | ||
protected ForwardResult forward(HttpRequest httpRequest, Manager sessionManager) { | ||
if (httpRequest.hasNotApplicationXW3FormUrlEncodedBody()) { | ||
throw new RuntimeException(); | ||
} | ||
|
||
registerNewUser(httpRequest); | ||
|
||
return new ForwardResult("index.html", HttpStatus.OK); | ||
} | ||
|
||
private void registerNewUser(HttpRequest httpRequest) { | ||
QueryParameter body = new QueryParameter(httpRequest.body()); | ||
String account = body.get("account").orElseThrow(); | ||
String password = body.get("password").orElseThrow(); | ||
String email = body.get("email").orElseThrow(); | ||
|
||
InMemoryUserRepository.save(new User(account, password, email)); | ||
} | ||
} |
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.