diff --git a/sdk/src/main/java/io/dapr/client/DaprClientGrpc.java b/sdk/src/main/java/io/dapr/client/DaprClientGrpc.java index 2434866a78..123a1b093a 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientGrpc.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientGrpc.java @@ -229,12 +229,7 @@ public Mono invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef } private Mono getMono(TypeRef type, byte[] data, Map headers) { - if (type.getType() instanceof ParameterizedType) { - Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments(); - if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) { - type = TypeRef.get(actualTypeArguments[0]); - } - } + type = DaprResponse.getSubType(type); return (Mono) Mono.just(new GrpcDaprResponse(data, headers,objectSerializer, type)); } diff --git a/sdk/src/main/java/io/dapr/client/DaprClientHttp.java b/sdk/src/main/java/io/dapr/client/DaprClientHttp.java index 6346fa8f58..f59282435d 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientHttp.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientHttp.java @@ -227,12 +227,7 @@ public Mono invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef private Mono getMono(TypeRef type, DaprHttp.Response r) { try { if (type.getType().getTypeName().startsWith(DaprResponse.class.getName())) { - if (type.getType() instanceof ParameterizedType) { - Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments(); - if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) { - type = TypeRef.get(actualTypeArguments[0]); - } - } + type = DaprResponse.getSubType(type); return (Mono) Mono.just(new HttpDaprResponse(r, objectSerializer, type)); } T object = objectSerializer.deserialize(r.getBody(), type); diff --git a/sdk/src/main/java/io/dapr/client/domain/response/DaprResponse.java b/sdk/src/main/java/io/dapr/client/domain/response/DaprResponse.java index 61a65c9ff6..b49eb3c970 100644 --- a/sdk/src/main/java/io/dapr/client/domain/response/DaprResponse.java +++ b/sdk/src/main/java/io/dapr/client/domain/response/DaprResponse.java @@ -13,8 +13,14 @@ package io.dapr.client.domain.response; +import io.dapr.utils.TypeRef; + import java.io.IOException; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Arrays; import java.util.Map; +import java.util.Objects; /** * Response. @@ -38,4 +44,43 @@ public interface DaprResponse { * @return response header */ Map getHeaders(); + + /** + * get sub type from type. + * @param type response type + * @param type + * @return sub type + */ + static TypeRef getSubType(TypeRef type) { + TypeRef resultType = type; + if (type.getType() instanceof ParameterizedType) { + Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments(); + if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) { + resultType = TypeRef.get(actualTypeArguments[0]); + } + } else { + resultType = TypeRef.get(byte[].class); + } + return resultType; + } + + /** + * get response bytes. + * @return bytes + */ + byte[] getSourceBytesData(); + + /** + * get repsonse bytes without `34`. + * string that serialized by objectmapper will surrounded by `"`. + * it will be removed when return bytes. + * @return bytes + */ + default byte[] getBytes() { + byte[] data = getSourceBytesData(); + if (data.length > 1 && data[0] == 34) { + data = Arrays.copyOfRange(data, 1, data.length - 1); + } + return data; + } } diff --git a/sdk/src/main/java/io/dapr/client/domain/response/GrpcDaprResponse.java b/sdk/src/main/java/io/dapr/client/domain/response/GrpcDaprResponse.java index 62a41b1619..976ee8f75e 100644 --- a/sdk/src/main/java/io/dapr/client/domain/response/GrpcDaprResponse.java +++ b/sdk/src/main/java/io/dapr/client/domain/response/GrpcDaprResponse.java @@ -54,6 +54,9 @@ public int getCode() { @Override public T getData() throws IOException { + if (type.getType() == byte[].class) { + return (T) getBytes(); + } return serializer.deserialize(data, type); } @@ -61,4 +64,11 @@ public T getData() throws IOException { public Map getHeaders() { return this.headers; } + + @Override + public byte[] getSourceBytesData() { + return data; + } + + } diff --git a/sdk/src/main/java/io/dapr/client/domain/response/HttpDaprResponse.java b/sdk/src/main/java/io/dapr/client/domain/response/HttpDaprResponse.java index ffe37e8f14..d60b4e3ca0 100644 --- a/sdk/src/main/java/io/dapr/client/domain/response/HttpDaprResponse.java +++ b/sdk/src/main/java/io/dapr/client/domain/response/HttpDaprResponse.java @@ -54,6 +54,9 @@ public T getData() throws IOException { if (type.getType() == String.class) { return (T) new String(data); } + if (type.getType() == byte[].class) { + return (T) getBytes(); + } return serializer.deserialize(data, type); } @@ -61,4 +64,9 @@ public T getData() throws IOException { public Map getHeaders() { return response.getHeaders(); } + + @Override + public byte[] getSourceBytesData() { + return response.getBody(); + } } diff --git a/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java b/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java index 8630b2baf5..324c07fa47 100644 --- a/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java @@ -650,6 +650,22 @@ public void invokeServiceTestReturnResponse() throws IOException { assertEquals(expected, res.getData()); } + @Test + public void invokeServiceTestReturnResponseWithBytes() throws IOException { + String expected = "Value"; + doAnswer((Answer) invocation -> { + StreamObserver observer = (StreamObserver) invocation.getArguments()[1]; + observer.onNext(CommonProtos.InvokeResponse.newBuilder().setData(getAny(expected)).build()); + observer.onCompleted(); + return null; + }).when(daprStub).invokeService(any(DaprProtos.InvokeServiceRequest.class), any()); + + Mono result = client.invokeMethod("appId", "method", "request", HttpExtension.NONE, null, new TypeRef() {}); + DaprResponse res = result.block(); + + assertEquals(expected, new String((byte[]) res.getData())); + } + @Test public void invokeServiceObjectTest() throws Exception { MyObject object = new MyObject(1, "Value"); diff --git a/sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java b/sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java index 8e895bba2a..67fb5369cd 100644 --- a/sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java @@ -439,6 +439,28 @@ public void invokeServiceReturnResponse() throws IOException { Assertions.assertEquals(resultHeaderValue,response.getHeaders().get(resultHeaderName)); } + @Test + public void invokeServiceReturnResponseBytes() throws IOException { + String resultString = "request success"; + String resultHeaderName = "test-header"; + String resultHeaderValue = "1"; + mockInterceptor.addRule() + .post("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder") + .respond(resultString) + .addHeader(resultHeaderName,resultHeaderValue); + + InvokeMethodRequest req = new InvokeMethodRequest("41", "neworder") + .setBody("request") + .setHttpExtension(HttpExtension.POST); + + Mono result = daprClientHttp.invokeMethod(req, new TypeRef() {}); + DaprResponse response = result.block(); + Assertions.assertNotNull(response); + Assertions.assertEquals(200, response.getCode()); + Assertions.assertEquals(resultString,new String((byte[]) response.getData())); + Assertions.assertEquals(resultHeaderValue,response.getHeaders().get(resultHeaderName)); + } + @Test public void invokeBinding() throws IOException { String resultString = "request success"; diff --git a/settings.xml b/settings.xml deleted file mode 100644 index cad39afb15..0000000000 --- a/settings.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - ossrh - ${env.OSSRH_USER_TOKEN} - ${env.OSSRH_PWD_TOKEN} - - - - - - true - - - ${env.GPG_KEY} - ${env.GPG_PWD} - - - - \ No newline at end of file