Skip to content

Commit

Permalink
refactor: 상수화 및 메서드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
Ohjintaek committed Sep 14, 2023
1 parent b7d7d29 commit 0bb3470
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
35 changes: 23 additions & 12 deletions tomcat/src/main/java/nextstep/jwp/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

public class LoginController extends AbstractController {

private static final String PARAM_DELIMITER = "&";
private static final String KEY_VALUE_DELIMITER = "=";
private static final int ACCOUNT_INDEX = 0;
private static final int PASSWORD_INDEX = 1;

@Override
public boolean canHandle(HttpRequest request) {
return request.getRequestUri()
Expand All @@ -26,26 +31,32 @@ public boolean canHandle(HttpRequest request) {

@Override
protected void doPost(HttpRequest request, HttpResponse response) {
final Map<String, String> logInfo = Arrays.stream(request.getRequestBody().split("&"))
.map(input -> input.split("="))
.collect(Collectors.toMap(info -> info[0], info -> info[1]));
final Map<String, String> authInfo = Arrays.stream(request.getRequestBody().split(PARAM_DELIMITER))
.map(input -> input.split(KEY_VALUE_DELIMITER))
.collect(Collectors.toMap(info -> info[ACCOUNT_INDEX], info -> info[PASSWORD_INDEX]));

response.setStatusLine(StatusLine.of(request.getHttpVersion(), HttpStatus.FOUND));
final Optional<User> savedUser = InMemoryUserRepository.findByAccount(logInfo.get("account"));
final Optional<User> savedUser = findUserByLogIn(authInfo);
if (savedUser.isPresent()) {
final User user = savedUser.get();
if (user.checkPassword(logInfo.get("password"))) {
response.addHeader(HttpHeader.LOCATION.getName(), "index.html");
final Session session = new Session(UUID.randomUUID().toString());
session.setAttribute("user", user);
SessionManager.add(session);
response.addCookie("JSESSIONID", session.getId());
return;
}
response.addHeader(HttpHeader.LOCATION.getName(), "index.html");
final Session session = new Session(UUID.randomUUID().toString());
session.setAttribute("user", user);
SessionManager.add(session);
response.addCookie("JSESSIONID", session.getId());
return;
}
response.addHeader(HttpHeader.LOCATION.getName(), "401.html");
}

private Optional<User> findUserByLogIn(final Map<String, String> logInfo) {
final Optional<User> savedUser = InMemoryUserRepository.findByAccount(logInfo.get("account"));
if (savedUser.isEmpty() || savedUser.get().checkPassword(logInfo.get("password"))) {
return Optional.empty();
}
return savedUser;
}

@Override
protected void doGet(HttpRequest request, HttpResponse response) throws Exception {
final String sessionId = request.getCookies().ofSessionId("JSESSIONID");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class HttpCookies {

private static final String DELIMITER = "\r\n";

private final Map<String, String> cookies = new LinkedHashMap<>();

public HttpCookies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void process(final Socket connection) {
final HttpRequest httpRequest = HttpRequest.from(bufferedReader);
final HttpResponse httpResponse = new HttpResponse();
requestMapping.service(httpRequest, httpResponse);
outputStream.write(httpResponse.toResponse().getBytes());
outputStream.write(httpResponse.cconvertToString().getBytes());
outputStream.flush();
} catch (Exception e) {
log.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void addCookie(final String name, final String value) {
cookies.save(name, value);
}

public String toResponse() {
public String cconvertToString() {
return String.join(DELIMITER,
statusLine.toResponse(),
cookies.toResponse() + responseHeaders.toResponse(),
Expand Down

0 comments on commit 0bb3470

Please sign in to comment.