-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[톰캣 구현하기 1, 2단계] 오도(문민혁) 미션 제출합니다. (#344)
* test: IOStream 학습 테스트 성공하도록 구현 * feat: GET /index.html 응답하기 구현 * feat: CSS 지원하기 구현 * feat: Query String 파싱 구현 * feat: 로그인 성공시 302, 실패시 401 반환 구현 * refactor: 로그인 부분 분리 * feat: POST 방식으로 회원가입 구현 * feat: 로그인 버튼 클릭 시 GET 방식 -> POST 방식으로 변경 * feat: Session 구현하기 구현 * refactor: HttpRequest 분리 * refactor: HttpResponse 분리 * refactor: Processor 메서드 중복 제거 * refactor: Request에서 Body 파싱하지 않도록 변경 * feat: ExceptionHandler 추가 * refactor: HttpResponse Builder 구현 * refactor: HttpResponse 빌드시 bytes 같이 생성되도록 변경 * refactor: log 내에서 메서드 실행 제거 * refactor: loadResourceAsString -> load 메서드명 개선 * refactor: SessionManager 필드로 변경 * refactor: 파라미터명 간소화
- Loading branch information
Showing
16 changed files
with
706 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.apache.catalina; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class Session { | ||
|
||
private final String id; | ||
private final Map<String, Object> values = new HashMap<>(); | ||
|
||
public Session(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public Optional<Object> getAttribute(String name) { | ||
if (values.containsKey(name)) { | ||
return Optional.of(values.get(name)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public void setAttribute(String name, Object value) { | ||
values.put(name, value); | ||
} | ||
|
||
public void removeAttribute(String name) { | ||
values.remove(name); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tomcat/src/main/java/org/apache/catalina/SessionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.apache.catalina; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class SessionManager { | ||
|
||
private static final Map<String, Session> SESSIONS = new HashMap<>(); | ||
|
||
public void add(Session session) { | ||
SESSIONS.put(session.getId(), session); | ||
} | ||
|
||
public Optional<Session> findSession(String id) { | ||
if (SESSIONS.containsKey(id)) { | ||
return Optional.of(SESSIONS.get(id)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public void remove(Session session) { | ||
SESSIONS.remove(session.getId()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tomcat/src/main/java/org/apache/coyote/http11/ContentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.apache.coyote.http11; | ||
|
||
public enum ContentType { | ||
|
||
TEXT_HTML("text/html;charset=utf-8"), | ||
TEXT_CSS("text/css;charset=utf-8"), | ||
; | ||
|
||
private final String value; | ||
|
||
ContentType(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String value() { | ||
return value; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tomcat/src/main/java/org/apache/coyote/http11/ExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import static org.apache.coyote.http11.ContentType.TEXT_HTML; | ||
|
||
import java.io.IOException; | ||
|
||
public class ExceptionHandler { | ||
|
||
private static final String RESOURCE_PATH_FORMAT = "static/%s.html"; | ||
|
||
public HttpResponse handleException(HttpException e) throws IOException { | ||
HttpStatus httpStatus = e.httpStatus(); | ||
int code = httpStatus.statusCode(); | ||
ResourceLoader resourceLoader = new ResourceLoader(); | ||
String body = resourceLoader.load(String.format(RESOURCE_PATH_FORMAT, code)); | ||
return HttpResponse.status(httpStatus) | ||
.contentType(TEXT_HTML) | ||
.body(body) | ||
.build(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tomcat/src/main/java/org/apache/coyote/http11/FormData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.apache.coyote.http11; | ||
|
||
import static org.apache.coyote.http11.HttpStatus.BAD_REQUEST; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class FormData { | ||
|
||
private static final String AND = "&"; | ||
private static final String EQUAL = "="; | ||
|
||
private final Map<String, String> formTable; | ||
|
||
private FormData(Map<String, String> formTable) { | ||
this.formTable = formTable; | ||
} | ||
|
||
public static FormData from(String formData) { | ||
Map<String, String> formTable = new HashMap<>(); | ||
String[] splitByAnd = formData.split(AND); | ||
for (String pair : splitByAnd) { | ||
String[] splitByEqual = pair.split(EQUAL, 2); | ||
String key = splitByEqual[0]; | ||
String value = splitByEqual[1]; | ||
formTable.put(key, value); | ||
} | ||
return new FormData(formTable); | ||
} | ||
|
||
public String get(String key) { | ||
if (formTable.containsKey(key)) { | ||
return formTable.get(key); | ||
} | ||
throw new HttpException(BAD_REQUEST, "데이터가 존재하지 않습니다"); | ||
} | ||
} |
Oops, something went wrong.