-
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단계] 케로(김지현) 미션 제출합니다. #480
Changes from 8 commits
895f85d
7303cd6
6f175a6
b6350b8
0e84058
1803462
54ec71d
7ce8966
4187e31
1ca0c3a
8011ced
b79b279
ec5fa7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import org.apache.coyote.request.Request; | ||
import org.apache.coyote.response.ResponseEntity; | ||
import org.apache.coyote.response.ResponseStatus; | ||
import org.apache.coyote.request.HttpRequest; | ||
import org.apache.coyote.response.HttpResponse; | ||
import org.apache.front.AbstractController; | ||
|
||
public class HelloWorldController implements Controller{ | ||
public class HelloWorldController extends AbstractController { | ||
|
||
@Override | ||
public ResponseEntity handle(final Request request) { | ||
return ResponseEntity.fromString(request, "Hello world!", ResponseStatus.OK); | ||
protected void doGet(final HttpRequest request, final HttpResponse response) { | ||
response.setStringAsBody("Hello world!"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,74 +4,74 @@ | |
import nextstep.jwp.exception.MemberNotFoundException; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.request.Cookie; | ||
import org.apache.coyote.request.Request; | ||
import org.apache.coyote.response.ResponseEntity; | ||
import org.apache.coyote.request.HttpRequest; | ||
import org.apache.coyote.response.HttpResponse; | ||
import org.apache.coyote.response.ResponseStatus; | ||
import org.apache.exception.MethodMappingFailException; | ||
import org.apache.exception.PageRedirectException; | ||
import org.apache.front.AbstractController; | ||
import org.apache.session.Session; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Optional; | ||
|
||
public class LoginController implements Controller { | ||
public class LoginController extends AbstractController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(LoginController.class); | ||
public static final String ACCOUNT_KEY = "account"; | ||
public static final String PASSWORD_KEY = "password"; | ||
private static final Logger log = LoggerFactory.getLogger(LoginController.class); | ||
|
||
@Override | ||
public ResponseEntity handle(final Request request) { | ||
if (request.isPost()) { | ||
return login(request); | ||
} | ||
if (request.isGet() && request.hasQueryString()) { | ||
return loginInConsole(request); | ||
} | ||
if (request.isGet()) { | ||
return loginPage(request); | ||
protected void doPost(final HttpRequest request, final HttpResponse response) { | ||
login(request, response); | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpRequest request, final HttpResponse response) { | ||
if (request.hasQueryString()) { | ||
loginInConsole(request, response); | ||
return; | ||
} | ||
throw new MethodMappingFailException(); | ||
loginPage(request, response); | ||
} | ||
|
||
private ResponseEntity loginInConsole(final Request request) { | ||
final String account = request.getQueryValueBy(ACCOUNT_KEY); | ||
final String password = request.getQueryValueBy(PASSWORD_KEY); | ||
private void loginInConsole(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final String account = httpRequest.getQueryValueBy(ACCOUNT_KEY); | ||
final String password = httpRequest.getQueryValueBy(PASSWORD_KEY); | ||
|
||
final User user = InMemoryUserRepository.findByAccount(account) | ||
.orElseThrow(MemberNotFoundException::new); | ||
if (user.checkPassword(password)) { | ||
log.info("user : {}", user); | ||
} | ||
return ResponseEntity.fromViewPath(request.httpVersion(), request.getPath(), ResponseStatus.OK); | ||
httpResponse.setViewPathAsBody(httpRequest.getPath()); | ||
} | ||
|
||
private ResponseEntity login(final Request request) { | ||
final String account = request.getBodyValue(ACCOUNT_KEY); | ||
final String password = request.getBodyValue(PASSWORD_KEY); | ||
private void login(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final String account = httpRequest.getBodyValue(ACCOUNT_KEY); | ||
final String password = httpRequest.getBodyValue(PASSWORD_KEY); | ||
final User user = InMemoryUserRepository.findByAccount(account) | ||
.orElseThrow(() -> new PageRedirectException.Unauthorized(request.httpVersion())); | ||
.orElseThrow(() -> new PageRedirectException.Unauthorized(httpResponse)); | ||
|
||
if (user.checkPassword(password)) { | ||
final Session session = request.getSession(false); | ||
final Session session = httpRequest.getSession(false); | ||
session.setAttribute("user", user); | ||
final ResponseEntity responseEntity = ResponseEntity.fromViewPath(request.httpVersion(), request.getPath(), ResponseStatus.MOVED_TEMP); | ||
responseEntity.setRedirect("/index.html"); | ||
responseEntity.addCookie(Cookie.ofJSessionId(session.getId())); | ||
return responseEntity; | ||
httpResponse.setViewPathAsBodyAndSetStatus(httpRequest.getPath(), ResponseStatus.MOVED_TEMP); | ||
httpResponse.setRedirect("/index.html"); | ||
httpResponse.addCookie(Cookie.ofJSessionId(session.getId())); | ||
return; | ||
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. 로그인에 성공하면 302 헤더와 함께 index.html 로 redirect 하도록 응답을 보내는 것 같아요! 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. 헉 그러게요... |
||
} | ||
throw new PageRedirectException.Unauthorized(request.httpVersion()); | ||
throw new PageRedirectException.Unauthorized(httpResponse); | ||
} | ||
|
||
private ResponseEntity loginPage(final Request request) { | ||
final Session session = request.getSession(false); | ||
private void loginPage(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final Session session = httpRequest.getSession(false); | ||
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.
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. getSession의 메서드 파라미터 명을 수정하여 다시 작성했습니다. |
||
final Optional<Object> user = session.getAttribute("user"); | ||
if(user.isPresent()){ | ||
final ResponseEntity responseEntity = ResponseEntity.fromViewPath(request.httpVersion(), request.getPath(), ResponseStatus.MOVED_TEMP); | ||
responseEntity.setRedirect("/index.html"); | ||
return responseEntity; | ||
if (user.isPresent()) { | ||
httpResponse.setViewPathAsBodyAndSetStatus(httpRequest.getPath(), ResponseStatus.MOVED_TEMP); | ||
httpResponse.setRedirect("/index.html"); | ||
return; | ||
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. 59번 라인의 코멘트와 동일합니다! |
||
} | ||
return ResponseEntity.fromViewPath(request.httpVersion(), request.getPath(), ResponseStatus.OK); | ||
httpResponse.setViewPathAsBody(httpRequest.getPath()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,47 +3,46 @@ | |
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.exception.DuplicationMemberException; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.request.Request; | ||
import org.apache.coyote.response.ResponseEntity; | ||
import org.apache.coyote.request.HttpRequest; | ||
import org.apache.coyote.response.HttpResponse; | ||
import org.apache.coyote.response.ResponseStatus; | ||
import org.apache.exception.MethodMappingFailException; | ||
import org.apache.front.AbstractController; | ||
|
||
import java.util.Optional; | ||
|
||
public class RegisterController implements Controller { | ||
public class RegisterController extends AbstractController { | ||
|
||
public static final String ACCOUNT_KEY = "account"; | ||
public static final String PASSWORD_KEY = "password"; | ||
public static final String EMAIL_KEY = "email"; | ||
|
||
@Override | ||
public ResponseEntity handle(final Request request) { | ||
if (request.isPost()) { | ||
return join(request); | ||
} | ||
if (request.isGet()) { | ||
return joinPage(request); | ||
} | ||
throw new MethodMappingFailException(); | ||
protected void doPost(final HttpRequest request, final HttpResponse response) { | ||
join(request, response); | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpRequest request, final HttpResponse response) { | ||
joinPage(request, response); | ||
} | ||
|
||
private ResponseEntity join(final Request request) { | ||
final String account = request.getBodyValue(ACCOUNT_KEY); | ||
final String password = request.getBodyValue(PASSWORD_KEY); | ||
final String email = request.getBodyValue(EMAIL_KEY); | ||
private void join(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
final String account = httpRequest.getBodyValue(ACCOUNT_KEY); | ||
final String password = httpRequest.getBodyValue(PASSWORD_KEY); | ||
final String email = httpRequest.getBodyValue(EMAIL_KEY); | ||
|
||
final Optional<User> user = InMemoryUserRepository.findByAccount(account); | ||
if (user.isPresent()) { | ||
throw new DuplicationMemberException(); | ||
} | ||
final User newUser = new User(account, password, email); | ||
InMemoryUserRepository.save(newUser); | ||
final ResponseEntity responseEntity = ResponseEntity.fromViewPath(request.httpVersion(), request.getPath(), ResponseStatus.MOVED_TEMP); | ||
responseEntity.setRedirect("/index.html"); | ||
return responseEntity; | ||
|
||
httpResponse.setViewPathAsBodyAndSetStatus(httpRequest.getPath(), ResponseStatus.MOVED_TEMP); | ||
httpResponse.setRedirect("/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. 위의 코멘트와 동일합니다 😄 |
||
} | ||
|
||
private ResponseEntity joinPage(final Request request) { | ||
return ResponseEntity.fromViewPath(request.httpVersion(), request.getPath(), ResponseStatus.OK); | ||
private void joinPage(final HttpRequest httpRequest, final HttpResponse httpResponse) { | ||
httpResponse.setViewPathAsBody(httpRequest.getPath()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,24 +8,30 @@ | |
import java.io.UncheckedIOException; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class Connector implements Runnable { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(Connector.class); | ||
|
||
private static final int DEFAULT_PORT = 8080; | ||
private static final int DEFAULT_ACCEPT_COUNT = 100; | ||
private static final int DEFAULT_ACCEPT_COUNT = 30; | ||
private static final int DEFAULT_MAX_THREAD = 30; | ||
Comment on lines
+19
to
+20
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. 이렇게 제한을 두신 이유가 궁금하네용 ㅎㅎ 케로가 코멘트에 말씀해주신 것 처럼 스레드에 대해서 더 공부하시게 된다면... 저도 알려주세용 (@^0^@)/ 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. 일단 로컬에서 돌리고 있기 때문에 많은 스레드를 둘 필요가 없다고 판단해서 처음에 10개를 사용하였는데, ci에서 테스트가 깨져서 스레드 풀 개수 때문일가?? 하고 30까지 수정했던것 입니다....ㅎㅅㅎ 아직도 잘 모르겠어요 ...ㅠㅠ 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. 사실 저도 잘 모르겠어용 ㅋㅋㅋㅋ 근데 지금 미션 상황에서는 케로 말씀대로 많은 accept count와 thread가 필요하지 않을 것 같아요! |
||
|
||
private final ServerSocket serverSocket; | ||
|
||
private final ExecutorService executorService; | ||
private boolean stopped; | ||
|
||
public Connector() { | ||
this(DEFAULT_PORT, DEFAULT_ACCEPT_COUNT); | ||
this(DEFAULT_PORT, DEFAULT_ACCEPT_COUNT, DEFAULT_MAX_THREAD); | ||
} | ||
|
||
public Connector(final int port, final int acceptCount) { | ||
public Connector(final int port, final int acceptCount, final int maxThreads) { | ||
this.serverSocket = createServerSocket(port, acceptCount); | ||
this.stopped = false; | ||
this.executorService = Executors.newFixedThreadPool(maxThreads); | ||
} | ||
|
||
private ServerSocket createServerSocket(final int port, final int acceptCount) { | ||
|
@@ -67,13 +73,14 @@ private void process(final Socket connection) { | |
return; | ||
} | ||
var processor = new Http11Processor(connection); | ||
new Thread(processor).start(); | ||
executorService.execute(processor); | ||
} | ||
|
||
public void stop() { | ||
stopped = true; | ||
try { | ||
serverSocket.close(); | ||
executorService.shutdown(); | ||
} catch (IOException e) { | ||
log.error(e.getMessage(), e); | ||
} | ||
|
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.
HelloWorldController에
doPost()
요청이 들어오면 어떻게 되나요?.?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.
피알 날리고 궁금해서 해봤는데 NPE 가 발생하더라고여...
이부분도 처리 처리되도록 수정하였습니다 !