Skip to content

Commit 23dcc45

Browse files
committed
[SLS gateway]feat: support protobuf data
1 parent cf81112 commit 23dcc45

File tree

23 files changed

+5048
-26
lines changed

23 files changed

+5048
-26
lines changed

alibabacloud-gateway-sls/java/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@
4949
<dependency>
5050
<groupId>com.aliyun</groupId>
5151
<artifactId>alibabacloud-gateway-spi</artifactId>
52-
<version>0.0.2</version>
52+
<version>0.0.3</version>
5353
</dependency>
5454
<dependency>
5555
<groupId>com.aliyun</groupId>
5656
<artifactId>credentials-java</artifactId>
57-
<version>0.3.6</version>
57+
<version>1.0.1</version>
5858
</dependency>
5959
<dependency>
6060
<groupId>com.aliyun</groupId>
@@ -64,7 +64,7 @@
6464
<dependency>
6565
<groupId>com.aliyun</groupId>
6666
<artifactId>openapiutil</artifactId>
67-
<version>0.2.1</version>
67+
<version>0.2.2</version>
6868
</dependency>
6969
<dependency>
7070
<groupId>com.aliyun</groupId>
@@ -84,12 +84,12 @@
8484
<dependency>
8585
<groupId>com.aliyun</groupId>
8686
<artifactId>darabonba-encode-util</artifactId>
87-
<version>0.0.2</version>
87+
<version>0.0.3</version>
8888
</dependency>
8989
<dependency>
9090
<groupId>com.aliyun</groupId>
9191
<artifactId>darabonba-signature-util</artifactId>
92-
<version>0.0.4</version>
92+
<version>0.0.5</version>
9393
</dependency>
9494
<dependency>
9595
<groupId>com.aliyun</groupId>

alibabacloud-gateway-sls/java/src/main/java/com/aliyun/gateway/sls/Client.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public void modifyRequest(com.aliyun.gateway.spi.models.InterceptorContext conte
7373
} else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "binary")) {
7474
// content-type: application/octet-stream
7575
bodyBytes = com.aliyun.teautil.Common.assertAsBytes(request.body);
76+
} else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "protobuf")) {
77+
bodyBytes = com.aliyun.gateway.sls.util.Client.serializeToPbBytes(request.body);
78+
request.headers.put("content-type", "application/x-protobuf");
7679
}
7780

7881
}
@@ -83,12 +86,12 @@ public void modifyRequest(com.aliyun.gateway.spi.models.InterceptorContext conte
8386
// for php bug, Argument #1 ($value) could not be passed by reference
8487
if (!com.aliyun.teautil.Common.isUnset(rawSizeRef)) {
8588
bodyRawSize = rawSizeRef;
86-
} else if (!com.aliyun.teautil.Common.isUnset(request.body)) {
89+
} else if (!com.aliyun.teautil.Common.isUnset(bodyBytes)) {
8790
bodyRawSize = "" + com.aliyun.gateway.sls.util.Client.bytesLength(bodyBytes) + "";
8891
}
8992

9093
// compress if needed, and set body and hash
91-
if (!com.aliyun.teautil.Common.isUnset(request.body)) {
94+
if (!com.aliyun.teautil.Common.isUnset(bodyBytes)) {
9295
if (!com.aliyun.teautil.Common.empty(finalCompressType)) {
9396
byte[] compressed = com.aliyun.gateway.sls.util.Client.compress(bodyBytes, finalCompressType);
9497
bodyBytes = compressed;
@@ -199,17 +202,19 @@ public String makeContentHash(byte[] content, String signatureVersion) throws Ex
199202
public void modifyResponse(com.aliyun.gateway.spi.models.InterceptorContext context, com.aliyun.gateway.spi.models.AttributeMap attributeMap) throws Exception {
200203
com.aliyun.gateway.spi.models.InterceptorContext.InterceptorContextRequest request = context.request;
201204
com.aliyun.gateway.spi.models.InterceptorContext.InterceptorContextResponse response = context.response;
202-
if (com.aliyun.teautil.Common.is4xx(response.statusCode) || com.aliyun.teautil.Common.is5xx(response.statusCode)) {
205+
Number statusCode = response.statusCode;
206+
String requestId = response.headers.get("x-log-requestid");
207+
if (com.aliyun.teautil.Common.is4xx(statusCode) || com.aliyun.teautil.Common.is5xx(statusCode)) {
203208
Object error = com.aliyun.teautil.Common.readAsJSON(response.body);
204209
java.util.Map<String, Object> resMap = com.aliyun.teautil.Common.assertAsMap(error);
205210
throw new TeaException(TeaConverter.buildMap(
206211
new TeaPair("code", resMap.get("errorCode")),
207212
new TeaPair("message", resMap.get("errorMessage")),
208213
new TeaPair("accessDeniedDetail", resMap.get("accessDeniedDetail")),
209214
new TeaPair("data", TeaConverter.buildMap(
210-
new TeaPair("httpCode", response.statusCode),
211-
new TeaPair("requestId", response.headers.get("x-log-requestid")),
212-
new TeaPair("statusCode", response.statusCode)
215+
new TeaPair("httpCode", statusCode),
216+
new TeaPair("requestId", requestId),
217+
new TeaPair("statusCode", statusCode)
213218
))
214219
));
215220
}
@@ -235,6 +240,9 @@ public void modifyResponse(com.aliyun.gateway.spi.models.InterceptorContext cont
235240
response.deserializedBody = obj;
236241
} else if (com.aliyun.teautil.Common.equalString(request.bodyType, "array")) {
237242
response.deserializedBody = com.aliyun.teautil.Common.readAsJSON(uncompressedData);
243+
} else if (com.aliyun.teautil.Common.equalString(request.bodyType, "protobuf")) {
244+
byte[] pbBytes = com.aliyun.teautil.Common.readAsBytes(uncompressedData);
245+
response.deserializedBody = com.aliyun.gateway.sls.util.Client.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
238246
} else {
239247
response.deserializedBody = com.aliyun.teautil.Common.readAsString(uncompressedData);
240248
}

alibabacloud-gateway-sls/main.tea

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ async function modifyRequest(context: SPI.InterceptorContext, attributeMap: SPI.
6464
} else if (String.equals(request.reqBodyType, 'binary')) {
6565
// content-type: application/octet-stream
6666
bodyBytes = Util.assertAsBytes(request.body);
67+
} else if (String.equals(request.reqBodyType, 'protobuf')) {
68+
bodyBytes = SLS_Util.serializeToPbBytes(request.body);
69+
request.headers['content-type'] = 'application/x-protobuf';
6770
}
6871
}
6972

@@ -72,12 +75,12 @@ async function modifyRequest(context: SPI.InterceptorContext, attributeMap: SPI.
7275
var rawSizeRef = request.headers['x-log-bodyrawsize']; // for php bug, Argument #1 ($value) could not be passed by reference
7376
if (!Util.isUnset(rawSizeRef)) {
7477
bodyRawSize = rawSizeRef;
75-
} else if (!Util.isUnset(request.body)) {
78+
} else if (!Util.isUnset(bodyBytes)) {
7679
bodyRawSize = `${SLS_Util.bytesLength(bodyBytes)}`;
7780
}
7881

7982
// compress if needed, and set body and hash
80-
if (!Util.isUnset(request.body)) {
83+
if (!Util.isUnset(bodyBytes)) {
8184
if (!Util.empty(finalCompressType)) {
8285
var compressed = SLS_Util.compress(bodyBytes, finalCompressType);
8386
bodyBytes = compressed;
@@ -175,17 +178,19 @@ async function makeContentHash(content: bytes, signatureVersion: string) : strin
175178
async function modifyResponse(context: SPI.InterceptorContext, attributeMap: SPI.AttributeMap): void {
176179
var request = context.request;
177180
var response = context.response;
178-
if (Util.is4xx(response.statusCode) || Util.is5xx(response.statusCode)) {
181+
var statusCode = response.statusCode;
182+
var requestId = response.headers['x-log-requestid'];
183+
if (Util.is4xx(statusCode) || Util.is5xx(statusCode)) {
179184
var error = Util.readAsJSON(response.body);
180185
var resMap = Util.assertAsMap(error);
181186
throw {
182187
code = resMap['errorCode'],
183188
message = resMap['errorMessage'],
184189
accessDeniedDetail = resMap['accessDeniedDetail'],
185190
data = {
186-
httpCode = response.statusCode,
187-
requestId = response.headers['x-log-requestid'],
188-
statusCode = response.statusCode,
191+
httpCode = statusCode,
192+
requestId = requestId,
193+
statusCode = statusCode,
189194
}
190195
};
191196
}
@@ -209,6 +214,9 @@ async function modifyResponse(context: SPI.InterceptorContext, attributeMap: SPI
209214
response.deserializedBody = obj;
210215
} else if (Util.equalString(request.bodyType, 'array')) {
211216
response.deserializedBody = Util.readAsJSON(uncompressedData);
217+
} else if (Util.equalString(request.bodyType, 'protobuf')) {
218+
var pbBytes = Util.readAsBytes(uncompressedData);
219+
response.deserializedBody = SLS_Util.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
212220
} else {
213221
response.deserializedBody = Util.readAsString(uncompressedData);
214222
}

alibabacloud-gateway-sls/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"@alicloud/tea-typescript": "^1.7.1",
2323
"@alicloud/gateway-spi": "^0.0.8",
24-
"@alicloud/credentials": "^2.3.1",
24+
"@alicloud/credentials": "^2.4.2",
2525
"@alicloud/tea-util": "^1.4.9",
2626
"@alicloud/openapi-util": "^0.3.2",
2727
"@alicloud/darabonba-string": "^1.0.2",

alibabacloud-gateway-sls/ts/src/client.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export default class Client extends SPI {
8484
} else if (String.equals(request.reqBodyType, "binary")) {
8585
// content-type: application/octet-stream
8686
bodyBytes = Util.assertAsBytes(request.body);
87+
} else if (String.equals(request.reqBodyType, "protobuf")) {
88+
bodyBytes = SLS_Util.serializeToPbBytes(request.body);
89+
request.headers["content-type"] = "application/x-protobuf";
8790
}
8891

8992
}
@@ -94,12 +97,12 @@ export default class Client extends SPI {
9497
// for php bug, Argument #1 ($value) could not be passed by reference
9598
if (!Util.isUnset(rawSizeRef)) {
9699
bodyRawSize = rawSizeRef;
97-
} else if (!Util.isUnset(request.body)) {
100+
} else if (!Util.isUnset(bodyBytes)) {
98101
bodyRawSize = `${await SLS_Util.bytesLength(bodyBytes)}`;
99102
}
100103

101104
// compress if needed, and set body and hash
102-
if (!Util.isUnset(request.body)) {
105+
if (!Util.isUnset(bodyBytes)) {
103106
if (!Util.empty(finalCompressType)) {
104107
let compressed = await SLS_Util.compress(bodyBytes, finalCompressType);
105108
bodyBytes = compressed;
@@ -183,6 +186,7 @@ export default class Client extends SPI {
183186

184187
let encodings = this._respBodyDecompressType[action];
185188
if (!Util.isUnset(encodings)) {
189+
186190
for (let c of encodings) {
187191
if (await SLS_Util.isDecompressorAvailable(c)) {
188192
headers["Accept-Encoding"] = c;
@@ -208,17 +212,19 @@ export default class Client extends SPI {
208212
async modifyResponse(context: $SPI.InterceptorContext, attributeMap: $SPI.AttributeMap): Promise<void> {
209213
let request = context.request;
210214
let response = context.response;
211-
if (Util.is4xx(response.statusCode) || Util.is5xx(response.statusCode)) {
215+
let statusCode = response.statusCode;
216+
let requestId = response.headers["x-log-requestid"];
217+
if (Util.is4xx(statusCode) || Util.is5xx(statusCode)) {
212218
let error = await Util.readAsJSON(response.body);
213219
let resMap = Util.assertAsMap(error);
214220
throw $tea.newError({
215221
code: resMap["errorCode"],
216222
message: resMap["errorMessage"],
217223
accessDeniedDetail: resMap["accessDeniedDetail"],
218224
data: {
219-
httpCode: response.statusCode,
220-
requestId: response.headers["x-log-requestid"],
221-
statusCode: response.statusCode,
225+
httpCode: statusCode,
226+
requestId: requestId,
227+
statusCode: statusCode,
222228
},
223229
});
224230
}
@@ -244,6 +250,9 @@ export default class Client extends SPI {
244250
response.deserializedBody = obj;
245251
} else if (Util.equalString(request.bodyType, "array")) {
246252
response.deserializedBody = await Util.readAsJSON(uncompressedData);
253+
} else if (Util.equalString(request.bodyType, "protobuf")) {
254+
let pbBytes = await Util.readAsBytes(uncompressedData);
255+
response.deserializedBody = SLS_Util.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
247256
} else {
248257
response.deserializedBody = await Util.readAsString(uncompressedData);
249258
}

alibabacloud-gateway-sls/util/java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<artifactId>lz4</artifactId>
5252
<version>1.3.0</version>
5353
</dependency>
54+
<dependency>
55+
<groupId>com.google.protobuf</groupId>
56+
<artifactId>protobuf-java</artifactId>
57+
<version>3.25.4</version>
58+
</dependency>
5459
<dependency>
5560
<groupId>com.aliyun</groupId>
5661
<artifactId>tea</artifactId>

alibabacloud-gateway-sls/util/java/src/main/java/com/aliyun/gateway/sls/util/Client.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// This file is auto-generated, don't edit it. Thanks.
22
package com.aliyun.gateway.sls.util;
33

4+
import com.aliyun.gateway.sls.util.model.ILog;
5+
import com.aliyun.gateway.sls.util.model.LogsResponseBody;
6+
47
import java.io.ByteArrayInputStream;
58
import java.io.InputStream;
9+
import java.util.Map;
610

711
public class Client {
812

@@ -49,4 +53,32 @@ public static Boolean isDecompressorAvailable(String compressType) throws Except
4953
public static Long bytesLength(byte[] src) throws Exception {
5054
return (long) src.length;
5155
}
56+
57+
public static byte[] serializeToPbBytes(Object request) {
58+
if (request instanceof ILog) {
59+
return ((ILog) request).serializeToPbBytes();
60+
}
61+
return null;
62+
}
63+
64+
public static LogsResponseBody deserializeFromPbBytes(byte[] uncompressedData, Integer statusCode, Map<String, String> headers) {
65+
return new LogsResponseBody(uncompressedData, statusCode, headers);
66+
}
67+
68+
public static int[] decodeVarInt32(byte[] dataBytes, int pos, int maxPos) {
69+
int value[] = {0, 0, 0};
70+
int shift = 0;
71+
int b;
72+
for (int i = pos; i < maxPos; ++i) {
73+
b = dataBytes[i] & 0xff;
74+
value[1] |= (b & 127) << shift;
75+
shift += 7;
76+
if ((b & 128) == 0) {
77+
value[2] = i + 1;
78+
value[0] = 1;
79+
break;
80+
}
81+
}
82+
return value;
83+
}
5284
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.aliyun.gateway.sls.util.exception;
2+
3+
import com.aliyun.tea.TeaConverter;
4+
import com.aliyun.tea.TeaException;
5+
import com.aliyun.tea.TeaPair;
6+
7+
public class LogException extends TeaException {
8+
public LogException(String code, String message, String requestId, int statusCode) {
9+
super(TeaConverter.buildMap(new TeaPair("code", code),
10+
new TeaPair("message", message),
11+
new TeaPair("data", TeaConverter.buildMap(
12+
new TeaPair("httpCode", statusCode),
13+
new TeaPair("requestId", requestId),
14+
new TeaPair("statusCode", statusCode)))));
15+
}
16+
17+
public LogException(String code, String message, String requestId) {
18+
this(code, message, requestId, -1);
19+
}
20+
}

0 commit comments

Comments
 (0)