Skip to content

Commit

Permalink
chore: CodeSmell 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 committed Sep 3, 2023
1 parent 24bab19 commit 0bb4a01
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.file.NoSuchFileException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -24,6 +25,7 @@
import org.apache.coyote.http11.handler.IndexHandler;
import org.apache.coyote.http11.handler.LoginHandler;
import org.apache.coyote.http11.handler.MethodNotAllowedHandler;
import org.apache.coyote.http11.handler.NotFoundHandler;
import org.apache.coyote.http11.handler.RegisterHandler;
import org.apache.coyote.http11.handler.StaticResourceHandler;
import org.apache.coyote.util.CookieParser;
Expand All @@ -36,6 +38,9 @@ public class Http11Processor implements Runnable, Processor {

private final Socket connection;
private final Map<String, Handler> handlerMap = new HashMap<>();
private final Handler staticResourceHandler = new StaticResourceHandler();
private final Handler methodNotAllowedHandler = new MethodNotAllowedHandler();
private final Handler notFoundHandler = new NotFoundHandler();

public Http11Processor(final Socket connection) {
this.connection = connection;
Expand Down Expand Up @@ -108,16 +113,18 @@ private String getRequestBody(HttpHeaders httpHeaders, BufferedReader bufferedRe
}

private HttpResponse handle(HttpRequest request) throws IOException {
Handler handler = handlerMap.getOrDefault(request.getPath(), StaticResourceHandler.INSTANCE);
Handler handler = handlerMap.getOrDefault(request.getPath(), staticResourceHandler);
try {
return handler.handle(request);
} catch (MethodNotAllowedException e) {
HttpResponse response = MethodNotAllowedHandler.INSTANCE.handle(request);
HttpResponse response = methodNotAllowedHandler.handle(request);
List<String> allowedMethods = e.getAllowedMethods().stream()
.map(Enum::name)
.collect(toList());
response.setHeader("Allow", allowedMethods);
return response;
} catch (NoSuchFileException e) {
return notFoundHandler.handle(request);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

public class MethodNotAllowedHandler implements Handler {

public static final MethodNotAllowedHandler INSTANCE = new MethodNotAllowedHandler();

private MethodNotAllowedHandler() {
}

@Override
public HttpResponse handle(HttpRequest request) throws IOException {
HttpResponse response = new HttpResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

public class NotFoundHandler implements Handler {

public static final NotFoundHandler INSTANCE = new NotFoundHandler();

private NotFoundHandler() {
}

@Override
public HttpResponse handle(HttpRequest request) throws IOException {
HttpResponse response = new HttpResponse();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.apache.coyote.http11.handler;

import java.io.IOException;
import java.nio.file.NoSuchFileException;
import org.apache.coyote.Handler;
import org.apache.coyote.common.HttpContentType;
import org.apache.coyote.common.HttpRequest;
Expand All @@ -11,24 +10,15 @@

public class StaticResourceHandler implements Handler {

public static final StaticResourceHandler INSTANCE = new StaticResourceHandler();

private StaticResourceHandler() {
}

@Override
public HttpResponse handle(HttpRequest request) throws IOException {
String path = request.getPath();
HttpResponse response = new HttpResponse();
response.setContentType(getContentType(path));
try {
String contentBody = ResourceResolver.resolve(path);
response.setContentBody(contentBody);
response.setHttpStatus(HttpStatus.OK);
return response;
} catch (NoSuchFileException e) {
return NotFoundHandler.INSTANCE.handle(request);
}
String contentBody = ResourceResolver.resolve(path);
response.setContentBody(contentBody);
response.setHttpStatus(HttpStatus.OK);
return response;
}

private HttpContentType getContentType(String path) {
Expand Down
21 changes: 18 additions & 3 deletions tomcat/src/test/java/org/apache/coyote/common/RequestUriTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,39 @@ class RequestUriTest {

@Test
void httpMethod가_null이면_예외() {
// given
HttpMethod method = null;
HttpPath path = HttpPath.from("/");
HttpProtocol protocol = HttpProtocol.HTTP11;

// when & then
assertThatThrownBy(() -> new RequestUri(null, HttpPath.from("/"), HttpProtocol.HTTP11))
assertThatThrownBy(() -> new RequestUri(method, path, protocol))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("HttpMethod는 null이 될 수 없습니다.");
}

@Test
void path가_null이면_예외() {
// given
HttpMethod method = HttpMethod.GET;
HttpPath path = null;
HttpProtocol protocol = HttpProtocol.HTTP11;

// when & then
assertThatThrownBy(() -> new RequestUri(HttpMethod.GET, null, HttpProtocol.HTTP11))
assertThatThrownBy(() -> new RequestUri(method, path, protocol))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Path는 null이 될 수 없습니다.");
}

@Test
void httpProtocol_null이면_예외() {
// given
HttpMethod method = HttpMethod.GET;
HttpPath path = HttpPath.from("/");
HttpProtocol protocol = null;

// when & then
assertThatThrownBy(() -> new RequestUri(HttpMethod.GET, HttpPath.from("/"), null))
assertThatThrownBy(() -> new RequestUri(method, path, protocol))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("HttpProtocol은 null이 될 수 없습니다.");
}
Expand Down

0 comments on commit 0bb4a01

Please sign in to comment.