-
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.
[톰캣 구현하기 - 3, 4단계] 히이로(문제웅) 미션 제출합니다. (#455)
* refactor: httpRequest 객체 분리 * refactor: InputStream의 보조 스트림은 request 객체 내부로 캡슐화 * refactor: httpRequest 에서 cookies 반환 시 방어적 복사 기능 구현 * refactor: Http11Response 객체 분리 * refactor: HttpRequest 객체명 Http11Request로 수정 * feat: 501 status enum 추가 * feat: Servlet 인터페이스 및 AbstractServlet 클래스 구현 * feat: LoginServlet 기능 분리 및 구현 * feat: RegisterServlet 기능 분리 및 구현 * feat: 서블릿 컨테이너 Context 클래스 구현 및 DefaultServlet 구현 * feat: 응답 메세지 바디 생성 메서드 공통 Util 클래스로 분리 * style: catalina, coyote 패키지 역할 별 분리 * test: thread 관련 학습 테스트 진행 * feat: tomcat connector 쓰레드 풀 설정 추가 * feat: 로그인 session 동시성 관리를 위한 ConcurrentHashMap 사용 * feat: HttpHeader enum 작성 * feat: Connector 상수 분리 * feat: Connector 쓰레드 풀 재설정 * refactor: Servlet 메서드 순서 변경 * refactor: AbstractServlet 상수 분리 * refactor: LoginServlet 객체 static 선언 추가 * refactor: LoginServlet 객체 내 SessionManager 전역으로 관리하도록 수정 * refactor: context를 Tomcat 객체에서 생성 후 의존성 주입하는 방향으로 수정 * refactor: startLine 객체 내 queryString을 queryParams로 수정
- Loading branch information
1 parent
d712003
commit aa584ed
Showing
32 changed files
with
653 additions
and
357 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
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
28 changes: 0 additions & 28 deletions
28
tomcat/src/main/java/nextstep/jwp/controller/LoginController.java
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
tomcat/src/main/java/nextstep/jwp/dto/LoginResponseDto.java
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
tomcat/src/main/java/nextstep/jwp/service/LoginService.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
tomcat/src/main/java/nextstep/org/apache/catalina/Context.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,36 @@ | ||
package nextstep.org.apache.catalina; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import nextstep.org.apache.catalina.servlet.DefaultServlet; | ||
import nextstep.org.apache.catalina.servlet.LoginServlet; | ||
import nextstep.org.apache.catalina.servlet.RegisterServlet; | ||
import nextstep.org.apache.catalina.servlet.Servlet; | ||
|
||
public class Context { | ||
|
||
private static final Map<String, Servlet> servletMappings; | ||
private static final String EXTENSION_DELIMITER = "."; | ||
|
||
static { | ||
servletMappings = new HashMap<>(); | ||
servletMappings.put("/login", new LoginServlet()); | ||
servletMappings.put("/register", new RegisterServlet()); | ||
servletMappings.put("default", new DefaultServlet()); | ||
} | ||
|
||
public Servlet getServlet(String pathInfo) { | ||
if (servletMappings.containsKey(removeExtension(pathInfo))) { | ||
return servletMappings.get(removeExtension(pathInfo)); | ||
} | ||
return servletMappings.get("default"); | ||
} | ||
|
||
private String removeExtension(String pathInfo) { | ||
int idx = pathInfo.indexOf(EXTENSION_DELIMITER); | ||
if (idx == -1) { | ||
return pathInfo; | ||
} | ||
return pathInfo.substring(0,idx); | ||
} | ||
} |
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
88 changes: 88 additions & 0 deletions
88
tomcat/src/main/java/nextstep/org/apache/catalina/servlet/AbstractServlet.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,88 @@ | ||
package nextstep.org.apache.catalina.servlet; | ||
|
||
import static nextstep.org.apache.coyote.http11.HttpUtil.selectFirstContentTypeOrDefault; | ||
|
||
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; | ||
import java.util.Optional; | ||
import nextstep.org.apache.coyote.http11.HttpHeader; | ||
import nextstep.org.apache.coyote.http11.request.Http11Request; | ||
import nextstep.org.apache.coyote.http11.response.Http11Response; | ||
import nextstep.org.apache.coyote.http11.Status; | ||
|
||
public abstract class AbstractServlet implements Servlet{ | ||
|
||
private static final String RESOURCES_PATH_PREFIX = "static"; | ||
protected static final String NOT_FOUND_DEFAULT_MESSAGE = "404 Not Found"; | ||
private static final String CHARSET_UTF_8 = ";charset=utf-8"; | ||
private static final String DEFAULT_EXTENSION = ".html"; | ||
private static final String NOT_FOUND_PAGE = "/404.html"; | ||
private static final String HTTP_GET_METHOD = "GET"; | ||
private static final String HTTP_POST_METHOD = "POST"; | ||
|
||
@Override | ||
public void service(Http11Request request, Http11Response response) throws Exception { | ||
String method = request.getMethod(); | ||
|
||
if (method.equals(HTTP_GET_METHOD)) { | ||
doGet(request, response); | ||
} else if (method.equals(HTTP_POST_METHOD)) { | ||
doPost(request, response); | ||
} else { | ||
response.setStatus(Status.NOT_IMPLEMENTED); | ||
} | ||
} | ||
|
||
protected abstract void doGet(Http11Request request, Http11Response response) throws Exception; | ||
|
||
protected abstract void doPost(Http11Request request, Http11Response response) throws Exception; | ||
|
||
protected Optional<String> createResponseBody(String requestPath) throws IOException { | ||
if (requestPath.equals("/")) { | ||
return Optional.of("Hello world!"); | ||
} | ||
|
||
String resourceName = RESOURCES_PATH_PREFIX + requestPath; | ||
if (!resourceName.contains(".")) { | ||
resourceName += DEFAULT_EXTENSION; | ||
} | ||
URL resource = getClass().getClassLoader().getResource(resourceName); | ||
|
||
if (Objects.isNull(resource)) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new String(Files.readAllBytes(new File(resource.getFile()).toPath()))); | ||
} | ||
|
||
protected void responseWithBody(Http11Request request, Http11Response response) throws IOException { | ||
Optional<String> responseBody = createResponseBody(request.getPathInfo()); | ||
String contentType = selectFirstContentTypeOrDefault(request.getHeader(HttpHeader.ACCEPT)); | ||
|
||
if (responseBody.isEmpty()) { | ||
responseWithNotFound(request, response); | ||
return; | ||
} | ||
|
||
response.setStatus(Status.OK) | ||
.setHeader(HttpHeader.CONTENT_TYPE, contentType + CHARSET_UTF_8) | ||
.setHeader(HttpHeader.CONTENT_LENGTH, String.valueOf( | ||
responseBody.get().getBytes(StandardCharsets.UTF_8).length)) | ||
.setBody(responseBody.get()); | ||
} | ||
|
||
private void responseWithNotFound(Http11Request request, Http11Response response) throws IOException { | ||
String notFoundPageBody = createResponseBody(NOT_FOUND_PAGE) | ||
.orElse(NOT_FOUND_DEFAULT_MESSAGE); | ||
String contentType = selectFirstContentTypeOrDefault(request.getHeader(HttpHeader.ACCEPT)); | ||
|
||
response.setStatus(Status.NOT_FOUND) | ||
.setHeader(HttpHeader.CONTENT_TYPE, contentType + CHARSET_UTF_8) | ||
.setHeader(HttpHeader.CONTENT_LENGTH, String.valueOf( | ||
notFoundPageBody.getBytes(StandardCharsets.UTF_8).length)) | ||
.setBody(notFoundPageBody); | ||
} | ||
} |
Oops, something went wrong.