-
Notifications
You must be signed in to change notification settings - Fork 388
/
JsonSerializer.java
402 lines (349 loc) · 15.3 KB
/
JsonSerializer.java
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jackrabbit.oak.json;
import static org.apache.jackrabbit.guava.common.base.Preconditions.checkNotNull;
import static org.apache.jackrabbit.guava.common.collect.Lists.newArrayList;
import static org.apache.jackrabbit.oak.api.Type.BINARY;
import static org.apache.jackrabbit.oak.api.Type.BOOLEAN;
import static org.apache.jackrabbit.oak.api.Type.DOUBLE;
import static org.apache.jackrabbit.oak.api.Type.LONG;
import static org.apache.jackrabbit.oak.api.Type.NAMES;
import static org.apache.jackrabbit.oak.api.Type.STRING;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import javax.jcr.PropertyType;
import org.apache.jackrabbit.guava.common.collect.ImmutableList;
import org.apache.jackrabbit.guava.common.collect.Lists;
import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.json.JsopTokenizer;
import org.apache.jackrabbit.oak.commons.json.JsopWriter;
import org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState;
import org.apache.jackrabbit.oak.plugins.memory.MemoryChildNodeEntry;
import org.apache.jackrabbit.oak.plugins.memory.StringPropertyState;
import org.apache.jackrabbit.oak.spi.state.AbstractNodeState;
import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class for serializing node and property states to JSON.
*/
public class JsonSerializer {
private static final Logger log = LoggerFactory.getLogger(JsonSerializer.class);
public static final String DEFAULT_FILTER_EXPRESSION =
"{\"properties\":[\"*\", \"-:childNodeCount\"]}";
private static final JsonFilter DEFAULT_FILTER = new JsonFilter(DEFAULT_FILTER_EXPRESSION);
private static final String ERROR_JSON_KEY = "_error";
private static final String ERROR_JSON_VALUE_PREFIX = "ERROR: ";
private final JsopWriter json;
private final int depth;
private final long offset;
private final int maxChildNodes;
private final JsonFilter filter;
private final BlobSerializer blobs;
private final boolean catchExceptions;
private JsonSerializer(
JsopWriter json, int depth, long offset, int maxChildNodes,
JsonFilter filter, BlobSerializer blobs, boolean catchExceptions) {
this.json = checkNotNull(json);
this.depth = depth;
this.offset = offset;
this.maxChildNodes = maxChildNodes;
this.filter = checkNotNull(filter);
this.blobs = checkNotNull(blobs);
this.catchExceptions = catchExceptions;
}
public JsonSerializer(
int depth, long offset, int maxChildNodes,
String filter, BlobSerializer blobs) {
this(new JsopBuilder(), depth, offset, maxChildNodes,
new JsonFilter(filter), blobs, false);
}
public JsonSerializer(JsopWriter json,
int depth, long offset, int maxChildNodes,
String filter, BlobSerializer blobs) {
this(json, depth, offset, maxChildNodes,
new JsonFilter(filter), blobs, false);
}
public JsonSerializer(JsopWriter json,
int depth, long offset, int maxChildNodes,
String filter, BlobSerializer blobs, boolean catchExceptions) {
this(json, depth, offset, maxChildNodes,
new JsonFilter(filter), blobs, catchExceptions);
}
public JsonSerializer(JsopWriter json, BlobSerializer blobs) {
this(json, Integer.MAX_VALUE, 0, Integer.MAX_VALUE,
DEFAULT_FILTER, blobs, false);
}
public JsonSerializer(JsopWriter json, String filter, BlobSerializer blobs) {
this(json, Integer.MAX_VALUE, 0, Integer.MAX_VALUE,
new JsonFilter(filter), blobs, false);
}
protected JsonSerializer getChildSerializer() {
return new JsonSerializer(
json, depth - 1, 0, maxChildNodes, filter, blobs, catchExceptions);
}
public void serialize(NodeState node) {
serialize(node, "");
}
public void serialize(NodeState node, String basePath) {
json.object();
try {
for (PropertyState property : node.getProperties()) {
String name = property.getName();
if (filter.includeProperty(name)) {
json.key(name);
try {
serialize(property);
} catch (Throwable t) {
if (catchExceptions) {
String message = "Cannot read property value " + basePath + "/" + name + " : " + t.getMessage();
log.error(message);
json.value(ERROR_JSON_VALUE_PREFIX + message);
} else {
throw t;
}
}
}
}
int index = 0;
int count = 0;
for (ChildNodeEntry child : getChildNodeEntries(node, basePath)) {
String name = child.getName();
if (filter.includeNode(name) && index++ >= offset) {
if (count++ >= maxChildNodes) {
break;
}
json.key(name);
if (depth > 0) {
getChildSerializer().serialize(child.getNodeState(), basePath + "/" + name);
} else {
json.object();
json.endObject();
}
}
}
} catch (Throwable t) {
if (catchExceptions) {
String message = "Cannot read node " + basePath + " : " + t.getMessage();
log.error(message);
json.key(ERROR_JSON_KEY);
json.value(ERROR_JSON_VALUE_PREFIX + message);
} else {
throw t;
}
}
json.endObject();
}
private Iterable<? extends ChildNodeEntry> getChildNodeEntries(NodeState node, String basePath) {
PropertyState order = node.getProperty(":childOrder");
if (order != null) {
List<String> names = ImmutableList.copyOf(order.getValue(NAMES));
List<ChildNodeEntry> entries = Lists.newArrayListWithCapacity(names.size());
for (String name : names) {
try {
entries.add(new MemoryChildNodeEntry(name, node.getChildNode(name)));
} catch (Throwable t) {
if (catchExceptions) {
String message = "Cannot read node " + basePath + "/" + name + " : " + t.getMessage();
log.error(message);
// return a placeholder child node entry for tracking the error into the JSON
entries.add(new MemoryChildNodeEntry(name, new AbstractNodeState() {
@Override
public boolean exists() {
return true;
}
@NotNull
@Override
public Iterable<? extends PropertyState> getProperties() {
return Collections.singleton(new StringPropertyState(ERROR_JSON_KEY, ERROR_JSON_VALUE_PREFIX + message));
}
@Override
public boolean hasChildNode(@NotNull String name) {
return false;
}
@NotNull
@Override
public NodeState getChildNode(@NotNull String name) throws IllegalArgumentException {
return EmptyNodeState.MISSING_NODE;
}
@NotNull
@Override
public Iterable<? extends ChildNodeEntry> getChildNodeEntries() {
return Collections.EMPTY_LIST;
}
@NotNull
@Override
public NodeBuilder builder() {
return new ReadOnlyBuilder(this);
}
}));
} else {
throw t;
}
}
}
return entries;
}
return node.getChildNodeEntries();
}
public void serialize(PropertyState property) {
Type<?> type = property.getType();
if (!type.isArray()) {
serialize(property, type, 0);
} else {
Type<?> base = type.getBaseType();
int count = property.count();
if (base == STRING || count > 0) {
json.array();
for (int i = 0; i < count; i++) {
serialize(property, base, i);
}
json.endArray();
} else {
// type-safe encoding of an empty array
json.value(TypeCodes.EMPTY_ARRAY
+ PropertyType.nameFromValue(type.tag()));
}
}
}
public void serialize(PropertyState property, Type<?> type, int index) {
if (type == BOOLEAN) {
json.value(property.getValue(BOOLEAN, index));
} else if (type == LONG) {
json.value(property.getValue(LONG, index));
} else if (type == DOUBLE) {
Double value = property.getValue(DOUBLE, index);
if (value.isNaN() || value.isInfinite()) {
json.value(TypeCodes.encode(type.tag(), value.toString()));
} else {
json.encodedValue(value.toString());
}
} else if (type == BINARY) {
Blob blob = property.getValue(BINARY, index);
json.value(TypeCodes.encode(type.tag(), blobs.serialize(blob)));
} else {
String value = property.getValue(STRING, index);
if (type != STRING || TypeCodes.split(value) != -1) {
value = TypeCodes.encode(type.tag(), value);
}
json.value(value);
}
}
public String toString() {
return json.toString();
}
/**
* Utility class for deciding which nodes and properties to serialize.
*/
private static class JsonFilter {
private static final Pattern EVERYTHING = Pattern.compile(".*");
private final List<Pattern> nodeIncludes = newArrayList(EVERYTHING);
private final List<Pattern> nodeExcludes = newArrayList();
private final List<Pattern> propertyIncludes = newArrayList(EVERYTHING);
private final List<Pattern> propertyExcludes = newArrayList();
JsonFilter(String filter) {
JsopTokenizer tokenizer = new JsopTokenizer(filter);
tokenizer.read('{');
for (boolean first = true; !tokenizer.matches('}'); first = false) {
if (!first) {
tokenizer.read(',');
}
String key = tokenizer.readString();
tokenizer.read(':');
List<Pattern> includes = newArrayList();
List<Pattern> excludes = newArrayList();
readPatterns(tokenizer, includes, excludes);
if (key.equals("nodes")) {
nodeIncludes.clear();
nodeIncludes.addAll(includes);
nodeExcludes.clear();
nodeExcludes.addAll(excludes);
} else if (key.equals("properties")) {
propertyIncludes.clear();
propertyIncludes.addAll(includes);
propertyExcludes.clear();
propertyExcludes.addAll(excludes);
} else {
throw new IllegalStateException(key);
}
}
}
private static void readPatterns(JsopTokenizer tokenizer, List<Pattern> includes,
List<Pattern> excludes) {
tokenizer.read('[');
for (boolean first = true; !tokenizer.matches(']'); first = false) {
if (!first) {
tokenizer.read(',');
}
String pattern = tokenizer.readString();
if (pattern.startsWith("-")) {
excludes.add(glob(pattern.substring(1)));
} else if (pattern.startsWith("\\-")) {
includes.add(glob(pattern.substring(1)));
} else {
includes.add(glob(pattern));
}
}
}
private static Pattern glob(String pattern) {
StringBuilder builder = new StringBuilder();
int star = pattern.indexOf('*');
while (star != -1) {
if (star > 0 && pattern.charAt(star - 1) == '\\') {
builder.append(Pattern.quote(pattern.substring(0, star - 1)));
builder.append(Pattern.quote("*"));
} else {
builder.append(Pattern.quote(pattern.substring(0, star)));
builder.append(".*");
}
pattern = pattern.substring(star + 1);
star = pattern.indexOf('*');
}
builder.append(Pattern.quote(pattern));
return Pattern.compile(builder.toString());
}
boolean includeNode(String name) {
return include(name, nodeIncludes, nodeExcludes);
}
boolean includeProperty(String name) {
return include(name, propertyIncludes, propertyExcludes);
}
private static boolean include(
String name, List<Pattern> includes, List<Pattern> excludes) {
for (Pattern include : includes) {
if (include.matcher(name).matches()) {
for (Pattern exclude : excludes) {
if (exclude.matcher(name).matches()) {
return false;
}
}
return true;
}
}
return false;
}
}
}