forked from snowplow/snowplow-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelfDescribingJson.java
More file actions
190 lines (170 loc) · 5.5 KB
/
SelfDescribingJson.java
File metadata and controls
190 lines (170 loc) · 5.5 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2015 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;
// Java
import java.util.LinkedHashMap;
import java.util.Map;
// Google
import com.google.common.base.Preconditions;
// Slf4j
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// This library
import com.snowplowanalytics.snowplow.tracker.Utils;
import com.snowplowanalytics.snowplow.tracker.constants.Parameter;
/**
* Builds a SelfDescribingJson object which can contain two fields:
* - Schema: the JsonSchema path for this Json
* - Data: the data for this Json
*/
public class SelfDescribingJson implements Payload {
private final Logger LOGGER = LoggerFactory.getLogger(SelfDescribingJson.class);
private final LinkedHashMap<String, Object> payload = new LinkedHashMap<>();
/**
* Creates a SelfDescribingJson with only a Schema
* String and an empty data map.
*
* @param schema the schema string
*/
public SelfDescribingJson(String schema) {
this(schema, new LinkedHashMap<String, Object>());
}
/**
* Creates a SelfDescribingJson with a Schema and a
* TrackerPayload object.
*
* @param schema the schema string
* @param data a TrackerPayload object to be embedded as
* the data
*/
public SelfDescribingJson(String schema, TrackerPayload data) {
setSchema(schema);
setData(data);
}
/**
* Creates a SelfDescribingJson with a Schema and a
* SelfDescribingJson object. This can be used to
* nest SDJs inside of each other.
*
* @param schema the schema string
* @param data a SelfDescribingJson object to be embedded as
* the data
*/
public SelfDescribingJson(String schema, SelfDescribingJson data) {
setSchema(schema);
setData(data);
}
/**
* Creates a SelfDescribingJson with a Schema and a
* data object.
*
* @param schema the schema string
* @param data an object to attempt to embed as data
*/
public SelfDescribingJson(String schema, Object data) {
setSchema(schema);
setData(data);
}
/**
* Sets the Schema for the SelfDescribingJson
*
* @param schema a valid schema string
*/
public SelfDescribingJson setSchema(String schema) {
Preconditions.checkNotNull(schema, "schema cannot be null");
Preconditions.checkArgument(!schema.isEmpty(), "schema cannot be empty.");
payload.put(Parameter.SCHEMA, schema);
return this;
}
/**
* Adds data to the SelfDescribingJson
* - Accepts a TrackerPayload object
*
* @param data the data to be added to the SelfDescribingJson
*/
public SelfDescribingJson setData(TrackerPayload data) {
if (data == null) {
return this;
}
payload.put(Parameter.DATA, data.getMap());
return this;
}
/**
* Adds data to the SelfDescribingJson
*
* @param data the data to be added to the SelfDescribingJson
*/
public SelfDescribingJson setData(Object data) {
if (data == null) {
return this;
}
payload.put(Parameter.DATA, data);
return this;
}
/**
* Allows us to add data from one SelfDescribingJson into another
* without copying over the Schema.
*
* @param data the payload to add to the SelfDescribingJson
*/
public SelfDescribingJson setData(SelfDescribingJson data) {
if (payload == null) {
return this;
}
payload.put(Parameter.DATA, data.getMap());
return this;
}
@Deprecated
@Override
public void add(String key, String value) {
LOGGER.info("Payload: add(String, String) method called - Doing nothing.");
}
@Deprecated
@Override
public void addMap(Map<String, String> map) {
LOGGER.info("Payload: addMap(Map<String, Object>) method called - Doing nothing.");
}
@Deprecated
@Override
public void addMap(Map map, boolean base64Encoded, String typeEncoded, String typeNotEncoded) {
LOGGER.info("Payload: addMap(Map, boolean, String, String) method called - Doing nothing.");
}
/**
* Returns the Payload as a Map.
*
* @return A Map of all the key-value entries
*/
@Override
public Map getMap() {
return payload;
}
/**
* Returns the byte size of a payload.
*
* @return A long representing the byte size of the payload.
*/
@Override
public long getByteSize() {
return Utils.getUTF8Length(toString());
}
/**
* Returns the Payload as a string. This is essentially the toString from the ObjectNode used
* to store the Payload.
*
* @return A string value of the Payload.
*/
@Override
public String toString() {
return Utils.mapToJSONString(payload);
}
}