13
13
14
14
package io .dapr .client .domain .response ;
15
15
16
+ import io .dapr .utils .TypeRef ;
17
+
16
18
import java .io .IOException ;
19
+ import java .lang .reflect .ParameterizedType ;
20
+ import java .lang .reflect .Type ;
21
+ import java .util .Arrays ;
17
22
import java .util .Map ;
23
+ import java .util .Objects ;
18
24
19
25
/**
20
26
* Response.
@@ -38,4 +44,43 @@ public interface DaprResponse<T> {
38
44
* @return response header
39
45
*/
40
46
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
+ }
41
86
}
0 commit comments