15 changed files with 589 additions and 24 deletions
@ -0,0 +1,55 @@ |
|||||||
|
/* |
||||||
|
* 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. |
||||||
|
* 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 java.net.URL; |
||||||
|
|
||||||
|
import org.springframework.core.MethodParameter; |
||||||
|
import org.springframework.lang.Nullable; |
||||||
|
import org.springframework.web.util.UriBuilderFactory; |
||||||
|
import org.springframework.web.util.UriTemplate; |
||||||
|
|
||||||
|
/** |
||||||
|
* An {@link HttpServiceArgumentResolver} that uses the provided |
||||||
|
* {@link UriBuilderFactory} to expand the {@link UriTemplate}. |
||||||
|
* <p>Unlike with the {@link UrlArgumentResolver}, |
||||||
|
* if the {@link UriBuilderFactoryArgumentResolver} is provided, |
||||||
|
* it will not override the entire {@link URL}, but just the {@code baseUri}. |
||||||
|
* <p>This allows for dynamically setting the {@code baseUri}, |
||||||
|
* while keeping the {@code path} specified through class
|
||||||
|
* and method annotations. |
||||||
|
* |
||||||
|
* @author Olga Maciaszek-Sharma |
||||||
|
* @since 6.1 |
||||||
|
*/ |
||||||
|
public class UriBuilderFactoryArgumentResolver implements HttpServiceArgumentResolver { |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean resolve( |
||||||
|
@Nullable Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) { |
||||||
|
|
||||||
|
if (!parameter.getParameterType().equals(UriBuilderFactory.class)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (argument != null) { |
||||||
|
requestValues.setUriBuilderFactory((UriBuilderFactory) argument); |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,72 @@ |
|||||||
|
/* |
||||||
|
* 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. |
||||||
|
* 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.lang.Nullable; |
||||||
|
import org.springframework.web.service.annotation.GetExchange; |
||||||
|
import org.springframework.web.util.DefaultUriBuilderFactory; |
||||||
|
import org.springframework.web.util.UriBuilderFactory; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
/** |
||||||
|
* Unit tests for {@link UriBuilderFactoryArgumentResolver}. |
||||||
|
* |
||||||
|
* @author Olga Maciaszek-Sharma |
||||||
|
*/ |
||||||
|
class UriBuilderFactoryArgumentResolverTests { |
||||||
|
|
||||||
|
private final TestExchangeAdapter client = new TestExchangeAdapter(); |
||||||
|
|
||||||
|
private final Service service = |
||||||
|
HttpServiceProxyFactory.builderFor(this.client).build() |
||||||
|
.createClient(Service.class); |
||||||
|
|
||||||
|
@Test |
||||||
|
void uriBuilderFactory(){ |
||||||
|
UriBuilderFactory factory = new DefaultUriBuilderFactory("https://example.com"); |
||||||
|
this.service.execute(factory); |
||||||
|
|
||||||
|
assertThat(getRequestValues().getUriBuilderFactory()).isEqualTo(factory); |
||||||
|
assertThat(getRequestValues().getUriTemplate()).isEqualTo("/path"); |
||||||
|
assertThat(getRequestValues().getUri()).isNull(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void ignoreNullUriBuilderFactory(){ |
||||||
|
this.service.execute(null); |
||||||
|
|
||||||
|
assertThat(getRequestValues().getUriBuilderFactory()).isEqualTo(null); |
||||||
|
assertThat(getRequestValues().getUriTemplate()).isEqualTo("/path"); |
||||||
|
assertThat(getRequestValues().getUri()).isNull(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private HttpRequestValues getRequestValues() { |
||||||
|
return this.client.getRequestValues(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private interface Service { |
||||||
|
|
||||||
|
@GetExchange("/path") |
||||||
|
void execute(@Nullable UriBuilderFactory uri); |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue