-
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.
[톰캣 구현하기 1, 2단계] 두둠(최영훈) 미션 제출합니다 (#335)
* test: 파일 입출력 학습 테스트 작성 * feat: GET /index.html 응답하기 구현 * feat: CSS 지원하기 구현 * refactor: 전체 구조 수정 * feat: Query String 파싱 구현 * feat: 로그인 여부에 따라 다른 페이지로 이동 구현 * feat: POST 방식으로 회원가입 구현 및 로그인 Post 방식으로 변경 * feat: Cookie에 JSESSIONID 값 저장하기 구현 * feat: Session 구현 * refactor: 의존관계 리펙토링 * refactor: 패키지 수정
- Loading branch information
1 parent
68db530
commit 3152d3d
Showing
35 changed files
with
970 additions
and
133 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
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 |
---|---|---|
@@ -1,11 +1,29 @@ | ||
package nextstep; | ||
|
||
import nextstep.jwp.HandlerResolver; | ||
import nextstep.jwp.JwpHttpDispatcher; | ||
import nextstep.jwp.SessionManager; | ||
import nextstep.jwp.handler.get.LoginGetHandler; | ||
import nextstep.jwp.handler.get.RegisterGetHandler; | ||
import nextstep.jwp.handler.get.RootGetHandler; | ||
import nextstep.jwp.handler.post.LoginPostHandler; | ||
import nextstep.jwp.handler.post.RegisterPostHandler; | ||
import org.apache.catalina.startup.Tomcat; | ||
import org.apache.coyote.http11.Handler; | ||
import java.util.Map; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
final var tomcat = new Tomcat(); | ||
private static final Map<String, Handler> httpGetHandlers = | ||
Map.of("/", new RootGetHandler(), | ||
"/login", new LoginGetHandler(new SessionManager()), | ||
"/register", new RegisterGetHandler()); | ||
private static final Map<String, Handler> httpPostHandlers = | ||
Map.of("/login", new LoginPostHandler(new SessionManager()), | ||
"/register", new RegisterPostHandler()); | ||
|
||
public static void main(final String[] args) { | ||
final var tomcat = new Tomcat(new JwpHttpDispatcher(new HandlerResolver(httpGetHandlers, httpPostHandlers))); | ||
tomcat.start(); | ||
} | ||
} |
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,26 @@ | ||
package nextstep.jwp; | ||
|
||
import org.apache.coyote.http11.Handler; | ||
import org.apache.coyote.http11.HttpMethod; | ||
import java.util.Map; | ||
|
||
public class HandlerResolver { | ||
|
||
private final Map<String, Handler> httpGetHandlers; | ||
private final Map<String, Handler> httpPostHandlers; | ||
|
||
public HandlerResolver(final Map<String, Handler> httpGetHandlers, final Map<String, Handler> httpPostHandlers) { | ||
this.httpGetHandlers = httpGetHandlers; | ||
this.httpPostHandlers = httpPostHandlers; | ||
} | ||
|
||
public Handler resolve(final HttpMethod httpMethod, final String path) { | ||
if (httpMethod.equals(HttpMethod.GET)) { | ||
return httpGetHandlers.get(path); | ||
} | ||
if (httpMethod.equals(HttpMethod.POST)) { | ||
return httpPostHandlers.get(path); | ||
} | ||
return null; | ||
} | ||
} |
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,46 @@ | ||
package nextstep.jwp; | ||
|
||
import org.apache.coyote.http11.ContentType; | ||
import org.apache.coyote.http11.Handler; | ||
import org.apache.coyote.http11.HttpDispatcher; | ||
import org.apache.coyote.http11.StatusCode; | ||
import org.apache.coyote.http11.request.Http11Request; | ||
import org.apache.coyote.http11.response.Http11Response; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.util.Objects; | ||
|
||
public class JwpHttpDispatcher implements HttpDispatcher { | ||
|
||
private static final String STATIC = "static"; | ||
|
||
private final HandlerResolver handlerResolver; | ||
|
||
public JwpHttpDispatcher(final HandlerResolver handlerResolver) { | ||
this.handlerResolver = handlerResolver; | ||
} | ||
|
||
@Override | ||
public Http11Response handle(final Http11Request request) throws IOException { | ||
final Handler handler = handlerResolver.resolve(request.getHttpMethod(), request.getPath()); | ||
if (handler != null) { | ||
return handler.resolve(request); | ||
} | ||
final var resource = getClass().getClassLoader().getResource(STATIC + request.getPath()); | ||
if (resource == null) { | ||
final var notFoundResource = getClass().getClassLoader().getResource(STATIC + "/404.html"); | ||
return makeHttp11Response(Objects.requireNonNull(notFoundResource), StatusCode.NOT_FOUND); | ||
} | ||
return makeHttp11Response(resource, StatusCode.OK); | ||
} | ||
|
||
private Http11Response makeHttp11Response(final URL resource, final StatusCode statusCode) throws IOException { | ||
final var actualFilePath = new File(resource.getPath()).toPath(); | ||
final var fileBytes = Files.readAllBytes(actualFilePath); | ||
final String responseBody = new String(fileBytes, StandardCharsets.UTF_8); | ||
return new Http11Response(statusCode, ContentType.findByPath(resource.getPath()), responseBody); | ||
} | ||
} |
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,26 @@ | ||
package nextstep.jwp; | ||
|
||
import org.apache.catalina.Manager; | ||
import org.apache.coyote.http11.Session; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class SessionManager implements Manager { | ||
|
||
private static final Map<String, Session> SESSIONS = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void add(final Session session) { | ||
SESSIONS.put(session.getId(), session); | ||
} | ||
|
||
@Override | ||
public Session findSession(final String id) { | ||
return SESSIONS.get(id); | ||
} | ||
|
||
@Override | ||
public void remove(final Session session) { | ||
SESSIONS.remove(session.getId()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
tomcat/src/main/java/nextstep/jwp/exception/BusinessException.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,7 @@ | ||
package nextstep.jwp.exception; | ||
|
||
public class BusinessException extends RuntimeException { | ||
public BusinessException(final String message) { | ||
super(message); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tomcat/src/main/java/nextstep/jwp/handler/get/LoginGetHandler.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,52 @@ | ||
package nextstep.jwp.handler.get; | ||
|
||
import nextstep.jwp.SessionManager; | ||
import nextstep.jwp.exception.BusinessException; | ||
import org.apache.coyote.http11.ContentType; | ||
import org.apache.coyote.http11.Handler; | ||
import org.apache.coyote.http11.Session; | ||
import org.apache.coyote.http11.StatusCode; | ||
import org.apache.coyote.http11.request.Http11Request; | ||
import org.apache.coyote.http11.response.Http11Response; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
|
||
public class LoginGetHandler implements Handler { | ||
|
||
private static final String STATIC = "static"; | ||
|
||
private final SessionManager sessionManager; | ||
|
||
public LoginGetHandler(final SessionManager sessionManager) { | ||
this.sessionManager = sessionManager; | ||
} | ||
|
||
@Override | ||
public Http11Response resolve(final Http11Request request) throws IOException { | ||
if (request.notContainJsessionId()) { | ||
final var resource = getClass().getClassLoader().getResource(STATIC + "/login.html"); | ||
return makeHttp11Response(resource, StatusCode.OK); | ||
} | ||
final String jsessionId = request.findJsessionId(); | ||
final Session session = sessionManager.findSession(jsessionId); | ||
validateSession(session); | ||
final var resource = getClass().getClassLoader().getResource(STATIC + "/index.html"); | ||
return makeHttp11Response(resource, StatusCode.FOUND); | ||
} | ||
|
||
private Http11Response makeHttp11Response(final URL resource, final StatusCode statusCode) throws IOException { | ||
final var actualFilePath = new File(resource.getPath()).toPath(); | ||
final var fileBytes = Files.readAllBytes(actualFilePath); | ||
final String responseBody = new String(fileBytes, StandardCharsets.UTF_8); | ||
return new Http11Response(statusCode, ContentType.findByPath(resource.getPath()), responseBody); | ||
} | ||
|
||
private void validateSession(final Session session) { | ||
if (session == null) { | ||
throw new BusinessException("세션이 적절하지 않습니다."); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tomcat/src/main/java/nextstep/jwp/handler/get/RegisterGetHandler.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,30 @@ | ||
package nextstep.jwp.handler.get; | ||
|
||
import org.apache.coyote.http11.ContentType; | ||
import org.apache.coyote.http11.Handler; | ||
import org.apache.coyote.http11.StatusCode; | ||
import org.apache.coyote.http11.request.Http11Request; | ||
import org.apache.coyote.http11.response.Http11Response; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
|
||
public class RegisterGetHandler implements Handler { | ||
|
||
private static final String STATIC = "static"; | ||
|
||
@Override | ||
public Http11Response resolve(final Http11Request request) throws IOException { | ||
final var resource = getClass().getClassLoader().getResource(STATIC + "/register.html"); | ||
return makeHttp11Response(resource, StatusCode.OK); | ||
} | ||
|
||
private Http11Response makeHttp11Response(final URL resource, final StatusCode statusCode) throws IOException { | ||
final var actualFilePath = new File(resource.getPath()).toPath(); | ||
final var fileBytes = Files.readAllBytes(actualFilePath); | ||
final String responseBody = new String(fileBytes, StandardCharsets.UTF_8); | ||
return new Http11Response(statusCode, ContentType.findByPath(resource.getPath()), responseBody); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tomcat/src/main/java/nextstep/jwp/handler/get/RootGetHandler.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,15 @@ | ||
package nextstep.jwp.handler.get; | ||
|
||
import org.apache.coyote.http11.ContentType; | ||
import org.apache.coyote.http11.Handler; | ||
import org.apache.coyote.http11.StatusCode; | ||
import org.apache.coyote.http11.request.Http11Request; | ||
import org.apache.coyote.http11.response.Http11Response; | ||
|
||
public class RootGetHandler implements Handler { | ||
|
||
@Override | ||
public Http11Response resolve(final Http11Request request) { | ||
return new Http11Response(StatusCode.OK, ContentType.HTML, "Hello world!"); | ||
} | ||
} |
Oops, something went wrong.