State.java
3.43 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.Cookie
* org.apache.sling.api.SlingHttpServletRequest
*/
package com.adobe.granite.ui.components;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.Cookie;
import org.apache.sling.api.SlingHttpServletRequest;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class State {
private static final List<String> BLACKLIST = Arrays.asList("csrftoken", "login-token");
private SlingHttpServletRequest request;
private Set<String> names;
public State(SlingHttpServletRequest request) {
this.request = request;
}
public String get(String name) {
Item i = this.getItem(name);
return i == null ? null : i.getString();
}
public String get(String name, String defaultValue) {
Item i = this.getItem(name);
return i == null ? defaultValue : i.getString();
}
public boolean get(String name, boolean defaultValue) {
Item i = this.getItem(name);
if (i == null || !"true".equals(i.getString()) && !"false".equals(i.getString())) {
return defaultValue;
}
return i.getBoolean();
}
public int get(String name, int defaultValue) {
try {
Item i = this.getItem(name);
return i == null ? defaultValue : i.getInt();
}
catch (NumberFormatException e) {
return defaultValue;
}
}
public Iterator<String> names() {
if (this.names == null) {
this.names = new LinkedHashSet<String>();
Cookie[] cookies = this.request.getCookies();
if (cookies != null) {
for (Cookie c : cookies) {
String name = c.getName();
if (BLACKLIST.contains(name.toLowerCase())) continue;
this.names.add(name);
}
}
}
return this.names.iterator();
}
public Item getItem(String name) {
if (BLACKLIST.contains(name.toLowerCase())) {
return null;
}
Cookie c = this.request.getCookie(name);
if (c == null) {
return null;
}
try {
return new ItemImpl(name, URLDecoder.decode(c.getValue(), "utf-8"));
}
catch (UnsupportedEncodingException impossible) {
throw new RuntimeException(impossible);
}
}
private class ItemImpl
implements Item {
private String name;
private String value;
public ItemImpl(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return this.name;
}
public String getString() {
return this.value;
}
public boolean getBoolean() {
return Boolean.parseBoolean(this.value);
}
public int getInt() throws NumberFormatException {
return Integer.parseInt(this.value);
}
}
public static interface Item {
public String getName();
public String getString();
public boolean getBoolean();
public int getInt() throws NumberFormatException;
}
}