-
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단계] 루쿠(백경환) 미션 제출합니다. #488
Changes from 29 commits
69e0cae
eaf05ed
4983f19
6e9a13d
dfc5091
21fa23a
0cad6ff
c8c49b3
09f7da5
b97149c
cf030ce
7f37022
8df0133
2dae997
be6dc82
a199ec5
b367bc5
e71dd0a
bc337a1
031bdf6
84c25a5
0086b13
959a333
72532ba
9b278bd
85917d6
df9ef9c
0d3e1f0
176a1ef
d8b6ab8
c7cb1a0
39abdb4
833eaea
8df60ee
147d760
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,29 @@ | ||
package org.apache.coyote.controller; | ||
|
||
import static org.apache.coyote.http11.HttpMethod.GET; | ||
import static org.apache.coyote.http11.HttpMethod.POST; | ||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
|
||
public abstract class AbstractController implements Controller { | ||
|
||
@Override | ||
public void service(HttpRequest request, HttpResponse response) throws Exception { | ||
if (request.getMethod() == GET) { | ||
doGet(request, response); | ||
return; | ||
} | ||
|
||
if (request.getMethod() == POST) { | ||
doPost(request, response); | ||
return; | ||
} | ||
|
||
throw new IllegalArgumentException("지원하지 않는 메서드 입니다."); | ||
} | ||
|
||
protected void doPost(HttpRequest request, HttpResponse response) throws Exception { /* NOOP */ } | ||
|
||
protected void doGet(HttpRequest request, HttpResponse response) throws Exception { /* NOOP */ } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.apache.coyote.controller; | ||
|
||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
|
||
public interface Controller { | ||
void service(HttpRequest request, HttpResponse response) throws Exception; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.apache.coyote.controller; | ||
|
||
import static org.apache.coyote.http11.HttpStatus.FOUND; | ||
import static org.apache.coyote.http11.HttpStatus.OK; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.HttpCookie; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.Session; | ||
import org.apache.coyote.http11.SessionManager; | ||
import org.apache.coyote.http11.Utils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class LoginController extends AbstractController { | ||
private static final Logger log = LoggerFactory.getLogger(LoginController.class); | ||
private static final SessionManager sessionManager = new SessionManager(); | ||
|
||
@Override | ||
protected void doPost(HttpRequest request, HttpResponse response) throws Exception { | ||
Map<String, String> queryParms = Utils.parseToQueryParms(request.getBody()); | ||
|
||
try { | ||
User user = InMemoryUserRepository.findByAccount(queryParms.get("account")) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 사용자 없음")); | ||
|
||
if (!user.checkPassword(queryParms.get("password"))) { | ||
throw new IllegalArgumentException("비밀번호 불일치"); | ||
} | ||
log.info("user: {}", user); | ||
|
||
Session session = new Session(UUID.randomUUID().toString()); | ||
session.setAttribute("user", user); | ||
sessionManager.add(session); | ||
response.setStatus(FOUND); | ||
response.setRedirectUrl("/index.html"); | ||
response.setCookie("JSESSIONID=" + session.getId()); | ||
|
||
} catch (IllegalArgumentException e) { | ||
log.error("error : {}", e); | ||
response.setStatus(FOUND); | ||
response.setRedirectUrl("/401.html"); | ||
|
||
} | ||
} | ||
|
||
@Override | ||
protected void doGet(HttpRequest request, HttpResponse response) throws Exception { | ||
HttpCookie cookie = new HttpCookie(request.getHeaders().get("Cookie")); | ||
|
||
String sessionId = cookie.findValue("JSESSIONID"); | ||
if (sessionManager.isExist(sessionId)) { | ||
response.setStatus(FOUND); | ||
response.setRedirectUrl("/index.html"); | ||
} else { | ||
response.setStatus(OK); | ||
response.setContentType("text/html"); | ||
response.setBody(Utils.readFile("static", "login.html")); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.apache.coyote.controller; | ||
|
||
import static org.apache.coyote.http11.HttpStatus.FOUND; | ||
import static org.apache.coyote.http11.HttpStatus.OK; | ||
|
||
import java.util.Map; | ||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.Utils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class RegisterController extends AbstractController { | ||
private static final Logger log = LoggerFactory.getLogger(RegisterController.class); | ||
|
||
@Override | ||
protected void doPost(HttpRequest request, HttpResponse response) throws Exception { | ||
Map<String, String> queryParms = Utils.parseToQueryParms(request.getBody()); | ||
User user = new User(queryParms.get("account"), queryParms.get("password"), | ||
queryParms.get("email")); | ||
InMemoryUserRepository.save(user); | ||
response.setStatus(FOUND); | ||
response.setRedirectUrl("/index.html"); | ||
} | ||
|
||
@Override | ||
protected void doGet(HttpRequest request, HttpResponse response) throws Exception { | ||
response.setStatus(OK); | ||
response.setContentType("text/html"); | ||
response.setBody(Utils.readFile("static", "register.html")); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.apache.coyote.controller; | ||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
|
||
public class RequestMapping { | ||
|
||
public Controller getController(HttpRequest request) { | ||
if (request.getPath().equals("/login")) { | ||
return new LoginController(); | ||
} | ||
|
||
if (request.getPath().equals("/register")) { | ||
return new RegisterController(); | ||
} | ||
|
||
if (request.getPath().equals("/")) { | ||
return new RootController(); | ||
} | ||
|
||
return new ResourceController(); | ||
} | ||
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. 마찬가지로 모든 요청마다 LoginController, RegisterController 등을 새로 만들어 주는 이유가 있나요?! 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. 예리하시군요,.. map에다가 담아 사용하도록 수정하였습니다 |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.apache.coyote.controller; | ||
|
||
import static org.apache.coyote.http11.HttpStatus.FOUND; | ||
import static org.apache.coyote.http11.HttpStatus.OK; | ||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.Utils; | ||
|
||
public class ResourceController extends AbstractController { | ||
|
||
protected void doGet(HttpRequest request, HttpResponse response) throws Exception { | ||
response.setStatus(OK); | ||
|
||
String path = request.getPath(); | ||
String fileName = path.substring(path.lastIndexOf('/') + 1); | ||
|
||
if (fileName.endsWith(".html")) { | ||
response.setContentType("text/html"); | ||
response.setBody(Utils.readFile("static", fileName)); | ||
return; | ||
} | ||
|
||
if (fileName.equals("styles.css")) { | ||
response.setContentType("text/css"); | ||
response.setBody(Utils.readFile("static/css", fileName)); | ||
return; | ||
} | ||
|
||
if (fileName.endsWith(".js") && !fileName.equals("scripts.js")) { | ||
response.setContentType("text/javascript"); | ||
response.setBody(Utils.readFile("static/assets", fileName)); | ||
return; | ||
} | ||
|
||
if (fileName.equals("scripts.js")) { | ||
response.setContentType("text/javascript"); | ||
response.setBody(Utils.readFile("static/js", fileName)); | ||
return; | ||
} | ||
|
||
if (fileName.equals("favicon.ico")) { | ||
response.setContentType("text/javascript"); | ||
response.setBody("Hello world!"); | ||
return; | ||
} | ||
Comment on lines
+18
to
+46
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. 맞습니다 오잉이 말씀해주신것처럼 정적파일이 추가될때마다 If문이 추가될거같아요.. |
||
|
||
response.setStatus(FOUND); | ||
response.setRedirectUrl("404.html"); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.apache.coyote.controller; | ||
|
||
import org.apache.coyote.http11.HttpRequest; | ||
import org.apache.coyote.http11.HttpResponse; | ||
import org.apache.coyote.http11.HttpStatus; | ||
|
||
public class RootController extends AbstractController { | ||
|
||
@Override | ||
protected void doGet(HttpRequest request, HttpResponse response) throws Exception { | ||
response.setStatus(HttpStatus.OK); | ||
response.setContentType("text/html"); | ||
response.setBody("Hello world!"); | ||
} | ||
|
||
} |
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.
queryParams에
account
,password
,email
중 하나라도 없으면 User에 잘못된 값이 들어갈 것 같은데,살짝의 방어로직을 추가해보는 것은 어떨까용??
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.
추가하였습니다!