Skip to content

Commit

Permalink
refactor: getter 대신 메시지를 던지도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
BGuga committed Sep 13, 2023
1 parent e861aa3 commit 5813104
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public void process(final Socket connection) {
}

private String getResponse(HttpRequest httpRequest, HttpResponse httpResponse) {
if (resourceProvider.haveResource(httpRequest.getRequestLine().getPath())) {
return resourceProvider.staticResourceResponse(httpRequest.getRequestLine().getPath());
String path = httpRequest.getRequestLine().getPath();
if (resourceProvider.haveResource(path)) {
return resourceProvider.staticResourceResponse(path);
}
if (handlerMapper.haveAvailableHandler(httpRequest)) {
handlerMapper.process(httpRequest, httpResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ public abstract class AbstractController implements Controller {

@Override
public void service(HttpRequest httpRequest, HttpResponse httpResponse) {
HttpMethod method = httpRequest.getRequestLine().getMethod();
if (method == HttpMethod.GET) {
if (httpRequest.isRequestOf(HttpMethod.GET)) {
doGet(httpRequest, httpResponse);
return;
}
if (method == HttpMethod.POST) {
if (httpRequest.isRequestOf(HttpMethod.POST)) {
doPost(httpRequest, httpResponse);
return;
}
if (method == HttpMethod.PUT) {
if (httpRequest.isRequestOf(HttpMethod.PUT)) {
doPut(httpRequest, httpResponse);
return;
}
if (method == HttpMethod.DELETE) {
if (httpRequest.isRequestOf(HttpMethod.DELETE)) {
doDelete(httpRequest, httpResponse);
return;
}
throw new UnsupportedOperationException("해당 HttpMethod 는 아직 지원하지 않습니다." + method);
throw new UnsupportedOperationException(
"해당 HttpMethod 는 아직 지원하지 않습니다." + httpRequest.getRequestLine().getMethod());
}

protected void doGet(HttpRequest httpRequest, HttpResponse httpResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static String readHeaders(BufferedReader inputReader) {

private static String readBody(BufferedReader inputReader, HttpRequestHeaders httpRequestHeaders) {
try {
Optional<String> length = httpRequestHeaders.getContentLength();
Optional<String> length = httpRequestHeaders.contentLength();
if (length.isEmpty()) {
return null;
}
Expand All @@ -61,6 +61,10 @@ private static String readBody(BufferedReader inputReader, HttpRequestHeaders ht
}
}

public boolean isRequestOf(HttpMethod method) {
return requestLine.isRequestOf(method);
}

public RequestLine getRequestLine() {
return requestLine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ private String valueOf(String header) {
return stringBuilder.toString().trim();
}

public Optional<Cookies> getCookie() {
return cookies;
public Optional<String> contentLength() {
return Optional.ofNullable(headers.get(CONTENT_LENGTH));
}

public Map<String, String> getHeaders() {
return headers;
}

public Optional<String> getContentLength() {
return Optional.ofNullable(headers.get(CONTENT_LENGTH));
public Optional<Cookies> getCookie() {
return cookies;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public HttpMethod getMethod() {
return httpMethod;
}

public boolean isRequestOf(HttpMethod method) {
return this.getMethod() == method;
}

public String getPath() {
return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
public class HttpResponseHeaders {

private static final ResourceProvider resourceProvider = new ResourceProvider();
public static final String CONTENT_TYPE = "Content-Type";
public static final String CONTENT_LENGTH = "Content-Length";

private final Map<String, String> headers;

Expand All @@ -19,8 +21,8 @@ public static HttpResponseHeaders from(ResponseEntity<Object> responseEntity, Op
Map<String, String> headers = new HashMap<>();
headers.putAll(responseEntity.getHeaders());
if (body.isPresent()) {
headers.put("Content-Type", typeOf(responseEntity));
headers.put("Content-Length", body.get().getBytes().length + " ");
headers.put(CONTENT_TYPE, typeOf(responseEntity));
headers.put(CONTENT_LENGTH, body.get().getBytes().length + " ");
}
return new HttpResponseHeaders(headers);
}
Expand Down

0 comments on commit 5813104

Please sign in to comment.