-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathGenericJsonSerializingTranscoder.java
More file actions
354 lines (318 loc) · 12.3 KB
/
GenericJsonSerializingTranscoder.java
File metadata and controls
354 lines (318 loc) · 12.3 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package net.spy.memcached.transcoders;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
import java.io.IOException;
import java.util.Date;
import net.spy.memcached.CachedData;
import net.spy.memcached.compat.SpyObject;
import net.spy.memcached.transcoders.compression.CompressionCodecIF;
import net.spy.memcached.transcoders.compression.GZIPCompressionCodec;
import static net.spy.memcached.transcoders.TranscoderUtils.COMPRESSED;
import static net.spy.memcached.transcoders.TranscoderUtils.SERIALIZED;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_BOOLEAN;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_BYTE;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_BYTEARRAY;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_DATE;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_DOUBLE;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_FLOAT;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_INT;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_LONG;
import static net.spy.memcached.transcoders.TranscoderUtils.SPECIAL_MASK;
/**
* Transcoder that serializes and deserializes objects to and from JSON.
*/
public class GenericJsonSerializingTranscoder extends SpyObject implements Transcoder<Object> {
private final ObjectMapper objectMapper;
private final int maxSize;
private final CompressionCodecIF compressionCodec;
private final TranscoderUtils tu;
private final boolean isCollection;
private final boolean forceJsonSerializeForCollection;
@Deprecated
public GenericJsonSerializingTranscoder(ObjectMapper objectMapper, String typeHintPropertyName,
int max) {
this(objectMapper, max);
if (typeHintPropertyName != null) {
@SuppressWarnings("deprecation")
StdTypeResolverBuilder typer = new ObjectMapper.DefaultTypeResolverBuilder(
ObjectMapper.DefaultTyping.EVERYTHING, this.objectMapper.getPolymorphicTypeValidator());
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
typer = typer.typeProperty(typeHintPropertyName);
this.objectMapper.setDefaultTyping(typer);
}
}
@Deprecated
public GenericJsonSerializingTranscoder(ObjectMapper objectMapper, int max) {
this(objectMapper, max, false, false, new GZIPCompressionCodec());
}
/**
* Constructor with full customization.
* Use static factory methods forKV() or forCollection() for default settings,
* or Builder for custom configurations.
*/
private GenericJsonSerializingTranscoder(ObjectMapper objectMapper, int max,
boolean isCollection,
boolean forceJsonSerializeForCollection,
CompressionCodecIF codec) {
if (objectMapper == null) {
throw new IllegalArgumentException("ObjectMapper must not be null");
}
this.objectMapper = objectMapper;
this.maxSize = max;
this.compressionCodec = codec;
this.tu = new TranscoderUtils(true);
this.isCollection = isCollection;
this.forceJsonSerializeForCollection = forceJsonSerializeForCollection;
}
/**
* Factory method for general key-value usage.
*
* @param objectMapper the object mapper to use. This transcoder enables
* polymorphic typing to preserve concrete types.
* Jackson polymorphic deserialization can be vulnerable
* for any untrusted JSON input if default typing is
* too permissive without proper validation.
* It is recommended to configure a restrictive
* {@code BasicPolymorphicTypeValidator}
*/
public static Builder forKV(ObjectMapper objectMapper) {
return new Builder(objectMapper).forKV();
}
/**
* Factory method for collection item usage.
*
* @param objectMapper the object mapper to use. This transcoder enables
* polymorphic typing to preserve concrete types.
* Jackson polymorphic deserialization can be vulnerable
* for any untrusted JSON input if default typing is
* too permissive without proper validation.
* It is recommended to configure a restrictive
* {@code BasicPolymorphicTypeValidator}
*/
public static Builder forCollection(ObjectMapper objectMapper) {
return new Builder(objectMapper).forCollection();
}
@Override
public int getMaxSize() {
return maxSize;
}
@Override
public boolean isForceSerializeForCollection() {
return forceJsonSerializeForCollection;
}
/**
* Set the compression threshold to the given number of bytes. This
* transcoder will attempt to compress any data being stored that's larger
* than this.
*
* @param threshold the number of bytes
*/
public void setCompressionThreshold(int threshold) {
compressionCodec.setCompressionThreshold(threshold);
}
/**
* Set the character set for string value transcoding (defaults to UTF-8).
*/
public void setCharset(String to) {
tu.setCharset(to);
}
public String getCharset() {
return tu.getCharset();
}
@Override
public Object decode(CachedData d) {
byte[] data = d.getData();
if (data == null) {
return null; // No data to decode
}
if ((d.getFlags() & COMPRESSED) != 0) {
data = compressionCodec.decompress(data);
}
Object rv = null;
int flags = d.getFlags() & SPECIAL_MASK;
if ((d.getFlags() & SERIALIZED) != 0 && data != null) {
rv = deserialize(data);
} else if (flags != 0 && data != null) {
switch (flags) {
case SPECIAL_BOOLEAN:
rv = tu.decodeBoolean(data);
break;
case SPECIAL_INT:
rv = tu.decodeInt(data);
break;
case SPECIAL_LONG:
rv = tu.decodeLong(data);
break;
case SPECIAL_DATE:
rv = new Date(tu.decodeLong(data));
break;
case SPECIAL_BYTE:
rv = tu.decodeByte(data);
break;
case SPECIAL_FLOAT:
rv = Float.intBitsToFloat(tu.decodeInt(data));
break;
case SPECIAL_DOUBLE:
rv = Double.longBitsToDouble(tu.decodeLong(data));
break;
case SPECIAL_BYTEARRAY:
rv = data;
break;
default:
getLogger().warn("Unable to decode: Unknown flag %x", flags);
}
} else {
rv = tu.decodeString(data);
}
return rv;
}
@Override
public CachedData encode(Object o) {
if (o == null) {
throw new NullPointerException("Can't encode null");
}
byte[] b;
int flags = 0;
if (isCollection && forceJsonSerializeForCollection) {
b = serialize(o);
flags |= SERIALIZED;
return new CachedData(flags, b, getMaxSize());
}
if (o instanceof String) {
b = tu.encodeString((String) o);
} else if (o instanceof Long) {
b = tu.encodeLong((Long) o);
flags |= SPECIAL_LONG;
} else if (o instanceof Integer) {
b = tu.encodeInt((Integer) o);
flags |= SPECIAL_INT;
} else if (o instanceof Boolean) {
b = tu.encodeBoolean((Boolean) o);
flags |= SPECIAL_BOOLEAN;
} else if (o instanceof Date) {
b = tu.encodeLong(((Date) o).getTime());
flags |= SPECIAL_DATE;
} else if (o instanceof Byte) {
b = tu.encodeByte((Byte) o);
flags |= SPECIAL_BYTE;
} else if (o instanceof Float) {
b = tu.encodeInt(Float.floatToRawIntBits((Float) o));
flags |= SPECIAL_FLOAT;
} else if (o instanceof Double) {
b = tu.encodeLong(Double.doubleToRawLongBits((Double) o));
flags |= SPECIAL_DOUBLE;
} else if (o instanceof byte[]) {
b = (byte[]) o;
flags |= SPECIAL_BYTEARRAY;
} else {
b = serialize(o);
flags |= SERIALIZED;
}
assert b != null;
if (!isCollection && compressionCodec.isCompressionCandidate(b)) {
byte[] compressed = compressionCodec.compress(b);
if (compressed.length < b.length) {
getLogger().debug("Compressed %s from %d to %d",
o.getClass().getName(), b.length, compressed.length);
b = compressed;
flags |= COMPRESSED;
} else {
getLogger().info(
"Compression increased the size of %s from %d to %d",
o.getClass().getName(), b.length, compressed.length);
}
}
return new CachedData(flags, b, getMaxSize());
}
private Object deserialize(byte[] data) {
try {
return objectMapper.readValue(data, Object.class);
} catch (DatabindException e) {
getLogger().warn("Caught DatabindException decoding %d bytes of data",
data == null ? 0 : data.length, e);
} catch (IOException e) {
getLogger().warn("Caught IOException decoding %d bytes of data",
data == null ? 0 : data.length, e);
}
return null;
}
private byte[] serialize(Object o) {
try {
return objectMapper.writeValueAsBytes(o);
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object, cause=" + e.getMessage(), e);
}
}
/**
* Builder for constructing GenericJsonSerializingTranscoder instances with custom settings.
*/
public static final class Builder {
private final ObjectMapper objectMapper;
private String typeHintPropertyName = "";
private int max;
private boolean isCollection;
private boolean forceJsonSerializeForCollection;
private CompressionCodecIF compressionCodec = new GZIPCompressionCodec();
private Builder(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
Builder forKV() {
this.max = CachedData.MAX_SIZE;
this.isCollection = false;
this.forceJsonSerializeForCollection = false;
return this;
}
Builder forCollection() {
this.max = SerializingTranscoder.MAX_COLLECTION_ELEMENT_SIZE;
this.isCollection = true;
this.forceJsonSerializeForCollection = false;
return this;
}
public Builder maxSize(int max) {
this.max = max;
return this;
}
/**
* @param typeHintPropertyName the property name to use for type hints.
* Use "@class" by default without setting this method.
* If {@code null} is given, do not set DefaultTyping
* of given ObjectMapper.
* If empty String is given, set DefaultTyping of ObjectMapper with
* default type property name ("@class").
* Otherwise, set DefaultTyping of ObjectMapper with given String
* to write type info into JSON.
*/
public Builder typeHintPropertyName(String typeHintPropertyName) {
this.typeHintPropertyName = typeHintPropertyName;
return this;
}
public Builder compressionCodec(CompressionCodecIF codec) {
this.compressionCodec = codec;
return this;
}
public Builder forceJsonSerializeForCollection() {
if (!isCollection) {
throw new IllegalStateException("forceJsonSerializationForCollection can only be " +
"used with collection transcoders");
}
this.forceJsonSerializeForCollection = true;
return this;
}
public GenericJsonSerializingTranscoder build() {
if (typeHintPropertyName != null) {
@SuppressWarnings("deprecation")
StdTypeResolverBuilder typer = new ObjectMapper.DefaultTypeResolverBuilder(
ObjectMapper.DefaultTyping.EVERYTHING, objectMapper.getPolymorphicTypeValidator());
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
typer = typer.typeProperty(typeHintPropertyName);
objectMapper.setDefaultTyping(typer);
}
return new GenericJsonSerializingTranscoder(objectMapper, max,
isCollection, forceJsonSerializeForCollection, compressionCodec);
}
}
}