Skip to content

Commit

Permalink
refactor: RequestBody 정팩메 반환 타입 수정 및 기본 자원 반환 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
HubCreator committed Sep 13, 2023
1 parent d1c9cd5 commit 32ab55d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void doPost(HttpRequest request, HttpResponse response) {
response.redirect(INDEX_URI.getFullPath());
return;
}
final RequestBody requestBody = request.getRequestBody();
final RequestBody requestBody = request.getRequestBody().orElseThrow(IllegalStateException::new);
final String account = requestBody.getParamValue("account");
final String password = requestBody.getParamValue("password");
final Optional<User> userOptional = InMemoryUserRepository.findByAccount(account);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected void doGet(HttpRequest request, HttpResponse response) throws Exceptio

@Override
protected void doPost(HttpRequest request, HttpResponse response) {
final RequestBody requestBody = request.getRequestBody();
final RequestBody requestBody = request.getRequestBody().orElseThrow(IllegalStateException::new);
final String accountValue = requestBody.getParamValue("account");
final Optional<User> userOptional = InMemoryUserRepository.findByAccount(accountValue);
if (userOptional.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
import org.apache.coyote.http11.response.HttpResponse;
import org.apache.coyote.http11.response.HttpStatus;
import org.apache.coyote.http11.response.ResponseBody;
import org.apache.coyote.http11.response.StaticResource;

import java.io.IOException;

public class RootController extends AbstractController {

@Override
protected void doGet(HttpRequest request, HttpResponse response) {
final ResponseBody responseBody = ResponseBody.rootContent();
response.setHttpStatus(HttpStatus.OK);
response.setResponseBody(responseBody);
response.setResponseHeaders(responseBody);
try {
final StaticResource staticResource = StaticResource.from(request.getRequestLine().getPath());
final ResponseBody responseBody = ResponseBody.from(staticResource);
response.setResponseBody(responseBody);
response.setHttpStatus(HttpStatus.OK);
response.setResponseHeaders(responseBody);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public class HttpRequest {

private final RequestLine requestLine;
private final RequestHeaders requestHeaders;
private final RequestBody requestBody;
private final Optional<RequestBody> requestBody;

private HttpRequest(final RequestLine requestLine,
final RequestHeaders requestHeaders,
final RequestBody requestBody) {
final Optional<RequestBody> requestBody) {
this.requestLine = requestLine;
this.requestHeaders = requestHeaders;
this.requestBody = requestBody;
Expand All @@ -26,15 +26,15 @@ private HttpRequest(final RequestLine requestLine,
public static HttpRequest from(final BufferedReader bufferedReader) throws IOException {
final RequestLine requestLine = RequestLine.from(bufferedReader.readLine());
final RequestHeaders requestHeaders = RequestHeaders.from(bufferedReader);
final RequestBody requestBody = RequestBody.of(requestHeaders, bufferedReader);
final Optional<RequestBody> requestBody = RequestBody.of(requestHeaders, bufferedReader);
return new HttpRequest(requestLine, requestHeaders, requestBody);
}

public RequestLine getRequestLine() {
return requestLine;
}

public RequestBody getRequestBody() {
public Optional<RequestBody> getRequestBody() {
return requestBody;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.StringTokenizer;

public class RequestBody {
Expand All @@ -16,9 +17,9 @@ private RequestBody(final Map<String, String> content) {
this.content = content;
}

public static RequestBody of(final RequestHeaders requestHeaders, final BufferedReader bufferedReader) throws IOException {
public static Optional<RequestBody> of(final RequestHeaders requestHeaders, final BufferedReader bufferedReader) throws IOException {
if (!requestHeaders.containsKey("Content-Length")) {
return EMPTY;
return Optional.empty();
}
final Map<String, String> result = new HashMap<>();
final int contentLength = Integer.parseInt(requestHeaders.getHeaderValue("Content-Length"));
Expand All @@ -33,10 +34,10 @@ public static RequestBody of(final RequestHeaders requestHeaders, final Buffered
final String[] split = param.split("=");
result.put(split[0], split[1]);
}
return new RequestBody(result);
return Optional.of(new RequestBody(result));
}

return EMPTY;
return Optional.empty();
}

public String getParamValue(final String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ public static ResponseBody from(final StaticResource staticResource) {
);
}

public static ResponseBody noContent() {
return new ResponseBody(new byte[]{}, null);
}

public static ResponseBody rootContent() {
return new ResponseBody("Hello world!".getBytes(), ContentType.HTML);
}

public byte[] getContent() {
return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ private StaticResource(final byte[] content, final String fileType) {
}

public static StaticResource from(final String fileName) throws IOException {
if (fileName.equals("/")) {
return new StaticResource("Hello World!".getBytes(), "");
}
return new StaticResource(extractContent(fileName), extractFileExtension(fileName));
}

Expand Down

0 comments on commit 32ab55d

Please sign in to comment.