forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeadersRequestCondition.java
196 lines (169 loc) · 5.78 KB
/
HeadersRequestCondition.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.result.condition;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.cors.reactive.CorsUtils;
import org.springframework.web.server.ServerWebExchange;
/**
* A logical conjunction (' && ') request condition that matches a request against
* a set of header expressions with syntax defined in {@link RequestMapping#headers()}.
*
* <p>Expressions passed to the constructor with header names 'Accept' or
* 'Content-Type' are ignored. See {@link ConsumesRequestCondition} and
* {@link ProducesRequestCondition} for those.
*
* @author Rossen Stoyanchev
* @since 5.0
*/
public final class HeadersRequestCondition extends AbstractRequestCondition<HeadersRequestCondition> {
private static final HeadersRequestCondition PRE_FLIGHT_MATCH = new HeadersRequestCondition();
private final Set<HeaderExpression> expressions;
/**
* Create a new instance from the given header expressions. Expressions with
* header names 'Accept' or 'Content-Type' are ignored. See {@link ConsumesRequestCondition}
* and {@link ProducesRequestCondition} for those.
* @param headers media type expressions with syntax defined in {@link RequestMapping#headers()};
* if 0, the condition will match to every request
*/
public HeadersRequestCondition(String... headers) {
this.expressions = parseExpressions(headers);
}
private static Set<HeaderExpression> parseExpressions(String... headers) {
Set<HeaderExpression> result = null;
if (!ObjectUtils.isEmpty(headers)) {
for (String header : headers) {
HeaderExpression expr = new HeaderExpression(header);
if ("Accept".equalsIgnoreCase(expr.name) || "Content-Type".equalsIgnoreCase(expr.name)) {
continue;
}
result = (result != null ? result : new LinkedHashSet<>(headers.length));
result.add(expr);
}
}
return (result != null ? result : Collections.emptySet());
}
private HeadersRequestCondition(Set<HeaderExpression> conditions) {
this.expressions = conditions;
}
/**
* Return the contained request header expressions.
*/
public Set<NameValueExpression<String>> getExpressions() {
return new LinkedHashSet<>(this.expressions);
}
@Override
protected Collection<HeaderExpression> getContent() {
return this.expressions;
}
@Override
protected String getToStringInfix() {
return " && ";
}
/**
* Returns a new instance with the union of the header expressions
* from "this" and the "other" instance.
*/
@Override
public HeadersRequestCondition combine(HeadersRequestCondition other) {
if (isEmpty() && other.isEmpty()) {
return this;
}
else if (other.isEmpty()) {
return this;
}
else if (isEmpty()) {
return other;
}
Set<HeaderExpression> set = new LinkedHashSet<>(this.expressions);
set.addAll(other.expressions);
return new HeadersRequestCondition(set);
}
/**
* Returns "this" instance if the request matches all expressions;
* or {@code null} otherwise.
*/
@Override
@Nullable
public HeadersRequestCondition getMatchingCondition(ServerWebExchange exchange) {
if (CorsUtils.isPreFlightRequest(exchange.getRequest())) {
return PRE_FLIGHT_MATCH;
}
for (HeaderExpression expression : this.expressions) {
if (!expression.match(exchange)) {
return null;
}
}
return this;
}
/**
* Compare to another condition based on header expressions. A condition
* is considered to be a more specific match, if it has:
* <ol>
* <li>A greater number of expressions.
* <li>A greater number of non-negated expressions with a concrete value.
* </ol>
* <p>It is assumed that both instances have been obtained via
* {@link #getMatchingCondition(ServerWebExchange)} and each instance
* contains the matching header expression only or is otherwise empty.
*/
@Override
public int compareTo(HeadersRequestCondition other, ServerWebExchange exchange) {
int result = other.expressions.size() - this.expressions.size();
if (result != 0) {
return result;
}
return (int) (getValueMatchCount(other.expressions) - getValueMatchCount(this.expressions));
}
private long getValueMatchCount(Set<HeaderExpression> expressions) {
long count = 0;
for (HeaderExpression e : expressions) {
if (e.getValue() != null && !e.isNegated()) {
count++;
}
}
return count;
}
/**
* Parses and matches a single header expression to a request.
*/
static class HeaderExpression extends AbstractNameValueExpression<String> {
public HeaderExpression(String expression) {
super(expression);
}
@Override
protected boolean isCaseSensitiveName() {
return false;
}
@Override
protected String parseValue(String valueExpression) {
return valueExpression;
}
@Override
protected boolean matchName(ServerWebExchange exchange) {
return (exchange.getRequest().getHeaders().get(this.name) != null);
}
@Override
protected boolean matchValue(ServerWebExchange exchange) {
return (this.value != null && this.value.equals(exchange.getRequest().getHeaders().getFirst(this.name)));
}
}
}