Skip to content

Commit e19754c

Browse files
authored
Merge pull request #26 from codingbaraGo/develop
main <- develop
2 parents 1888b19 + bbbe0d5 commit e19754c

31 files changed

Lines changed: 888 additions & 57 deletions

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ dependencies {
1616
implementation 'ch.qos.logback:logback-classic:1.2.3'
1717
testImplementation 'org.assertj:assertj-core:3.16.1'
1818

19+
testImplementation 'org.mockito:mockito-core:4.11.0'
20+
testImplementation('org.mockito:mockito-junit-jupiter:4.11.0') {
21+
exclude group: 'org.junit.jupiter'
22+
exclude group: 'org.junit.platform'
23+
}
24+
1925

2026
}
2127

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package dependency;
2+
3+
import webserver.RegisterHandlerImpl;
4+
import webserver.exception.ExceptionHandlerMapping;
5+
import webserver.exception.handler.ErrorExceptionHandler;
6+
import webserver.exception.handler.ServiceExceptionHandler;
7+
import webserver.exception.handler.UnhandledErrorHandler;
8+
import webserver.http.request.HttpBufferedReaderRequestConverter;
9+
import webserver.http.request.HttpRequestConverter;
10+
import webserver.http.response.HttpBufferedStreamResponseConverter;
11+
import webserver.http.response.HttpResponseConverter;
12+
import webserver.web.WasServlet;
13+
import webserver.web.handler.StaticContentHandler;
14+
import webserver.web.handler.WebHandler;
15+
import webserver.web.handler.response.handler.StaticContentResponseHandler;
16+
import webserver.web.handler.response.handler.ViewResponseHandler;
17+
import webserver.web.handler.response.handler.WebHandlerResponseHandler;
18+
19+
import java.util.List;
20+
21+
public class AppConfig {
22+
//Http
23+
public HttpBufferedReaderRequestConverter httpBufferedReaderRequestConverter(){
24+
return new HttpBufferedReaderRequestConverter();
25+
}
26+
27+
public HttpBufferedStreamResponseConverter httpBufferedStreamResponseConverter(){
28+
return new HttpBufferedStreamResponseConverter();
29+
}
30+
31+
public HttpRequestConverter httpRequestConverter(){
32+
return httpBufferedReaderRequestConverter();
33+
}
34+
public HttpResponseConverter httpResponseConverter(){
35+
return httpBufferedStreamResponseConverter();
36+
}
37+
38+
39+
//Web
40+
public WasServlet wasServlet(){
41+
return new WasServlet(
42+
webHandlerList(),
43+
webHandlerResponseHandlerList()
44+
);
45+
}
46+
47+
private List<WebHandler> webHandlerList(){
48+
return List.of(
49+
staticContentHandler(),
50+
registerHandlerImpl()
51+
);
52+
}
53+
private RegisterHandlerImpl registerHandlerImpl(){
54+
return new RegisterHandlerImpl();
55+
}
56+
57+
private List<WebHandlerResponseHandler> webHandlerResponseHandlerList(){
58+
return List.of(
59+
staticContentResponseHandler(),
60+
viewResponseHandler()
61+
);
62+
}
63+
private StaticContentHandler staticContentHandler(){
64+
return new StaticContentHandler();
65+
}
66+
private ViewResponseHandler viewResponseHandler(){
67+
return new ViewResponseHandler();
68+
}
69+
private StaticContentResponseHandler staticContentResponseHandler(){
70+
return new StaticContentResponseHandler();
71+
}
72+
73+
74+
//Exception
75+
public ExceptionHandlerMapping exceptionHandlerMapping(){
76+
return new ExceptionHandlerMapping(
77+
List.of(
78+
serviceExceptionHandler(),
79+
errorExceptionHandler(),
80+
unhandledErrorHandler()
81+
)
82+
);
83+
}
84+
85+
private ServiceExceptionHandler serviceExceptionHandler(){
86+
return new ServiceExceptionHandler();
87+
}
88+
private UnhandledErrorHandler unhandledErrorHandler(){
89+
return new UnhandledErrorHandler();
90+
}
91+
private ErrorExceptionHandler errorExceptionHandler(){
92+
return new ErrorExceptionHandler();
93+
}
94+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dependency;
2+
3+
import webserver.exception.ExceptionHandlerMapping;
4+
import webserver.http.request.HttpRequestConverter;
5+
import webserver.http.response.HttpResponseConverter;
6+
import webserver.web.WasServlet;
7+
8+
public class DependencyLoader {
9+
private final AppConfig appConfig;
10+
11+
public final HttpRequestConverter httpRequestConverter;
12+
public final HttpResponseConverter httpResponseConverter;
13+
public final ExceptionHandlerMapping exceptionHandlerMapping;
14+
public final WasServlet wasServlet;
15+
16+
public DependencyLoader(){
17+
this.appConfig = new AppConfig();
18+
this.httpRequestConverter = appConfig.httpRequestConverter();
19+
this.httpResponseConverter = appConfig.httpResponseConverter();
20+
this.exceptionHandlerMapping = appConfig.exceptionHandlerMapping();
21+
this.wasServlet = appConfig.wasServlet();
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package webserver;
2+
3+
import db.Database;
4+
import model.User;
5+
import webserver.http.HttpMethod;
6+
import webserver.http.request.HttpRequest;
7+
import webserver.web.handler.DynamicViewHandler;
8+
import webserver.web.handler.response.view.ViewResponse;
9+
10+
public class RegisterHandlerImpl extends DynamicViewHandler {
11+
public RegisterHandlerImpl() {
12+
super(HttpMethod.GET,
13+
"/create");
14+
}
15+
16+
@Override
17+
public ViewResponse handle(HttpRequest request) {
18+
String userId = request.getQueryValue("userId");
19+
String password = request.getQueryValue("password");
20+
String name = request.getQueryValue("name");
21+
String email = request.getQueryValue("email");
22+
Database.addUser(new User(userId, password, name, email));
23+
return ViewResponse.of("/login");
24+
}
25+
}

src/main/java/webserver/WebServer.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import java.net.ServerSocket;
44
import java.net.Socket;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
57

8+
import dependency.DependencyLoader;
69
import org.slf4j.Logger;
710
import org.slf4j.LoggerFactory;
11+
import webserver.http.HttpServlet;
812

913
public class WebServer {
1014
private static final Logger logger = LoggerFactory.getLogger(WebServer.class);
1115
private static final int DEFAULT_PORT = 8080;
16+
private static final DependencyLoader LOADER = new DependencyLoader();
17+
private static final ExecutorService executor = Executors.newFixedThreadPool(32);
1218

1319
public static void main(String args[]) throws Exception {
1420
int port = 0;
@@ -25,8 +31,15 @@ public static void main(String args[]) throws Exception {
2531
// 클라이언트가 연결될때까지 대기한다.
2632
Socket connection;
2733
while ((connection = listenSocket.accept()) != null) {
28-
Thread thread = new Thread(new RequestHandler(connection));
29-
thread.start();
34+
Socket singleConnection = connection;
35+
executor.submit(() -> {
36+
HttpServlet httpServlet = new HttpServlet(LOADER.wasServlet,
37+
LOADER.exceptionHandlerMapping,
38+
LOADER.httpResponseConverter,
39+
LOADER.httpRequestConverter,
40+
singleConnection);
41+
httpServlet.run();
42+
});
3043
}
3144
}
3245
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package webserver.exception;
2+
3+
4+
import webserver.http.HttpStatus;
5+
6+
public enum ErrorCode {
7+
/* Internal Error */
8+
INTERNAL_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "500_INTERNAL", "서버 내부 오류가 발생했습니다."),
9+
10+
/* Request Error */
11+
INVALID_INPUT(HttpStatus.BAD_REQUEST, "400_INVALID_INPUT", "입력 값이 올바르지 않습니다."),
12+
MISSING_PARAMETER(HttpStatus.BAD_REQUEST, "400_MISSING_PARAM", "필수 파라미터가 누락되었습니다."),
13+
VALIDATION_FAILED(HttpStatus.BAD_REQUEST, "400_VALIDATION_FAIL", "유효성 검증에 실패했습니다."),
14+
15+
FORBIDDEN(HttpStatus.FORBIDDEN, "403_FORBIDDEN", "권한이 없습니다."),
16+
17+
NO_SUCH_RESOURCE(HttpStatus.NOT_FOUND, "404_NO_SUCH_RESOURCE", "요청한 리소스를 찾을 수 없습니다."),
18+
19+
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "405_METHOD_NOT_ALLOWED", "허용되지 않은 HTTP 메서드입니다."),
20+
;
21+
22+
23+
private final HttpStatus status;
24+
private final String code;
25+
private final String message;
26+
27+
public HttpStatus getStatus() {
28+
return status;
29+
}
30+
31+
public String getCode() {
32+
return code;
33+
}
34+
35+
public String getMessage() {
36+
return message;
37+
}
38+
39+
ErrorCode(HttpStatus status, String code, String message) {
40+
this.status = status;
41+
this.code = code;
42+
this.message = message;
43+
}
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package webserver.exception;
2+
3+
public class ErrorException extends RuntimeException {
4+
private final ErrorCode errorCode;
5+
private final Throwable throwable;
6+
7+
public ErrorException(String message) {
8+
super(message);
9+
this.errorCode = ErrorCode.INTERNAL_ERROR;
10+
this.throwable = null;
11+
}
12+
13+
public ErrorException(String message, Throwable t) {
14+
super(message);
15+
this.errorCode = ErrorCode.INTERNAL_ERROR;
16+
this.throwable = t;
17+
}
18+
19+
public ErrorCode getErrorCode() {
20+
return errorCode;
21+
}
22+
23+
public Throwable getThrowable() {
24+
return throwable;
25+
}
26+
}

src/main/java/webserver/exception/ExceptionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import java.net.Socket;
44

55
public interface ExceptionHandler {
6-
boolean support(Exception e);
7-
void handle(Exception e, Socket connection);
6+
boolean support(Throwable e);
7+
void handle(Throwable e, Socket connection);
88
}

src/main/java/webserver/exception/ExceptionHandlerMapping.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public ExceptionHandlerMapping (List<ExceptionHandler> handlers){
1010
this.handlers = handlers;
1111
}
1212

13-
public void handle(Exception e, Socket connection){
14-
ExceptionHandler handlerAdaptor = handlers.stream().filter(handler -> handler.support(e)).findFirst().orElseThrow();
15-
handlerAdaptor.handle(e, connection);
13+
public void handle(Throwable e, Socket connection){
14+
ExceptionHandler handler = handlers.stream().filter(h -> h.support(e)).findFirst().orElseThrow();
15+
handler.handle(e, connection);
1616
}
1717

1818
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package webserver.exception;
2+
3+
public class ServiceException extends RuntimeException {
4+
private final ErrorCode errorCode;
5+
6+
public ServiceException(ErrorCode errorCode) {
7+
super(errorCode.getMessage());
8+
this.errorCode = errorCode;
9+
}
10+
11+
public ServiceException(ErrorCode errorCode, String customMsg) {
12+
super((customMsg == null || customMsg.isBlank()) ? errorCode.getMessage() : customMsg);
13+
this.errorCode = errorCode;
14+
}
15+
16+
public ErrorCode getErrorCode() {
17+
return errorCode;
18+
}
19+
}

0 commit comments

Comments
 (0)