-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HttpHeaders argument resolver for HTTP service
Signed-off-by: Yanming Zhou <[email protected]>
- Loading branch information
Showing
4 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...eb/src/main/java/org/springframework/web/service/invoker/HttpHeadersArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2002-2025 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.service.invoker; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.jspecify.annotations.Nullable; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* {@link HttpServiceArgumentResolver} that resolves the target | ||
* request's HTTP headers from an {@link HttpHeaders} argument. | ||
* | ||
* @author Yanming Zhou | ||
*/ | ||
public class HttpHeadersArgumentResolver implements HttpServiceArgumentResolver { | ||
|
||
private static final Log logger = LogFactory.getLog(HttpHeadersArgumentResolver.class); | ||
|
||
|
||
@Override | ||
public boolean resolve( | ||
@Nullable Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) { | ||
|
||
parameter = parameter.nestedIfOptional(); | ||
|
||
if (!parameter.getNestedParameterType().equals(HttpHeaders.class)) { | ||
return false; | ||
} | ||
|
||
if (argument instanceof Optional<?> optionalValue) { | ||
argument = optionalValue.orElse(null); | ||
} | ||
|
||
if (argument == null) { | ||
Assert.isTrue(parameter.isOptional(), "HttpHeaders is required"); | ||
return true; | ||
} | ||
|
||
((HttpHeaders) argument).forEach((name, values) -> { | ||
requestValues.addHeader(name, values.toArray(new String[0])); | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...c/test/java/org/springframework/web/service/invoker/HttpHeadersArgumentResolverTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2002-2025 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.service.invoker; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.web.service.annotation.GetExchange; | ||
import org.springframework.web.service.annotation.HttpExchange; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link HttpHeadersArgumentResolver}. | ||
* | ||
* @author Yanming Zhou | ||
*/ | ||
class HttpHeadersArgumentResolverTests { | ||
|
||
private final TestExchangeAdapter client = new TestExchangeAdapter(); | ||
|
||
private final Service service = | ||
HttpServiceProxyFactory.builderFor(this.client).build().createClient(Service.class); | ||
|
||
@Test | ||
void headers() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("foo", "bar"); | ||
headers.add("test", "testValue1"); | ||
headers.add("test", "testValue2"); | ||
this.service.execute(headers); | ||
|
||
HttpHeaders actualHeaders = this.client.getRequestValues().getHeaders(); | ||
assertThat(actualHeaders.get("foo")).containsOnly("bar"); | ||
assertThat(actualHeaders.get("test")).containsExactlyInAnyOrder("testValue1", "testValue2"); | ||
} | ||
|
||
@Test | ||
void doesNotOverrideAnnotationHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("foo", "bar"); | ||
this.service.executeWithAnnotationHeaders(headers); | ||
|
||
HttpHeaders actualHeaders = this.client.getRequestValues().getHeaders(); | ||
assertThat(actualHeaders.get("foo")).containsExactlyInAnyOrder("foo", "bar"); | ||
assertThat(actualHeaders.get("bar")).containsOnly("bar"); | ||
} | ||
|
||
private interface Service { | ||
|
||
@GetExchange | ||
void execute(HttpHeaders headers); | ||
|
||
@HttpExchange(method = "GET", headers = {"foo=foo", "bar=bar"}) | ||
void executeWithAnnotationHeaders(HttpHeaders headers); | ||
|
||
} | ||
|
||
} |