This is an automated email from the ASF dual-hosted git repository. albumenj pushed a commit to branch 3.2 in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push: new 501a7a7653 resteasy response status code support (#13586) 501a7a7653 is described below commit 501a7a7653bc8db09a46db3541109266e6a1a90b Author: suncairong163 <105478245+suncairong...@users.noreply.github.com> AuthorDate: Wed Dec 27 23:18:28 2023 +0800 resteasy response status code support (#13586) * resteasy response status code support * fix code format --- .../intercept/ResteasyStatusCodeInterceptor.java | 49 ++++++++++++++++++++++ ...pc.protocol.rest.filter.RestResponseInterceptor | 1 + .../rpc/protocol/rest/ResteasyResponseTest.java | 16 +++++++ .../rpc/protocol/rest/rest/RestDemoService.java | 14 ++++++- .../protocol/rest/rest/RestDemoServiceImpl.java | 5 +++ 5 files changed, 84 insertions(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/extension/resteasy/intercept/ResteasyStatusCodeInterceptor.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/extension/resteasy/intercept/ResteasyStatusCodeInterceptor.java new file mode 100644 index 0000000000..ee988194c9 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/extension/resteasy/intercept/ResteasyStatusCodeInterceptor.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.dubbo.rpc.protocol.rest.extension.resteasy.intercept; + +import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.rpc.protocol.rest.extension.resteasy.ResteasyContext; +import org.apache.dubbo.rpc.protocol.rest.filter.RestResponseInterceptor; +import org.apache.dubbo.rpc.protocol.rest.filter.context.RestInterceptContext; + +import org.jboss.resteasy.specimpl.AbstractBuiltResponse; + +@Activate( + value = "resteasy-resStatus", + onClass = { + "javax.ws.rs.ext.WriterInterceptorContext", + "org.jboss.resteasy.specimpl.BuiltResponse", + "org.jboss.resteasy.plugins.server.netty.NettyHttpRequest", + "org.jboss.resteasy.plugins.server.netty.NettyHttpResponse" + }, + order = Integer.MAX_VALUE) +public class ResteasyStatusCodeInterceptor implements RestResponseInterceptor, ResteasyContext { + + @Override + public void intercept(RestInterceptContext restResponseInterceptor) throws Exception { + Object result = restResponseInterceptor.getResult(); + + if (result == null || (!(result instanceof AbstractBuiltResponse))) { + return; + } + + AbstractBuiltResponse abstractBuiltResponse = (AbstractBuiltResponse) result; + + restResponseInterceptor.getResponse().setStatus(abstractBuiltResponse.getStatus()); + } +} diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.filter.RestResponseInterceptor b/dubbo-rpc/dubbo-rpc-rest/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.filter.RestResponseInterceptor index 4637962aef..b72ba51011 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.filter.RestResponseInterceptor +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.rest.filter.RestResponseInterceptor @@ -1,2 +1,3 @@ resteasy=org.apache.dubbo.rpc.protocol.rest.extension.resteasy.intercept.ResteasyWriterInterceptorAdapter invoke=org.apache.dubbo.rpc.protocol.rest.filter.ServiceInvokeRestResponseInterceptor +resteasy-resStatus=org.apache.dubbo.rpc.protocol.rest.extension.resteasy.intercept.ResteasyStatusCodeInterceptor diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ResteasyResponseTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ResteasyResponseTest.java index fceab90fa1..39d2b0202d 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ResteasyResponseTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ResteasyResponseTest.java @@ -67,6 +67,22 @@ public class ResteasyResponseTest { Assertions.assertNotNull(response); } + @Test + void testResponseCustomStatusCode() { + RestDemoService server = new RestDemoServiceImpl(); + URL url = this.registerProvider(exportUrl, server, RestDemoService.class); + + URL nettyUrl = url.addParameter(SERVER_KEY, "netty").addParameter("timeout", 3000000); + + protocol.export(proxy.getInvoker(new RestDemoServiceImpl(), RestDemoService.class, nettyUrl)); + + RestDemoService demoService = this.proxy.getProxy(protocol.refer(RestDemoService.class, nettyUrl)); + + Response response = demoService.deleteUserById("uid"); + + Assertions.assertEquals(response.getStatus(), 200); + } + private URL registerProvider(URL url, Object impl, Class<?> interfaceClass) { ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass); ProviderModel providerModel = new ProviderModel(url.getServiceKey(), impl, serviceDescriptor, null, null); diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java index de00e90184..728ce07884 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java @@ -16,7 +16,15 @@ */ package org.apache.dubbo.rpc.protocol.rest.rest; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -50,4 +58,8 @@ public interface RestDemoService { @DELETE @Path("{uid}") String deleteUserByUid(@PathParam("uid") String uid); + + @DELETE + @Path("/deleteUserById/{uid}") + public Response deleteUserById(@PathParam("uid") String uid); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java index a71f4b2095..63a4734e85 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java @@ -69,6 +69,11 @@ public class RestDemoServiceImpl implements RestDemoService { throw new RuntimeException(); } + @Override + public Response deleteUserById(String uid) { + return Response.status(300).entity("deleted").build(); + } + public static Map<String, Object> getAttachments() { return context; }