Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
746e1cb
refactor: InMemoryUserRepository id 필드 Primitive type으로 변경
Hyeon9mak Aug 31, 2021
ea7256b
refactor: Controllers에서 컨트롤러 탐색 시간이 줄어들도록 리팩토링
Hyeon9mak Aug 31, 2021
7a543f4
style: 코드 리포맷팅
Hyeon9mak Aug 31, 2021
f6aa31c
refactor: 각 RestController URI 매칭 로직 제거
Hyeon9mak Aug 31, 2021
19e37b9
refactor: Controllers 이름을 RequestMapping으로 변경
Hyeon9mak Aug 31, 2021
a3ef8c9
feat: http cookie 구현
Hyeon9mak Aug 31, 2021
8a83690
refactor: http header enum으로 wrapping
Hyeon9mak Sep 1, 2021
d5251cd
docs: Todo 리스트 작성
Hyeon9mak Sep 1, 2021
29db92f
feat: HttpCookie 구현
Hyeon9mak Sep 1, 2021
d4731b4
feat: HttpSession, HttpSessions 구현
Hyeon9mak Sep 1, 2021
327afba
refactor: HttpSession 구현에 따른 LoginController 로직 수정
Hyeon9mak Sep 1, 2021
187d8e9
refactor: http 상태코드와 함께 Redirect가 아닌 Body를 포함한 response를 보내도록 수정
Hyeon9mak Sep 1, 2021
136c296
refactor: GET '/login' 요청시 분기처리 수정
Hyeon9mak Sep 2, 2021
e80912c
refactor: 정적 자원 경로 Enum으로 변경
Hyeon9mak Sep 4, 2021
31a6f5c
refactor: InMemoryUserRepository의 ID 타입 변경
Hyeon9mak Sep 4, 2021
e964de8
refactor: HttpResponse 로깅시 body는 로깅하지 않도록 수정
Hyeon9mak Sep 4, 2021
34c0a72
style: HardWrap 값 증가에 따른 코드 리포맷팅
Hyeon9mak Sep 4, 2021
8dfed80
refactor: 클래스 내부에서 사용되는 private 메서드 명을 더 직관적으로 변경
Hyeon9mak Sep 4, 2021
a8774e5
refactor: 실제 예외가 아닌 상황에서 try-catch를 통해 분기를 하지 않도록 변경
Hyeon9mak Sep 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 쿠키 세션 구현하기
- [x] HttpCookie에 JSESSION 값 저장하기
- [x] HttpCookie 테스트 코드 작성
- [x] HttpSession 구현하기
- [x] HttpCookie, HttpSession으로 변경된 객체 테스트 코드 작성

# HTTP 서버 구현하기

- [x] 서버를 실행시켜서 브라우저로 서버(`http://localhost:8080/index.html`)에 접속하면 index.html 페이지를 보여준다.
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/nextstep/jwp/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@

public interface Controller {

boolean matchUri(String uri);

HttpResponse doService(HttpRequest httpRequest) throws IOException;
HttpResponse service(HttpRequest httpRequest) throws IOException;
}
50 changes: 0 additions & 50 deletions app/src/main/java/nextstep/jwp/controller/Controllers.java

This file was deleted.

51 changes: 41 additions & 10 deletions app/src/main/java/nextstep/jwp/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package nextstep.jwp.controller;

import static nextstep.jwp.controller.StaticResourcePath.INDEX_PAGE;
import static nextstep.jwp.controller.StaticResourcePath.NOT_FOUND_PAGE;
import static nextstep.jwp.controller.StaticResourcePath.UNAUTHORIZED_PAGE;
import static nextstep.jwp.http.common.HttpStatus.FOUND;
import static nextstep.jwp.http.common.HttpStatus.OK;

import java.io.IOException;
import nextstep.jwp.controller.request.LoginRequest;
import nextstep.jwp.controller.response.LoginResponse;
import nextstep.jwp.exception.StaticResourceNotFoundException;
import nextstep.jwp.exception.UnauthorizedException;
import nextstep.jwp.http.common.HttpStatus;
import nextstep.jwp.http.request.HttpRequest;
import nextstep.jwp.http.response.HttpResponse;
import nextstep.jwp.model.StaticResource;
import nextstep.jwp.service.LoginService;
import nextstep.jwp.service.StaticResourceService;
import org.slf4j.Logger;
Expand All @@ -15,25 +25,51 @@ public class LoginController extends RestController {
private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

private final LoginService loginService;
private final StaticResourceService staticResourceService;

public LoginController(LoginService loginService, StaticResourceService staticResourceService) {
super(staticResourceService);
this.loginService = loginService;
this.staticResourceService = staticResourceService;
}

@Override
protected HttpResponse doGet(HttpRequest httpRequest) throws IOException {
try {
if (httpRequest.hasCookie() && loginService.isAlreadyLogin(httpRequest.getCookie())) {
return HttpResponse.redirect(FOUND, INDEX_PAGE.getValue());
}

StaticResource staticResource = staticResourceService.findByPathWithExtension(httpRequest.getUri(), ".html");

return HttpResponse.withBody(HttpStatus.OK, staticResource);
} catch (StaticResourceNotFoundException e) {
StaticResource staticResource = staticResourceService.findByPath(NOT_FOUND_PAGE.getValue());

LOGGER.warn(e.getMessage());

return HttpResponse.withBody(HttpStatus.NOT_FOUND, staticResource);
}
}

@Override
protected HttpResponse doPost(HttpRequest httpRequest) {
protected HttpResponse doPost(HttpRequest httpRequest) throws IOException {
try {
if (httpRequest.hasCookie() && loginService.isAlreadyLogin(httpRequest.getCookie())) {
return HttpResponse.redirect(FOUND, INDEX_PAGE.getValue());
}

LoginRequest loginRequest = getLoginRequest(httpRequest);
loginService.login(loginRequest);
LoginResponse loginResponse = loginService.login(loginRequest);

LOGGER.debug("Login Success.");

return HttpResponse.redirect(HttpStatus.FOUND, "/index.html");
StaticResource resource = staticResourceService.findByPath(INDEX_PAGE.getValue());
return HttpResponse.withBodyAndCookie(OK, resource, loginResponse.toCookieString());
} catch (UnauthorizedException e) {
LOGGER.debug("Login Failed.");

return HttpResponse.redirect(HttpStatus.UNAUTHORIZED, "/401.html");
StaticResource resource = staticResourceService.findByPath(UNAUTHORIZED_PAGE.getValue());
return HttpResponse.withBody(HttpStatus.UNAUTHORIZED, resource);
}
}

Expand All @@ -45,9 +81,4 @@ private LoginRequest getLoginRequest(HttpRequest httpRequest) {

return new LoginRequest(account, password);
}

@Override
public boolean matchUri(String uri) {
return uri.startsWith("/login");
}
}
37 changes: 28 additions & 9 deletions app/src/main/java/nextstep/jwp/controller/RegisterController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package nextstep.jwp.controller;

import static nextstep.jwp.controller.StaticResourcePath.CONFLICT_PAGE;
import static nextstep.jwp.controller.StaticResourcePath.INDEX_PAGE;
import static nextstep.jwp.controller.StaticResourcePath.NOT_FOUND_PAGE;

import java.io.IOException;
import nextstep.jwp.controller.request.RegisterRequest;
import nextstep.jwp.exception.DuplicateAccountException;
import nextstep.jwp.exception.StaticResourceNotFoundException;
import nextstep.jwp.http.common.HttpStatus;
import nextstep.jwp.http.request.HttpRequest;
import nextstep.jwp.http.response.HttpResponse;
import nextstep.jwp.model.StaticResource;
import nextstep.jwp.service.RegisterService;
import nextstep.jwp.service.StaticResourceService;
import org.slf4j.Logger;
Expand All @@ -15,25 +22,42 @@ public class RegisterController extends RestController {
private static final Logger LOGGER = LoggerFactory.getLogger(RegisterController.class);

private final RegisterService registerService;
private final StaticResourceService staticResourceService;

public RegisterController(RegisterService registerService, StaticResourceService staticResourceService) {
super(staticResourceService);
this.registerService = registerService;
this.staticResourceService = staticResourceService;
}

@Override
protected HttpResponse doGet(HttpRequest httpRequest) throws IOException {
try {
StaticResource staticResource = staticResourceService.findByPathWithExtension(httpRequest.getUri(), ".html");

return HttpResponse.withBody(HttpStatus.OK, staticResource);
} catch (StaticResourceNotFoundException e) {
StaticResource staticResource = staticResourceService.findByPath(NOT_FOUND_PAGE.getValue());

LOGGER.warn(e.getMessage());

return HttpResponse.withBody(HttpStatus.NOT_FOUND, staticResource);
}
}

@Override
protected HttpResponse doPost(HttpRequest httpRequest) {
protected HttpResponse doPost(HttpRequest httpRequest) throws IOException {
try {
RegisterRequest registerRequest = getRegisterRequest(httpRequest);
registerService.registerUser(registerRequest);

LOGGER.debug("Register Success.");

return HttpResponse.redirect(HttpStatus.MOVED_PERMANENTLY, "/index.html");
return HttpResponse.redirect(HttpStatus.MOVED_PERMANENTLY, INDEX_PAGE.getValue());
} catch (DuplicateAccountException e) {
LOGGER.debug("Register Failed.");

return HttpResponse.redirect(HttpStatus.CONFLICT, "/409.html");
StaticResource resource = staticResourceService.findByPath(CONFLICT_PAGE.getValue());
return HttpResponse.withBody(HttpStatus.CONFLICT, resource);
}
}

Expand All @@ -46,9 +70,4 @@ private RegisterRequest getRegisterRequest(HttpRequest httpRequest) {

return new RegisterRequest(account, password, email);
}

@Override
public boolean matchUri(String uri) {
return uri.startsWith("/register");
}
}
37 changes: 5 additions & 32 deletions app/src/main/java/nextstep/jwp/controller/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,20 @@
import static nextstep.jwp.http.request.Method.GET;

import java.io.IOException;
import nextstep.jwp.exception.StaticResourceNotFoundException;
import nextstep.jwp.http.common.HttpStatus;
import nextstep.jwp.http.request.HttpRequest;
import nextstep.jwp.http.response.HttpResponse;
import nextstep.jwp.model.StaticResource;
import nextstep.jwp.service.StaticResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class RestController implements Controller {

private static final Logger LOGGER = LoggerFactory.getLogger(RestController.class);

private final StaticResourceService staticResourceService;

public RestController(StaticResourceService staticResourceService) {
this.staticResourceService = staticResourceService;
}

protected abstract HttpResponse doPost(HttpRequest httpRequest);

private HttpResponse doGet(HttpRequest httpRequest) throws IOException {
try {
StaticResource staticResource = staticResourceService.findByPathWithExtension(
httpRequest.getUri(), ".html");

return HttpResponse.withBody(HttpStatus.OK, staticResource);
} catch (StaticResourceNotFoundException e) {
StaticResource staticResource = staticResourceService.findByPath("/404.html");

LOGGER.warn(e.getMessage());

return HttpResponse.withBody(HttpStatus.NOT_FOUND, staticResource);
}
}

public HttpResponse doService(HttpRequest httpRequest) throws IOException {
public HttpResponse service(HttpRequest httpRequest) throws IOException {
if (httpRequest.hasMethod(GET)) {
return doGet(httpRequest);
}

return doPost(httpRequest);
}

protected abstract HttpResponse doGet(HttpRequest httpRequest) throws IOException;

protected abstract HttpResponse doPost(HttpRequest httpRequest) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nextstep.jwp.controller;

import static nextstep.jwp.controller.StaticResourcePath.NOT_FOUND_PAGE;

import java.io.IOException;
import nextstep.jwp.exception.StaticResourceNotFoundException;
import nextstep.jwp.http.common.HttpStatus;
Expand All @@ -26,7 +28,7 @@ private HttpResponse doGet(HttpRequest httpRequest) throws IOException {

return HttpResponse.withBody(HttpStatus.OK, staticResource);
} catch (StaticResourceNotFoundException e) {
StaticResource staticResource = staticResourceService.findByPath("/404.html");
StaticResource staticResource = staticResourceService.findByPath(NOT_FOUND_PAGE.getValue());

LOGGER.warn(e.getMessage());

Expand All @@ -35,12 +37,7 @@ private HttpResponse doGet(HttpRequest httpRequest) throws IOException {
}

@Override
public HttpResponse doService(HttpRequest httpRequest) throws IOException {
public HttpResponse service(HttpRequest httpRequest) throws IOException {
return doGet(httpRequest);
}

@Override
public boolean matchUri(String uri) {
return false;
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/nextstep/jwp/controller/StaticResourcePath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nextstep.jwp.controller;

public enum StaticResourcePath {

INDEX_PAGE("/index.html"),
UNAUTHORIZED_PAGE("/401.html"),
NOT_FOUND_PAGE("/404.html"),
CONFLICT_PAGE("/409.html");

private final String value;

StaticResourcePath(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nextstep.jwp.controller.response;

public class LoginResponse {

private final String sessionKey;
private final String sessionValue;

public LoginResponse(String sessionKey, String sessionValue) {
this.sessionKey = sessionKey;
this.sessionValue = sessionValue;
}

public String toCookieString() {
return String.format("%s=%s;", sessionKey, sessionValue);
}
}
Loading