Skip to content

Commit e8f3f4f

Browse files
committed
DaprResponse support bytes[]
1 parent 8bf8a19 commit e8f3f4f

File tree

8 files changed

+103
-38
lines changed

8 files changed

+103
-38
lines changed

sdk/src/main/java/io/dapr/client/DaprClientGrpc.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,7 @@ public <T> Mono<T> invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef
229229
}
230230

231231
private <T> Mono<T> getMono(TypeRef<T> type, byte[] data, Map<String,String> headers) {
232-
if (type.getType() instanceof ParameterizedType) {
233-
Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments();
234-
if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) {
235-
type = TypeRef.get(actualTypeArguments[0]);
236-
}
237-
}
232+
type = DaprResponse.getSubType(type);
238233
return (Mono<T>) Mono.just(new GrpcDaprResponse<T>(data, headers,objectSerializer, type));
239234
}
240235

sdk/src/main/java/io/dapr/client/DaprClientHttp.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,7 @@ public <T> Mono<T> invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef
227227
private <T> Mono<T> getMono(TypeRef<T> type, DaprHttp.Response r) {
228228
try {
229229
if (type.getType().getTypeName().startsWith(DaprResponse.class.getName())) {
230-
if (type.getType() instanceof ParameterizedType) {
231-
Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments();
232-
if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) {
233-
type = TypeRef.get(actualTypeArguments[0]);
234-
}
235-
}
230+
type = DaprResponse.getSubType(type);
236231
return (Mono<T>) Mono.just(new HttpDaprResponse<T>(r, objectSerializer, type));
237232
}
238233
T object = objectSerializer.deserialize(r.getBody(), type);

sdk/src/main/java/io/dapr/client/domain/response/DaprResponse.java

+45
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313

1414
package io.dapr.client.domain.response;
1515

16+
import io.dapr.utils.TypeRef;
17+
1618
import java.io.IOException;
19+
import java.lang.reflect.ParameterizedType;
20+
import java.lang.reflect.Type;
21+
import java.util.Arrays;
1722
import java.util.Map;
23+
import java.util.Objects;
1824

1925
/**
2026
* Response.
@@ -38,4 +44,43 @@ public interface DaprResponse<T> {
3844
* @return response header
3945
*/
4046
Map<String,String> getHeaders();
47+
48+
/**
49+
* get sub type from type.
50+
* @param type response type
51+
* @param <T> type
52+
* @return sub type
53+
*/
54+
static <T> TypeRef getSubType(TypeRef<T> type) {
55+
TypeRef resultType = type;
56+
if (type.getType() instanceof ParameterizedType) {
57+
Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments();
58+
if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) {
59+
resultType = TypeRef.get(actualTypeArguments[0]);
60+
}
61+
} else {
62+
resultType = TypeRef.get(byte[].class);
63+
}
64+
return resultType;
65+
}
66+
67+
/**
68+
* get response bytes.
69+
* @return bytes
70+
*/
71+
byte[] getSourceBytesData();
72+
73+
/**
74+
* get repsonse bytes without `34`.
75+
* string that serialized by objectmapper will surrounded by `"`.
76+
* it will be removed when return bytes.
77+
* @return bytes
78+
*/
79+
default byte[] getBytes() {
80+
byte[] data = getSourceBytesData();
81+
if (data.length > 1 && data[0] == 34) {
82+
data = Arrays.copyOfRange(data, 1, data.length - 1);
83+
}
84+
return data;
85+
}
4186
}

sdk/src/main/java/io/dapr/client/domain/response/GrpcDaprResponse.java

+10
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,21 @@ public int getCode() {
5454

5555
@Override
5656
public T getData() throws IOException {
57+
if (type.getType() == byte[].class) {
58+
return (T) getBytes();
59+
}
5760
return serializer.deserialize(data, type);
5861
}
5962

6063
@Override
6164
public Map<String, String> getHeaders() {
6265
return this.headers;
6366
}
67+
68+
@Override
69+
public byte[] getSourceBytesData() {
70+
return data;
71+
}
72+
73+
6474
}

sdk/src/main/java/io/dapr/client/domain/response/HttpDaprResponse.java

+8
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,19 @@ public T getData() throws IOException {
5454
if (type.getType() == String.class) {
5555
return (T) new String(data);
5656
}
57+
if (type.getType() == byte[].class) {
58+
return (T) getBytes();
59+
}
5760
return serializer.deserialize(data, type);
5861
}
5962

6063
@Override
6164
public Map<String, String> getHeaders() {
6265
return response.getHeaders();
6366
}
67+
68+
@Override
69+
public byte[] getSourceBytesData() {
70+
return response.getBody();
71+
}
6472
}

sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,22 @@ public void invokeServiceTestReturnResponse() throws IOException {
650650
assertEquals(expected, res.getData());
651651
}
652652

653+
@Test
654+
public void invokeServiceTestReturnResponseWithBytes() throws IOException {
655+
String expected = "Value";
656+
doAnswer((Answer<Void>) invocation -> {
657+
StreamObserver<CommonProtos.InvokeResponse> observer = (StreamObserver<CommonProtos.InvokeResponse>) invocation.getArguments()[1];
658+
observer.onNext(CommonProtos.InvokeResponse.newBuilder().setData(getAny(expected)).build());
659+
observer.onCompleted();
660+
return null;
661+
}).when(daprStub).invokeService(any(DaprProtos.InvokeServiceRequest.class), any());
662+
663+
Mono<DaprResponse> result = client.invokeMethod("appId", "method", "request", HttpExtension.NONE, null, new TypeRef<DaprResponse>() {});
664+
DaprResponse res = result.block();
665+
666+
assertEquals(expected, new String((byte[]) res.getData()));
667+
}
668+
653669
@Test
654670
public void invokeServiceObjectTest() throws Exception {
655671
MyObject object = new MyObject(1, "Value");

sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,28 @@ public void invokeServiceReturnResponse() throws IOException {
439439
Assertions.assertEquals(resultHeaderValue,response.getHeaders().get(resultHeaderName));
440440
}
441441

442+
@Test
443+
public void invokeServiceReturnResponseBytes() throws IOException {
444+
String resultString = "request success";
445+
String resultHeaderName = "test-header";
446+
String resultHeaderValue = "1";
447+
mockInterceptor.addRule()
448+
.post("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
449+
.respond(resultString)
450+
.addHeader(resultHeaderName,resultHeaderValue);
451+
452+
InvokeMethodRequest req = new InvokeMethodRequest("41", "neworder")
453+
.setBody("request")
454+
.setHttpExtension(HttpExtension.POST);
455+
456+
Mono<DaprResponse> result = daprClientHttp.invokeMethod(req, new TypeRef<DaprResponse>() {});
457+
DaprResponse response = result.block();
458+
Assertions.assertNotNull(response);
459+
Assertions.assertEquals(200, response.getCode());
460+
Assertions.assertEquals(resultString,new String((byte[]) response.getData()));
461+
Assertions.assertEquals(resultHeaderValue,response.getHeaders().get(resultHeaderName));
462+
}
463+
442464
@Test
443465
public void invokeBinding() throws IOException {
444466
String resultString = "request success";

settings.xml

-26
This file was deleted.

0 commit comments

Comments
 (0)