-
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단계] 망고(고재철) 미션 제출합니다. (#460)
* docs: 3, 4단계 요구사항 정리 * refactor: RequestLine 객체 생성 * refactor: RequestHeader 객체 생성 * refactor: HttpRequest 객체 분리 * refactor: StatusLine 객체 생성 * refactor: ResponseHeader 객체 생성 * refactor: HttpResponse 객체 분리 * feat: ResponseBody 객체 추가 * test: HttpResponse toString 테스트 추가 * feat: HttpResponse 빈 객체 생성자 추가 * feat: Controller 인터페이스 추가 * feat: HomeController 구현 * feat: ResourceController 구현 * feat: RegisterController 구현 * feat: LoginController 구현 * refactor: RequestMapping 객체 추가 * test: 캐시 학습 테스트 완료 * test: Thread 학습 테스트 완료 * feat: Thread Pool 적용 * refactor: SessionManager 동시성 컬렉션 적용 * refactor: 405 Method Not Allowed 반영 * refactor: HttpMethod enum 생성 * refactor: Request Parameter 구하는 책임 변경 * refactor: HttpRequest에서 Cookie 값 반환하도록 책임 변경 * refactor: httpRequest, httpResponse 변수명 통일
- Loading branch information
1 parent
ce909aa
commit 82e4db7
Showing
40 changed files
with
1,649 additions
and
214 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
6 changes: 6 additions & 0 deletions
6
study/src/main/java/cache/com/example/cachecontrol/CacheWebConfig.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,13 +1,19 @@ | ||
package cache.com.example.cachecontrol; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.CacheControl; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import org.springframework.web.servlet.mvc.WebContentInterceptor; | ||
|
||
@Configuration | ||
public class CacheWebConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addInterceptors(final InterceptorRegistry registry) { | ||
final var cacheControl = CacheControl.noCache().cachePrivate(); | ||
final var webContentInterceptor = new WebContentInterceptor(); | ||
webContentInterceptor.addCacheMapping(cacheControl, "/**"); | ||
registry.addInterceptor(webContentInterceptor); | ||
} | ||
} |
15 changes: 11 additions & 4 deletions
15
study/src/main/java/cache/com/example/etag/EtagFilterConfiguration.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,12 +1,19 @@ | ||
package cache.com.example.etag; | ||
|
||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.filter.ShallowEtagHeaderFilter; | ||
|
||
import static cache.com.example.version.CacheBustingWebConfig.PREFIX_STATIC_RESOURCES; | ||
|
||
@Configuration | ||
public class EtagFilterConfiguration { | ||
|
||
// @Bean | ||
// public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() { | ||
// return null; | ||
// } | ||
@Bean | ||
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() { | ||
final var filterRegistrationBean = new FilterRegistrationBean<>(new ShallowEtagHeaderFilter()); | ||
filterRegistrationBean.addUrlPatterns("/etag", PREFIX_STATIC_RESOURCES + "/*"); | ||
return filterRegistrationBean; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,3 +7,6 @@ server: | |
max-connections: 1 | ||
threads: | ||
max: 2 | ||
compression: | ||
enabled: true | ||
min-response-size: 10 |
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
37 changes: 37 additions & 0 deletions
37
tomcat/src/main/java/nextstep/jwp/controller/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import nextstep.jwp.exception.HttpRequestException; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public abstract class AbstractController implements Controller { | ||
|
||
protected static final String TEXT_HTML = "text/html;charset=utf-8"; | ||
protected static final String TEXT_CSS = "text/css;"; | ||
protected static final String INDEX_PAGE = "/index.html"; | ||
protected static final String UNAUTHORIZED_PAGE = "/401.html"; | ||
protected static final String HEADER_LOCATION = "Location"; | ||
protected static final String HEADER_SET_COOKIE = "Set-Cookie"; | ||
protected static final String HEADER_CONTENT_TYPE = "Content-Type"; | ||
protected static final String HEADER_CONTENT_LENGTH = "Content-Length"; | ||
protected static final String HTTP_METHOD_EXCEPTION_MESSAGE = "올바르지 않은 HTTP Method 입니다."; | ||
|
||
@Override | ||
public void service(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException { | ||
if (httpRequest.getRequestLine().getMethod().equals("POST")) { | ||
doPost(httpRequest, httpResponse); | ||
return; | ||
} | ||
if (httpRequest.getRequestLine().getMethod().equals("GET")) { | ||
doGet(httpRequest, httpResponse); | ||
return; | ||
} | ||
throw new HttpRequestException(HTTP_METHOD_EXCEPTION_MESSAGE); | ||
} | ||
|
||
protected abstract void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException; | ||
|
||
protected abstract void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException; | ||
} |
11 changes: 11 additions & 0 deletions
11
tomcat/src/main/java/nextstep/jwp/controller/Controller.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,11 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Controller { | ||
|
||
void service(final HttpRequest request, final HttpResponse httpResponse) throws IOException; | ||
} |
26 changes: 26 additions & 0 deletions
26
tomcat/src/main/java/nextstep/jwp/controller/HomeController.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 nextstep.jwp.controller; | ||
|
||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.ResponseBody; | ||
import org.apache.coyote.http11.response.StatusLine; | ||
|
||
public class HomeController extends AbstractController { | ||
|
||
@Override | ||
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.METHOD_NOT_ALLOWED); | ||
httpResponse.setStatusLine(statusLine); | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.OK); | ||
final var responseBody = ResponseBody.fromText("Hello world!"); | ||
httpResponse.setStatusLine(statusLine); | ||
httpResponse.addResponseHeader("Content-Type", TEXT_HTML); | ||
httpResponse.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length)); | ||
httpResponse.setResponseBody(responseBody); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
tomcat/src/main/java/nextstep/jwp/controller/LoginController.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,78 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.HttpCookie; | ||
import org.apache.coyote.http11.Session; | ||
import org.apache.coyote.http11.SessionManager; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.ResponseBody; | ||
import org.apache.coyote.http11.response.StatusLine; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class LoginController extends AbstractController { | ||
|
||
private static final SessionManager SESSION_MANAGER = new SessionManager(); | ||
|
||
@Override | ||
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final HttpCookie cookie = httpRequest.getCookie(); | ||
final User user = findUserBySessionId(cookie.getJSessionId()); | ||
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.FOUND); | ||
httpResponse.setStatusLine(statusLine); | ||
if (user == null) { | ||
handleFirstLogin(httpRequest, httpResponse); | ||
return; | ||
} | ||
httpResponse.addResponseHeader(HEADER_LOCATION, INDEX_PAGE); | ||
} | ||
|
||
private void handleFirstLogin(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final Map<String, String> requestBodyValues = httpRequest.getRequestParameters(); | ||
final Optional<User> user = InMemoryUserRepository.findByAccount(requestBodyValues.get("account")); | ||
if (user.isEmpty() || !user.get().checkPassword(requestBodyValues.get("password"))) { | ||
httpResponse.addResponseHeader(HEADER_LOCATION, UNAUTHORIZED_PAGE); | ||
return; | ||
} | ||
final String sessionId = addSession(user.get()); | ||
httpResponse.addResponseHeader(HEADER_LOCATION, INDEX_PAGE); | ||
httpResponse.addResponseHeader(HEADER_SET_COOKIE, "JSESSIONID=" + sessionId); | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException { | ||
final HttpCookie cookie = httpRequest.getCookie(); | ||
final User user = findUserBySessionId(cookie.getJSessionId()); | ||
if (user != null) { | ||
doPost(httpRequest, httpResponse); | ||
return; | ||
} | ||
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.OK); | ||
final var responseBody = ResponseBody.fromUri("/login.html"); | ||
httpResponse.setStatusLine(statusLine); | ||
httpResponse.addResponseHeader(HEADER_CONTENT_TYPE, TEXT_HTML); | ||
httpResponse.addResponseHeader(HEADER_CONTENT_LENGTH, String.valueOf(responseBody.getBody().getBytes().length)); | ||
httpResponse.setResponseBody(responseBody); | ||
} | ||
|
||
private User findUserBySessionId(final String sessionId) { | ||
if (sessionId == null) { | ||
return null; | ||
} | ||
final Session session = SESSION_MANAGER.findSession(sessionId) | ||
.orElseGet(Session::create); | ||
return (User) session.getAttribute("user"); | ||
} | ||
|
||
private String addSession(final User user) { | ||
final var session = Session.create(); | ||
session.setAttribute("user", user); | ||
SESSION_MANAGER.add(session); | ||
return session.getId(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tomcat/src/main/java/nextstep/jwp/controller/RegisterController.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,37 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.ResponseBody; | ||
import org.apache.coyote.http11.response.StatusLine; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class RegisterController extends AbstractController { | ||
|
||
@Override | ||
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final Map<String, String> requestBodyValues = httpRequest.getRequestParameters(); | ||
final var user = new User(requestBodyValues.get("account"), requestBodyValues.get("password"), | ||
requestBodyValues.get("email")); | ||
InMemoryUserRepository.save(user); | ||
|
||
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.FOUND); | ||
httpResponse.setStatusLine(statusLine); | ||
httpResponse.addResponseHeader("Location", INDEX_PAGE); | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException { | ||
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.OK); | ||
final var responseBody = ResponseBody.fromUri("/register.html"); | ||
httpResponse.setStatusLine(statusLine); | ||
httpResponse.addResponseHeader("Content-Type", TEXT_HTML); | ||
httpResponse.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length)); | ||
httpResponse.setResponseBody(responseBody); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tomcat/src/main/java/nextstep/jwp/controller/ResourceController.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.jwp.controller; | ||
|
||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.ResponseBody; | ||
import org.apache.coyote.http11.response.StatusLine; | ||
|
||
import java.io.IOException; | ||
|
||
public class ResourceController extends AbstractController { | ||
|
||
@Override | ||
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.METHOD_NOT_ALLOWED); | ||
httpResponse.setStatusLine(statusLine); | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException { | ||
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.OK); | ||
final String uri = httpRequest.getRequestLine().getPath().split("\\?")[0]; | ||
final var responseBody = ResponseBody.fromUri(uri); | ||
httpResponse.setStatusLine(statusLine); | ||
httpResponse.addResponseHeader("Content-Type", getContentType(uri)); | ||
httpResponse.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length)); | ||
httpResponse.setResponseBody(responseBody); | ||
} | ||
|
||
private String getContentType(final String uri) { | ||
if (uri.endsWith(".css")) { | ||
return TEXT_CSS; | ||
} | ||
return TEXT_HTML; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tomcat/src/main/java/nextstep/jwp/exception/HttpRequestException.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,8 @@ | ||
package nextstep.jwp.exception; | ||
|
||
public class HttpRequestException extends RuntimeException { | ||
|
||
public HttpRequestException(final String message) { | ||
super(message); | ||
} | ||
} |
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
Oops, something went wrong.