-
Notifications
You must be signed in to change notification settings - Fork 195
/
RsWithCookie.java
168 lines (158 loc) · 5.33 KB
/
RsWithCookie.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.facets.cookies;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.takes.Response;
import org.takes.rs.RsEmpty;
import org.takes.rs.RsWithHeader;
import org.takes.rs.RsWrap;
/**
* Response decorator, with an additional cookie.
*
* The decorator validates cookie name according
* to <a href="http://tools.ietf.org/html/rfc2616#section-2.2">RFC 2616</a>
* and cookie value according
* to <a href="http://tools.ietf.org/html/rfc6265#section-4.1.1">RFC 6265</a>
*
* <p>Use this decorator in order to return a response with a "Set-Cookie"
* header inside, for example:
*
* <pre> return new RsWithCookie(
* new RsText("hello, world!"),
* "u", "Jeff",
* "Path=/", "Expires=Wed, 13 Jan 2021 22:23:01 GMT"
* );</pre>
*
* <p>This response will contain this header:
*
* <pre> Set-Cookie: u=Jeff;Path=/;Expires=Wed, 13 Jan 2021 22:23:01 GMT</pre>
*
* <p>The class is immutable and thread-safe.
*
* @since 0.1
*/
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public final class RsWithCookie extends RsWrap {
/**
* Cookie value validation regexp.
* @checkstyle LineLengthCheck (3 lines)
*/
private static final Pattern CVALUE_PTRN = Pattern.compile(
"[\\x21\\x23-\\x2B\\x2D-\\x3A\\x3C-\\x5B\\x5D-\\x7E]*|\"[\\x21\\x23-\\x2B\\x2D-\\x3A\\x3C-\\x5B\\x5D-\\x7E]*\""
);
/**
* Cookie name validation regexp.
*/
private static final Pattern CNAME_PTRN = Pattern.compile(
"[\\x20-\\x7E&&[^()<>@,;:\\\"/\\[\\]?={} ]]+"
);
/**
* Cookie header name.
*/
private static final CharSequence SET_COOKIE = "Set-Cookie";
/**
* Ctor.
* @param name Cookie name
* @param value Value of it
* @param attrs Optional attributes, for example "Path=/"
*/
public RsWithCookie(final CharSequence name, final CharSequence value,
final CharSequence... attrs) {
this(new RsEmpty(), name, value, attrs);
}
/**
* Ctor.
* @param res Original response
* @param name Cookie name
* @param value Value of it
* @param attrs Optional attributes, for example "Path=/"
* @checkstyle ParameterNumberCheck (10 lines)
*/
public RsWithCookie(final Response res, final CharSequence name,
final CharSequence value, final CharSequence... attrs) {
super(
new RsWithHeader(
res,
RsWithCookie.SET_COOKIE,
RsWithCookie.make(
RsWithCookie.validName(name),
RsWithCookie.validValue(value),
attrs
)
)
);
}
/**
* Build cookie string.
* @param name Cookie name
* @param value Value of it
* @param attrs Optional attributes, for example "Path=/"
* @return Text
*/
private static String make(final CharSequence name,
final CharSequence value, final CharSequence... attrs) {
final StringBuilder text = new StringBuilder(
String.format("%s=%s;", name, value)
);
for (final CharSequence attr : attrs) {
text.append(attr).append(';');
}
return text.toString();
}
/**
* Checks value according RFC 6265 section 4.1.1.
* @param value Cookie value
* @return Cookie value
*/
private static CharSequence validValue(final CharSequence value) {
if (!RsWithCookie.CVALUE_PTRN.matcher(value).matches()) {
throw new IllegalArgumentException(
String.format(
"Cookie value \"%s\" contains invalid characters",
value
)
);
}
return value;
}
/**
* Checks name according RFC 2616, section 2.2.
* @param name Cookie name;
* @return Cookie name
*/
private static CharSequence validName(final CharSequence name) {
if (!RsWithCookie.CNAME_PTRN.matcher(name).matches()) {
throw new IllegalArgumentException(
String.format(
"Cookie name \"%s\" contains invalid characters",
name
)
);
}
return name;
}
}