Skip to content

Commit

Permalink
refactor: 메소드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
woosung1223 committed Sep 2, 2023
1 parent cd6c625 commit 25f7e24
Showing 1 changed file with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.UUID;
import nextstep.jwp.db.InMemorySessionRepository;
import nextstep.jwp.db.InMemoryUserRepository;
import nextstep.jwp.model.User;
import org.apache.coyote.http11.ContentTypeParser;
import org.apache.coyote.http11.message.Cookie;
import org.apache.coyote.http11.message.Headers;
Expand Down Expand Up @@ -33,12 +32,28 @@ public Response handle(Request request) throws IOException {
private Response responseWhenHttpMethodIsGet(Request request) throws IOException {
Headers requestHeaders = request.getHeaders();
Cookie cookie = requestHeaders.getCookie();
String absolutePath = "login.html";

if (cookie.hasKey("JSESSIONID")) {
absolutePath = "index.html";
return responseForLoggedIn(request);
}
return responseForNotLoggedIn(request);
}

private Response responseForLoggedIn(Request request) throws IOException {
String absolutePath = "index.html";
String resource = findResourceWithPath(absolutePath);
Headers headers = new Headers(Map.of(
"Content-Type", ContentTypeParser.parse(absolutePath),
"Content-Length", String.valueOf(resource.getBytes().length)
));
ResponseBody responseBody = new ResponseBody(resource);

return Response.from(request.getHttpVersion(), HttpStatus.FOUND,
headers, responseBody);
}

private Response responseForNotLoggedIn(Request request) throws IOException {
String absolutePath = "login.html";
String resource = findResourceWithPath(absolutePath);
Headers headers = new Headers(Map.of(
"Content-Type", ContentTypeParser.parse(absolutePath),
Expand All @@ -55,43 +70,39 @@ private Response responseWhenHttpMethodIsPost(Request request) throws IOExceptio
String account = requestBody.get("account");
String password = requestBody.get("password");

// TODO: 없는 계정으로 로그인 했을 때에도 responseWhenLoginFail로 이동
User user = InMemoryUserRepository.findByAccountAndPassword(account, password)
.orElseThrow();

if (user.hasSameCredential(account, password)) {
if (InMemoryUserRepository.hasSameCredential(account, password)) {
return responseWhenLoginSuccess(request);
}
return responseWhenLoginFail(request);
}

private Response responseWhenLoginFail(Request request) throws IOException {
String absolutePath = "401.html";
private Response responseWhenLoginSuccess(Request request) throws IOException {
UUID sessionId = saveSession(request);

String absolutePath = "index.html";
String resource = findResourceWithPath(absolutePath);
Headers headers = new Headers(Map.of(
"Content-Type", ContentTypeParser.parse(absolutePath),
"Content-Length", String.valueOf(resource.getBytes().length)
"Content-Length", String.valueOf(resource.getBytes().length),
"Set-Cookie", "JSESSIONID=" + sessionId
));
ResponseBody responseBody = new ResponseBody(resource);

return Response.from(request.getHttpVersion(), HttpStatus.UNAUTHORIZED,
return Response.from(request.getHttpVersion(), HttpStatus.FOUND,
headers, responseBody);
}

private Response responseWhenLoginSuccess(Request request) throws IOException {
UUID sessionId = saveSession(request);
private Response responseWhenLoginFail(Request request) throws IOException {
String absolutePath = "401.html";

String absolutePath = "index.html";
String resource = findResourceWithPath(absolutePath);
Headers headers = new Headers(Map.of(
"Content-Type", ContentTypeParser.parse(absolutePath),
"Content-Length", String.valueOf(resource.getBytes().length),
"Set-Cookie", "JSESSIONID=" + sessionId
"Content-Length", String.valueOf(resource.getBytes().length)
));
ResponseBody responseBody = new ResponseBody(resource);

return Response.from(request.getHttpVersion(), HttpStatus.FOUND,
return Response.from(request.getHttpVersion(), HttpStatus.UNAUTHORIZED,
headers, responseBody);
}

Expand Down

0 comments on commit 25f7e24

Please sign in to comment.