Expose HttpExchange metadata to argument resolvers

See gh-33220
This commit is contained in:
rstoyanchev 2024-07-16 16:59:22 +01:00
parent 0bfc8e27cc
commit dcabddddc2
2 changed files with 77 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@ -77,7 +77,7 @@ public abstract class AbstractNamedValueArgumentResolver implements HttpServiceA
public boolean resolve(
@Nullable Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
NamedValueInfo info = getNamedValueInfo(parameter);
NamedValueInfo info = getNamedValueInfo(parameter, requestValues);
if (info == null) {
return false;
}
@ -102,10 +102,10 @@ public abstract class AbstractNamedValueArgumentResolver implements HttpServiceA
}
@Nullable
private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
private NamedValueInfo getNamedValueInfo(MethodParameter parameter, HttpRequestValues.Metadata requestValues) {
NamedValueInfo info = this.namedValueInfoCache.get(parameter);
if (info == null) {
info = createNamedValueInfo(parameter);
info = createNamedValueInfo(parameter, requestValues);
if (info == null) {
return null;
}
@ -122,6 +122,18 @@ public abstract class AbstractNamedValueArgumentResolver implements HttpServiceA
@Nullable
protected abstract NamedValueInfo createNamedValueInfo(MethodParameter parameter);
/**
* Variant of {@link #createNamedValueInfo(MethodParameter)} that also provides
* access to the static values set from {@code @HttpExchange} attributes.
* @since 6.2
*/
@Nullable
protected NamedValueInfo createNamedValueInfo(
MethodParameter parameter, HttpRequestValues.Metadata requestValues) {
return createNamedValueInfo(parameter);
}
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
String name = info.name;
if (info.name.isEmpty()) {

View File

@ -183,10 +183,42 @@ public class HttpRequestValues {
}
/**
* Expose static metadata from {@code @HttpExchange} annotation attributes.
* @since 6.2
*/
public interface Metadata {
/**
* Return the HTTP method, if known.
*/
@Nullable
HttpMethod getHttpMethod();
/**
* Return the URI template, if set already.
*/
@Nullable
String getUriTemplate();
/**
* Return the content type, if set already.
*/
@Nullable
MediaType getContentType();
/**
* Return the acceptable media types, if set already.
*/
@Nullable
List<MediaType> getAcceptMediaTypes();
}
/**
* Builder for {@link HttpRequestValues}.
*/
public static class Builder {
public static class Builder implements Metadata {
@Nullable
private HttpMethod httpMethod;
@ -357,6 +389,34 @@ public class HttpRequestValues {
this.bodyValue = bodyValue;
}
// Implementation of {@link Metadata} methods
@Override
@Nullable
public HttpMethod getHttpMethod() {
return this.httpMethod;
}
@Override
@Nullable
public String getUriTemplate() {
return this.uriTemplate;
}
@Override
@Nullable
public MediaType getContentType() {
return (this.headers != null ? this.headers.getContentType() : null);
}
@Override
@Nullable
public List<MediaType> getAcceptMediaTypes() {
return (this.headers != null ? this.headers.getAccept() : null);
}
/**
* Build the {@link HttpRequestValues} instance.
*/