-
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.
refactor: AbstractResourceHandler 삭제 및 리팩터링
- Loading branch information
Showing
10 changed files
with
107 additions
and
38 deletions.
There are no files selected for viewing
34 changes: 33 additions & 1 deletion
34
tomcat/src/main/java/nextstep/jwp/controller/AbstractController.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 |
---|---|---|
@@ -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)); | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
tomcat/src/main/java/org/apache/coyote/http11/handler/AbstractResourceHandler.java
This file was deleted.
Oops, something went wrong.
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
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
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
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,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 © Your Website 2021</div> | ||
<div> | ||
<a href="#">Privacy Policy</a> | ||
· | ||
<a href="#">Terms & 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> |