-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3단계 - Tomcat 구현하기] 구름(김민수) 미션 제출합니다. (#665)
* fix: replace javax with jakarta on imports (#500) * refactor: 세션 클래스 이동 * refactor: step3 요구사항 형식에 맞게 수정 * refactor: front controller를 톰캣에 넘겨서 의존 방향 개선 * refactor: 객체지향적으로 개선 * test: 세션 테스트 추가 * test: 쿠키 테스트 추가 * test: request 테스트 * test: response 테스트 * refactor: Post NotFound 처리 * refactor: 쿠키 getOrDefault로 변경 --------- Co-authored-by: Donghoon Lee <[email protected]>
- Loading branch information
Showing
51 changed files
with
1,138 additions
and
302 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
package com.techcourse; | ||
|
||
import java.util.Map; | ||
import com.techcourse.handler.FrontController; | ||
import com.techcourse.handler.HandlerMapping; | ||
import com.techcourse.handler.IndexHandler; | ||
import com.techcourse.handler.LoginHandler; | ||
import com.techcourse.handler.RegisterHandler; | ||
import com.techcourse.handler.UserHandler; | ||
import org.apache.catalina.Controller; | ||
import org.apache.catalina.startup.Tomcat; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
final var tomcat = new Tomcat(); | ||
HandlerMapping handlerMapping = new HandlerMapping(Map.of( | ||
"/", new IndexHandler(), | ||
"/login", new LoginHandler(), | ||
"/register", new RegisterHandler(), | ||
"/user", new UserHandler() | ||
)); | ||
Controller controller = new FrontController(handlerMapping); | ||
|
||
Tomcat tomcat = new Tomcat(controller); | ||
tomcat.start(); | ||
} | ||
} |
29 changes: 12 additions & 17 deletions
29
tomcat/src/main/java/com/techcourse/handler/FrontController.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 |
---|---|---|
@@ -1,44 +1,39 @@ | ||
package com.techcourse.handler; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.catalina.Controller; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class FrontController { | ||
public class FrontController implements Controller { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(FrontController.class); | ||
public static final List<String> ALLOWED_STATIC_RESOURCES = List.of("html", "css", "js", "png", "jpg", "ico", "svg"); | ||
|
||
private final Map<String, RequestHandler> handlers = new HashMap<>(); | ||
private final HandlerMapping handlerMapping; | ||
|
||
public FrontController() { | ||
handlers.put("/", new IndexHandler()); | ||
handlers.put("/login", new LoginHandler()); | ||
handlers.put("/register", new RegisterHandler()); | ||
handlers.put("/user", new UserHandler()); | ||
public FrontController(HandlerMapping handlerMapping) { | ||
this.handlerMapping = handlerMapping; | ||
} | ||
|
||
public void handleRequest(HttpRequest request, HttpResponse response) throws IOException { | ||
RequestHandler handler = handlers.get(request.getPath()); | ||
public void service(HttpRequest request, HttpResponse response) throws IOException { | ||
Controller handler = handlerMapping.getController(request); | ||
log.debug("Request Line: {}", request.getRequestLine()); | ||
log.debug("Body: {}", request.getParams()); | ||
|
||
if (handler != null) { | ||
handler.handle(request, response); | ||
handler.service(request, response); | ||
return; | ||
} | ||
|
||
List<String> allowedStaticResources = List.of("html", "css", "js", "png", "jpg", "ico", "svg"); | ||
if (allowedStaticResources.contains(request.getExtension())) { | ||
new StaticResourceHandler().handle(request, response); | ||
if (ALLOWED_STATIC_RESOURCES.contains(request.getExtension())) { | ||
new StaticResourceHandler().service(request, response); | ||
return; | ||
} | ||
|
||
response.sendRedirect("/404.html"); | ||
response.write(); | ||
new NotFoundHandler().service(request, response); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tomcat/src/main/java/com/techcourse/handler/HandlerMapping.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,20 @@ | ||
package com.techcourse.handler; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.catalina.Controller; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
|
||
public class HandlerMapping { | ||
|
||
private final Map<String, Controller> handlers; | ||
|
||
public HandlerMapping(Map<String, Controller> handlers) { | ||
this.handlers = new HashMap<>(handlers); | ||
} | ||
|
||
public Controller getController(HttpRequest request) { | ||
String path = request.getPathWithExtension(); | ||
return handlers.get(path); | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
tomcat/src/main/java/com/techcourse/handler/IndexHandler.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
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
26 changes: 26 additions & 0 deletions
26
tomcat/src/main/java/com/techcourse/handler/NotFoundHandler.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,26 @@ | ||
package com.techcourse.handler; | ||
|
||
import java.io.IOException; | ||
import org.apache.catalina.AbstractController; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
|
||
public class NotFoundHandler extends AbstractController { | ||
|
||
@Override | ||
public void doGet(HttpRequest request, HttpResponse response) throws IOException { | ||
responseTo404Page(response); | ||
} | ||
|
||
@Override | ||
public void doPost(HttpRequest request, HttpResponse response) throws IOException { | ||
responseTo404Page(response); | ||
} | ||
|
||
private void responseTo404Page(HttpResponse response) throws IOException { | ||
response.setHttpStatus(HttpStatus.NOT_FOUND); | ||
response.setStaticResourceResponse("/404.html"); | ||
response.write(); | ||
} | ||
} |
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
10 changes: 0 additions & 10 deletions
10
tomcat/src/main/java/com/techcourse/handler/RequestHandler.java
This file was deleted.
Oops, something went wrong.
5 changes: 3 additions & 2 deletions
5
tomcat/src/main/java/com/techcourse/handler/StaticResourceHandler.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
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
8 changes: 4 additions & 4 deletions
8
...ourse/handler/AbstractRequestHandler.java → ...g/apache/catalina/AbstractController.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
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,10 @@ | ||
package org.apache.catalina; | ||
|
||
import java.io.IOException; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
public interface Controller { | ||
|
||
void service(HttpRequest request, HttpResponse response) throws IOException; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
.../apache/coyote/http11/SessionManager.java → ...a/org/apache/catalina/SessionManager.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
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
8 changes: 0 additions & 8 deletions
8
tomcat/src/main/java/org/apache/coyote/http11/HttpMethod.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.