Skip to content
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조 - 함형주 #1

Open
wants to merge 61 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
879debe
feat (RequestHandler) : index.html 응답
hamhyeongju Apr 2, 2023
7a02b41
feat (CustomHandler) : create
hamhyeongju Apr 2, 2023
a422d16
feat (IndexHandler) : create
hamhyeongju Apr 2, 2023
4250440
feat (RequestHandler) : CustomHandler 도입
hamhyeongju Apr 2, 2023
32861a6
feat (RequestHandler) : getRequestTarget() 메서드 분리
hamhyeongju Apr 2, 2023
202a111
feat (RequestHandler) : getRequestTarget() split 값 변경
hamhyeongju Apr 2, 2023
f7da28c
feat (UserFormHandler) : create
hamhyeongju Apr 2, 2023
3ed8c66
feat (form.html) : change http method
hamhyeongju Apr 2, 2023
7653244
feat (CustomHandler) : add parameter
hamhyeongju Apr 2, 2023
e32cd86
feat (RequestHandler) : create getUriFromRequestTarget()
hamhyeongju Apr 2, 2023
1b553cb
feat (SignUpHandler) : create
hamhyeongju Apr 2, 2023
6721615
feat (form.html) : change http method
hamhyeongju Apr 2, 2023
7766209
feat (RequestHandler) : create getRequestContentLength()
hamhyeongju Apr 2, 2023
0b04e80
feat (RequestHandler) : create response302Header()
hamhyeongju Apr 2, 2023
54b8f28
refactor (RequestHandler) : response302Header() 실행 조건 HTTP 메서드 사용
hamhyeongju Apr 2, 2023
56b06b1
rename (SignUpFormHandler) : from UserFormHandler
hamhyeongju Apr 2, 2023
93fc568
feat (LoginFormHandler) : create
hamhyeongju Apr 2, 2023
e0868a2
feat (LoginFailFormHandler) : create
hamhyeongju Apr 2, 2023
ddde0a2
feat (LoginHandler) : create
hamhyeongju Apr 2, 2023
cfeb215
feat (SignUpHandler) : 반환 값 viewName으로 변경
hamhyeongju Apr 2, 2023
00ca9c7
feat (RequestHandler) : 로그인 시 쿠키 값 추가
hamhyeongju Apr 2, 2023
5769f27
fix(login_failed.html): 잘못된 view 수정
jung-woo-kim Apr 2, 2023
b81de08
feat (HttpRequest) : create
hamhyeongju Apr 3, 2023
d469f60
feat (HttpResponse) : create
hamhyeongju Apr 3, 2023
80f80ca
feat (FrontHandler) : create
hamhyeongju Apr 3, 2023
8a346dc
feat (RequestHandler) : FrontHandler 도입
hamhyeongju Apr 3, 2023
96a95fc
feat (HttpRequest) : header 파싱
hamhyeongju Apr 3, 2023
627c167
feat (HttpResponse) : 구현
hamhyeongju Apr 3, 2023
17b61b6
feat (CustomHandler) : 파라미터 변경
hamhyeongju Apr 3, 2023
ce275e7
feat (SignUpHandler) : 응답 헤더 설정 추가
hamhyeongju Apr 3, 2023
efbc6a0
feat (LoginHandler) : 응답 헤더 설정 추가
hamhyeongju Apr 3, 2023
2558948
feat (UserListHandler) : list.html 출력
hamhyeongju Apr 3, 2023
9a710cc
feat (FrontHandler) : HttpResponse에 기능 위임
hamhyeongju Apr 3, 2023
b0b5aa6
feat (CssHandler) : create
hamhyeongju Apr 3, 2023
b586e20
feat (Http) : create
hamhyeongju Apr 3, 2023
9a5c1df
rename (Controller) : from CustomHandler
hamhyeongju Jan 7, 2024
ce1c5fc
rename (ForwardController) : from IndexHandler
hamhyeongju Jan 7, 2024
4fc6fad
remove
hamhyeongju Jan 7, 2024
c8a159d
rename (FrontController) : from FrontHandler
hamhyeongju Jan 8, 2024
4c9e430
refactor (HttpResponse) : forward()
hamhyeongju Jan 8, 2024
b510c73
refactor (FrontController)
hamhyeongju Jan 8, 2024
ae3cc99
refactor (ForwardController)
hamhyeongju Jan 8, 2024
ddec33f
refactor (Controller) : change process() return type to void
hamhyeongju Jan 9, 2024
ebefa54
remove (CssHandler)
hamhyeongju Jan 9, 2024
237c3bd
feat (ForwardController) : check "/" url
hamhyeongju Jan 10, 2024
d1724b9
refactor (index.html) : edit a tag
hamhyeongju Jan 10, 2024
aa8ed95
rename (scripts.js) : from script.js
hamhyeongju Jan 10, 2024
9bfb4b0
add (HttpResponseStartLine)
hamhyeongju Jan 10, 2024
082b9f1
refactor (HttpResponse)
hamhyeongju Jan 10, 2024
e462b34
refactor (SignUpHandler) : redirect()
hamhyeongju Jan 10, 2024
82c3c28
refactor (HttpRequest) : add getHeader() and remove getHeaderMap()
hamhyeongju Jan 10, 2024
112cf8b
refactor (UserListHandler)
hamhyeongju Jan 10, 2024
adde06d
refactor (FrontController)
hamhyeongju Jan 10, 2024
8dcdb56
feat (HttpRequest) : add getParamValue()
hamhyeongju Jan 10, 2024
13d32e5
refactor (HttpRequest) : edit typo
hamhyeongju Jan 10, 2024
2163fd7
move
hamhyeongju Jan 13, 2024
f9ce2d8
add (RequestMapper) : map http request and controller
hamhyeongju Jan 13, 2024
90f18ae
rename
hamhyeongju Jan 13, 2024
ead89af
feat (RequestMapper) : separate controller mapping
hamhyeongju Jan 13, 2024
e9e3423
refactor (LoginController) : simplify code
hamhyeongju Jan 15, 2024
8891954
refactor (RequestMapper)
hamhyeongju Jan 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/webserver/CustomHandler/CssHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package webserver.CustomHandler;

import webserver.HttpRequest;
import webserver.HttpResponse;
import webserver.constant.Http;

import java.io.IOException;

import static webserver.constant.Http.*;

public class CssHandler implements CustomHandler{

@Override
public byte[] process(HttpRequest request, HttpResponse response) throws IOException {
response.setHeader(CONTENT_TYPE.getValue(), TEXT_CSS.getValue());
return "Content-Type".getBytes();
}
}
11 changes: 11 additions & 0 deletions src/main/java/webserver/CustomHandler/CustomHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package webserver.CustomHandler;

import webserver.HttpRequest;
import webserver.HttpResponse;

import java.io.IOException;

public interface CustomHandler {

byte[] process(HttpRequest request, HttpResponse response) throws IOException;
}
25 changes: 25 additions & 0 deletions src/main/java/webserver/CustomHandler/IndexHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package webserver.CustomHandler;

import http.util.IOUtils;
import webserver.HttpRequest;
import webserver.HttpResponse;
import webserver.constant.Http;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import static webserver.constant.Http.*;

public class IndexHandler implements CustomHandler{

private final File file = new File(WEBAPP.getValue() + INDEX.getValue());

@Override
public byte[] process(HttpRequest request, HttpResponse response) throws IOException {
BufferedReader fr = new BufferedReader(new FileReader(file));
String fileData = IOUtils.readData(fr, (int) file.length());
return fileData.getBytes();
}
}
25 changes: 25 additions & 0 deletions src/main/java/webserver/CustomHandler/LoginFailFormHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package webserver.CustomHandler;

import http.util.IOUtils;
import webserver.HttpRequest;
import webserver.HttpResponse;
import webserver.constant.Http;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import static webserver.constant.Http.*;

public class LoginFailFormHandler implements CustomHandler{

private final File file = new File(WEBAPP.getValue() + USER_LOGIN_FAILED.getValue());

@Override
public byte[] process(HttpRequest request, HttpResponse response) throws IOException {
BufferedReader fr = new BufferedReader(new FileReader(file));
String fileData = IOUtils.readData(fr, (int) file.length());
return fileData.getBytes();
}
}
24 changes: 24 additions & 0 deletions src/main/java/webserver/CustomHandler/LoginFormHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package webserver.CustomHandler;

import http.util.IOUtils;
import webserver.HttpRequest;
import webserver.HttpResponse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import static webserver.constant.Http.*;

public class LoginFormHandler implements CustomHandler{

private final File file = new File(WEBAPP.getValue() + LOGIN_FORM.getValue());

@Override
public byte[] process(HttpRequest request, HttpResponse response) throws IOException {
BufferedReader fr = new BufferedReader(new FileReader(file));
String fileData = IOUtils.readData(fr, (int) file.length());
return fileData.getBytes();
}
}
47 changes: 47 additions & 0 deletions src/main/java/webserver/CustomHandler/LoginHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package webserver.CustomHandler;

import db.MemoryUserRepository;
import model.User;
import webserver.HttpRequest;
import webserver.HttpResponse;
import webserver.constant.Http;

import java.io.IOException;
import java.util.Map;

import static webserver.constant.Http.*;

public class LoginHandler implements CustomHandler{

private final MemoryUserRepository repository;

public LoginHandler() {
repository = MemoryUserRepository.getInstance();
}

@Override
public byte[] process(HttpRequest request, HttpResponse response) throws IOException {
try {
Map<String, String> paramMap = request.getParamMap();
User findById = repository.findUserById(paramMap.get("userId"));
return passwordCheck(paramMap, findById, response);
} catch (NullPointerException e) {
setStatusCodeAndLocation(USER_LOGIN_FAILED.getValue(), response);
return USER_LOGIN_FAILED.getValue().getBytes();
}
}

private static byte[] passwordCheck(Map<String, String> paramMap, User findById, HttpResponse response) {
if (!findById.getPassword().equals(paramMap.get("password"))) {
return USER_LOGIN_FAILED.getValue().getBytes();
}
setStatusCodeAndLocation(INDEX.getValue(), response);
response.setHeader(SET_COOKIE.getValue(), LOGINED_TRUE.getValue());
return INDEX.getValue().getBytes();
}

private static void setStatusCodeAndLocation(String location, HttpResponse response) {
response.setStatusCode(FOUND.getValue());
response.setHeader(LOCATION.getValue(), location);
}
}
24 changes: 24 additions & 0 deletions src/main/java/webserver/CustomHandler/SignUpFormHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package webserver.CustomHandler;

import http.util.IOUtils;
import webserver.HttpRequest;
import webserver.HttpResponse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import static webserver.constant.Http.*;

public class SignUpFormHandler implements CustomHandler{

private final File file = new File(WEBAPP.getValue() + SIGNUP_FORM.getValue());

@Override
public byte[] process(HttpRequest request, HttpResponse response) throws IOException {
BufferedReader fr = new BufferedReader(new FileReader(file));
String fileData = IOUtils.readData(fr, (int) file.length());
return fileData.getBytes();
}
}
44 changes: 44 additions & 0 deletions src/main/java/webserver/CustomHandler/SignUpHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package webserver.CustomHandler;

import db.MemoryUserRepository;
import model.User;
import webserver.HttpRequest;
import webserver.HttpResponse;
import webserver.constant.Http;

import java.io.IOException;
import java.util.Map;

import static webserver.constant.Http.*;

public class SignUpHandler implements CustomHandler {

private final MemoryUserRepository repository;

public SignUpHandler() {
repository = MemoryUserRepository.getInstance();
}

@Override
public byte[] process(HttpRequest request, HttpResponse response) throws IOException {
Map<String, String> paramMap = request.getParamMap();

User user = new User(
paramMap.get("userId"),
paramMap.get("password"),
paramMap.get("name"),
paramMap.get("email")
);

repository.addUser(user);

setStatusCodeAndLocation(INDEX.getValue(), response);

return INDEX.getValue().getBytes();
}

private static void setStatusCodeAndLocation(String location, HttpResponse response) {
response.setStatusCode(FOUND.getValue());
response.setHeader(LOCATION.getValue(), location);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 곳에서도 redirect가 필요하다면, 이 코드가 똑같이 또 필요하지 않을까요?
HttpResponse에 sendRedirect() 메서드를 만들어보세요!

}
40 changes: 40 additions & 0 deletions src/main/java/webserver/CustomHandler/UserListHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package webserver.CustomHandler;

import http.util.IOUtils;
import webserver.HttpRequest;
import webserver.HttpResponse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

import static webserver.constant.Http.*;

public class UserListHandler implements CustomHandler{

private final File file = new File(WEBAPP.getValue() + LIST.getValue());

@Override
public byte[] process(HttpRequest request, HttpResponse response) throws IOException {
System.out.println("진입");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sysout보단 Logger를 활용해보세요!

Map<String, String> headerMap = request.getHeaderMap();
String cookieValue = headerMap.get(COOKIE.getValue());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headerMap을 getter로 꺼내와서 값을 읽어오는 것보단 request.getHeader(COOKIE.getValue()) 이런 식으로 하는 처리하는 것이 좋아요.
디미터 규칙을 지키려고 노력해보세요!

String[] split = cookieValue.split(";");
System.out.println("split[0] = " + split[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cookieValue를 parsing하는 일은 util 클래스를 하나 만들어도 더욱 좋을 것 같습니다!🧐

if (split[0].equals(LOGINED_TRUE.getValue())) {
BufferedReader fr = new BufferedReader(new FileReader(file));
String fileData = IOUtils.readData(fr, (int) file.length());
return fileData.getBytes();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파일로부터 데이터를 읽어와서 ~ 해주는 작업을 HttpResponse에 캡슐화해보세요!
다른 핸들러들에서도 계속 중복이 일어나는 것 같아요🧐

}

setStatusCodeAndLocation(response);
return INDEX.getValue().getBytes();
}

private static void setStatusCodeAndLocation(HttpResponse response) {
response.setStatusCode(FOUND.getValue());
response.setHeader(LOCATION.getValue(), INDEX.getValue());
}
}
54 changes: 54 additions & 0 deletions src/main/java/webserver/FrontHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package webserver;

import webserver.CustomHandler.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static webserver.constant.Http.*;

public class FrontHandler {

private static final Map<String, CustomHandler> handlerMappingMap = new HashMap<>();
private static FrontHandler frontHandler;

private FrontHandler() {
}

public static FrontHandler getInstance() {
if (handlerMappingMap.isEmpty()) {
frontHandler = new FrontHandler();
handlerMapping();
return frontHandler;
}
return frontHandler;
}

private static void handlerMapping() {
handlerMappingMap.put(INDEX.getValue(), new IndexHandler());
handlerMappingMap.put("/", new IndexHandler());
handlerMappingMap.put(SIGNUP_FORM.getValue(), new SignUpFormHandler());
handlerMappingMap.put(SIGNUP.getValue(), new SignUpHandler());
handlerMappingMap.put(LOGIN_FORM.getValue(), new LoginFormHandler());
handlerMappingMap.put(USER_LOGIN_FAILED.getValue(), new LoginFailFormHandler());
handlerMappingMap.put(LOGIN.getValue(), new LoginHandler());
handlerMappingMap.put(USER_LIST.getValue(), new UserListHandler());
handlerMappingMap.put(CSS.getValue(), new CssHandler());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

단순히 파일 내용을 읽어오는 작업만 있는 핸들러들은 FowardHandler를 만들어서 FowardHandler(String url)로 하나로 묶을 수 있을 것 같아요!


public void service(HttpRequest request, HttpResponse response) throws IOException {

String requestUri = request.getRequestUri();
CustomHandler handler;
if (requestUri.contains(CSS.getValue())) handler = handlerMappingMap.get(CSS.getValue());
else handler = handlerMappingMap.get(request.getRequestUri());

byte[] bytes = handler.process(request, response);

response.createStartLine();
response.createHeader();
response.responseBody(bytes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런식으로 3가지 메서드를 호출하게 하면, 다른 개발자가 HttpResponse 객체를 사용한다고 했을 때 하나만 빼먹을 수 있을 것 같지 않나요?🧐
HttpResponse 객체에 forward(String url) 라는 메서드를 만들어서 이 3가지 메서드를 캡슐화해보는 것은 어떤가요?
이렇게 한다면 각 컨트롤러에서 파일의 내용을 읽어 getBytes해야하는 중복도 없앨 수 있어요!

또 redirect 처리도 보다 자유롭게 할 수 있습니다.


}
}
Loading