This repository was archived by the owner on Jul 10, 2024. It is now read-only.
forked from dstendardi/snowplow-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackerPayload.java
More file actions
125 lines (108 loc) · 4.17 KB
/
Copy pathTrackerPayload.java
File metadata and controls
125 lines (108 loc) · 4.17 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (c) 2014 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.tracker.payload;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.snowplowanalytics.snowplow.tracker.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class TrackerPayload implements Payload {
private final ObjectMapper objectMapper = Util.defaultMapper();
private final Logger LOGGER = LoggerFactory.getLogger(TrackerPayload.class);
private ObjectNode objectNode = objectMapper.createObjectNode();
@Override
public void add(String key, String value) {
if (value == null || value.isEmpty()) {
LOGGER.debug("kv-value is empty. Returning out without adding key..");
return;
}
LOGGER.debug("Adding new key: {} with value: {}", key, value);
objectNode.put(key, value);
}
@Override
public void add(String key, Object value) {
if (value == null) {
LOGGER.debug("kv-value is empty. Returning out without adding key..");
return;
}
LOGGER.debug("Adding new key: {} with object value: {}", key, value);
try {
objectNode.putPOJO(key, objectMapper.writeValueAsString(value));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
@Override
public void addMap(Map<String, Object> map) {
// Return if we don't have a map
if (map == null) {
LOGGER.debug("Map passed in is null. Returning without adding map..");
return;
}
Set<String> keys = map.keySet();
for(String key : keys) {
add(key, map.get(key));
}
}
@Override
public void addMap(Map map, Boolean base64_encoded, String type_encoded, String type_no_encoded) {
// Return if we don't have a map
if (map == null) {
LOGGER.debug("Map passed in is null. Returning nothing..");
return;
}
String mapString;
try {
mapString = objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
e.printStackTrace();
return; // Return because we can't continue
}
if (base64_encoded) { // base64 encoded data
objectNode.put(type_encoded, Util.base64Encode(mapString));
} else { // add it as a child node
add(type_no_encoded, mapString);
}
}
public JsonNode getNode() {
return objectNode;
}
@Override
public Map getMap() {
HashMap<String, String> map = new HashMap<String, String>();
try {
LOGGER.debug("Attempting to create a Map structure from ObjectNode.");
map = objectMapper.readValue(objectNode.toString(), new TypeReference<Map>(){});
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
@Override
public String toString() {
return objectNode.toString();
}
}