-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[톰캣 구현하기 - 1, 2단계] 다즐(최우창) 미션 제출합니다. #316
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fb8216a
test: 학습 테스트 작성
woo-chang 6a7068b
feat: GET /index.html 응답하기 기능 구현
woo-chang 4f0eef2
feat: HttpException 커스텀 예외 구현
woo-chang c1dad92
feat: http 공통 클래스 구현
woo-chang cae315f
feat: http request 클래스 구현
woo-chang 388d011
feat: http response 클래스 구현
woo-chang 2109717
feat: 요청 매핑을 위한 클래스 구현
woo-chang c3ca7d1
refactor: ACCEPT 헤더 처리 수정
woo-chang baa35a6
feat: Handler 클래스 기능 구현
woo-chang 130a18d
feat: Processor 클래스 기능 구현
woo-chang bb97098
test: GET 요청 본문 제거
woo-chang 33211e7
refactor(ViewResolver): 존재하지 않는 페이지 예외 처리
woo-chang 7612461
refactor: 요청 헤더 파싱 기능 수정
woo-chang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
tomcat/src/main/java/org/apache/coyote/handle/ViewResolver.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,49 @@ | ||
package org.apache.coyote.handle; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import org.apache.coyote.common.ContentType; | ||
import org.apache.coyote.common.HttpStatus; | ||
import org.apache.coyote.response.HttpResponse; | ||
|
||
public class ViewResolver { | ||
|
||
private static final String STATIC_DIRECTORY = "static"; | ||
private static final String SLASH = File.separator; | ||
private static final String NOT_FOUND = "404.html"; | ||
private static final ViewResolver VIEW_RESOLVER = new ViewResolver(); | ||
|
||
private ViewResolver() { | ||
} | ||
|
||
public static ViewResolver getInstance() { | ||
return VIEW_RESOLVER; | ||
} | ||
|
||
public void renderPage(final HttpResponse httpResponse, final HttpStatus httpStatus, final String page) | ||
throws IOException { | ||
try { | ||
httpResponse.setStatus(httpStatus); | ||
httpResponse.setContentType(getContentType(page)); | ||
httpResponse.setContent(getBody(page)); | ||
} catch (NullPointerException e) { | ||
httpResponse.setStatus(HttpStatus.NOT_FOUND); | ||
httpResponse.setContentType(ContentType.TEXT_HTML.getType()); | ||
httpResponse.setContent(getBody(NOT_FOUND)); | ||
} | ||
} | ||
|
||
private String getContentType(final String page) { | ||
final String extension = page.substring(page.lastIndexOf(".") + 1); | ||
String contentType = ContentType.getTypeFrom(extension); | ||
return contentType == null ? ContentType.APPLICATION_OCTET_STREAM.getType() : contentType; | ||
} | ||
|
||
private String getBody(final String page) throws IOException { | ||
final URL resource = ClassLoader.getSystemClassLoader().getResource(STATIC_DIRECTORY + SLASH + page); | ||
final File file = new File(resource.getFile()); | ||
return new String(Files.readAllBytes(file.toPath())); | ||
} | ||
} |
31 changes: 1 addition & 30 deletions
31
tomcat/src/main/java/org/apache/coyote/handle/handler/FileHandler.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
3 changes: 2 additions & 1 deletion
3
tomcat/src/main/java/org/apache/coyote/handle/handler/Handler.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
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
63 changes: 63 additions & 0 deletions
63
tomcat/src/test/java/org/apache/coyote/handle/ViewResolverTest.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,63 @@ | ||
package org.apache.coyote.handle; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import org.apache.coyote.common.HttpStatus; | ||
import org.apache.coyote.common.HttpVersion; | ||
import org.apache.coyote.response.HttpResponse; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class ViewResolverTest { | ||
|
||
@Nested | ||
class 페이지_렌더링 { | ||
|
||
@Test | ||
void 존재하는_페이지라면_해당_페이지를_렌더링한다() throws Exception { | ||
final HttpResponse httpResponse = new HttpResponse(HttpVersion.HTTP_11); | ||
final HttpStatus httpStatus = HttpStatus.OK; | ||
final String page = "index.html"; | ||
|
||
final ViewResolver viewResolver = ViewResolver.getInstance(); | ||
viewResolver.renderPage(httpResponse, httpStatus, page); | ||
|
||
final URL resource = getClass().getClassLoader().getResource("static/index.html"); | ||
final String expected = String.join("\r\n", | ||
"HTTP/1.1 200 OK ", | ||
"Content-Type: text/html;charset=utf-8 ", | ||
"Content-Length: 5564 ", | ||
"", | ||
new String(Files.readAllBytes(new File(resource.getFile()).toPath())) | ||
); | ||
assertThat(httpResponse).hasToString(expected); | ||
} | ||
|
||
@Test | ||
void 존재하지_않는_페이지라면_404_페이지를_렌더링한다() throws Exception { | ||
final HttpResponse httpResponse = new HttpResponse(HttpVersion.HTTP_11); | ||
final HttpStatus httpStatus = HttpStatus.OK; | ||
final String page = "hello.html"; | ||
|
||
final ViewResolver viewResolver = ViewResolver.getInstance(); | ||
viewResolver.renderPage(httpResponse, httpStatus, page); | ||
|
||
final URL resource = getClass().getClassLoader().getResource("static/404.html"); | ||
final String expected = String.join("\r\n", | ||
"HTTP/1.1 404 Not Found ", | ||
"Content-Type: text/html;charset=utf-8 ", | ||
"Content-Length: 2426 ", | ||
"", | ||
new String(Files.readAllBytes(new File(resource.getFile()).toPath())) | ||
); | ||
assertThat(httpResponse).hasToString(expected); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와우 이렇게 빠르게 ViewResolver를 도입해 해결하다니.. 멋집니다~! 보면 코드 수정도 많고 구조 변화도 있었던 것 같은데,, 고생했어요 ~!!