Skip to content
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단계] 마코(이규성) 미션 제출합니다. #332

Merged
merged 10 commits into from
Sep 5, 2023
3 changes: 2 additions & 1 deletion tomcat/src/main/java/common/FileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class FileReader {

private static final String STATIC_RESOURCE_PATH = "static";
private static final String EXTENSION_HTML = ".html";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEFAULT_FILE_EXTENSION 은 어떤가요?!
기본 확장자가 .html인걸 더 잘 알려주는 것 같아요!


private FileReader() {
}
Expand All @@ -23,7 +24,7 @@ private static URL findResource(String fileName) {
URL resource = FileReader.class.getClassLoader()
.getResource(STATIC_RESOURCE_PATH + fileName);
if (resource == null) {
fileName = fileName + ".html";
fileName = fileName + EXTENSION_HTML;
resource = FileReader.class.getClassLoader()
.getResource(STATIC_RESOURCE_PATH + fileName);
}
Expand Down
152 changes: 0 additions & 152 deletions tomcat/src/main/java/nextstep/handler/Handler.java

This file was deleted.

30 changes: 30 additions & 0 deletions tomcat/src/main/java/nextstep/mvc/ModelAndView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nextstep.mvc;

import java.util.HashMap;
import java.util.Map;

public class ModelAndView {

private final String viewName;
private final Map<String, String> model = new HashMap<>();
Comment on lines +6 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은데요?


public ModelAndView(final String viewName) {
this.viewName = viewName;
}

public ModelAndView() {
this(null);
}

public void setAttribute(String key, String value) {
model.put(key, value);
}

public String getViewName() {
return viewName;
}

public Map<String, String> getModel() {
return model;
}
}
41 changes: 41 additions & 0 deletions tomcat/src/main/java/nextstep/mvc/View.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nextstep.mvc;

import java.util.Map;
import org.apache.coyote.http11.HttpHeaders;
import org.apache.coyote.http11.HttpResponse;

public class View {

private static final String DEFAULT_CHAR_SET = "text/html;charset=utf-8";

private String content;
private final String contentType;

public View() {
this.contentType = DEFAULT_CHAR_SET;
}

public View(final String content, final String contentType) {
this.content = content;
this.contentType = contentType;
}

public void render(Map<String, String> model, HttpResponse httpResponse) {
if (content == null) {
StringBuilder sb = new StringBuilder();
for (final var value : model.values()) {
sb.append(value);
}
httpResponse.addHeader(HttpHeaders.CONTENT_TYPE, DEFAULT_CHAR_SET);
httpResponse.addHeader(HttpHeaders.CONTENT_LENGTH,
String.valueOf(sb.toString().getBytes().length));
httpResponse.setBody(sb.toString());
return;
}
Comment on lines +24 to +34
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

early return 👍


httpResponse.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
httpResponse.addHeader(HttpHeaders.CONTENT_LENGTH,
String.valueOf(content.getBytes().length));
httpResponse.setBody(content);
}
}
15 changes: 15 additions & 0 deletions tomcat/src/main/java/nextstep/mvc/ViewResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nextstep.mvc;

import common.FileReader;
import java.io.IOException;
import org.apache.coyote.http11.SupportContentType;

public class ViewResolver {

private ViewResolver() {
}

public static View resolve(final String viewName) throws IOException {
return new View(FileReader.readFile(viewName), SupportContentType.getContentType(viewName));
}
}
Loading