Skip to content

Commit

Permalink
refactor: httpRequest, httpResponse 변수명 통일
Browse files Browse the repository at this point in the history
  • Loading branch information
Go-Jaecheol committed Sep 14, 2023
1 parent aafb47e commit 37a4aaa
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public abstract class AbstractController implements Controller {
protected static final String HTTP_METHOD_EXCEPTION_MESSAGE = "올바르지 않은 HTTP Method 입니다.";

@Override
public void service(final HttpRequest request, final HttpResponse response) throws IOException {
if (request.getRequestLine().getMethod().equals("POST")) {
doPost(request, response);
public void service(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException {
if (httpRequest.getRequestLine().getMethod().equals("POST")) {
doPost(httpRequest, httpResponse);
return;
}
if (request.getRequestLine().getMethod().equals("GET")) {
doGet(request, response);
if (httpRequest.getRequestLine().getMethod().equals("GET")) {
doGet(httpRequest, httpResponse);
return;
}
throw new HttpRequestException(HTTP_METHOD_EXCEPTION_MESSAGE);
}

protected abstract void doPost(final HttpRequest request, final HttpResponse response) throws IOException;
protected abstract void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException;

protected abstract void doGet(final HttpRequest request, final HttpResponse response) throws IOException;
protected abstract void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException;
}
18 changes: 9 additions & 9 deletions tomcat/src/main/java/nextstep/jwp/controller/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
public class HomeController extends AbstractController {

@Override
protected void doPost(final HttpRequest request, final HttpResponse response) {
final var statusLine = StatusLine.of(request.getRequestLine().getProtocol(), HttpStatus.METHOD_NOT_ALLOWED);
response.setStatusLine(statusLine);
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) {
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.METHOD_NOT_ALLOWED);
httpResponse.setStatusLine(statusLine);
}

@Override
protected void doGet(final HttpRequest request, final HttpResponse response) {
final var statusLine = StatusLine.of(request.getRequestLine().getProtocol(), HttpStatus.OK);
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) {
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.OK);
final var responseBody = ResponseBody.fromText("Hello world!");
response.setStatusLine(statusLine);
response.addResponseHeader("Content-Type", TEXT_HTML);
response.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length));
response.setResponseBody(responseBody);
httpResponse.setStatusLine(statusLine);
httpResponse.addResponseHeader("Content-Type", TEXT_HTML);
httpResponse.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length));
httpResponse.setResponseBody(responseBody);
}
}
38 changes: 19 additions & 19 deletions tomcat/src/main/java/nextstep/jwp/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,44 @@ public class LoginController extends AbstractController {
private static final SessionManager SESSION_MANAGER = new SessionManager();

@Override
protected void doPost(final HttpRequest request, final HttpResponse response) {
final HttpCookie cookie = request.getCookie();
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) {
final HttpCookie cookie = httpRequest.getCookie();
final User user = findUserBySessionId(cookie.getJSessionId());
final var statusLine = StatusLine.of(request.getRequestLine().getProtocol(), HttpStatus.FOUND);
response.setStatusLine(statusLine);
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.FOUND);
httpResponse.setStatusLine(statusLine);
if (user == null) {
handleFirstLogin(request, response);
handleFirstLogin(httpRequest, httpResponse);
return;
}
response.addResponseHeader(HEADER_LOCATION, INDEX_PAGE);
httpResponse.addResponseHeader(HEADER_LOCATION, INDEX_PAGE);
}

private void handleFirstLogin(final HttpRequest request, final HttpResponse response) {
final Map<String, String> requestBodyValues = request.getRequestParameters();
private void handleFirstLogin(final HttpRequest httpRequest, final HttpResponse httpResponse) {
final Map<String, String> requestBodyValues = httpRequest.getRequestParameters();
final Optional<User> user = InMemoryUserRepository.findByAccount(requestBodyValues.get("account"));
if (user.isEmpty() || !user.get().checkPassword(requestBodyValues.get("password"))) {
response.addResponseHeader(HEADER_LOCATION, UNAUTHORIZED_PAGE);
httpResponse.addResponseHeader(HEADER_LOCATION, UNAUTHORIZED_PAGE);
return;
}
final String sessionId = addSession(user.get());
response.addResponseHeader(HEADER_LOCATION, INDEX_PAGE);
response.addResponseHeader(HEADER_SET_COOKIE, "JSESSIONID=" + sessionId);
httpResponse.addResponseHeader(HEADER_LOCATION, INDEX_PAGE);
httpResponse.addResponseHeader(HEADER_SET_COOKIE, "JSESSIONID=" + sessionId);
}

@Override
protected void doGet(final HttpRequest request, final HttpResponse response) throws IOException {
final HttpCookie cookie = request.getCookie();
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException {
final HttpCookie cookie = httpRequest.getCookie();
final User user = findUserBySessionId(cookie.getJSessionId());
if (user != null) {
doPost(request, response);
doPost(httpRequest, httpResponse);
return;
}
final var statusLine = StatusLine.of(request.getRequestLine().getProtocol(), HttpStatus.OK);
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.OK);
final var responseBody = ResponseBody.fromUri("/login.html");
response.setStatusLine(statusLine);
response.addResponseHeader(HEADER_CONTENT_TYPE, TEXT_HTML);
response.addResponseHeader(HEADER_CONTENT_LENGTH, String.valueOf(responseBody.getBody().getBytes().length));
response.setResponseBody(responseBody);
httpResponse.setStatusLine(statusLine);
httpResponse.addResponseHeader(HEADER_CONTENT_TYPE, TEXT_HTML);
httpResponse.addResponseHeader(HEADER_CONTENT_LENGTH, String.valueOf(responseBody.getBody().getBytes().length));
httpResponse.setResponseBody(responseBody);
}

private User findUserBySessionId(final String sessionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
public class RegisterController extends AbstractController {

@Override
protected void doPost(final HttpRequest request, final HttpResponse response) {
final Map<String, String> requestBodyValues = request.getRequestParameters();
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) {
final Map<String, String> requestBodyValues = httpRequest.getRequestParameters();
final var user = new User(requestBodyValues.get("account"), requestBodyValues.get("password"),
requestBodyValues.get("email"));
InMemoryUserRepository.save(user);

final var statusLine = StatusLine.of(request.getRequestLine().getProtocol(), HttpStatus.FOUND);
response.setStatusLine(statusLine);
response.addResponseHeader("Location", INDEX_PAGE);
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.FOUND);
httpResponse.setStatusLine(statusLine);
httpResponse.addResponseHeader("Location", INDEX_PAGE);
}

@Override
protected void doGet(final HttpRequest request, final HttpResponse response) throws IOException {
final var statusLine = StatusLine.of(request.getRequestLine().getProtocol(), HttpStatus.OK);
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException {
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.OK);
final var responseBody = ResponseBody.fromUri("/register.html");
response.setStatusLine(statusLine);
response.addResponseHeader("Content-Type", TEXT_HTML);
response.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length));
response.setResponseBody(responseBody);
httpResponse.setStatusLine(statusLine);
httpResponse.addResponseHeader("Content-Type", TEXT_HTML);
httpResponse.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length));
httpResponse.setResponseBody(responseBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
public class ResourceController extends AbstractController {

@Override
protected void doPost(final HttpRequest request, final HttpResponse response) {
final var statusLine = StatusLine.of(request.getRequestLine().getProtocol(), HttpStatus.METHOD_NOT_ALLOWED);
response.setStatusLine(statusLine);
protected void doPost(final HttpRequest httpRequest, final HttpResponse httpResponse) {
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.METHOD_NOT_ALLOWED);
httpResponse.setStatusLine(statusLine);
}

@Override
protected void doGet(final HttpRequest request, final HttpResponse response) throws IOException {
final var statusLine = StatusLine.of(request.getRequestLine().getProtocol(), HttpStatus.OK);
final String uri = request.getRequestLine().getPath().split("\\?")[0];
protected void doGet(final HttpRequest httpRequest, final HttpResponse httpResponse) throws IOException {
final var statusLine = StatusLine.of(httpRequest.getRequestLine().getProtocol(), HttpStatus.OK);
final String uri = httpRequest.getRequestLine().getPath().split("\\?")[0];
final var responseBody = ResponseBody.fromUri(uri);
response.setStatusLine(statusLine);
response.addResponseHeader("Content-Type", getContentType(uri));
response.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length));
response.setResponseBody(responseBody);
httpResponse.setStatusLine(statusLine);
httpResponse.addResponseHeader("Content-Type", getContentType(uri));
httpResponse.addResponseHeader("Content-Length", String.valueOf(responseBody.getBody().getBytes().length));
httpResponse.setResponseBody(responseBody);
}

private String getContentType(final String uri) {
Expand Down

0 comments on commit 37a4aaa

Please sign in to comment.