Skip to content

Commit

Permalink
refactor: RequestLine의 httpMethod enum으로 변경 및 커스텀 예외 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
zillionme committed Sep 6, 2023
1 parent 5d54908 commit a4776fa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.apache.coyote.http11.exception;

public class InvalidRequestException extends RuntimeException {

public InvalidRequestException() {
super("유효하지 않은 요청입니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package org.apache.coyote.http11.request;

import org.apache.coyote.http11.exception.InvalidRequestException;

public class RequestLine {

private final String requestMethod;
private final RequestMethod requestMethod;
private final String requestUrl;
private final String requestProtocol;

public RequestLine(final String requestMethod, final String requestUrl, final String requestProtocol) {
public RequestLine(final RequestMethod requestMethod, final String requestUrl, final String requestProtocol) {
this.requestMethod = requestMethod;
this.requestUrl = requestUrl;
this.requestProtocol = requestProtocol;
}

public static RequestLine from(final String line) {
String[] requestParts = line.split(" ");
if(requestParts.length != 3) {
throw new IllegalArgumentException(); //todo : 유효하지 않은 헤더 예외처리
if (requestParts.length != 3) {
throw new InvalidRequestException();
}
return new RequestLine(requestParts[0], requestParts[1], requestParts[2]);
RequestMethod requestMethod = RequestMethod.getRequestMethod(requestParts[0]);
return new RequestLine(requestMethod, requestParts[1], requestParts[2]);
}

public String getRequestMethod() {
public RequestMethod getRequestMethod() {
return requestMethod;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.apache.coyote.http11.request;

import org.apache.coyote.http11.exception.InvalidRequestException;

import java.util.Arrays;

public enum RequestMethod {
GET,
POST,
PATCH,
UPDATE,
DELETE,
;

RequestMethod() {
}

public static RequestMethod getRequestMethod(String value) {
return Arrays.stream(RequestMethod.values())
.filter(requestMethod -> requestMethod.name().equals(value))
.findFirst()
.orElseThrow(InvalidRequestException::new);
}
}

0 comments on commit a4776fa

Please sign in to comment.