Skip to content

Commit

Permalink
refactor : 쿠키 객체 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kpeel5839 committed Sep 11, 2023
1 parent e32c497 commit a2bb988
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void doPost(

if (user.isPresent() && user.get().checkPassword(password)) {
HttpSession httpSession = httpRequest.getHttpSession(true);
httpResponse.addCookie("JSESSIONID=" + httpSession.getId());
httpResponse.addCookie("JSESSIONID", httpSession.getId());
httpResponse.sendRedirect("/index.html");
httpSession.add("user", user.get());
log.info("로그인 성공! 아이디 : {}", account);
Expand Down
22 changes: 22 additions & 0 deletions tomcat/src/main/java/org/apache/coyote/http11/Cookie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.apache.coyote.http11;

import java.util.HashMap;
import java.util.Map;

public class Cookie {

private final Map<String, String> values;

public Cookie() {
values = new HashMap<>();
}

public void put(String key, String value) {
values.put(key, value);
}

public Map<String, String> getValues() {
return values;
}

}
35 changes: 28 additions & 7 deletions tomcat/src/main/java/org/apache/coyote/http11/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;

public class HttpResponse {
Expand All @@ -13,9 +14,11 @@ public class HttpResponse {
private final Map<String, String> attribute = new LinkedHashMap<>();
private HttpStatus status;
private String responseBody;
private Cookie cookie;

public void setStatus(HttpStatus status) {
this.status = status;
cookie = new Cookie();
}

public void setResponseBody(String responseBody) {
Expand All @@ -26,22 +29,40 @@ public void sendRedirect(String redirectionUrl) {
attribute.put(LOCATION.getValue(), redirectionUrl);
}

public void addCookie(String cookie) {
attribute.put(SET_COOKIE.getValue(), cookie);
public void addCookie(String key, String value) {
cookie.put(key, value);
}

public void setHeader(String key, String value) {
attribute.put(key, value);
}

private String getAttributeString() {
return String.join("\r\n",
attribute.entrySet()
.stream()
.map(entry -> entry.getKey() + ": " + entry.getValue() + " ")
.collect(Collectors.joining("\r\n")),
getCookieString());
}

private String getCookieString() {
Map<String, String> values = cookie.getValues();

if (values.isEmpty()) {
return "";
}

StringJoiner cookieString = new StringJoiner(";");
values.forEach((key, value) -> cookieString.add(String.format("%s=%s", key, value)));

return SET_COOKIE.getValue() + ": " + cookieString;
}

public String toString() {
return String.join("\r\n",
PROTOCOL + " " + status.getValue() + " " + status.name() + " ",
attribute.entrySet()
.stream()
.map(entry -> entry.getKey() + ": " + entry.getValue() + " ")
.collect(Collectors.joining("\r\n")),
"",
getAttributeString(),
responseBody
);
}
Expand Down

0 comments on commit a2bb988

Please sign in to comment.