From 5cd5b32b8ba74f27411aeecc4ae95f3963794d86 Mon Sep 17 00:00:00 2001 From: suncairong163 <105478245+suncairong163@users.noreply.github.com> Date: Mon, 10 Apr 2023 07:34:02 +0800 Subject: [PATCH] Feature/3.2 beta7 rest formdata fix (#12040) * Fix conflict * Fix header * fix import * fix import * fix import * fix import * fix import * Fix json * revert some change * Remove jsonfactory * Remove jsonfactory * Simplify rest client * Fix protocol * fix codec * tmp disable test * rest metadata resolver add interface judge * add rest metadata resolve unit test & fix AbstractServiceRestMetadataResolver * org.apache.dubbo.metadata.rest.ParamType null exclude & add default accept header * RestProtocolTest refer add context path unit test * some fix * ADD TODO * RESOLVE HTTP client java.net.SocketException: socket closed * RESOLVE HTTP client java.net.SocketException: socket closed * add spring mvc rest protocol unit test * rest protocol http response code deal * rest protocol http response message * fix some review advice * fix some review advice * add rest metadata resolve unit test & fix AbstractServiceRestMetadataResolver * org.apache.dubbo.metadata.rest.ParamType null exclude & add default accept header * RestProtocolTest refer add context path unit test * some fix * fix some review advice * add spring mvc rest protocol unit test * rest protocol http response code deal * rest protocol http response message * URLConnectionRestClient getMessage * remove unused import * import fix * code merge * remove unused import * code merge * code merge * some fix * code merge * code merge * code merge * code merge * Rest http server * rest protocol do export * code merge * rest protocol provider * rest protocol provider * rest protocol response * rest protocol netty request * Fix conflicts * change restResult InputStream to bytes protect from fd leak * merge code * Fix import * Fix import * Fix uts * Remove unused code * Fix logger * Update okhttp version * Update okhttp version * rest protocol add AnotherUserRestService service ut * rest protocol add String & byteArray codec * Fix version * Fix uts * rest protocol add XMLCodec * stream release * ServiceRestMetadata port change int to Integer * service RestMetadata service map init * xml codec change for xxe * Fix import * code style * code style * Fix shade * change for rest client recreate & destroy bugs * remove recreate double check for code merge * rest client destroy check * add todo * consumer merge code * rest response * BodyProviderParamParser bytes * merge upstream 3.2 to 3.2_consumer_proxy_invocation_handler * merge upstream 3.2 to feature/3.2-rest_protocol_provider * fix rest provider ut * path compare * path compare * fix provider rest ut * fix provider rest ut * fix provider rest ut * testJaxrsPathPattern ut * JAXRSServiceRestMetadataResolverTest ut * JAXRSServiceRestMetadataResolverTest ut * add netty http server * add netty http server response encode * rest netty http server codec * rest netty http server codec * some fix netty http server * add spring mvc ut for netty http server * okhttp_version to 3.14.2 to resolve dependency conflict * Fix license * add rest protocol exception mapper * add TODO ExceptionMapper should be static or instance * add TODO OKHttpRestClient implements of version >=4.0 * fix licence format * fix ut * fix testPathMatcher * url context path * format context path from url * fix illegal logger method invocations: * remove org.jetbrains.annotations.NotNull * add Newly created SPI interface to dubbo-all * some format * merge 3.2 * merge 3.2 * merge 3.2 * Revert "merge 3.2" This reverts commit ad19804437f22d422578572b8f8fbc8b63b68b09. * merge upstream 3.2 * change HttpHandler throws IOException * deal with RPCInvocation build error * request is present * remove request facade unused code * remove thirdpart web server code * remove thirdpart web server code * remove unused imports * remove unused code * rest protocol directly implement AbstractProtocol * code style * change method modifier * form body java bean convert * change header name to RestHeaderEnum * attachment header * http handler error info * rest header distinguish from attachment * some log & double path check * remove RestMethodMetadata ServiceRestMetadata refer * reconstruct http handler * content-type judge * add RestInvoker * setTargetServiceUniqueName * path mapper & http handler * remove unused code * netty thread pool * RestHttpRequestDecoder @ChannelHandler.Sharable * add rest client ut * fix metadata resolver test * fix metadata resolver test * add some description to code * add error test * error message append * add request address * request address * http netty server reconstruct * http netty server TODO add SslServerTlsHandler * replace all pair use * pair fix * add NumberUtils ut * add hex number ut * add no annotation primitive ut * remove unsed code * throw exception * add ut * rename constants & remove unused code * add ut * add ut * add ut * add ut * remove ut * add restClient ut * add restClient ut * add DataParseUtils ut * add path matcher ut * add request template ut * add request template ut * fix xml codec * some fix * Class to Class * media type judge fix and add codec ut * remove print * add header map param ut * add header map param null ut * MediaTypeUtil ut * add param & header & multimap ut * remove unused import * fix error log format * test header param * test header number param * add MediaType ut * add MediaType & metadata resolve ut * imports * add netty request facade ut * some format * add ExceptionMapperTest * add hasExceptionMapper pre judge * fix exception mapper throws * change static ExceptionMapper to instance * resolve Java_Zulu_jdk/17.0.6-10/x64 param is not throwable , exception mapper ut * fix reflects util some detail * remove unused code * add null arg ut * rest invoker response future complete Exceptionally * fix http netty server executor * set rpc context request & response * add rest token attachment * add http method ut * remove unused code * some fix * remove keep-alive header * remove unused code * close channel * add response connection:close head * fix no content-type * fix no content-type form --------- Co-authored-by: Albumen Kevin Co-authored-by: suncr --- .../java/org/apache/dubbo/metadata/rest/ParamType.java | 4 ++++ .../param/parse/consumer/FormConsumerParamParser.java | 9 +++++++++ .../parse/consumer/HeaderConsumerParamParser.java | 2 +- .../parse/consumer/ParameterConsumerParamParser.java | 2 +- .../param/parse/provider/BodyProviderParamParser.java | 2 +- .../rpc/protocol/rest/handler/NettyHttpHandler.java | 10 +++++----- .../protocol/rest/message/HttpMessageCodecManager.java | 4 ++++ .../rpc/protocol/rest/message/codec/JsonCodec.java | 1 + .../rpc/protocol/rest/message/codec/StringCodec.java | 3 +++ .../apache/dubbo/rpc/protocol/rest/DemoService.java | 2 -- .../dubbo/rpc/protocol/rest/DemoServiceImpl.java | 5 ----- 11 files changed, 29 insertions(+), 15 deletions(-) diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ParamType.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ParamType.java index 43902cfd6e..457da7ebd4 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ParamType.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ParamType.java @@ -39,6 +39,10 @@ public enum ParamType { FORM(addSupportTypes(JAXRSClassConstants.FORM_PARAM_ANNOTATION_CLASS, SpringMvcClassConstants.REQUEST_BODY_ANNOTATION_CLASS)), + PROVIDER_BODY(addSupportTypes( + JAXRSClassConstants.REST_EASY_BODY_ANNOTATION_CLASS,JAXRSClassConstants.FORM_PARAM_ANNOTATION_CLASS, + SpringMvcClassConstants.REQUEST_BODY_ANNOTATION_CLASS, BodyTag.class)), + EMPTY(addSupportTypes()); private List annotationClasses; diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/FormConsumerParamParser.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/FormConsumerParamParser.java index 6ecd7c2f16..2b2e04f1ba 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/FormConsumerParamParser.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/FormConsumerParamParser.java @@ -17,13 +17,17 @@ package org.apache.dubbo.rpc.protocol.rest.annotation.param.parse.consumer; import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.metadata.rest.ArgInfo; import org.apache.dubbo.metadata.rest.ParamType; +import org.apache.dubbo.metadata.rest.media.MediaType; import org.apache.dubbo.remoting.http.RequestTemplate; +import org.apache.dubbo.rpc.protocol.rest.constans.RestConstant; import org.apache.dubbo.rpc.protocol.rest.util.DataParseUtils; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -64,6 +68,11 @@ public class FormConsumerParamParser implements BaseConsumerParamParser { requestTemplate.body(tmp, Map.class); } + Collection headers = requestTemplate.getHeaders(RestConstant.CONTENT_TYPE); + if (CollectionUtils.isEmpty(headers)) { + requestTemplate.addHeader(RestConstant.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE.value); + } + } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/HeaderConsumerParamParser.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/HeaderConsumerParamParser.java index dd25756a4d..d99a01164d 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/HeaderConsumerParamParser.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/HeaderConsumerParamParser.java @@ -47,7 +47,7 @@ public class HeaderConsumerParamParser implements BaseConsumerParamParser { } } else { // others - requestTemplate.addHeader(argInfo.getParamName(), headerValue); + requestTemplate.addHeader(argInfo.getAnnotationNameAttribute(), headerValue); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/ParameterConsumerParamParser.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/ParameterConsumerParamParser.java index 23e85ef239..11509a7965 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/ParameterConsumerParamParser.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/consumer/ParameterConsumerParamParser.java @@ -45,7 +45,7 @@ public class ParameterConsumerParamParser implements BaseConsumerParamParser { requestTemplate.addParam(String.valueOf(name), paramValues.get(name)); } } else { - requestTemplate.addParam(argInfo.getParamName(), paramValue); + requestTemplate.addParam(argInfo.getAnnotationNameAttribute(), paramValue); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/provider/BodyProviderParamParser.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/provider/BodyProviderParamParser.java index ad3fadfae9..294de98fcb 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/provider/BodyProviderParamParser.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/provider/BodyProviderParamParser.java @@ -52,6 +52,6 @@ public class BodyProviderParamParser extends ProviderParamParser { @Override protected ParamType getParamType() { - return ParamType.BODY; + return ParamType.PROVIDER_BODY; } } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/handler/NettyHttpHandler.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/handler/NettyHttpHandler.java index 2bf21d8261..a3243685ab 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/handler/NettyHttpHandler.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/handler/NettyHttpHandler.java @@ -68,7 +68,7 @@ public class NettyHttpHandler implements HttpHandler returnType) { String accept = request.getHeader(RestHeaderEnum.ACCEPT.getHeader()); - MediaType mediaType = MediaTypeUtil.convertMediaType(null, accept); + MediaType mediaType = MediaTypeUtil.convertMediaType(returnType, accept); return mediaType; } @@ -176,7 +176,7 @@ public class NettyHttpHandler implements HttpHandler returnType) { try { // media type judge - getAcceptMediaType(requestFacade); + getAcceptMediaType(requestFacade,returnType); } catch (UnSupportContentTypeException e) { // return type judge MediaType mediaType = HttpMessageCodecManager.typeSupport(returnType); diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/HttpMessageCodecManager.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/HttpMessageCodecManager.java index 9393f0969a..27b6ef57e7 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/HttpMessageCodecManager.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/HttpMessageCodecManager.java @@ -31,6 +31,10 @@ public class HttpMessageCodecManager { public static Object httpMessageDecode(byte[] body, Class type, MediaType mediaType) throws Exception { + if (body == null || body.length == 0) { + return null; + } + for (HttpMessageCodec httpMessageCodec : httpMessageCodecs) { if (httpMessageCodec.contentTypeSupport(mediaType, type) || typeJudge(mediaType, type, httpMessageCodec)) { return httpMessageCodec.decode(body, type); diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java index a134ec01c7..f5e82ce49a 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java @@ -39,6 +39,7 @@ public class JsonCodec implements HttpMessageCodec { static { unSupportClasses.add(byte[].class); + unSupportClasses.add(String.class); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/StringCodec.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/StringCodec.java index 6324b3325f..d30b2e5a11 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/StringCodec.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/StringCodec.java @@ -33,6 +33,9 @@ public class StringCodec implements HttpMessageCodec { @Override public Object decode(byte[] body, Class targetType) throws Exception { + if (body == null || body.length == 0) { + return null; + } return new String(body); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java index 980355844e..85e61837d8 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java @@ -51,8 +51,6 @@ public interface DemoService { @POST @Path("number") - @Produces({MediaType.TEXT_PLAIN}) - @Consumes({MediaType.APPLICATION_FORM_URLENCODED}) Long testFormBody(@FormParam("number") Long number); boolean isCalled(); diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java index 7a908d93ad..f5e7c2369a 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java @@ -24,7 +24,6 @@ import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; -import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.HeaderParam; import javax.ws.rs.core.MediaType; @@ -46,10 +45,6 @@ public class DemoServiceImpl implements DemoService { return "Hello, " + name; } - @POST - @Path("number") - @Produces({javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED}) - @Consumes({MediaType.APPLICATION_FORM_URLENCODED}) @Override public Long testFormBody(Long number) { return number;