23
23
*/
24
24
package org .cactoos .http ;
25
25
26
- import java .util .HashMap ;
27
- import java .util .Map ;
26
+ import java .util .Iterator ;
28
27
import org .cactoos .Input ;
29
28
import org .cactoos .Text ;
29
+ import org .cactoos .iterable .LengthOf ;
30
+ import org .cactoos .list .Mapped ;
31
+ import org .cactoos .map .MapEntry ;
30
32
import org .cactoos .map .MapEnvelope ;
33
+ import org .cactoos .map .MapOf ;
31
34
import org .cactoos .text .SplitText ;
32
35
33
36
/**
@@ -51,20 +54,23 @@ public final class HtCookies extends MapEnvelope<String, String> {
51
54
* @param rsp Response
52
55
*/
53
56
public HtCookies (final Input rsp ) {
54
- super (() -> {
55
- final Map <String , String > result = new HashMap <>();
56
- final String cookie = new HtHeaders (rsp ).get ("set-cookie" );
57
- for (final Text item : new SplitText (cookie , ";\\ s+" )) {
58
- final String [] entry = item .asString ().split ("=" , 2 );
59
- if (entry .length == 2 ) {
60
- result .put (entry [0 ], entry [1 ]);
61
- } else {
62
- throw new IllegalArgumentException (
63
- "Incorrect HTTP Response cookie"
57
+ super (() -> new MapOf <>(
58
+ new Mapped <>(
59
+ entry -> {
60
+ final Iterable <Text > parts = new SplitText (entry , "=" );
61
+ if (new LengthOf (parts ).intValue () != 2 ) {
62
+ throw new IllegalArgumentException (
63
+ "Incorrect HTTP Response cookie"
64
+ );
65
+ }
66
+ final Iterator <Text > iter = parts .iterator ();
67
+ return new MapEntry <>(
68
+ iter .next ().asString (),
69
+ iter .next ().asString ()
64
70
);
65
- }
66
- }
67
- return result ;
68
- } );
71
+ },
72
+ new SplitText ( new HtHeaders ( rsp ). get ( "set-cookie" ), "; \\ s+" )
73
+ )
74
+ ) );
69
75
}
70
76
}
0 commit comments