-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[톰캣 구현하기 3, 4단계] 키아라(김도희) 미션 제출합니다 #487
Changes from 8 commits
0f76244
4fb087f
babbe58
e760088
95a97b1
b4d710c
7c77cce
ac42d34
b4e37f2
4902f38
3dfb1ec
dd09d04
299ddca
5c3403c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cache.com.example.cachecontrol; | ||
|
||
import org.springframework.http.CacheControl; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import static org.springframework.http.HttpHeaders.CACHE_CONTROL; | ||
|
||
public class CacheControlInterceptor implements HandlerInterceptor { | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
CacheControl cacheControl = CacheControl.noCache().cachePrivate(); | ||
String cacheControlValue = cacheControl.getHeaderValue(); | ||
response.addHeader(CACHE_CONTROL, cacheControlValue); | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
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; | ||
|
||
@Configuration | ||
public class EtagFilterConfiguration { | ||
|
||
// @Bean | ||
// public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() { | ||
// return null; | ||
// } | ||
@Bean | ||
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() { | ||
final var registration = new FilterRegistrationBean<>(new ShallowEtagHeaderFilter()); | ||
registration.addUrlPatterns("/etag"); | ||
registration.addUrlPatterns("/resources/*"); | ||
return registration; | ||
} | ||
} |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package nextstep.jwp.exception; | ||
|
||
public class ResourceNotFoundException extends RuntimeException { | ||
|
||
public ResourceNotFoundException(Exception e) { | ||
super(e); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package nextstep.jwp.presentation; | ||
|
||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
import static org.apache.coyote.http11.Method.*; | ||
|
||
public abstract class AbstractController implements Controller { | ||
|
||
@Override | ||
public HttpResponse service(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
|
||
if (GET.matches(httpRequest.getMethod())) { | ||
return doGet(httpRequest, httpResponse); | ||
} | ||
if (POST.matches(httpRequest.getMethod())) { | ||
return doPost(httpRequest, httpResponse); | ||
} | ||
throw new IllegalArgumentException("지원하지 않는 HTTP Method 입니다."); | ||
} | ||
|
||
protected abstract HttpResponse doGet(HttpRequest httpRequest, HttpResponse httpResponse); | ||
|
||
protected abstract HttpResponse doPost(HttpRequest httpRequest, HttpResponse httpResponse); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
package nextstep.jwp.presentation; | ||
|
||
import org.apache.coyote.http11.request.RequestReader; | ||
import org.apache.coyote.http11.response.Response; | ||
|
||
import java.io.IOException; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
public interface Controller { | ||
|
||
Response service(RequestReader requestReader) throws IOException; | ||
HttpResponse service(HttpRequest httpRequest, HttpResponse httpResponse); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
package nextstep.jwp.presentation; | ||
|
||
import org.apache.coyote.http11.request.RequestReader; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.request.FileReader; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
public class IndexController extends AbstractController { | ||
|
||
import static org.apache.coyote.http11.response.StatusCode.OK; | ||
private static final IndexController INSTANCE = new IndexController(); | ||
|
||
public class IndexController implements Controller { | ||
private IndexController() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. private으로 잘 막아주셨네요!! |
||
} | ||
|
||
public static IndexController getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public HttpResponse doGet(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
String body = FileReader.read(httpRequest.getUri()); | ||
return httpResponse | ||
.addBaseHeader(httpRequest.getContentType()) | ||
.addBody(body); | ||
} | ||
|
||
@Override | ||
public Response service(RequestReader requestReader) throws IOException { | ||
return new Response() | ||
.addResponseLine(requestReader.getProtocol(), OK) | ||
.addBaseHeader(requestReader.getContentType()) | ||
.createBodyByFile(requestReader.getUri()); | ||
public HttpResponse doPost(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,75 +2,65 @@ | |
|
||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.request.RequestReader; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.request.FileReader; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
import org.apache.coyote.http11.response.SessionManager; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.apache.coyote.http11.response.Header.LOCATION; | ||
import static org.apache.coyote.http11.Method.GET; | ||
import static org.apache.coyote.http11.Method.POST; | ||
import static org.apache.coyote.http11.response.StatusCode.FOUND; | ||
import static org.apache.coyote.http11.response.StatusCode.OK; | ||
import static org.apache.coyote.http11.response.StatusCode.UNAUTHORIZED; | ||
import static org.reflections.Reflections.log; | ||
|
||
public class LoginController implements Controller { | ||
|
||
private static final String INDEX = "/index.html"; | ||
private static final String UNAUTHORIZED_HTML = "/401.html"; | ||
public class LoginController extends AbstractController { | ||
|
||
@Override | ||
public Response service(RequestReader requestReader) throws IOException { | ||
String method = requestReader.getMethod(); | ||
private static final LoginController INSTANCE = new LoginController(); | ||
|
||
if (GET.matches(method)) { | ||
return loginPage(requestReader); | ||
} | ||
if (POST.matches(method)) { | ||
return tryLogin(requestReader); | ||
} | ||
private LoginController() { | ||
} | ||
|
||
return null; | ||
public static LoginController getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
private Response loginPage(RequestReader requestReader) throws IOException { | ||
if (requestReader.hasSessionId()) { | ||
return new Response() | ||
.addResponseLine(requestReader.getProtocol(), FOUND) | ||
.addHeader(LOCATION, INDEX) | ||
.addBaseHeader(requestReader.getContentType()); | ||
@Override | ||
public HttpResponse doGet(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
if (httpRequest.hasSessionId()) { | ||
return httpResponse | ||
.addBaseHeader(httpRequest.getContentType()) | ||
.redirect("/index.html"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 다 |
||
} | ||
return new Response() | ||
.addResponseLine(requestReader.getProtocol(), OK) | ||
.addBaseHeader(requestReader.getContentType()) | ||
.createBodyByFile(requestReader.getUri()); | ||
String body = FileReader.read(httpRequest.getUri()); | ||
return httpResponse | ||
.addBaseHeader(httpRequest.getContentType()) | ||
.addBody(body); | ||
} | ||
|
||
private Response tryLogin(RequestReader requestReader) throws IOException { | ||
@Override | ||
public HttpResponse doPost(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
try { | ||
String sessionId = login(requestReader); | ||
return new Response() | ||
.addResponseLine(requestReader.getProtocol(), FOUND) | ||
.addBaseHeader(requestReader.getContentType()) | ||
.addHeader(LOCATION, INDEX) | ||
.setCookie(sessionId); | ||
login(httpRequest, httpResponse); | ||
return httpResponse | ||
.addBaseHeader(httpRequest.getContentType()) | ||
.redirect("/index.html"); | ||
|
||
} catch (IllegalArgumentException e) { | ||
return new Response() | ||
.addResponseLine(requestReader.getProtocol(), UNAUTHORIZED) | ||
.addBaseHeader(requestReader.getContentType()) | ||
.createBodyByFile(UNAUTHORIZED_HTML); | ||
String body = FileReader.read("/401.html"); | ||
return httpResponse | ||
.setStatusCode(UNAUTHORIZED) | ||
.addBaseHeader(httpRequest.getContentType()) | ||
.addBody(body); | ||
} | ||
} | ||
|
||
private String login(RequestReader requestReader) { | ||
User user = InMemoryUserRepository.findByAccount(requestReader.getBodyValue("account")) | ||
private void login(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
User user = InMemoryUserRepository.findByAccount(httpRequest.getBodyValue("account")) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 아이디의 사용자가 존재하지 않습니다")); | ||
|
||
if (!user.checkPassword(requestReader.getBodyValue("password"))) { | ||
if (!user.checkPassword(httpRequest.getBodyValue("password"))) { | ||
throw new IllegalArgumentException("비밀번호가 일치하지 않습니다"); | ||
} | ||
|
||
return SessionManager.getSessionId(user); | ||
String sessionId = SessionManager.getSessionId(user); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
httpResponse.setCookie(sessionId); | ||
log.info("로그인 성공: {}", SessionManager.getSession(sessionId).getAttribute("user")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,28 @@ | ||
package nextstep.jwp.presentation; | ||
|
||
import org.apache.coyote.http11.request.RequestReader; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
import static org.apache.coyote.http11.response.StatusCode.OK; | ||
public class MainController extends AbstractController{ | ||
|
||
public class MainController implements Controller{ | ||
private static final MainController INSTANCE = new MainController(); | ||
|
||
private MainController() { | ||
} | ||
|
||
public static MainController getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public HttpResponse doGet(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
return httpResponse | ||
.addBaseHeader(httpRequest.getContentType()) | ||
.addBody("Hello world!"); | ||
} | ||
|
||
@Override | ||
public Response service(RequestReader requestReader) { | ||
return new Response() | ||
.addResponseLine(requestReader.getProtocol(), OK) | ||
.addBaseHeader(requestReader.getContentType()) | ||
.createBodyByText("Hello world!"); | ||
public HttpResponse doPost(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 null을 반환하기보다 예외를 던지는 방식은 어떤가요? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
package nextstep.jwp.presentation; | ||
|
||
import org.apache.coyote.http11.request.RequestReader; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.request.FileReader; | ||
import org.apache.coyote.http11.request.HttpRequest; | ||
import org.apache.coyote.http11.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
public class OtherController extends AbstractController { | ||
|
||
import static org.apache.coyote.http11.response.StatusCode.OK; | ||
private static final OtherController INSTANCE = new OtherController(); | ||
|
||
public class OtherController implements Controller { | ||
private OtherController() { | ||
} | ||
|
||
public static OtherController getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
protected HttpResponse doGet(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
String body = FileReader.read(httpRequest.getUri()); | ||
return httpResponse | ||
.addBaseHeader(httpRequest.getContentType()) | ||
.addBody(body); | ||
} | ||
|
||
@Override | ||
public Response service(RequestReader requestReader) throws IOException { | ||
return new Response() | ||
.addResponseLine(requestReader.getProtocol(), OK) | ||
.addBaseHeader(requestReader.getContentType()) | ||
.createBodyByFile(requestReader.getUri()); | ||
protected HttpResponse doPost(HttpRequest httpRequest, HttpResponse httpResponse) { | ||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3차 요구사항에 맞춰 반환 타입을
void
로 변경하면 좋을 것 같습니다Http11Processor.process()
에서 HttpResponse 객체를 생성하고, 생성한 response에 setter로 response 필드를 추가해주는 형식으로 변경할 수 있을 것 같아요