-
Notifications
You must be signed in to change notification settings - Fork 17
[SLS gateway]feat: support sls pb serialize for java #44
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
// This file is auto-generated, don't edit it. Thanks. | ||
package com.aliyun.gateway.sls.util; | ||
|
||
import com.aliyun.tea.TeaConverter; | ||
import com.aliyun.tea.TeaException; | ||
import com.aliyun.tea.TeaPair; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.net.InetAddress; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class Client { | ||
|
||
|
@@ -13,4 +20,76 @@ public static InputStream readAndUncompressBlock(InputStream stream, String comp | |
String data = new String(rawData, "UTF-8"); | ||
return new ByteArrayInputStream(data.getBytes()); | ||
} | ||
} | ||
|
||
public static byte[] readAndCompressBlock(byte[] stream, String compressType) throws Exception { | ||
byte[] compressedData = CompressorFactory.getCompressor(compressType).compress(stream); | ||
return compressedData; | ||
} | ||
|
||
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()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ArrayList -> List |
||
if (logTags != null && logTags.size() > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !logTags.empty() |
||
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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tagBuilder未最终设入到Logs中 |
||
} | ||
} | ||
ArrayList<Object> logItems = (ArrayList<Object>) body.get("Logs"); | ||
for (Object obj : logItems) { | ||
Logs.Log.Builder logsBuilder = logs.addLogsBuilder(); | ||
HashMap<String, Object> logItem = (HashMap<String, Object>) obj; | ||
logsBuilder.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 = logsBuilder.addContentsBuilder(); | ||
contentBuilder.setKey(realContent.get("Key")); | ||
contentBuilder.setValue(realContent.get("Value")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. contentBuilder未最终设到Logs中 |
||
} | ||
} | ||
logBytes = logs.build().toByteArray(); | ||
|
||
int bodySize = logBytes.length; | ||
if (bodySize > 50 * 1024 * 1024) { | ||
throw new TeaException(TeaConverter.buildMap( | ||
new TeaPair("code", "InvalidLogSize"), | ||
new TeaPair("message", "logItems' size exceeds maximum limitation : " + 50 * 1024 * 1024 + " bytes"))); | ||
} else if (bodySize > 10 * 1024 * 1024) { | ||
throw new TeaException(TeaConverter.buildMap( | ||
new TeaPair("code", "PostBodyTooLarge"), | ||
new TeaPair("message", "body size " + bodySize + " must little than " + 10 * 1024 * 1024))); | ||
} | ||
|
||
return logBytes; | ||
} | ||
|
||
public static String getBytesLength(byte[] stream) throws Exception { | ||
return String.valueOf(stream.length); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.aliyun.gateway.sls.util; | ||
|
||
import com.aliyun.tea.TeaConverter; | ||
import com.aliyun.tea.TeaException; | ||
import com.aliyun.tea.TeaPair; | ||
import net.jpountz.lz4.LZ4Compressor; | ||
import net.jpountz.lz4.LZ4Exception; | ||
import net.jpountz.lz4.LZ4Factory; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.zip.Deflater; | ||
|
||
interface Compressor { | ||
byte[] compress(byte[] data) throws Exception; | ||
} | ||
|
||
class Lz4Compressor implements Compressor { | ||
@Override | ||
public byte[] compress(byte[] data) { | ||
final int rawSize = data.length; | ||
LZ4Factory factory = LZ4Factory.fastestInstance(); | ||
|
||
// compress data | ||
LZ4Compressor compressor = factory.fastCompressor(); | ||
|
||
int maxCompressedLength = compressor.maxCompressedLength(rawSize); | ||
int encodingSize = 0; | ||
byte[] rawCompressed = new byte[maxCompressedLength]; | ||
try { | ||
encodingSize = compressor.compress(data, 0, rawSize, rawCompressed, 0, maxCompressedLength); | ||
} catch (LZ4Exception e) { | ||
throw new TeaException(TeaConverter.buildMap(new TeaPair("CompressException", e.getMessage()))); | ||
} | ||
|
||
if (encodingSize <= 0) { | ||
throw new TeaException(TeaConverter.buildMap(new TeaPair("CompressException", "Invalid encoding size"))); | ||
} | ||
|
||
byte[] ret = new byte[encodingSize]; | ||
System.arraycopy(rawCompressed, 0, ret, 0, encodingSize); | ||
|
||
return ret; | ||
} | ||
} | ||
|
||
class GzipCompressor implements Compressor { | ||
@Override | ||
public byte[] compress(byte[] data) { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(data.length); | ||
Deflater compressor = new Deflater(); | ||
try { | ||
compressor.setInput(data); | ||
compressor.finish(); | ||
byte[] buf = new byte[10240]; | ||
while (!compressor.finished()) { | ||
int count = compressor.deflate(buf); | ||
out.write(buf, 0, count); | ||
} | ||
return out.toByteArray(); | ||
} finally { | ||
compressor.end(); | ||
} | ||
} | ||
} | ||
|
||
public class CompressorFactory { | ||
public static Compressor getCompressor(String compressType) { | ||
if ("lz4".equals(compressType)) { | ||
return new Lz4Compressor(); | ||
} | ||
|
||
throw new IllegalArgumentException("Invalid compressType: " + compressType); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return CompressorFactory.getCompressor(compressType).compress(stream);