Add RequestAttributeArgumentResolver

Closes gh-28458
This commit is contained in:
rstoyanchev
2022-05-17 14:02:23 +01:00
parent 495507e5d4
commit 496c1dcae1
7 changed files with 199 additions and 20 deletions

View File

@@ -70,6 +70,8 @@ public final class HttpRequestValues {
private final MultiValueMap<String, String> cookies;
private final Map<String, Object> attributes;
@Nullable
private final Object bodyValue;
@@ -82,7 +84,7 @@ public final class HttpRequestValues {
private HttpRequestValues(HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVariables,
HttpHeaders headers, MultiValueMap<String, String> cookies,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue,
@Nullable Publisher<?> body, @Nullable ParameterizedTypeReference<?> bodyElementType) {
@@ -94,6 +96,7 @@ public final class HttpRequestValues {
this.uriVariables = uriVariables;
this.headers = headers;
this.cookies = cookies;
this.attributes = attributes;
this.bodyValue = bodyValue;
this.body = body;
this.bodyElementType = bodyElementType;
@@ -142,12 +145,19 @@ public final class HttpRequestValues {
}
/**
* Return the cookies for the request, if any.
* Return the cookies for the request, or an empty map.
*/
public MultiValueMap<String, String> getCookies() {
return this.cookies;
}
/**
* Return the attributes associated with the request, or an empty map.
*/
public Map<String, Object> getAttributes() {
return this.attributes;
}
/**
* Return the request body as a value to be serialized, if set.
* <p>This is mutually exclusive with {@link #getBody()}.
@@ -209,6 +219,9 @@ public final class HttpRequestValues {
@Nullable
private MultiValueMap<String, String> requestParams;
@Nullable
private Map<String, Object> attributes;
@Nullable
private Object bodyValue;
@@ -325,6 +338,17 @@ public final class HttpRequestValues {
return this;
}
/**
* Configure an attribute to associate with the request.
* @param name the attribute name
* @param value the attribute value
*/
public Builder addAttribute(String name, Object value) {
this.attributes = (this.attributes != null ? this.attributes : new HashMap<>());
this.attributes.put(name, value);
return this;
}
/**
* Set the request body as a concrete value to be serialized.
* <p>This is mutually exclusive with, and resets any previously set
@@ -388,9 +412,12 @@ public final class HttpRequestValues {
MultiValueMap<String, String> cookies = (this.cookies != null ?
new LinkedMultiValueMap<>(this.cookies) : EMPTY_COOKIES_MAP);
Map<String, Object> attributes = (this.attributes != null ?
new HashMap<>(this.attributes) : Collections.emptyMap());
return new HttpRequestValues(
this.httpMethod, uri, uriTemplate, uriVars, headers, cookies, bodyValue,
this.body, this.bodyElementType);
this.httpMethod, uri, uriTemplate, uriVars, headers, cookies, attributes,
bodyValue, this.body, this.bodyElementType);
}
private String appendQueryParams(

View File

@@ -185,14 +185,21 @@ public final class HttpServiceProxyFactory {
}
private List<HttpServiceArgumentResolver> initArgumentResolvers(ConversionService conversionService) {
List<HttpServiceArgumentResolver> resolvers = new ArrayList<>(this.customResolvers);
// Annotation-based
resolvers.add(new RequestHeaderArgumentResolver(conversionService));
resolvers.add(new RequestBodyArgumentResolver(this.reactiveAdapterRegistry));
resolvers.add(new PathVariableArgumentResolver(conversionService));
resolvers.add(new RequestParamArgumentResolver(conversionService));
resolvers.add(new CookieValueArgumentResolver(conversionService));
resolvers.add(new RequestAttributeArgumentResolver());
// Specific type
resolvers.add(new UrlArgumentResolver());
resolvers.add(new HttpMethodArgumentResolver());
return resolvers;
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2022 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.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.RequestAttribute;
/**
* {@link HttpServiceArgumentResolver} for {@link RequestAttribute @RequestAttribute}
* annotated arguments.
*
* <p>The argument may be a single variable value or a {@code Map} with multiple
* variables and values.
*
* <p>If the value is required but {@code null}, {@link IllegalArgumentException}
* is raised. The value is not required if:
* <ul>
* <li>{@link RequestAttribute#required()} is set to {@code false}
* <li>The argument is declared as {@link java.util.Optional}
* </ul>
*
* @author Rossen Stoyanchev
* @since 6.0
*/
public class RequestAttributeArgumentResolver extends AbstractNamedValueArgumentResolver {
@Override
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
RequestAttribute annot = parameter.getParameterAnnotation(RequestAttribute.class);
return (annot == null ? null :
new NamedValueInfo(annot.name(), annot.required(), null, "request attribute", false));
}
@Override
protected void addRequestValue(String name, Object value, HttpRequestValues.Builder requestValues) {
requestValues.addAttribute(name, value);
}
}