Skip to content

[SLS gateway]feat: support protobuf data #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions alibabacloud-gateway-sls/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-gateway-spi</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>credentials-java</artifactId>
<version>0.3.6</version>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
Expand All @@ -64,7 +64,7 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>openapiutil</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
Expand All @@ -84,12 +84,12 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>darabonba-encode-util</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>darabonba-signature-util</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public void modifyRequest(com.aliyun.gateway.spi.models.InterceptorContext conte
} else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "binary")) {
// content-type: application/octet-stream
bodyBytes = com.aliyun.teautil.Common.assertAsBytes(request.body);
} else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "protobuf")) {
bodyBytes = com.aliyun.gateway.sls.util.Client.serializeToPbBytes(request.body);
request.headers.put("content-type", "application/x-protobuf");
}

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

// compress if needed, and set body and hash
if (!com.aliyun.teautil.Common.isUnset(request.body)) {
if (!com.aliyun.teautil.Common.isUnset(bodyBytes)) {
if (!com.aliyun.teautil.Common.empty(finalCompressType)) {
byte[] compressed = com.aliyun.gateway.sls.util.Client.compress(bodyBytes, finalCompressType);
bodyBytes = compressed;
Expand Down Expand Up @@ -199,17 +202,19 @@ public String makeContentHash(byte[] content, String signatureVersion) throws Ex
public void modifyResponse(com.aliyun.gateway.spi.models.InterceptorContext context, com.aliyun.gateway.spi.models.AttributeMap attributeMap) throws Exception {
com.aliyun.gateway.spi.models.InterceptorContext.InterceptorContextRequest request = context.request;
com.aliyun.gateway.spi.models.InterceptorContext.InterceptorContextResponse response = context.response;
if (com.aliyun.teautil.Common.is4xx(response.statusCode) || com.aliyun.teautil.Common.is5xx(response.statusCode)) {
Number statusCode = response.statusCode;
String requestId = response.headers.get("x-log-requestid");
if (com.aliyun.teautil.Common.is4xx(statusCode) || com.aliyun.teautil.Common.is5xx(statusCode)) {
Object error = com.aliyun.teautil.Common.readAsJSON(response.body);
java.util.Map<String, Object> resMap = com.aliyun.teautil.Common.assertAsMap(error);
throw new TeaException(TeaConverter.buildMap(
new TeaPair("code", resMap.get("errorCode")),
new TeaPair("message", resMap.get("errorMessage")),
new TeaPair("accessDeniedDetail", resMap.get("accessDeniedDetail")),
new TeaPair("data", TeaConverter.buildMap(
new TeaPair("httpCode", response.statusCode),
new TeaPair("requestId", response.headers.get("x-log-requestid")),
new TeaPair("statusCode", response.statusCode)
new TeaPair("httpCode", statusCode),
new TeaPair("requestId", requestId),
new TeaPair("statusCode", statusCode)
))
));
}
Expand All @@ -235,6 +240,9 @@ public void modifyResponse(com.aliyun.gateway.spi.models.InterceptorContext cont
response.deserializedBody = obj;
} else if (com.aliyun.teautil.Common.equalString(request.bodyType, "array")) {
response.deserializedBody = com.aliyun.teautil.Common.readAsJSON(uncompressedData);
} else if (com.aliyun.teautil.Common.equalString(request.bodyType, "protobuf")) {
byte[] pbBytes = com.aliyun.teautil.Common.readAsBytes(uncompressedData);
response.deserializedBody = com.aliyun.gateway.sls.util.Client.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
} else {
response.deserializedBody = com.aliyun.teautil.Common.readAsString(uncompressedData);
}
Expand Down
20 changes: 14 additions & 6 deletions alibabacloud-gateway-sls/main.tea
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ async function modifyRequest(context: SPI.InterceptorContext, attributeMap: SPI.
} else if (String.equals(request.reqBodyType, 'binary')) {
// content-type: application/octet-stream
bodyBytes = Util.assertAsBytes(request.body);
} else if (String.equals(request.reqBodyType, 'protobuf')) {
bodyBytes = SLS_Util.serializeToPbBytes(request.body);
request.headers['content-type'] = 'application/x-protobuf';
}
}

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

// compress if needed, and set body and hash
if (!Util.isUnset(request.body)) {
if (!Util.isUnset(bodyBytes)) {
if (!Util.empty(finalCompressType)) {
var compressed = SLS_Util.compress(bodyBytes, finalCompressType);
bodyBytes = compressed;
Expand Down Expand Up @@ -175,17 +178,19 @@ async function makeContentHash(content: bytes, signatureVersion: string) : strin
async function modifyResponse(context: SPI.InterceptorContext, attributeMap: SPI.AttributeMap): void {
var request = context.request;
var response = context.response;
if (Util.is4xx(response.statusCode) || Util.is5xx(response.statusCode)) {
var statusCode = response.statusCode;
var requestId = response.headers['x-log-requestid'];
if (Util.is4xx(statusCode) || Util.is5xx(statusCode)) {
var error = Util.readAsJSON(response.body);
var resMap = Util.assertAsMap(error);
throw {
code = resMap['errorCode'],
message = resMap['errorMessage'],
accessDeniedDetail = resMap['accessDeniedDetail'],
data = {
httpCode = response.statusCode,
requestId = response.headers['x-log-requestid'],
statusCode = response.statusCode,
httpCode = statusCode,
requestId = requestId,
statusCode = statusCode,
}
};
}
Expand All @@ -209,6 +214,9 @@ async function modifyResponse(context: SPI.InterceptorContext, attributeMap: SPI
response.deserializedBody = obj;
} else if (Util.equalString(request.bodyType, 'array')) {
response.deserializedBody = Util.readAsJSON(uncompressedData);
} else if (Util.equalString(request.bodyType, 'protobuf')) {
var pbBytes = Util.readAsBytes(uncompressedData);
response.deserializedBody = SLS_Util.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
} else {
response.deserializedBody = Util.readAsString(uncompressedData);
}
Expand Down
2 changes: 1 addition & 1 deletion alibabacloud-gateway-sls/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@alicloud/tea-typescript": "^1.7.1",
"@alicloud/gateway-spi": "^0.0.8",
"@alicloud/credentials": "^2.3.1",
"@alicloud/credentials": "^2.4.2",
"@alicloud/tea-util": "^1.4.9",
"@alicloud/openapi-util": "^0.3.2",
"@alicloud/darabonba-string": "^1.0.2",
Expand Down
21 changes: 15 additions & 6 deletions alibabacloud-gateway-sls/ts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export default class Client extends SPI {
} else if (String.equals(request.reqBodyType, "binary")) {
// content-type: application/octet-stream
bodyBytes = Util.assertAsBytes(request.body);
} else if (String.equals(request.reqBodyType, "protobuf")) {
bodyBytes = SLS_Util.serializeToPbBytes(request.body);
request.headers["content-type"] = "application/x-protobuf";
}

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

// compress if needed, and set body and hash
if (!Util.isUnset(request.body)) {
if (!Util.isUnset(bodyBytes)) {
if (!Util.empty(finalCompressType)) {
let compressed = await SLS_Util.compress(bodyBytes, finalCompressType);
bodyBytes = compressed;
Expand Down Expand Up @@ -183,6 +186,7 @@ export default class Client extends SPI {

let encodings = this._respBodyDecompressType[action];
if (!Util.isUnset(encodings)) {

for (let c of encodings) {
if (await SLS_Util.isDecompressorAvailable(c)) {
headers["Accept-Encoding"] = c;
Expand All @@ -208,17 +212,19 @@ export default class Client extends SPI {
async modifyResponse(context: $SPI.InterceptorContext, attributeMap: $SPI.AttributeMap): Promise<void> {
let request = context.request;
let response = context.response;
if (Util.is4xx(response.statusCode) || Util.is5xx(response.statusCode)) {
let statusCode = response.statusCode;
let requestId = response.headers["x-log-requestid"];
if (Util.is4xx(statusCode) || Util.is5xx(statusCode)) {
let error = await Util.readAsJSON(response.body);
let resMap = Util.assertAsMap(error);
throw $tea.newError({
code: resMap["errorCode"],
message: resMap["errorMessage"],
accessDeniedDetail: resMap["accessDeniedDetail"],
data: {
httpCode: response.statusCode,
requestId: response.headers["x-log-requestid"],
statusCode: response.statusCode,
httpCode: statusCode,
requestId: requestId,
statusCode: statusCode,
},
});
}
Expand All @@ -244,6 +250,9 @@ export default class Client extends SPI {
response.deserializedBody = obj;
} else if (Util.equalString(request.bodyType, "array")) {
response.deserializedBody = await Util.readAsJSON(uncompressedData);
} else if (Util.equalString(request.bodyType, "protobuf")) {
let pbBytes = await Util.readAsBytes(uncompressedData);
response.deserializedBody = SLS_Util.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
} else {
response.deserializedBody = await Util.readAsString(uncompressedData);
}
Expand Down
5 changes: 5 additions & 0 deletions alibabacloud-gateway-sls/util/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<artifactId>lz4</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.25.4</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.gateway.sls.util;

import com.aliyun.gateway.sls.util.model.ILog;
import com.aliyun.gateway.sls.util.model.LogsResponseBody;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;

public class Client {

Expand Down Expand Up @@ -49,4 +53,32 @@ public static Boolean isDecompressorAvailable(String compressType) throws Except
public static Long bytesLength(byte[] src) throws Exception {
return (long) src.length;
}

public static byte[] serializeToPbBytes(Object request) {
if (request instanceof ILog) {
return ((ILog) request).serializeToPbBytes();
}
return null;
}

public static LogsResponseBody deserializeFromPbBytes(byte[] uncompressedData, Integer statusCode, Map<String, String> headers) {
return new LogsResponseBody(uncompressedData, statusCode, headers);
}

public static int[] decodeVarInt32(byte[] dataBytes, int pos, int maxPos) {
int value[] = {0, 0, 0};
int shift = 0;
int b;
for (int i = pos; i < maxPos; ++i) {
b = dataBytes[i] & 0xff;
value[1] |= (b & 127) << shift;
shift += 7;
if ((b & 128) == 0) {
value[2] = i + 1;
value[0] = 1;
break;
}
}
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.aliyun.gateway.sls.util.exception;

import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaPair;

public class LogException extends TeaException {
public LogException(String code, String message, String requestId, int statusCode) {
super(TeaConverter.buildMap(new TeaPair("code", code),
new TeaPair("message", message),
new TeaPair("data", TeaConverter.buildMap(
new TeaPair("httpCode", statusCode),
new TeaPair("requestId", requestId),
new TeaPair("statusCode", statusCode)))));
}

public LogException(String code, String message, String requestId) {
this(code, message, requestId, -1);
}
}
Loading
Loading