/
JacksonExporter.java
161 lines (140 loc) · 6.39 KB
/
JacksonExporter.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
/*
* 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.sling.models.jacksonexporter.impl;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import com.fasterxml.jackson.databind.MapperFeature;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.Order;
import org.apache.sling.commons.osgi.RankedServices;
import org.apache.sling.models.export.spi.ModelExporter;
import org.apache.sling.models.factory.ExportException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.sling.models.jacksonexporter.ModuleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
@Component
@Service
public class JacksonExporter implements ModelExporter {
private static final Logger log = LoggerFactory.getLogger(JacksonExporter.class);
private static final String SERIALIZATION_FEATURE_PREFIX = SerializationFeature.class.getSimpleName() + ".";
private static final int SERIALIZATION_FEATURE_PREFIX_LENGTH = SERIALIZATION_FEATURE_PREFIX.length();
private static final String MAPPER_FEATURE_PREFIX = MapperFeature.class.getSimpleName() + ".";
private static final int MAPPER_FEATURE_PREFIX_LENGTH = MAPPER_FEATURE_PREFIX.length();
@Reference(name = "moduleProvider", referenceInterface = ModuleProvider.class,
cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC)
private final RankedServices<ModuleProvider> moduleProviders = new RankedServices<ModuleProvider>(Order.ASCENDING);
@Override
public boolean isSupported(@Nonnull Class<?> clazz) {
return clazz.equals(String.class) || clazz.equals(Map.class);
}
@Override
public <T> T export(@Nonnull Object model, @Nonnull Class<T> clazz, @Nonnull Map<String, String> options)
throws ExportException {
ObjectMapper mapper = new ObjectMapper();
for (Map.Entry<String, String> optionEntry : options.entrySet()) {
String key = optionEntry.getKey();
if (key.startsWith(SERIALIZATION_FEATURE_PREFIX)) {
String enumName = key.substring(SERIALIZATION_FEATURE_PREFIX_LENGTH);
try {
SerializationFeature feature = SerializationFeature.valueOf(enumName);
mapper.configure(feature, Boolean.valueOf(optionEntry.getValue()));
} catch (IllegalArgumentException e) {
log.warn("Bad SerializationFeature option");
}
} else if (key.startsWith(MAPPER_FEATURE_PREFIX)) {
String enumName = key.substring(MAPPER_FEATURE_PREFIX_LENGTH);
try {
MapperFeature feature = MapperFeature.valueOf(enumName);
mapper.configure(feature, Boolean.valueOf(optionEntry.getValue()));
} catch (IllegalArgumentException e) {
log.warn("Bad SerializationFeature option");
}
}
}
for (ModuleProvider moduleProvider : moduleProviders) {
mapper.registerModule(moduleProvider.getModule());
}
if (clazz.equals(Map.class)) {
return (T) mapper.convertValue(model, Map.class);
} else if (clazz.equals(String.class)) {
final JsonFactory f = new JsonFactory();
f.setCharacterEscapes(new EscapeCloseScriptBlocks());
StringWriter writer = new StringWriter();
JsonGenerator jgen;
final boolean printTidy;
if (options.containsKey("tidy")) {
printTidy = Boolean.valueOf(options.get("tidy"));
} else {
printTidy = false;
}
try {
jgen = f.createGenerator(writer);
if (printTidy) {
mapper.writerWithDefaultPrettyPrinter().writeValue(jgen, model);
} else {
mapper.writeValue(jgen, model);
}
} catch (final IOException e) {
throw new ExportException(e);
}
return (T) writer.toString();
} else {
return null;
}
}
protected void bindModuleProvider(final ModuleProvider moduleProvider, final Map<String, Object> props) {
moduleProviders.bind(moduleProvider, props);
}
protected void unbindModuleProvider(final ModuleProvider moduleProvider, final Map<String, Object> props) {
moduleProviders.unbind(moduleProvider, props);
}
@Override
public @Nonnull String getName() {
return "jackson";
}
private static class EscapeCloseScriptBlocks extends CharacterEscapes {
private final int[] escapes;
EscapeCloseScriptBlocks() {
int[] baseEscapes = standardAsciiEscapesForJSON();
baseEscapes['<'] = CharacterEscapes.ESCAPE_STANDARD;
baseEscapes['>'] = CharacterEscapes.ESCAPE_STANDARD;
escapes = baseEscapes;
}
@Override
public int[] getEscapeCodesForAscii() {
return escapes;
}
@Override
public SerializableString getEscapeSequence(final int arg0) {
return null;
}
}
}