-
Notifications
You must be signed in to change notification settings - Fork 24
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주차 미션 / 서버 3조 이윤희 #19
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package Controller; | ||
|
||
import http.util.HttpRequest; | ||
import http.util.HttpResponse; | ||
|
||
public interface Controller { | ||
void execute(HttpRequest request, HttpResponse response) throws Exception; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package Controller; | ||
|
||
import http.util.HttpRequest; | ||
import http.util.HttpResponse; | ||
|
||
public class ForwardController implements Controller { | ||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws Exception { | ||
response.forward(request.getPath()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package Controller; | ||
|
||
import http.util.HttpRequest; | ||
import http.util.HttpResponse; | ||
|
||
public class HomeController implements Controller { | ||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws Exception { | ||
response.forward("/index.html"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package Controller; | ||
|
||
import http.util.HttpRequest; | ||
import http.util.HttpResponse; | ||
|
||
public class ListController implements Controller { | ||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws Exception { | ||
if ("logined=true".equals(request.getHeaders().get("Cookie"))) { | ||
response.forward("/user/list.html"); | ||
} else { | ||
response.redirect("/user/login.html"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package Controller; | ||
|
||
import db.MemoryUserRepository; | ||
import db.Repository; | ||
import http.util.HttpRequest; | ||
import http.util.HttpRequestUtils; | ||
import http.util.HttpResponse; | ||
import model.User; | ||
|
||
import java.util.Map; | ||
|
||
import static enumClass.Url.INDEX_PAGE; | ||
import static enumClass.Url.USER_LOGIN_FAILED_PAGE; | ||
import static enumClass.UserQueryKey.*; | ||
|
||
public class LoginController implements Controller { | ||
private final Repository repository = MemoryUserRepository.getInstance(); | ||
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. 필드 주입보다는 생성자 주입을 더 권하고 싶어요. 필드 주입을 사용하면 다른 repository 객체로 바꿀 때 이 부분을 수정해야하거든요 |
||
|
||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws Exception { | ||
Map<String, String> params = request.getBody() != null ? HttpRequestUtils.parseQueryParameter(request.getBody()) : 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. 파라미터를 파싱하는 부분은 httpRequest에서 처리해보는 건 어떨까요? |
||
if (params != null) { | ||
User user = repository.findUserById(params.get(USERID.getValue())); | ||
if (user != null && user.getPassword().equals(params.get(PASSWORD.getValue()))) { | ||
response.redirect(INDEX_PAGE.getValue()); | ||
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. Set-cookie는 어디서 하는 걸까요...? |
||
} else { | ||
response.redirect(USER_LOGIN_FAILED_PAGE.getValue()); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package Controller; | ||
|
||
import db.MemoryUserRepository; | ||
import db.Repository; | ||
import http.util.HttpRequest; | ||
import http.util.HttpRequestUtils; | ||
import http.util.HttpResponse; | ||
import model.User; | ||
|
||
import java.util.Map; | ||
|
||
import static enumClass.Url.INDEX_PAGE; | ||
import static enumClass.Url.USER_FORM_PAGE; | ||
import static enumClass.UserQueryKey.*; | ||
|
||
public class SignUpController implements Controller { | ||
private final Repository repository = MemoryUserRepository.getInstance(); | ||
|
||
@Override | ||
public void execute(HttpRequest request, HttpResponse response) throws Exception { | ||
Map<String, String> params = request.getBody() != null ? HttpRequestUtils.parseQueryParameter(request.getBody()) : null; | ||
if (params != null) { | ||
User user = new User(params.get(USERID.getValue()), params.get(PASSWORD.getValue()), params.get(NAME.getValue()), params.get(EMAIL.getValue())); | ||
repository.addUser(user); | ||
response.redirect(INDEX_PAGE.getValue()); | ||
} else { | ||
response.redirect(USER_FORM_PAGE.getValue()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package enumClass; | ||
|
||
public enum HttpHeader { | ||
|
||
CONTENT_TYPE("Content-Type"), | ||
CONTENT_LENGTH("Content-Length"), | ||
LOCATION("Location"), | ||
SET_COOKIE("Set-Cookie"), | ||
COOKIE("Cookie"); | ||
|
||
String value; | ||
|
||
private HttpHeader(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package enumClass; | ||
|
||
public enum HttpMethod { | ||
|
||
GET("GET"), | ||
POST("POST"); | ||
|
||
final String value; | ||
|
||
private HttpMethod(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package enumClass; | ||
|
||
public enum StatusCode { | ||
|
||
OK("200 OK"), | ||
FOUND("302 Found"), | ||
NOTFOUND("404 Not Found"); | ||
|
||
final String value; | ||
|
||
private StatusCode(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package enumClass; | ||
|
||
public enum Url { | ||
|
||
WEBAPP("webapp"), | ||
ROOT("/"), | ||
INDEX_PAGE("/index.html"), | ||
USER_FORM_PAGE("/user/form.html"), | ||
USER_SIGNUP("/user/signup"), | ||
USER_LOGIN("/user/login"), | ||
USER_LOGIN_PAGE("/user/login.html"), | ||
USER_LOGIN_FAILED_PAGE("/user/login_failed.html"), | ||
USER_USERLIST("/user/userList"), | ||
USER_LIST_PAGE("/user/list.html"), | ||
STYLE_CSS(".css"); | ||
|
||
final String value; | ||
|
||
private Url(String value){ | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package enumClass; | ||
|
||
public enum UserQueryKey { | ||
|
||
USERID("userId"), | ||
PASSWORD("password"), | ||
NAME("name"), | ||
EMAIL("email"); | ||
|
||
String value; | ||
|
||
private UserQueryKey(String value){ | ||
this.value = value; | ||
} | ||
|
||
public String getValue(){ | ||
return value; | ||
} | ||
} |
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.
url을 enum으로 관리하니까 enum을 사용하는 게 어떨까요?