From 534d1cd35bdf3e16f1045e8237883af7d78cc9c6 Mon Sep 17 00:00:00 2001 From: "hongxue.zou" Date: Wed, 29 Mar 2023 18:25:49 +0800 Subject: [PATCH] Polishing This commit includes a null-safety fix in HttpComponentsHeadersAdapter. Closes gh-30267 --- .../reactive/HttpComponentsHeadersAdapter.java | 3 ++- .../reactive/JettyClientHttpResponse.java | 6 +++--- .../client/reactive/JettyHeadersAdapter.java | 17 +++++++++-------- .../reactive/ReactorClientHttpResponse.java | 8 +++----- .../ReactorNetty2ClientHttpResponse.java | 8 +++----- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsHeadersAdapter.java b/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsHeadersAdapter.java index 2b6a855dc3..261e97e846 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsHeadersAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsHeadersAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -53,6 +53,7 @@ class HttpComponentsHeadersAdapter implements MultiValueMap { @Override + @Nullable public String getFirst(String key) { Header header = this.message.getFirstHeader(key); return (header != null ? header.getValue() : null); diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java index 675267a1b3..7d0e4e7319 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -44,7 +44,7 @@ import org.springframework.util.MultiValueMap; */ class JettyClientHttpResponse implements ClientHttpResponse { - private static final Pattern SAMESITE_PATTERN = Pattern.compile("(?i).*SameSite=(Strict|Lax|None).*"); + private static final Pattern SAME_SITE_PATTERN = Pattern.compile("(?i).*SameSite=(Strict|Lax|None).*"); private final ReactiveResponse reactiveResponse; @@ -90,7 +90,7 @@ class JettyClientHttpResponse implements ClientHttpResponse { @Nullable private static String parseSameSite(String headerValue) { - Matcher matcher = SAMESITE_PATTERN.matcher(headerValue); + Matcher matcher = SAME_SITE_PATTERN.matcher(headerValue); return (matcher.matches() ? matcher.group(1) : null); } diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/JettyHeadersAdapter.java b/spring-web/src/main/java/org/springframework/http/client/reactive/JettyHeadersAdapter.java index e874cac13d..f63122d337 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/JettyHeadersAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/JettyHeadersAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -45,6 +45,7 @@ class JettyHeadersAdapter implements MultiValueMap { private final HttpFields headers; + private static final String IMMUTABLE_HEADER_ERROR = "Immutable headers"; JettyHeadersAdapter(HttpFields headers) { this.headers = headers; @@ -59,7 +60,7 @@ class JettyHeadersAdapter implements MultiValueMap { @Override public void add(String key, @Nullable String value) { if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) { - throw new IllegalStateException("Immutable headers"); + throw new IllegalStateException(IMMUTABLE_HEADER_ERROR); } mutableHttpFields.add(key, value); } @@ -77,7 +78,7 @@ class JettyHeadersAdapter implements MultiValueMap { @Override public void set(String key, @Nullable String value) { if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) { - throw new IllegalStateException("Immutable headers"); + throw new IllegalStateException(IMMUTABLE_HEADER_ERROR); } mutableHttpFields.put(key, value); } @@ -133,7 +134,7 @@ class JettyHeadersAdapter implements MultiValueMap { @Override public List put(String key, List value) { if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) { - throw new IllegalStateException("Immutable headers"); + throw new IllegalStateException(IMMUTABLE_HEADER_ERROR); } List oldValues = get(key); mutableHttpFields.put(key, value); @@ -144,7 +145,7 @@ class JettyHeadersAdapter implements MultiValueMap { @Override public List remove(Object key) { if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) { - throw new IllegalStateException("Immutable headers"); + throw new IllegalStateException(IMMUTABLE_HEADER_ERROR); } if (key instanceof String name) { List oldValues = get(key); @@ -162,7 +163,7 @@ class JettyHeadersAdapter implements MultiValueMap { @Override public void clear() { if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) { - throw new IllegalStateException("Immutable headers"); + throw new IllegalStateException(IMMUTABLE_HEADER_ERROR); } mutableHttpFields.clear(); } @@ -236,7 +237,7 @@ class JettyHeadersAdapter implements MultiValueMap { @Override public List setValue(List value) { if (!(headers instanceof HttpFields.Mutable mutableHttpFields)) { - throw new IllegalStateException("Immutable headers"); + throw new IllegalStateException(IMMUTABLE_HEADER_ERROR); } List previousValues = headers.getValuesList(this.key); mutableHttpFields.put(this.key, value); @@ -284,7 +285,7 @@ class JettyHeadersAdapter implements MultiValueMap { @Override public void remove() { if (!(headers instanceof HttpFields.Mutable mutableHttpFields)) { - throw new IllegalStateException("Immutable headers"); + throw new IllegalStateException(IMMUTABLE_HEADER_ERROR); } if (this.currentName == null) { throw new IllegalStateException("No current Header in iterator"); diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java index 35b15f7345..0b1d4b67c0 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -153,10 +153,8 @@ class ReactorClientHttpResponse implements ClientHttpResponse { @Nullable private static String getSameSite(Cookie cookie) { - if (cookie instanceof DefaultCookie defaultCookie) { - if (defaultCookie.sameSite() != null) { - return defaultCookie.sameSite().name(); - } + if (cookie instanceof DefaultCookie defaultCookie && defaultCookie.sameSite() != null) { + return defaultCookie.sameSite().name(); } return null; } diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorNetty2ClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorNetty2ClientHttpResponse.java index ba6cdba491..597f4809c8 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorNetty2ClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorNetty2ClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -137,10 +137,8 @@ class ReactorNetty2ClientHttpResponse implements ClientHttpResponse { @Nullable private static String getSameSite(HttpSetCookie cookie) { - if (cookie instanceof DefaultHttpSetCookie defaultCookie) { - if (defaultCookie.sameSite() != null) { - return defaultCookie.sameSite().name(); - } + if (cookie instanceof DefaultHttpSetCookie defaultCookie && defaultCookie.sameSite() != null) { + return defaultCookie.sameSite().name(); } return null; }