-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
SnakeYAMLDataFormat.java
475 lines (394 loc) · 15.4 KB
/
SnakeYAMLDataFormat.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* 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.camel.component.snakeyaml;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.Exchange;
import org.apache.camel.component.snakeyaml.custom.CustomClassLoaderConstructor;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.spi.DataFormatName;
import org.apache.camel.spi.annotations.Dataformat;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.BaseConstructor;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.inspector.TrustedTagInspector;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;
/**
* Marshal and unmarshal Java objects to and from YAML using <a href="http://www.snakeyaml.org">SnakeYAML</a>
*/
@Dataformat("snakeYaml")
public final class SnakeYAMLDataFormat extends ServiceSupport implements DataFormat, DataFormatName, CamelContextAware {
private CamelContext camelContext;
private final ThreadLocal<WeakReference<Yaml>> yamlCache;
private Function<CamelContext, BaseConstructor> constructor;
private Function<CamelContext, Representer> representer;
private Function<CamelContext, DumperOptions> dumperOptions;
private Function<CamelContext, Resolver> resolver;
private ClassLoader classLoader;
private String unmarshalTypeName;
private Class<?> unmarshalType;
private List<TypeDescription> typeDescriptions;
private ConcurrentMap<Class<?>, Tag> classTags;
private boolean useApplicationContextClassLoader = true;
private boolean prettyFlow;
private boolean allowAnyType;
private List<TypeFilter> typeFilters;
private int maxAliasesForCollections = 50;
private boolean allowRecursiveKeys;
public SnakeYAMLDataFormat() {
this(null);
}
public SnakeYAMLDataFormat(Class<?> type) {
this.yamlCache = new ThreadLocal<>();
this.constructor = this::defaultConstructor;
this.representer = this::defaultRepresenter;
this.dumperOptions = this::defaultDumperOptions;
this.resolver = this::defaultResolver;
if (type != null) {
this.unmarshalType = type;
this.typeFilters = new CopyOnWriteArrayList<>();
this.typeFilters.add(TypeFilters.types(type));
}
}
@Override
public String getDataFormatName() {
return "snakeYaml";
}
@Override
public CamelContext getCamelContext() {
return camelContext;
}
@Override
public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}
@Override
public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
try (OutputStreamWriter osw = new OutputStreamWriter(stream, ExchangeHelper.getCharsetName(exchange))) {
getYaml(exchange.getContext()).dump(graph, osw);
}
}
@Override
public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
try (InputStreamReader isr = new InputStreamReader(stream, ExchangeHelper.getCharsetName(exchange))) {
Class<?> unmarshalObjectType = unmarshalType != null ? unmarshalType : Object.class;
return getYaml(exchange.getContext()).loadAs(isr, unmarshalObjectType);
}
}
@Override
protected void doInit() throws Exception {
super.doInit();
if (unmarshalTypeName != null && unmarshalType == null) {
setUnmarshalType(camelContext.getClassResolver().resolveClass(unmarshalTypeName));
}
}
protected Yaml getYaml(CamelContext context) {
Yaml yaml = null;
WeakReference<Yaml> ref = yamlCache.get();
if (ref != null) {
yaml = ref.get();
}
if (yaml == null) {
LoaderOptions options = new LoaderOptions();
options.setTagInspector(new TrustedTagInspector());
options.setAllowRecursiveKeys(allowRecursiveKeys);
options.setMaxAliasesForCollections(maxAliasesForCollections);
yaml = new Yaml(
this.constructor.apply(context),
this.representer.apply(context),
this.dumperOptions.apply(context),
options,
this.resolver.apply(context));
yamlCache.set(new WeakReference<>(yaml));
}
return yaml;
}
public Function<CamelContext, BaseConstructor> getConstructor() {
return constructor;
}
/**
* BaseConstructor to construct incoming documents.
*/
public void setConstructor(Function<CamelContext, BaseConstructor> constructor) {
this.constructor = constructor;
}
public Function<CamelContext, Representer> getRepresenter() {
return representer;
}
/**
* Representer to emit outgoing objects.
*/
public void setRepresenter(Function<CamelContext, Representer> representer) {
this.representer = representer;
}
public Function<CamelContext, DumperOptions> getDumperOptions() {
return dumperOptions;
}
/**
* DumperOptions to configure outgoing objects.
*/
public void setDumperOptions(Function<CamelContext, DumperOptions> dumperOptions) {
this.dumperOptions = dumperOptions;
}
public Function<CamelContext, Resolver> getResolver() {
return resolver;
}
/**
* Resolver to detect implicit type
*/
public void setResolver(Function<CamelContext, Resolver> resolver) {
this.resolver = resolver;
}
public ClassLoader getClassLoader() {
return classLoader;
}
/**
* Set a custom classloader
*/
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
public String getUnmarshalTypeName() {
return unmarshalTypeName;
}
public void setUnmarshalTypeName(String unmarshalTypeName) {
this.unmarshalTypeName = unmarshalTypeName;
}
public Class<?> getUnmarshalType() {
return this.unmarshalType;
}
/**
* Class of the object to be created
*/
public void setUnmarshalType(Class<?> unmarshalType) {
this.unmarshalType = unmarshalType;
addTypeFilters(TypeFilters.types(unmarshalType));
}
public List<TypeDescription> getTypeDescriptions() {
return typeDescriptions;
}
/**
* Make YAML aware how to parse a custom Class.
*/
public void setTypeDescriptions(List<TypeDescription> typeDescriptions) {
this.typeDescriptions = new CopyOnWriteArrayList<>(typeDescriptions);
}
public void addTypeDescriptions(Collection<TypeDescription> typeDescriptions) {
if (this.typeDescriptions == null) {
this.typeDescriptions = new CopyOnWriteArrayList<>();
}
this.typeDescriptions.addAll(typeDescriptions);
}
public void addTypeDescriptions(TypeDescription... typeDescriptions) {
addTypeDescriptions(Arrays.asList(typeDescriptions));
}
public void addTypeDescription(Class<?> type, Tag tag) {
if (this.typeDescriptions == null) {
this.typeDescriptions = new CopyOnWriteArrayList<>();
}
this.typeDescriptions.add(new TypeDescription(type, tag));
}
public Map<Class<?>, Tag> getClassTags() {
return classTags;
}
/**
* Define a tag for the <code>Class</code> to serialize.
*/
public void setClassTags(Map<Class<?>, Tag> classTags) {
this.classTags = new ConcurrentHashMap<>();
this.classTags.putAll(classTags);
}
public void addClassTags(Class<?> type, Tag tag) {
if (this.classTags == null) {
this.classTags = new ConcurrentHashMap<>();
}
this.classTags.put(type, tag);
}
public boolean isUseApplicationContextClassLoader() {
return useApplicationContextClassLoader;
}
/**
* Use ApplicationContextClassLoader as custom ClassLoader
*/
public void setUseApplicationContextClassLoader(boolean useApplicationContextClassLoader) {
this.useApplicationContextClassLoader = useApplicationContextClassLoader;
}
public boolean isPrettyFlow() {
return prettyFlow;
}
/**
* Force the emitter to produce a pretty YAML document when using the flow style.
*/
public void setPrettyFlow(boolean prettyFlow) {
this.prettyFlow = prettyFlow;
}
/**
* Convenience method to set class tag for bot <code>Constructor</code> and <code>Representer</code>
*/
public void addTag(Class<?> type, Tag tag) {
addClassTags(type, tag);
addTypeDescription(type, tag);
}
public List<TypeFilter> getTypeFilters() {
return typeFilters;
}
/**
* Set the types SnakeYAML is allowed to un-marshall
*/
public void setTypeFilters(List<TypeFilter> typeFilters) {
this.typeFilters = new CopyOnWriteArrayList<>(typeFilters);
}
public void setTypeFilterDefinitions(List<String> typeFilterDefinitions) {
this.typeFilters = new CopyOnWriteArrayList<>();
for (String definition : typeFilterDefinitions) {
TypeFilters.valueOf(definition).ifPresent(this.typeFilters::add);
}
}
public void addTypeFilters(Collection<TypeFilter> typeFilters) {
if (this.typeFilters == null) {
this.typeFilters = new CopyOnWriteArrayList<>();
}
this.typeFilters.addAll(typeFilters);
}
public void addTypeFilters(TypeFilter... typeFilters) {
addTypeFilters(Arrays.asList(typeFilters));
}
public boolean isAllowAnyType() {
return allowAnyType;
}
/**
* Allow any class to be un-marshaled, same as setTypeFilters(TypeFilters.allowAll())
*/
public void setAllowAnyType(boolean allowAnyType) {
this.allowAnyType = allowAnyType;
}
public int getMaxAliasesForCollections() {
return maxAliasesForCollections;
}
/**
* Set the maximum amount of aliases allowed for collections.
*/
public void setMaxAliasesForCollections(int maxAliasesForCollections) {
this.maxAliasesForCollections = maxAliasesForCollections;
}
public boolean isAllowRecursiveKeys() {
return allowRecursiveKeys;
}
/**
* Set whether recursive keys are allowed.
*/
public void setAllowRecursiveKeys(boolean allowRecursiveKeys) {
this.allowRecursiveKeys = allowRecursiveKeys;
}
// ***************************
// Defaults
// ***************************
private BaseConstructor defaultConstructor(CamelContext context) {
Collection<TypeFilter> yamlTypeFilters = this.typeFilters;
if (allowAnyType) {
yamlTypeFilters = Collections.singletonList(TypeFilters.allowAll());
}
LoaderOptions options = new LoaderOptions();
options.setTagInspector(new TrustedTagInspector());
options.setAllowRecursiveKeys(allowRecursiveKeys);
options.setMaxAliasesForCollections(maxAliasesForCollections);
BaseConstructor yamlConstructor;
if (yamlTypeFilters != null) {
ClassLoader yamlClassLoader = this.classLoader;
if (yamlClassLoader == null && useApplicationContextClassLoader) {
yamlClassLoader = context.getApplicationContextClassLoader();
}
yamlConstructor = yamlClassLoader != null
? typeFilterConstructor(yamlClassLoader, yamlTypeFilters, options)
: typeFilterConstructor(yamlTypeFilters, options);
} else {
yamlConstructor = new SafeConstructor(options);
}
if (typeDescriptions != null && yamlConstructor instanceof Constructor) {
for (TypeDescription typeDescription : typeDescriptions) {
((Constructor) yamlConstructor).addTypeDescription(typeDescription);
}
}
return yamlConstructor;
}
private Representer defaultRepresenter(CamelContext context) {
Representer yamlRepresenter = new Representer(new DumperOptions());
if (classTags != null) {
for (Map.Entry<Class<?>, Tag> entry : classTags.entrySet()) {
yamlRepresenter.addClassTag(entry.getKey(), entry.getValue());
}
}
return yamlRepresenter;
}
private DumperOptions defaultDumperOptions(CamelContext context) {
DumperOptions yamlDumperOptions = new DumperOptions();
yamlDumperOptions.setPrettyFlow(prettyFlow);
return yamlDumperOptions;
}
private Resolver defaultResolver(CamelContext context) {
return new Resolver();
}
// ***************************
// Constructors
// ***************************
private static Constructor typeFilterConstructor(final Collection<TypeFilter> typeFilters, LoaderOptions options) {
return new Constructor(options) {
@Override
protected Class<?> getClassForName(String name) throws ClassNotFoundException {
if (typeFilters.stream().noneMatch(f -> f.test(name))) {
throw new IllegalArgumentException("Type " + name + " is not allowed");
}
return super.getClassForName(name);
}
};
}
private static Constructor typeFilterConstructor(
final ClassLoader classLoader, final Collection<TypeFilter> typeFilters,
LoaderOptions options) {
return new CustomClassLoaderConstructor(classLoader, options) {
@Override
protected Class<?> getClassForName(String name) throws ClassNotFoundException {
if (typeFilters.stream().noneMatch(f -> f.test(name))) {
throw new IllegalArgumentException("Type " + name + " is not allowed");
}
return super.getClassForName(name);
}
};
}
}