|
7 | 7 | * "License"); you may not use this file except in compliance |
8 | 8 | * with the License. You may obtain a copy of the License at |
9 | 9 | * |
10 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
11 | 11 | * |
12 | 12 | * Unless required by applicable law or agreed to in writing, |
13 | 13 | * software distributed under the License is distributed on an |
|
18 | 18 | */ |
19 | 19 | package org.jooby; |
20 | 20 |
|
21 | | -import static java.util.Objects.requireNonNull; |
| 21 | +import com.google.common.base.Splitter; |
| 22 | +import com.google.common.base.Strings; |
| 23 | +import com.google.common.io.BaseEncoding; |
| 24 | +import javaslang.Tuple; |
| 25 | +import javaslang.Tuple2; |
| 26 | +import javaslang.control.Try; |
| 27 | +import org.jooby.internal.CookieImpl; |
22 | 28 |
|
| 29 | +import javax.crypto.Mac; |
| 30 | +import javax.crypto.spec.SecretKeySpec; |
23 | 31 | import java.net.URLDecoder; |
24 | 32 | import java.net.URLEncoder; |
25 | 33 | import java.nio.charset.StandardCharsets; |
| 34 | +import java.util.Collections; |
26 | 35 | import java.util.Iterator; |
27 | 36 | import java.util.Map; |
| 37 | +import java.util.Objects; |
28 | 38 | import java.util.Optional; |
29 | 39 | import java.util.function.Function; |
30 | 40 | import java.util.regex.Pattern; |
31 | 41 | import java.util.stream.Collectors; |
32 | 42 |
|
33 | | -import javax.crypto.Mac; |
34 | | -import javax.crypto.spec.SecretKeySpec; |
35 | | - |
36 | | -import org.jooby.internal.CookieImpl; |
37 | | - |
38 | | -import com.google.common.base.Splitter; |
39 | | -import com.google.common.base.Strings; |
40 | | -import com.google.common.io.BaseEncoding; |
41 | | - |
42 | | -import javaslang.Tuple; |
43 | | -import javaslang.Tuple2; |
44 | | -import javaslang.control.Try; |
| 43 | +import static java.util.Objects.requireNonNull; |
45 | 44 |
|
46 | 45 | /** |
47 | 46 | * Creates a cookie, a small amount of information sent by a server to |
@@ -84,18 +83,27 @@ public interface Cookie { |
84 | 83 | * {@link URLDecoder}. |
85 | 84 | */ |
86 | 85 | public static final Function<String, Map<String, String>> URL_DECODER = value -> { |
| 86 | + if (value == null) { |
| 87 | + return Collections.emptyMap(); |
| 88 | + } |
87 | 89 | Function<String, String> decode = v -> Try |
88 | | - .of(() -> URLDecoder.decode(v, StandardCharsets.UTF_8.name())).get(); |
89 | | - return Splitter.on('&').trimResults().omitEmptyStrings() |
| 90 | + .of(() -> URLDecoder.decode(v, StandardCharsets.UTF_8.name())) |
| 91 | + .get(); |
| 92 | + return Splitter.on('&') |
| 93 | + .trimResults() |
| 94 | + .omitEmptyStrings() |
90 | 95 | .splitToList(value) |
91 | 96 | .stream() |
92 | 97 | .map(v -> { |
93 | 98 | Iterator<String> it = Splitter.on('=').trimResults().omitEmptyStrings() |
94 | 99 | .split(v) |
95 | 100 | .iterator(); |
96 | | - Tuple2<String, String> t2 = Tuple.of(decode.apply(it.next()), decode.apply(it.next())); |
| 101 | + Tuple2<String, String> t2 = Tuple |
| 102 | + .of(decode.apply(it.next()), it.hasNext() ? decode.apply(it.next()) : null); |
97 | 103 | return t2; |
98 | | - }).collect(Collectors.toMap(it -> it._1, it -> it._2)); |
| 104 | + }) |
| 105 | + .filter(it -> Objects.nonNull(it._2)) |
| 106 | + .collect(Collectors.toMap(it -> it._1, it -> it._2)); |
99 | 107 | }; |
100 | 108 |
|
101 | 109 | /** |
@@ -349,7 +357,7 @@ public Optional<Boolean> secure() { |
349 | 357 | * </p> |
350 | 358 | * |
351 | 359 | * @param maxAge an integer specifying the maximum age of the cookie in seconds; if negative, |
352 | | - * means the cookie is not stored; if zero, deletes the cookie. |
| 360 | + * means the cookie is not stored; if zero, deletes the cookie. |
353 | 361 | * @return This definition. |
354 | 362 | */ |
355 | 363 | public Definition maxAge(final int maxAge) { |
@@ -494,7 +502,7 @@ public static boolean valid(final String value, final String secret) { |
494 | 502 | * </p> |
495 | 503 | * |
496 | 504 | * @return An integer specifying the maximum age of the cookie in seconds; if negative, means |
497 | | - * the cookie persists until browser shutdown |
| 505 | + * the cookie persists until browser shutdown |
498 | 506 | */ |
499 | 507 | int maxAge(); |
500 | 508 |
|
|
0 commit comments