Skip to content

Commit

Permalink
refactor: Response Builder 패턴 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
echo724 committed Sep 12, 2023
1 parent 01c8fe9 commit 1762664
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class AuthenticationExceptionController extends ExceptionController {

@Override
protected void createErrorResponse(final HttpResponse response) {
response.addStatus(HttpStatus.FOUND)
.addLocation("/login.html");
response.status(HttpStatus.FOUND)
.location("/login.html");
}

public static Controller getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ protected void doPost(final HttpRequest request, final HttpResponse response) th
@Override
protected void doGet(final HttpRequest request, final HttpResponse response) throws Exception {
final var responseBody = "Hello world!";
response.addStatus(HttpStatus.OK)
.addContentType(ContentType.HTML)
.addBody(responseBody);
response.status(HttpStatus.OK)
.contentType(ContentType.HTML)
.body(responseBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ protected void doPost(final HttpRequest request, final HttpResponse response) th
.orElseThrow(() -> new AuthenticationException("아이디 또는 비밀번호가 틀립니다."));
final SessionManager sessionManager = SessionManager.getInstance();
final String sessionId = sessionManager.createSession(loginUser);
response.addStatus(HttpStatus.FOUND)
.addLocation("/index.html")
.addSetCookie(new Cookie("JSESSIONID", sessionId));
response.status(HttpStatus.FOUND)
.location("/index.html")
.setCookie(new Cookie("JSESSIONID", sessionId));
}

@Override
protected void doGet(final HttpRequest request, final HttpResponse response) throws Exception {
if (request.getHeaders().getCookies().containsKey("JSESSIONID")) {
response.addStatus(HttpStatus.FOUND)
.addLocation("/index.html");
response.status(HttpStatus.FOUND)
.location("/index.html");
return;
}
response.addStatus(HttpStatus.FOUND)
.addLocation("/login.html");
response.status(HttpStatus.FOUND)
.location("/login.html");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class NotFoundExceptionController extends ExceptionController {

@Override
protected void createErrorResponse(final HttpResponse response) {
response.addStatus(HttpStatus.FOUND)
.addLocation("/404.html");
response.status(HttpStatus.FOUND)
.location("/404.html");
}

public static Controller getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ protected void doPost(final HttpRequest request, final HttpResponse response) th
InMemoryUserRepository.save(user);
final SessionManager sessionManager = SessionManager.getInstance();
final String sessionId = sessionManager.createSession(user);
response.addStatus(HttpStatus.FOUND)
.addLocation("/index.html")
.addSetCookie(new Cookie("JSESSIONID", sessionId));
response.status(HttpStatus.FOUND)
.location("/index.html")
.setCookie(new Cookie("JSESSIONID", sessionId));
}

@Override
protected void doGet(final HttpRequest request, final HttpResponse response) throws Exception {
response.addStatus(HttpStatus.FOUND)
.addLocation("/register.html");
response.status(HttpStatus.FOUND)
.location("/register.html");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ protected void doGet(final HttpRequest request, final HttpResponse response) thr
final String extension = resourcePath.substring(resourcePath.lastIndexOf(".") + 1);
final ContentType contentType = ContentType.from(extension);
final String content = getResourceContent(resourcePath);
response.addStatus(HttpStatus.OK)
.addContentType(contentType)
.addBody(content);
response.status(HttpStatus.OK)
.contentType(contentType)
.body(content);
}
}
81 changes: 48 additions & 33 deletions tomcat/src/main/java/org/apache/coyote/http11/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.coyote.http11.header.ContentType;
import org.apache.coyote.http11.header.Cookie;
import org.apache.coyote.http11.header.HeaderType;
import org.apache.coyote.http11.header.HttpStatus;
import org.apache.coyote.http11.header.HttpVersion;

Expand All @@ -12,51 +13,65 @@

public class HttpResponse {
private static final String HEADER_SEPARATOR = ": ";
private final Map<String, String> headers = new LinkedHashMap<>();
private final HttpResponseBuilder builder = new HttpResponseBuilder();
private HttpStatus status;
private String body;

public HttpResponse addStatus(final HttpStatus status) {
public HttpResponseBuilder status(final HttpStatus status) {
this.status = status;
return this;
builder.status(status);
return builder;
}

public HttpResponse addContentType(final ContentType contentType) {
headers.put("Content-Type", contentType.getType());
return this;
public String build() {
return builder.build();
}

public HttpResponse addLocation(final String location) {
headers.put("Location", location);
return this;
public int getStatus() {
return status.getCode();
}

public String build() {
List<String> response = new ArrayList<>();
response.add(HttpVersion.HTTP_1_1.getVersion() + " " + status.getStatusResponse() + " ");
headers.forEach((key, value) -> response.add(key + HEADER_SEPARATOR + value + " "));
response.add("");
response.add(body);
return String.join("\r\n", response);
}
public static class HttpResponseBuilder {
private final Map<HeaderType, String> headers = new LinkedHashMap<>();
private HttpStatus status;
private String body;

public HttpResponse addSetCookie(final Cookie cookie) {
headers.put("Set-Cookie", cookie.getName() + "=" + cookie.getValue());
return this;
}
public void status(final HttpStatus status) {
this.status = status;
}

public HttpResponse addBody(final String responseBody) {
addContentLength(responseBody.getBytes().length);
this.body = responseBody;
return this;
}
public HttpResponseBuilder contentType(final ContentType contentType) {
headers.put(HeaderType.CONTENT_TYPE, contentType.getType());
return this;
}

public HttpResponse addContentLength(final int length) {
headers.put("Content-Length", String.valueOf(length));
return this;
}
public HttpResponseBuilder location(final String location) {
headers.put(HeaderType.LOCATION, location);
return this;
}

public int getStatus() {
return status.getCode();
public HttpResponseBuilder body(final String responseBody) {
contentLength(responseBody.getBytes().length);
this.body = responseBody;
return this;
}

public HttpResponseBuilder contentLength(final int length) {
headers.put(HeaderType.CONTENT_LENGTH, String.valueOf(length));
return this;
}

public String build() {
List<String> response = new ArrayList<>();
response.add(HttpVersion.HTTP_1_1.getVersion() + " " + status.getStatusResponse() + " ");
headers.forEach((key, value) -> response.add(key.getHeader() + HEADER_SEPARATOR + value + " "));
response.add("");
response.add(body);
return String.join("\r\n", response);
}

public HttpResponseBuilder setCookie(final Cookie cookie) {
headers.put(HeaderType.SET_COOKIE, cookie.getName() + "=" + cookie.getValue());
return this;
}
}
}

0 comments on commit 1762664

Please sign in to comment.