Skip to content

Commit 534d1cd

Browse files
hongxue.zousdeleuze
hongxue.zou
authored andcommitted
Polishing
This commit includes a null-safety fix in HttpComponentsHeadersAdapter. Closes gh-30267
1 parent ca545ac commit 534d1cd

File tree

5 files changed

+20
-22
lines changed

5 files changed

+20
-22
lines changed

spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsHeadersAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,7 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
5353

5454

5555
@Override
56+
@Nullable
5657
public String getFirst(String key) {
5758
Header header = this.message.getFirstHeader(key);
5859
return (header != null ? header.getValue() : null);

spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@
4444
*/
4545
class JettyClientHttpResponse implements ClientHttpResponse {
4646

47-
private static final Pattern SAMESITE_PATTERN = Pattern.compile("(?i).*SameSite=(Strict|Lax|None).*");
47+
private static final Pattern SAME_SITE_PATTERN = Pattern.compile("(?i).*SameSite=(Strict|Lax|None).*");
4848

4949

5050
private final ReactiveResponse reactiveResponse;
@@ -90,7 +90,7 @@ public MultiValueMap<String, ResponseCookie> getCookies() {
9090

9191
@Nullable
9292
private static String parseSameSite(String headerValue) {
93-
Matcher matcher = SAMESITE_PATTERN.matcher(headerValue);
93+
Matcher matcher = SAME_SITE_PATTERN.matcher(headerValue);
9494
return (matcher.matches() ? matcher.group(1) : null);
9595
}
9696

spring-web/src/main/java/org/springframework/http/client/reactive/JettyHeadersAdapter.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
4545

4646
private final HttpFields headers;
4747

48+
private static final String IMMUTABLE_HEADER_ERROR = "Immutable headers";
4849

4950
JettyHeadersAdapter(HttpFields headers) {
5051
this.headers = headers;
@@ -59,7 +60,7 @@ public String getFirst(String key) {
5960
@Override
6061
public void add(String key, @Nullable String value) {
6162
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
62-
throw new IllegalStateException("Immutable headers");
63+
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
6364
}
6465
mutableHttpFields.add(key, value);
6566
}
@@ -77,7 +78,7 @@ public void addAll(MultiValueMap<String, String> values) {
7778
@Override
7879
public void set(String key, @Nullable String value) {
7980
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
80-
throw new IllegalStateException("Immutable headers");
81+
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
8182
}
8283
mutableHttpFields.put(key, value);
8384
}
@@ -133,7 +134,7 @@ public List<String> get(Object key) {
133134
@Override
134135
public List<String> put(String key, List<String> value) {
135136
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
136-
throw new IllegalStateException("Immutable headers");
137+
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
137138
}
138139
List<String> oldValues = get(key);
139140
mutableHttpFields.put(key, value);
@@ -144,7 +145,7 @@ public List<String> put(String key, List<String> value) {
144145
@Override
145146
public List<String> remove(Object key) {
146147
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
147-
throw new IllegalStateException("Immutable headers");
148+
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
148149
}
149150
if (key instanceof String name) {
150151
List<String> oldValues = get(key);
@@ -162,7 +163,7 @@ public void putAll(Map<? extends String, ? extends List<String>> map) {
162163
@Override
163164
public void clear() {
164165
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
165-
throw new IllegalStateException("Immutable headers");
166+
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
166167
}
167168
mutableHttpFields.clear();
168169
}
@@ -236,7 +237,7 @@ public List<String> getValue() {
236237
@Override
237238
public List<String> setValue(List<String> value) {
238239
if (!(headers instanceof HttpFields.Mutable mutableHttpFields)) {
239-
throw new IllegalStateException("Immutable headers");
240+
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
240241
}
241242
List<String> previousValues = headers.getValuesList(this.key);
242243
mutableHttpFields.put(this.key, value);
@@ -284,7 +285,7 @@ public String next() {
284285
@Override
285286
public void remove() {
286287
if (!(headers instanceof HttpFields.Mutable mutableHttpFields)) {
287-
throw new IllegalStateException("Immutable headers");
288+
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
288289
}
289290
if (this.currentName == null) {
290291
throw new IllegalStateException("No current Header in iterator");

spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -153,10 +153,8 @@ public MultiValueMap<String, ResponseCookie> getCookies() {
153153

154154
@Nullable
155155
private static String getSameSite(Cookie cookie) {
156-
if (cookie instanceof DefaultCookie defaultCookie) {
157-
if (defaultCookie.sameSite() != null) {
158-
return defaultCookie.sameSite().name();
159-
}
156+
if (cookie instanceof DefaultCookie defaultCookie && defaultCookie.sameSite() != null) {
157+
return defaultCookie.sameSite().name();
160158
}
161159
return null;
162160
}

spring-web/src/main/java/org/springframework/http/client/reactive/ReactorNetty2ClientHttpResponse.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,10 +137,8 @@ public MultiValueMap<String, ResponseCookie> getCookies() {
137137

138138
@Nullable
139139
private static String getSameSite(HttpSetCookie cookie) {
140-
if (cookie instanceof DefaultHttpSetCookie defaultCookie) {
141-
if (defaultCookie.sameSite() != null) {
142-
return defaultCookie.sameSite().name();
143-
}
140+
if (cookie instanceof DefaultHttpSetCookie defaultCookie && defaultCookie.sameSite() != null) {
141+
return defaultCookie.sameSite().name();
144142
}
145143
return null;
146144
}

0 commit comments

Comments
 (0)