JSONWriterUtil.java
2.33 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.xss.XSSProtectionService
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.commons;
import com.day.cq.commons.Item;
import com.day.cq.xss.XSSProtectionService;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JSONWriterUtil {
private static final Logger log = LoggerFactory.getLogger(JSONWriterUtil.class);
public static final String KEY_SUFFIX_XSS = "_xss";
public static void put(JSONObject object, String key, String value, WriteMode writeMode, XSSProtectionService xss) throws JSONException {
Item item = new Item(key, value, writeMode, xss, log);
if (item.isPlainText()) {
object.put(item.getKey(), (Object)item.getValue());
}
if (item.isAvoidXSS() && null != xss) {
object.put(item.getXssKey(), (Object)item.getXssValue());
}
}
public static void putOptional(JSONObject object, String key, String value, WriteMode writeMode, XSSProtectionService xss) throws JSONException {
if (value != null) {
JSONWriterUtil.put(object, key, value, writeMode, xss);
}
}
public static void write(JSONWriter writer, String key, String value, WriteMode writeMode, XSSProtectionService xss) throws JSONException {
Item item = new Item(key, value, writeMode, xss, log);
if (item.isPlainText()) {
writer.key(item.getKey()).value((Object)item.getValue());
}
if (item.isAvoidXSS() && null != xss) {
writer.key(item.getXssKey()).value((Object)item.getXssValue());
}
}
public static void writeOptional(JSONWriter writer, String key, String value, WriteMode writeMode, XSSProtectionService xss) throws JSONException {
if (value != null) {
JSONWriterUtil.write(writer, key, value, writeMode, xss);
}
}
public static enum WriteMode {
PLAIN_TEXT,
AVOID_XSS,
BOTH;
private WriteMode() {
}
}
}