Skip to content

Commit a2bb988

Browse files
committed
refactor : 쿠키 객체 추가
1 parent e32c497 commit a2bb988

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

tomcat/src/main/java/org/apache/coyote/dynamichandler/LoginHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void doPost(
7474

7575
if (user.isPresent() && user.get().checkPassword(password)) {
7676
HttpSession httpSession = httpRequest.getHttpSession(true);
77-
httpResponse.addCookie("JSESSIONID=" + httpSession.getId());
77+
httpResponse.addCookie("JSESSIONID", httpSession.getId());
7878
httpResponse.sendRedirect("/index.html");
7979
httpSession.add("user", user.get());
8080
log.info("로그인 성공! 아이디 : {}", account);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.apache.coyote.http11;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class Cookie {
7+
8+
private final Map<String, String> values;
9+
10+
public Cookie() {
11+
values = new HashMap<>();
12+
}
13+
14+
public void put(String key, String value) {
15+
values.put(key, value);
16+
}
17+
18+
public Map<String, String> getValues() {
19+
return values;
20+
}
21+
22+
}

tomcat/src/main/java/org/apache/coyote/http11/HttpResponse.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.LinkedHashMap;
77
import java.util.Map;
8+
import java.util.StringJoiner;
89
import java.util.stream.Collectors;
910

1011
public class HttpResponse {
@@ -13,9 +14,11 @@ public class HttpResponse {
1314
private final Map<String, String> attribute = new LinkedHashMap<>();
1415
private HttpStatus status;
1516
private String responseBody;
17+
private Cookie cookie;
1618

1719
public void setStatus(HttpStatus status) {
1820
this.status = status;
21+
cookie = new Cookie();
1922
}
2023

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

29-
public void addCookie(String cookie) {
30-
attribute.put(SET_COOKIE.getValue(), cookie);
32+
public void addCookie(String key, String value) {
33+
cookie.put(key, value);
3134
}
3235

3336
public void setHeader(String key, String value) {
3437
attribute.put(key, value);
3538
}
3639

40+
private String getAttributeString() {
41+
return String.join("\r\n",
42+
attribute.entrySet()
43+
.stream()
44+
.map(entry -> entry.getKey() + ": " + entry.getValue() + " ")
45+
.collect(Collectors.joining("\r\n")),
46+
getCookieString());
47+
}
48+
49+
private String getCookieString() {
50+
Map<String, String> values = cookie.getValues();
51+
52+
if (values.isEmpty()) {
53+
return "";
54+
}
55+
56+
StringJoiner cookieString = new StringJoiner(";");
57+
values.forEach((key, value) -> cookieString.add(String.format("%s=%s", key, value)));
58+
59+
return SET_COOKIE.getValue() + ": " + cookieString;
60+
}
61+
3762
public String toString() {
3863
return String.join("\r\n",
3964
PROTOCOL + " " + status.getValue() + " " + status.name() + " ",
40-
attribute.entrySet()
41-
.stream()
42-
.map(entry -> entry.getKey() + ": " + entry.getValue() + " ")
43-
.collect(Collectors.joining("\r\n")),
44-
"",
65+
getAttributeString(),
4566
responseBody
4667
);
4768
}

0 commit comments

Comments
 (0)