From de4080a1d279946c1abc43fd2474c5d6259c8192 Mon Sep 17 00:00:00 2001 From: Sean Yang Date: Sat, 26 Oct 2024 10:49:34 +0800 Subject: [PATCH] A couple of rest bugfix --- .../support/jaxrs/HeaderParamArgumentResolver.java | 2 +- .../spring/RequestHeaderArgumentResolver.java | 2 +- .../protocol/tri/h12/HttpContextCallbackFilter.java | 2 +- .../rpc/protocol/tri/rest/RestHttpMessageCodec.java | 12 ++++++++++++ .../tri/rest/mapping/RestRequestHandlerMapping.java | 5 +++++ .../rest/support/basic/ParamArgumentResolver.java | 2 +- 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/dubbo-plugin/dubbo-rest-jaxrs/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/jaxrs/HeaderParamArgumentResolver.java b/dubbo-plugin/dubbo-rest-jaxrs/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/jaxrs/HeaderParamArgumentResolver.java index 8989c0ab062..63a71b94353 100644 --- a/dubbo-plugin/dubbo-rest-jaxrs/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/jaxrs/HeaderParamArgumentResolver.java +++ b/dubbo-plugin/dubbo-rest-jaxrs/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/jaxrs/HeaderParamArgumentResolver.java @@ -43,6 +43,6 @@ protected Object resolveCollectionValue(NamedValueMeta meta, HttpRequest request @Override protected Object resolveMapValue(NamedValueMeta meta, HttpRequest request, HttpResponse response) { - return request.headers(); + return request.headers().asMap(); } } diff --git a/dubbo-plugin/dubbo-rest-spring/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/spring/RequestHeaderArgumentResolver.java b/dubbo-plugin/dubbo-rest-spring/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/spring/RequestHeaderArgumentResolver.java index df554d8eff2..acf7e40723e 100644 --- a/dubbo-plugin/dubbo-rest-spring/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/spring/RequestHeaderArgumentResolver.java +++ b/dubbo-plugin/dubbo-rest-spring/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/spring/RequestHeaderArgumentResolver.java @@ -43,6 +43,6 @@ protected Object resolveCollectionValue(NamedValueMeta meta, HttpRequest request @Override protected Object resolveMapValue(NamedValueMeta meta, HttpRequest request, HttpResponse response) { - return request.headers(); + return request.headers().asMap(); } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/HttpContextCallbackFilter.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/HttpContextCallbackFilter.java index 902da71c49f..fa482948956 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/HttpContextCallbackFilter.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/HttpContextCallbackFilter.java @@ -29,7 +29,7 @@ import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.protocol.tri.TripleConstants; -@Activate(group = CommonConstants.PROVIDER, order = 29000) +@Activate(group = CommonConstants.PROVIDER, order = 10) public class HttpContextCallbackFilter implements Filter, BaseFilter.Listener { @Override diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/RestHttpMessageCodec.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/RestHttpMessageCodec.java index 13cc0bd6159..653794d4b92 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/RestHttpMessageCodec.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/RestHttpMessageCodec.java @@ -26,6 +26,7 @@ import org.apache.dubbo.remoting.http12.message.HttpMessageEncoder; import org.apache.dubbo.remoting.http12.message.MediaType; import org.apache.dubbo.rpc.protocol.tri.rest.argument.ArgumentResolver; +import org.apache.dubbo.rpc.protocol.tri.rest.argument.TypeConverter; import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.ParameterMeta; import java.io.ByteArrayInputStream; @@ -34,6 +35,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; +import java.util.Optional; public final class RestHttpMessageCodec implements HttpMessageDecoder, HttpMessageEncoder { @@ -43,6 +45,7 @@ public final class RestHttpMessageCodec implements HttpMessageDecoder, HttpMessa private final HttpResponse response; private final ParameterMeta[] parameters; private final ArgumentResolver argumentResolver; + private final TypeConverter typeConverter; private final HttpMessageEncoder messageEncoder; private final Charset charset; @@ -51,11 +54,13 @@ public RestHttpMessageCodec( HttpResponse response, ParameterMeta[] parameters, ArgumentResolver argumentResolver, + TypeConverter typeConverter, HttpMessageEncoder messageEncoder) { this.request = request; this.response = response; this.parameters = parameters; this.argumentResolver = argumentResolver; + this.typeConverter = typeConverter; this.messageEncoder = messageEncoder; charset = request.charsetOrDefault(); } @@ -106,6 +111,10 @@ private InputStream decodeInputStream(InputStream is) { public void encode(OutputStream os, Object data) throws EncodeException { if (data != null) { Class type = data.getClass(); + if (type == Optional.class) { + encode(os, ((Optional) data).orElse(null)); + return; + } try { if (type == byte[].class) { os.write((byte[]) data); @@ -121,6 +130,9 @@ public void encode(OutputStream os, Object data) throws EncodeException { } return; } + if (messageEncoder.mediaType().isPureText() && type != String.class) { + data = typeConverter.convert(data, String.class); + } } catch (HttpStatusException e) { throw e; } catch (Exception e) { diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/RestRequestHandlerMapping.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/RestRequestHandlerMapping.java index faf9ed61139..2057d8e3988 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/RestRequestHandlerMapping.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/RestRequestHandlerMapping.java @@ -36,6 +36,8 @@ import org.apache.dubbo.rpc.protocol.tri.rest.RestHttpMessageCodec; import org.apache.dubbo.rpc.protocol.tri.rest.argument.ArgumentResolver; import org.apache.dubbo.rpc.protocol.tri.rest.argument.CompositeArgumentResolver; +import org.apache.dubbo.rpc.protocol.tri.rest.argument.GeneralTypeConverter; +import org.apache.dubbo.rpc.protocol.tri.rest.argument.TypeConverter; import org.apache.dubbo.rpc.protocol.tri.rest.mapping.condition.MethodsCondition; import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.HandlerMeta; import org.apache.dubbo.rpc.protocol.tri.rest.util.RequestUtils; @@ -51,6 +53,7 @@ public final class RestRequestHandlerMapping implements RequestHandlerMapping { private final RequestMappingRegistry requestMappingRegistry; private final ArgumentResolver argumentResolver; + private final TypeConverter typeConverter; private final ContentNegotiator contentNegotiator; private final CodecUtils codecUtils; @@ -58,6 +61,7 @@ public RestRequestHandlerMapping(FrameworkModel frameworkModel) { ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory(); requestMappingRegistry = beanFactory.getOrRegisterBean(DefaultRequestMappingRegistry.class); argumentResolver = beanFactory.getOrRegisterBean(CompositeArgumentResolver.class); + typeConverter = beanFactory.getOrRegisterBean(GeneralTypeConverter.class); contentNegotiator = beanFactory.getOrRegisterBean(ContentNegotiator.class); codecUtils = beanFactory.getOrRegisterBean(CodecUtils.class); } @@ -98,6 +102,7 @@ public RequestHandler getRequestHandler(URL url, HttpRequest request, HttpRespon response, meta.getParameters(), argumentResolver, + typeConverter, codecUtils.determineHttpMessageEncoder(url, responseMediaType)); if (HttpMethods.supportBody(method) && !RequestUtils.isFormOrMultiPart(request)) { diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/basic/ParamArgumentResolver.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/basic/ParamArgumentResolver.java index 14e55e186b2..cbf906408db 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/basic/ParamArgumentResolver.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/support/basic/ParamArgumentResolver.java @@ -151,7 +151,7 @@ protected Object resolveMapValue(NamedValueMeta meta, HttpRequest request, HttpR case Param: return RequestUtils.getParametersMap(request); case Header: - return request.headers(); + return request.headers().asMap(); case Cookie: Collection cookies = request.cookies(); if (cookies.isEmpty()) {