5
5
6
6
import java .util .LinkedHashMap ;
7
7
import java .util .Map ;
8
+ import java .util .StringJoiner ;
8
9
import java .util .stream .Collectors ;
9
10
10
11
public class HttpResponse {
@@ -13,9 +14,11 @@ public class HttpResponse {
13
14
private final Map <String , String > attribute = new LinkedHashMap <>();
14
15
private HttpStatus status ;
15
16
private String responseBody ;
17
+ private Cookie cookie ;
16
18
17
19
public void setStatus (HttpStatus status ) {
18
20
this .status = status ;
21
+ cookie = new Cookie ();
19
22
}
20
23
21
24
public void setResponseBody (String responseBody ) {
@@ -26,22 +29,40 @@ public void sendRedirect(String redirectionUrl) {
26
29
attribute .put (LOCATION .getValue (), redirectionUrl );
27
30
}
28
31
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 );
31
34
}
32
35
33
36
public void setHeader (String key , String value ) {
34
37
attribute .put (key , value );
35
38
}
36
39
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
+
37
62
public String toString () {
38
63
return String .join ("\r \n " ,
39
64
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 (),
45
66
responseBody
46
67
);
47
68
}
0 commit comments