-
Notifications
You must be signed in to change notification settings - Fork 16
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주차 미션 / 서버 1조 - 강지윤 #3
base: main
Are you sure you want to change the base?
Changes from all commits
8745167
94caea5
ada7eaa
fa672a1
f6a4548
01ca071
0ed89e8
796ecbd
a3e6429
4f5a5ea
e99fcfe
c44071c
9ab930a
c08c3c4
af54aac
0211f62
77d2599
77908b1
1275148
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,11 @@ | ||
package controller; | ||
|
||
import http.request.HttpRequest; | ||
|
||
import http.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Controller { | ||
void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package controller; | ||
|
||
import http.HttpHeaders; | ||
import http.constant.HttpHeader; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import static http.constant.URL.ROOT; | ||
|
||
public class JustGetController implements Controller { | ||
|
||
public JustGetController(){ | ||
} | ||
|
||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
// request 분석 | ||
String requestURL = httpRequest.getPath(); | ||
String TYPE = "html"; | ||
if(requestURL.contains(".css")) | ||
TYPE = "css"; | ||
|
||
// 1 startLine - forward 메소드 내 구현 | ||
|
||
// 2 | ||
byte[] body = Files.readAllBytes(Paths.get(ROOT.getURL() + httpRequest.getPath())); | ||
httpResponse.setBody(body); | ||
|
||
// 3 | ||
HttpHeaders httpHeaders = new HttpHeaders(); | ||
httpHeaders.put(HttpHeader.CONTENT_LENGTH, Integer.toString(body.length)); | ||
httpHeaders.put(HttpHeader.CONTENT_TYPE, "text/"+TYPE+";charset=utf-8"); | ||
httpResponse.setHeaders(httpHeaders); | ||
|
||
// forward 내부에 writeResponse 메소드 존재. | ||
httpResponse.forward(requestURL); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package controller; | ||
|
||
import db.MemoryUserRepository; | ||
import db.Repository; | ||
import http.HttpHeaders; | ||
import http.constant.HttpHeader; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
import model.User; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static http.constant.URL.LOGIN_FAILED_PATH; | ||
import static http.constant.URL.MAIN_PATH; | ||
import static model.UserQueryKey.*; | ||
|
||
public class LoginController implements Controller { | ||
private final Repository repository; | ||
|
||
|
||
public LoginController(){ | ||
this.repository = MemoryUserRepository.getInstance(); | ||
} | ||
|
||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
|
||
// query 분석 | ||
Map<String, String> query = httpRequest.getQuery(); | ||
|
||
if(query == null || (query != null && query.size() < 2)) | ||
throw new IllegalArgumentException("로그인 정보를 모두 채워주세요."); | ||
User user = repository.findUserById(query.get(USERID.getKey())); | ||
|
||
HttpHeaders httpHeaders = new HttpHeaders(); | ||
String path = LOGIN_FAILED_PATH.getURL(); | ||
|
||
// 로그인 성공 | ||
if(user != null && user.getPassword().equals(query.get(PASSWORD.getKey()))){ | ||
path = MAIN_PATH.getURL(); | ||
httpHeaders.put(HttpHeader.SET_COOKIE, "logined=true"); | ||
} | ||
|
||
// header | ||
httpHeaders.put(HttpHeader.LOCATION, path); | ||
httpResponse.setHeaders(httpHeaders); | ||
|
||
// 로그인 성공 - redirect MAIN_PATH | ||
httpResponse.redirect(path); | ||
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. 마찬가지로 redirect가 헤더의 Location:path와 status code를 변경함은 고정되어있기 때문에 httpResponse.redirect(path) 메서드 내부로 로직을 분리하면 중복을 제거할 수 있을 것 같아요!🧐 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package controller; | ||
|
||
import db.MemoryUserRepository; | ||
import db.Repository; | ||
import http.HttpHeaders; | ||
import http.constant.HttpHeader; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
import model.User; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static http.constant.URL.MAIN_PATH; | ||
import static model.UserQueryKey.*; | ||
|
||
public class SignUpController implements Controller { | ||
|
||
private final Repository repository = MemoryUserRepository.getInstance(); | ||
|
||
public SignUpController(){ | ||
} | ||
|
||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
// request 분석 | ||
String requestURL = httpRequest.getPath(); | ||
System.out.println(requestURL); | ||
|
||
// method == get | ||
// GET일 땐 starLine의 requestURL에서 query 얻어올 수 있음. | ||
Map<String, String> query = httpRequest.getQuery(); | ||
|
||
if(query == null || (query != null && query.size() < 4)) | ||
throw new IllegalArgumentException("회원가입 정보를 모두 채워주세요."); | ||
User user = new User(query.get(USERID.getKey()), query.get(PASSWORD.getKey()), query.get(NAME.getKey()), query.get(EMAIL.getKey())); | ||
repository.addUser(user); | ||
|
||
// 1 startLine - redirect 메소드 내 구현 | ||
|
||
// 2 body x | ||
|
||
// 3 headers | ||
HttpHeaders httpHeaders = new HttpHeaders(); | ||
httpHeaders.put(HttpHeader.LOCATION, MAIN_PATH.getURL()); | ||
httpResponse.setHeaders(httpHeaders); | ||
|
||
// redirect 내부에 writeResponse 메소드 존재. | ||
httpResponse.redirect(MAIN_PATH.getURL()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package controller; | ||
|
||
import http.HttpHeaders; | ||
import http.constant.HttpHeader; | ||
import http.request.HttpRequest; | ||
import http.response.HttpResponse; | ||
|
||
import java.io.IOException; | ||
import static http.constant.URL.*; | ||
|
||
public class UserListController implements Controller { | ||
|
||
public UserListController(){ | ||
} | ||
|
||
@Override | ||
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) throws IOException { | ||
// request | ||
HttpHeaders headers = httpRequest.getHeader(); | ||
System.out.println(headers.toString()); | ||
String cookie = headers.get(HttpHeader.COOKIE); | ||
String path = LOGIN_PATH.getURL(); | ||
|
||
if(cookie != null && cookie.contains("logined=true")){ | ||
path = USERLIST_PATH.getURL(); | ||
} | ||
|
||
HttpHeaders httpHeaders = new HttpHeaders(); | ||
httpHeaders.put(HttpHeader.LOCATION, path); | ||
httpResponse.setHeaders(httpHeaders); | ||
|
||
httpResponse.redirect(path); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package http; | ||
|
||
import http.constant.HttpHeader; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class HttpHeaders { | ||
private Map<HttpHeader, String> headers = new HashMap<>(); | ||
private static final String DELIMITER = ": "; | ||
private static final String CRLF = "\r\n"; | ||
|
||
public HttpHeaders(){} | ||
|
||
public void put(HttpHeader header, String value) { | ||
headers.put(header, value); | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
String str = ""; | ||
if(headers.isEmpty()) | ||
return str; | ||
for(HttpHeader key : headers.keySet()){ | ||
str += key.getHeader() + DELIMITER + headers.get(key) + CRLF; | ||
} | ||
return str + CRLF; | ||
} | ||
|
||
public boolean containsKey(HttpHeader header) { | ||
return headers.containsKey(header); | ||
} | ||
|
||
public String get(HttpHeader header){ | ||
return headers.get(header); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package http.constant; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum HttpHeader { | ||
CONTENT_TYPE("Content-Type"), CONTENT_LENGTH("Content-Length"), SET_COOKIE("Set-Cookie"), COOKIE("Cookie"), LOCATION("Location"); | ||
|
||
private String header; | ||
|
||
HttpHeader(String header){ | ||
this.header = header; | ||
} | ||
|
||
public static HttpHeader of(String header) { | ||
return Arrays.stream(values()) | ||
.filter(h -> header.equals(h.header)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException(String.format("해당되는 Http Header가 없습니다."))); | ||
} | ||
|
||
public String getHeader() { | ||
return header; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package http.constant; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum HttpMethod { | ||
GET("GET"), POST("POST"); | ||
|
||
private String method; | ||
|
||
HttpMethod(String method){ | ||
this.method = method; | ||
} | ||
|
||
|
||
public static HttpMethod of(String method) { | ||
return Arrays.stream(values()) | ||
.filter(m -> method.equals(m.method)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException(String.format("해당되는 Http Method가 없습니다."))); | ||
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. Enum 클래스의 valueOf 메서드 활용해보세요! 👍 |
||
} | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package http.constant; | ||
|
||
public enum HttpStatus { | ||
OK("200 OK"), REDIRECT("302 FOUND"); | ||
|
||
private final String status; | ||
|
||
HttpStatus(String status) { | ||
this.status = status; | ||
} | ||
|
||
public String getStatus() { | ||
return status; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package http.constant; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum URL { | ||
USERLIST_PATH("/user/list.html"), USERLIST_ACTION("/user/userList"), LOGIN_ACTION("/user/login"), SIGNUP_ACTION("/user/signup"), ROOT("./webapp"), MAIN_PATH("/index.html"), LOGIN_PATH("/user/login.html"), LOGIN_FAILED_PATH("/user/login_failed.html"); | ||
|
||
private String url; | ||
|
||
URL(String url){ | ||
this.url = url; | ||
} | ||
|
||
public static URL of(String url) { | ||
return Arrays.stream(values()) | ||
.filter(u -> url.equals(u.url)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException(String.format("처리가 불가능한 url입니다."))); | ||
} | ||
|
||
public String getURL() { | ||
return url; | ||
} | ||
|
||
} | ||
|
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.
forward(String url) 메서드의 의미에 대해서 한번 되짚어 보시면 좋을 것 같아요!
forward: url의 위치 뷰의 파일을 response body에 작성한다.
즉, 파일 read와 Header 세팅을 HttpResponse 내부로 캡슐화할 수 있지 않을까요??🧐🧐