Skip to content

Commit

Permalink
fix: session 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
chaewon121 committed Sep 14, 2023
1 parent 93e171b commit ac53939
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.apache.coyote.http11;
package org.apache.catalina;

import nextstep.jwp.LoginHandler;
import nextstep.jwp.model.User;
import org.apache.catalina.session.HttpSession;
import org.apache.catalina.session.SessionManager;
import org.apache.coyote.http11.request.HttpMethod;
import org.apache.coyote.http11.request.HttpRequest;
import org.apache.coyote.http11.response.*;
Expand Down Expand Up @@ -28,14 +32,34 @@ public enum RequestMappingHandler {
}

public static Controller findController(final HttpRequest request) {
String resourcePath = request.getRequestLine().getRequestUrl();
HttpMethod requestMethod = request.getRequestLine().getHttpMethod();
final String resourcePath = request.getRequestLine().getRequestUrl();
final HttpMethod requestMethod = request.getRequestLine().getHttpMethod();

return Arrays.stream(values())
final Controller findController = Arrays.stream(values())
.filter(value -> value.condition.test(resourcePath, requestMethod))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("잘못된 url 요청입니다."))
.getController();

if (findController instanceof LoginPostController
&& (!request.hasJSessionId())) {
addSession(request);
}
if (findController instanceof LoginGetController
&& request.hasJSessionId()
&& SessionManager.isExist(request.getJSessionId())) {
request.setHasValidatedSessionTrue();
}
return findController;
}

private static void addSession(final HttpRequest request) {
final SessionManager sessionManager = new SessionManager();
final LoginHandler loginHandler = new LoginHandler();
final User user = loginHandler.getUser(request.getRequestBody());
final HttpSession httpSession = new HttpSession("user", user);
sessionManager.add(httpSession);
request.addJSessionId(httpSession.getId());
}

public static boolean isFileGetUrl(final String resourcePath, final HttpMethod requestMethod) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.apache.coyote.http11;

import nextstep.jwp.exception.UncheckedServletException;
import org.apache.catalina.RequestMappingHandler;
import org.apache.coyote.Processor;
import org.apache.coyote.http11.request.HttpRequest;
import org.apache.coyote.http11.response.Controller;
Expand Down Expand Up @@ -51,7 +52,4 @@ public void process(final Socket connection) {
}
}

// 쿠키값 빼내올때 문제점
// doPost, doGet 추가수정
// 세션 전략 세우기
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ public class HttpRequest {
private final RequestLine requestLine;
private final RequestHeader requestHeader;
private final String requestBody;
private boolean hasValidatedSession;

public HttpRequest(final BufferedReader reader) throws IOException, URISyntaxException {
final List<String> lines = readAllLines(reader);

requestLine = createRequestLine(lines);
requestHeader = createRequestHeader(lines);
requestBody = addRequestBody(lines, reader);
hasValidatedSession = false;
}

private static List<String> readAllLines(final BufferedReader reader) {
Expand Down Expand Up @@ -70,10 +72,18 @@ private int getContentLength(List<String> lines) {
return 0;
}

public void addJSessionId(final String jSessionId) {
requestHeader.addSession(jSessionId);
}

public boolean hasJSessionId() {
return requestHeader.hasJSessionId();
}

public void setHasValidatedSessionTrue() {
this.hasValidatedSession = true;
}

public String getJSessionId() {
return requestHeader.getJSessionId();
}
Expand All @@ -89,4 +99,8 @@ public RequestHeader getRequestHeader() {
public String getRequestBody() {
return requestBody;
}

public boolean isHasValidatedSession() {
return hasValidatedSession;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public String getJSessionId() {
final String[] parts = cookie.split("=");
return parts[1];
}

public void addSession(String jSessionId){
headers.put(COOKIE, "JSessionId=" + jSessionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.coyote.http11.ContentType;
import org.apache.coyote.http11.request.HttpRequest;
import org.apache.catalina.session.SessionManager;

public class LoginGetController implements Controller {

Expand All @@ -12,14 +11,13 @@ public void service(final HttpRequest request,
final String resourcePath = request.getRequestLine().getRequestUrl() + ".html";
final String responseBody = ResourceResolver.resolve(resourcePath);

if (request.hasJSessionId() && SessionManager.isExist(request.getJSessionId())) {
response.setStatusCode(StatusCode.OK);
response.setResponseBody(ResourceResolver.resolve("/index.html"));
response.addHeader("Content-Type", ContentType.from("/index.html").getContentType() + ";charset=utf-8");
response.addHeader("Content-Length", String.valueOf(ResourceResolver.resolve("/index.html").getBytes().length));
if (request.isHasValidatedSession()) {
final String jSessionId = request.getJSessionId();
response.addJSessionId(jSessionId);
response.setStatusCode(StatusCode.FOUND);
response.addHeader("Location", "/index.html");
return;
}

response.setStatusCode(StatusCode.OK);
response.setResponseBody(ResourceResolver.resolve(resourcePath));
response.addHeader("Content-Type", ContentType.from(resourcePath).getContentType() + ";charset=utf-8");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.apache.coyote.http11.response;

import nextstep.jwp.LoginHandler;
import nextstep.jwp.model.User;
import org.apache.catalina.session.HttpSession;
import org.apache.catalina.session.SessionManager;
import org.apache.coyote.http11.ContentType;
import org.apache.coyote.http11.request.HttpRequest;

Expand All @@ -22,16 +19,8 @@ public void service(final HttpRequest request,

private void successLoginResponse(final HttpRequest request,
final HttpResponse response) {
final SessionManager sessionManager = new SessionManager();
if (!request.hasJSessionId()) {
final LoginHandler loginHandler = new LoginHandler();
final User user = loginHandler.getUser(request.getRequestBody());
final HttpSession httpSession = new HttpSession("user", user);
sessionManager.add(httpSession);

final HttpSession session = sessionManager.findSession(httpSession.getId());
response.addJSessionId(session.getId());
}
final String jSessionId = request.getJSessionId();
response.addJSessionId(jSessionId);
response.setStatusCode(StatusCode.FOUND);
response.addHeader("Location", "/index.html");
}
Expand Down

0 comments on commit ac53939

Please sign in to comment.