/
JsonPathEngine.java
96 lines (86 loc) · 3.79 KB
/
JsonPathEngine.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
/**
* 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.jsonpath;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Configuration.Defaults;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.internal.DefaultsImpl;
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.WrappedFile;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileConverter;
public class JsonPathEngine {
private final JsonPath path;
private final Configuration configuration;
public JsonPathEngine(String expression) {
this(expression, false, null);
}
public JsonPathEngine(String expression, boolean suppressExceptions, Option[] options) {
Defaults defaults = DefaultsImpl.INSTANCE;
if (options != null) {
Configuration.ConfigurationBuilder builder = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options);
if (suppressExceptions) {
builder.options(Option.SUPPRESS_EXCEPTIONS);
}
this.configuration = builder.build();
} else {
Configuration.ConfigurationBuilder builder = Configuration.builder().jsonProvider(defaults.jsonProvider());
if (suppressExceptions) {
builder.options(Option.SUPPRESS_EXCEPTIONS);
}
this.configuration = builder.build();
}
this.path = JsonPath.compile(expression);
}
public Object read(Exchange exchange) throws IOException, InvalidPayloadException {
Object json = exchange.getIn().getBody();
if (json instanceof GenericFile) {
try {
json = GenericFileConverter.genericFileToInputStream((GenericFile<?>) json, exchange);
} catch (NoTypeConversionAvailableException e) {
json = ((WrappedFile<?>) json).getFile();
}
} else if (json instanceof WrappedFile) {
json = ((WrappedFile<?>) json).getFile();
}
// the message body type should use the suitable read method
if (json instanceof String) {
String str = (String) json;
return path.read(str, configuration);
} else if (json instanceof InputStream) {
InputStream is = (InputStream) json;
return path.read(is, Charset.defaultCharset().displayName(), configuration);
} else if (json instanceof File) {
File file = (File) json;
return path.read(file, configuration);
} else if (json instanceof URL) {
URL url = (URL) json;
return path.read(url, configuration);
}
// fallback as input stream
InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
return path.read(is);
}
}