Skip to content

Commit

Permalink
refactor: AbstractResourceHandler 삭제 및 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
hectick committed Sep 12, 2023
1 parent 45c68b4 commit c1053a1
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
package nextstep.jwp.controller;

import static org.apache.coyote.http11.response.HttpResponseHeader.ALLOW;
import static org.apache.coyote.http11.response.HttpResponseHeader.CONTENT_LENGTH;
import static org.apache.coyote.http11.response.HttpResponseHeader.CONTENT_TYPE;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.catalina.controller.Controller;
import org.apache.coyote.http11.request.HttpMethod;
import org.apache.coyote.http11.request.HttpRequest;
import org.apache.coyote.http11.response.ContentType;
import org.apache.coyote.http11.response.HttpResponse;
import org.apache.coyote.http11.response.StatusCode;

public abstract class AbstractController implements Controller {

private static final String METHOD_NOT_ALLOWED_PAGE_PATH = "/405.html";
private static final String RESOURCE_DIRECTORY = "static";
private static final String ALLOW_METHOD = "GET, POST";

@Override
public void service(final HttpRequest request, final HttpResponse response) throws IOException {
if(HttpMethod.POST == request.getHttpMethod()) {
doPost(request, response);
return;
}

if(HttpMethod.GET == request.getHttpMethod()) {
doGet(request, response);
return;
}
throw new IllegalArgumentException("지원하지 않는 http 메서드입니다.");

resolveMethodNowAllowedPage(response);
}

protected abstract void doPost(HttpRequest request, HttpResponse response) throws IOException;

protected abstract void doGet(HttpRequest request, HttpResponse response) throws IOException;

private void resolveMethodNowAllowedPage(final HttpResponse response) throws IOException {
final String body = generateBody();
response.setStatusCode(StatusCode.METHOD_NOT_ALLOWED);
response.setBody(body);
response.addHeader(ALLOW, ALLOW_METHOD);
response.addHeader(CONTENT_TYPE, ContentType.HTML.getContentType());
response.addHeader(CONTENT_LENGTH, String.valueOf(body.getBytes().length));
}

private String generateBody() throws IOException {
String fileName = RESOURCE_DIRECTORY + METHOD_NOT_ALLOWED_PAGE_PATH;
URL resource = getClass().getClassLoader().getResource(fileName);
final Path path = new File(resource.getPath()).toPath();
return new String(Files.readAllBytes(path));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
import static org.apache.coyote.http11.response.HttpResponseHeader.CONTENT_TYPE;

import java.io.IOException;
import org.apache.coyote.http11.request.HttpMethod;
import org.apache.coyote.http11.request.HttpRequest;
import org.apache.coyote.http11.response.ContentType;
import org.apache.coyote.http11.response.HttpResponse;
import org.apache.coyote.http11.response.StatusCode;

public class DefaultResourceHandler extends AbstractResourceHandler {
public class DefaultResourceHandler implements ResourceHandler {

private static final String PATH = "/";

@Override
public boolean supports(final HttpRequest httpRequest) {
return PATH.equals(httpRequest.getPath());
public boolean supports(final HttpRequest request) {
return PATH.equals(request.getPath()) && HttpMethod.GET == request.getHttpMethod();
}

@Override
protected void doGet(final HttpRequest request, final HttpResponse response) throws IOException {
public void service(final HttpRequest request, final HttpResponse response) throws IOException {
final String body = "Hello world!";
response.setStatusCode(StatusCode.OK);
response.setBody(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public interface ResourceHandler {

boolean supports(final HttpRequest httpRequest);
boolean supports(final HttpRequest request);

void service(HttpRequest request, HttpResponse response) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class ResourceHandlerMapper {

private static final List<ResourceHandler> handlers = new ArrayList<>();
private static final ResourceHandler unsupportedResourceHandler = new UnsupportedResourceHandler();
private static final ResourceHandler unsupportedResourceHandler = new UnsupportedMethodHandler();

static {
handlers.add(new DefaultResourceHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.coyote.http11.request.HttpMethod;
import org.apache.coyote.http11.request.HttpRequest;
import org.apache.coyote.http11.response.ContentType;
import org.apache.coyote.http11.response.HttpResponse;
import org.apache.coyote.http11.response.StatusCode;

public class StaticResourceHandler extends AbstractResourceHandler {
public class StaticResourceHandler implements ResourceHandler {

private static final String NOT_FOUND_PAGE_PATH = "/404.html";
private static final String RESOURCE_DIRECTORY = "static";

@Override
public boolean supports(final HttpRequest httpRequest) {
return true;
public boolean supports(final HttpRequest request) {
return HttpMethod.GET == request.getHttpMethod();
}

@Override
protected void doGet(final HttpRequest request, final HttpResponse response) throws IOException {
public void service(final HttpRequest request, final HttpResponse response) throws IOException {
String fileName = RESOURCE_DIRECTORY + request.getPath();
URL resource = getClass().getClassLoader().getResource(fileName);
if (resource != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.coyote.http11.handler;

import static org.apache.coyote.http11.response.HttpResponseHeader.ALLOW;
import static org.apache.coyote.http11.response.HttpResponseHeader.CONTENT_LENGTH;
import static org.apache.coyote.http11.response.HttpResponseHeader.CONTENT_TYPE;

Expand All @@ -13,24 +14,26 @@
import org.apache.coyote.http11.response.HttpResponse;
import org.apache.coyote.http11.response.StatusCode;

public class UnsupportedResourceHandler implements ResourceHandler {
public class UnsupportedMethodHandler implements ResourceHandler {

private static final String NOT_FOUND_PAGE_PATH = "/404.html";
private static final String METHOD_NOT_ALLOWED_PAGE_PATH = "/405.html";
private static final String RESOURCE_DIRECTORY = "static";
private static final String ALLOW_METHOD = "GET";

@Override
public boolean supports(final HttpRequest httpRequest) {
public boolean supports(final HttpRequest request) {
return false;
}

@Override
public void service(final HttpRequest request, final HttpResponse response) throws IOException {
String fileName = RESOURCE_DIRECTORY + NOT_FOUND_PAGE_PATH;
String fileName = RESOURCE_DIRECTORY + METHOD_NOT_ALLOWED_PAGE_PATH;
URL resource = getClass().getClassLoader().getResource(fileName);
final Path path = new File(resource.getPath()).toPath();
final String body = new String(Files.readAllBytes(path));
response.setStatusCode(StatusCode.NOT_FOUND);
response.setStatusCode(StatusCode.METHOD_NOT_ALLOWED);
response.setBody(body);
response.addHeader(ALLOW, ALLOW_METHOD);
response.addHeader(CONTENT_TYPE, ContentType.HTML.getContentType());
response.addHeader(CONTENT_LENGTH, String.valueOf(body.getBytes().length));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public enum HttpResponseHeader {
SET_COOKIE("Set-Cookie"),
LOCATION("Location"),
CONTENT_TYPE("Content-Type"),
CONTENT_LENGTH("Content-Length");
CONTENT_LENGTH("Content-Length"),
ALLOW("Allow");

private final String header;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public enum StatusCode {
OK(200),
FOUND(302),
UNAUTHORIZED(401),
NOT_FOUND(404);
NOT_FOUND(404),
METHOD_NOT_ALLOWED(405);

private final int code;

Expand Down
50 changes: 50 additions & 0 deletions tomcat/src/main/resources/static/405.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>405 Error - SB Admin</title>
<link href="css/styles.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js" crossorigin="anonymous"></script>
</head>
<body>
<div id="layoutError">
<div id="layoutError_content">
<main>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="text-center mt-4">
<p class="lead">This http method is not allowed in this server.</p>
<a href="index.html">
<i class="fas fa-arrow-left me-1"></i>
Return to Dashboard
</a>
</div>
</div>
</div>
</div>
</main>
</div>
<div id="layoutError_footer">
<footer class="py-4 bg-light mt-auto">
<div class="container-fluid px-4">
<div class="d-flex align-items-center justify-content-between small">
<div class="text-muted">Copyright &copy; Your Website 2021</div>
<div>
<a href="#">Privacy Policy</a>
&middot;
<a href="#">Terms &amp; Conditions</a>
</div>
</div>
</div>
</footer>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="js/scripts.js"></script>
</body>
</html>

0 comments on commit c1053a1

Please sign in to comment.