Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions alibabacloud-gateway-sls/Teafile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scope": "alibabacloud",
"name": "GatewaySLS",
"version": "0.1.1",
"version": "0.1.2",
"main": "./main.tea",
"maintainers": [
{
Expand Down Expand Up @@ -92,7 +92,8 @@
},
"releaselog": {
"changelog": [
"[go/java]feat: support parsing compressed data."
"[go/java]feat: support parsing compressed data.",
"[java]feat: support pb serialization."
],
"compatible": true
}
Expand Down
2 changes: 1 addition & 1 deletion alibabacloud-gateway-sls/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-util</artifactId>
<version>0.2.16</version>
<version>0.2.20</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public void modifyRequest(com.aliyun.gateway.spi.models.InterceptorContext conte

if (!com.aliyun.teautil.Common.isUnset(request.body)) {
if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "protobuf")) {
// var bodyMap = Util.assertAsMap(request.body);
// 缺少body的Content-MD5计算,以及protobuf处理
byte[] serializedBody = com.aliyun.gateway.sls.util.Client.serializeLogGroupToPB(request.body);
request.headers.put("content-md5", com.aliyun.darabonbastring.Client.toUpper(com.aliyun.darabonba.encode.Encoder.hexEncode(com.aliyun.darabonba.signature.Signer.MD5SignForBytes(serializedBody))));
request.stream = Tea.toReadable(serializedBody);
request.headers.put("content-type", "application/x-protobuf");
} else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "json")) {
String bodyStr = com.aliyun.teautil.Common.toJSONString(request.body);
Expand Down
5 changes: 3 additions & 2 deletions alibabacloud-gateway-sls/main.tea
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ async function modifyRequest(context: SPI.InterceptorContext, attributeMap: SPI.
}
if (!Util.isUnset(request.body)) {
if (String.equals(request.reqBodyType, 'protobuf')) {
// var bodyMap = Util.assertAsMap(request.body);
// 缺少body的Content-MD5计算,以及protobuf处理
var serializedBody = SLS_Util.serializeLogGroupToPB(request.body);
request.headers['content-md5'] = String.toUpper(EncodeUtil.hexEncode(SignatureUtil.MD5SignForBytes(serializedBody)));
request.stream = serializedBody;
request.headers['content-type'] = 'application/x-protobuf';
} else if (String.equals(request.reqBodyType, 'json')) {
var bodyStr = Util.toJSONString(request.body);
Expand Down
2 changes: 1 addition & 1 deletion alibabacloud-gateway-sls/util/Teafile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scope": "alibabacloud",
"name": "GatewaySLS_Util",
"version": "0.0.1",
"version": "0.0.2",
"main": "./main.tea",
"maintainers": [
{
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 @@ -61,6 +61,11 @@
<artifactId>tea-util</artifactId>
<version>0.2.14</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;

public class Client {

Expand All @@ -13,4 +17,53 @@ public static InputStream readAndUncompressBlock(InputStream stream, String comp
String data = new String(rawData, "UTF-8");
return new ByteArrayInputStream(data.getBytes());
}
}

public static byte[] serializeLogGroupToPB(Object logGroup) throws Exception {
byte[] logBytes = null;
Logs.LogGroup.Builder logs = Logs.LogGroup.newBuilder();
HashMap<String, Object> body;
if (logGroup instanceof HashMap) {
body = (HashMap<String, Object>) logGroup;
} else {
throw new IllegalArgumentException("Invalid body type " + logGroup.getClass());
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!(logGroup instanceof HashMap))
{
        throw;
}
Hash xx = logGroup?

Map xx = xxx


String topic = (String) body.get("Topic");
if (topic != null) {
logs.setTopic((String) body.get("Topic"));
} else {
throw new IllegalArgumentException("Topic is null");
}
String source = (String) body.get("Source");
if (source != null) {
logs.setSource((String) body.get("Source"));
} else {
try {
logs.setSource(InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {}
}

ArrayList<Object> logTags = (ArrayList<Object>) body.get("LogTags");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrayList -> List

for (Object obj : logTags) {
HashMap<String, String> tag = (HashMap<String, String>) obj;
Logs.LogTag.Builder tagBuilder = logs.addLogTagsBuilder();
tagBuilder.setKey(tag.get("Key"));
tagBuilder.setValue(tag.get("Value"));
}
ArrayList<Object> logItems = (ArrayList<Object>) body.get("Logs");
for (Object obj : logItems) {
Logs.Log.Builder log = logs.addLogsBuilder();
HashMap<String, Object> logItem = (HashMap<String, Object>) obj;
log.setTime((Integer) logItem.get("Time"));
ArrayList<Object> contents = (ArrayList<Object>) logItem.get("Contents");
for (Object content : contents) {
HashMap<String, String> realContent = (HashMap<String, String>) content;
Logs.Log.Content.Builder contentBuilder = log.addContentsBuilder();
contentBuilder.setKey(realContent.get("Key"));
contentBuilder.setValue(realContent.get("Value"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contentBuilder未最终设到Logs中

}
}
logBytes = logs.build().toByteArray();
return logBytes;
}
}
Loading