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조 장현준 #10

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ repositories {
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'

testImplementation("org.assertj:assertj-core:3.26.3")
}

test {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/constant/Format.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package constant;

public enum Format {
HTML("html"),
CSS("css");

private String format;

Format(String format) {
this.format = format;
}

public String getFormat() {
return format;
}
}
20 changes: 20 additions & 0 deletions src/main/java/constant/HttpHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package constant;

public enum HttpHeader {
CONTENT_TYPE("Content-Type"),
LOCATION("Location"),
CONTENT_LENGTH("Content-Length"),
COOKIE("Cookie"),
SET_COOKIE("Set-Cookie")
;

private final String header;

HttpHeader(String header) {
this.header = header;
}

public String getHeader() {
return header;
}
}
19 changes: 19 additions & 0 deletions src/main/java/constant/HttpMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package constant;

public enum HttpMethod {
GET("GET"), POST("POST");

private final String method;

HttpMethod(String method) {
this.method = method;
}

public String getMethod() {
return method;
}

public boolean isEqual(String method) {
return this.method.equals(method);
}
}
16 changes: 16 additions & 0 deletions src/main/java/constant/HttpStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package constant;

public enum HttpStatus {
REDIRECT("302 Found"),
OK("200 OK");

private final String status;

HttpStatus(String status) {
this.status = status;
}

public String getStatus() {
return status;
}
}
18 changes: 18 additions & 0 deletions src/main/java/constant/QueryKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package constant;

public enum QueryKey {
USER_ID("userId"),
PASSWORD("password"),
NAME("name"),
EMAIL("email");

private final String key;

QueryKey(String key) {
this.key = key;
}

public String getKey() {
return key;
}
}
24 changes: 24 additions & 0 deletions src/main/java/constant/URL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package constant;

public enum URL {
ROOT("./webapp"),
INDEX("/index.html"),
LOGIN_HTML("/user/login.html"),
LOGIN("/user/login"),
LIST("/user/list.html"),
USER_LIST("/user/userList"),
LOGIN_FAILED("/user/login_failed.html"),
SIGNUP("/user/signup"),
CSS("/css/styles.css")
;

private final String url;

URL(String url) {
this.url = url;
}

public String getUrl() {
return url;
}
}
10 changes: 10 additions & 0 deletions src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package controller;

import http.request.HttpRequest;
import http.response.HttpResponse;

import java.io.IOException;

public interface Controller {
void execute(HttpRequest request, HttpResponse response) throws IOException;
}
13 changes: 13 additions & 0 deletions src/main/java/controller/CssController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package controller;

import http.request.HttpRequest;
import http.response.HttpResponse;

import java.io.IOException;

public class CssController implements Controller {
@Override
public void execute(HttpRequest request, HttpResponse response) throws IOException {
response.setCss(request.getUrl());
}
}
14 changes: 14 additions & 0 deletions src/main/java/controller/IndexController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package controller;

import http.request.HttpRequest;
import http.response.HttpResponse;

import java.io.IOException;

public class IndexController implements Controller {

@Override
public void execute(HttpRequest request, HttpResponse response) throws IOException {
response.forward(request.getUrl());
}
}
33 changes: 33 additions & 0 deletions src/main/java/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package controller;

import db.MemoryUserRepository;
import http.request.HttpRequest;
import http.response.HttpResponse;
import model.User;

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

import static constant.QueryKey.PASSWORD;
import static constant.QueryKey.USER_ID;
import static constant.URL.INDEX;
import static constant.URL.LOGIN_FAILED;

public class LoginController implements Controller {

@Override
public void execute(HttpRequest request, HttpResponse response) throws IOException {
Map<String, String> loginInfo = request.getQueryParametersfromBody();
User findUser = findUser(loginInfo);
if (findUser != null && findUser.getPassword().equals(loginInfo.get(PASSWORD.getKey()))) {
response.redirect(INDEX.getUrl(), true);
return;
}
response.redirect(LOGIN_FAILED.getUrl(), false);
}

private static User findUser(Map<String, String> loginInfo) {
MemoryUserRepository memoryUserRepository = MemoryUserRepository.getInstance();
return memoryUserRepository.findUserById(loginInfo.get(USER_ID.getKey()));
}
Comment on lines +29 to +32

Choose a reason for hiding this comment

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

repository 객체를 생성자 주입으로 받아서 사용해보는 건 어떨까요?

}
30 changes: 30 additions & 0 deletions src/main/java/controller/SignUpController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package controller;

import db.MemoryUserRepository;
import http.request.HttpRequest;
import http.response.HttpResponse;
import model.User;

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

import static constant.QueryKey.*;
import static constant.QueryKey.EMAIL;
import static constant.URL.INDEX;

public class SignUpController implements Controller {

@Override
public void execute(HttpRequest request, HttpResponse response) throws IOException {
createNewUser(request.getQueryParameters());
response.redirect(INDEX.getUrl(), request.isLogin());
}

private void createNewUser(Map<String, String> queryParameter) {
//새로운 User 객체 생성 후 Repository에 추가
User newUser = new User(queryParameter.get(USER_ID.getKey()), queryParameter.get(PASSWORD.getKey()), queryParameter.get(NAME.getKey()), queryParameter.get(EMAIL.getKey()));
MemoryUserRepository memoryUserRepository = MemoryUserRepository.getInstance();
memoryUserRepository.addUser(newUser);
// System.out.println("newUser_name = " + memoryUserRepository.findUserById(queryParameter.get("userId")).getName());
}
}
20 changes: 20 additions & 0 deletions src/main/java/controller/UserListController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controller;

import http.request.HttpRequest;
import http.response.HttpResponse;

import java.io.IOException;

import static constant.URL.LIST;
import static constant.URL.LOGIN_HTML;

public class UserListController implements Controller {
@Override
public void execute(HttpRequest request, HttpResponse response) throws IOException {
if (request.isLogin()) {
response.redirect(LIST.getUrl(), request.isLogin());
return;
}
response.redirect(LOGIN_HTML.getUrl(), request.isLogin());
}
}
94 changes: 94 additions & 0 deletions src/main/java/http/request/HttpRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package http.request;

import constant.URL;
import http.util.HttpRequestUtils;
import http.util.IOUtils;

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

import static constant.HttpMethod.GET;
import static constant.HttpMethod.POST;
import static http.util.IOUtils.readData;

public class HttpRequest {
private final HttpRequestStartLine httpRequestStartLine;
private final HttpRequestHeader httpRequestHeader;
private final String body;

private HttpRequest(HttpRequestStartLine httpRequestStartLine, HttpRequestHeader httpRequestHeader, String body) {
this.httpRequestStartLine = httpRequestStartLine;
this.httpRequestHeader = httpRequestHeader;
this.body = body;
}

public static HttpRequest from(BufferedReader br) throws IOException {
//startLine 추출
String startLine = br.readLine();

HttpRequestStartLine requestStartLine = HttpRequestStartLine.from(startLine);
HttpRequestHeader requestHeader = HttpRequestHeader.from(br);
String body = readBody(br, requestHeader);

return new HttpRequest(requestStartLine, requestHeader, body);
}

private static String readBody(BufferedReader br, HttpRequestHeader requestHeader) throws IOException {
if (!requestHeader.isEmptyContent()) {
return readData(br, requestHeader.getContentLength());
}
return "";
}

public boolean isGetMethod() {
return GET.isEqual(httpRequestStartLine.getMethod());
}

public boolean isPostMethod() {
return POST.isEqual(httpRequestStartLine.getMethod());
}

public String getUrl() {
if(httpRequestStartLine.getUrl().equals("/")) {
return URL.INDEX.getUrl();
}
return httpRequestStartLine.getUrl();
}

public int getContentLength() {
return httpRequestHeader.getContentLength();
}

public boolean isLogin() {
return httpRequestHeader.getLoginStatus();
}


public Map<String, String> getQueryParameters() {
if (isGetMethod()) {
//url에서 queryString 분리
return getQueryParametersfromUrl();
}
if (isPostMethod()) {
//body에서 queryString 추출
return getQueryParametersfromBody();
}
else {
System.out.println("올바른 데이터 전송 방식이 아닙니다.");
return null;
}
}

public Map<String, String> getQueryParametersfromBody() {
//POST 메서드인 경우
return HttpRequestUtils.parseQueryParameter(body);
}
public Map<String, String> getQueryParametersfromUrl() {
//GET 메서드인 경우
String url = getUrl();
String queryString = url.substring(url.lastIndexOf("?") + 1);
return HttpRequestUtils.parseQueryParameter(queryString);
}

}
Loading