ClientState.java
4.64 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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.Cookie
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
*/
package com.adobe.granite.ui.components;
import com.adobe.granite.ui.components.AttrBuilder;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.Cookie;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
@Deprecated
public class ClientState {
private final String COOKIE_NAME = "cui-state";
private final String STATE_GLOBAL_NAMESPACE = "global";
private JSONObject json = null;
public ClientState(SlingHttpServletRequest slingRequest) {
Cookie cookie = slingRequest.getCookie("cui-state");
try {
String cookieVal = URLDecoder.decode(cookie.getValue(), "UTF-8");
this.json = new JSONObject(cookieVal);
}
catch (Exception ex) {
this.json = null;
}
}
private void addAttribute(AttrBuilder attr, String attribute, JSONObject attributeJson) throws JSONException {
if ("class".equals(attribute)) {
String[] attrClassList;
String attrClass = attributeJson.optString("val");
for (String anAttrClassList : attrClassList = attrClass.split("\\s+")) {
attr.addClass(anAttrClassList);
}
} else if ("rel".equals(attribute)) {
String attrRel = attributeJson.optString("val");
attr.addRel(attrRel);
} else if ("href".equals(attribute)) {
String attrHref = attributeJson.optString("val");
attr.addHref(attribute, attrHref);
} else if (attribute.matches("^data-.+")) {
String attrData = attributeJson.optString("val");
attr.addOther(attribute.replaceFirst("^data-.+", ""), attrData);
} else if (attributeJson.optBoolean("val")) {
attr.addBoolean(attribute, attributeJson.getBoolean("val"));
} else {
String attrString = attributeJson.optString("val");
attr.add(attribute, attrString);
}
}
public JSONObject getState(String selector) {
return this.getState(selector, "global");
}
public JSONObject getState(String selector, String namespace) {
try {
JSONObject namespaceJson = this.json.getJSONObject(namespace);
JSONObject selectorJson = namespaceJson.getJSONObject(selector);
return selectorJson;
}
catch (Exception ex) {
return null;
}
}
public JSONObject getState(String selector, String[] attributes) {
return this.getState(selector, attributes, "global");
}
public JSONObject getState(String selector, String[] attributes, String namespace) {
try {
List<String> attrList = null;
if (attributes != null) {
attrList = Arrays.asList(attributes);
}
JSONObject selectorJson = this.getState(selector, namespace);
Iterator keys = selectorJson.keys();
while (keys.hasNext()) {
String attribute = (String)keys.next();
if (attrList == null || attrList.contains(attribute)) continue;
selectorJson.remove(attribute);
}
return selectorJson;
}
catch (Exception ex) {
return null;
}
}
public boolean restoreState(AttrBuilder attr, String selector) {
return this.restoreState(attr, selector, "global");
}
public boolean restoreState(AttrBuilder attr, String selector, String namespace) {
return this.restoreState(attr, selector, null, namespace);
}
public boolean restoreState(AttrBuilder attr, String selector, String[] attributes) {
return this.restoreState(attr, selector, attributes, "global");
}
public boolean restoreState(AttrBuilder attr, String selector, String[] attributes, String namespace) {
try {
JSONObject selectorJson = this.getState(selector, attributes, namespace);
Iterator keys = selectorJson.keys();
while (keys.hasNext()) {
String attribute = (String)keys.next();
JSONObject attributeJson = selectorJson.getJSONObject(attribute);
this.addAttribute(attr, attribute, attributeJson);
}
return true;
}
catch (Exception ex) {
return false;
}
}
}