-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudWatchMessageModelSumologicTransformer.java
More file actions
86 lines (71 loc) · 2.69 KB
/
Copy pathCloudWatchMessageModelSumologicTransformer.java
File metadata and controls
86 lines (71 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.sumologic.client;
import java.io.IOException;
import com.amazonaws.services.kinesis.model.Record;
import com.sumologic.client.SimpleKinesisMessageModel;
import com.sumologic.client.implementations.SumologicTransformer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.ByteArrayInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.zip.GZIPInputStream;
/**
* A custom transfomer for {@link SimpleKinesisMessageModel} records in JSON format. The output is in a format
* usable for insertions to Sumologic.
*/
public class CloudWatchMessageModelSumologicTransformer implements
SumologicTransformer<SimpleKinesisMessageModel> {
private static final Log LOG = LogFactory.getLog(CloudWatchMessageModelSumologicTransformer.class);
/**
* Creates a new KinesisMessageModelSumologicTransformer.
*/
public CloudWatchMessageModelSumologicTransformer() {
super();
}
@Override
public String fromClass(SimpleKinesisMessageModel message) {
return message.toString();
}
@Override
public SimpleKinesisMessageModel toClass(Record record) throws IOException {
byte[] decodedRecord = record.getData().array();
String stringifiedRecord = decompressGzip(decodedRecord);
if (stringifiedRecord == null) {
LOG.error("Unable to decompress the record: "+new String(record.getData().array()));
LOG.error("Not attempting to transform into a Message Model");
return null;
}
return new SimpleKinesisMessageModel(stringifiedRecord);
}
public static String decompressGzip(byte[] compressedData) {
try {
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressedData));
BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String outStr = "";
String line;
while ((line=bf.readLine())!=null) {
outStr += line;
}
return outStr;
} catch (IOException exc) {
LOG.warn("Exception during decompression of data: " + exc.getMessage());
return null;
}
}
public static String byteBufferToString(ByteBuffer buffer){
String data = "";
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
try{
int old_position = buffer.position();
data = decoder.decode(buffer).toString();
buffer.position(old_position);
}catch (Exception e){
e.printStackTrace();
return "";
}
return data;
}
}