WebPreferencesRequestGet.java
2.5 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
package com.mumfrey.webprefs.framework;
import java.util.HashSet;
import java.util.Set;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.mumfrey.webprefs.exceptions.InvalidRequestException;
import com.mumfrey.webprefs.exceptions.InvalidResponseException;
import com.mumfrey.webprefs.interfaces.IWebPreferencesResponse;
import com.mumfrey.webprefs.interfaces.IWebPreferencesServiceDelegate;
class WebPreferencesRequestGet extends WebPreferencesRequestAbstract
{
private static final long serialVersionUID = 1L;
@Expose @SerializedName("get")
private final Set<String> keys = new HashSet<String>();
@Expose @SerializedName("private")
private boolean isPrivate;
public WebPreferencesRequestGet(IWebPreferencesServiceDelegate delegate, String uuid, Set<String> keys)
{
this(delegate, uuid, keys, false);
}
public WebPreferencesRequestGet(IWebPreferencesServiceDelegate delegate, String uuid, Set<String> keys, boolean isPrivate)
{
super(delegate, uuid);
if (isPrivate && delegate.getSession() == null)
{
throw new InvalidRequestException(RequestFailureReason.NO_SESSION, "Cannot request private values without supplying a session");
}
this.validate(keys);
this.keys.addAll(keys);
this.isPrivate = isPrivate;
}
@Override
protected String getPath()
{
return "/get";
}
@Override
public boolean isValidationRequired()
{
return this.isPrivate;
}
@Override
public Set<String> getKeys()
{
return this.keys;
}
@Override
protected void validateResponse(IWebPreferencesResponse response)
{
if (response.hasValues())
{
Set<String> responseKeys = response.getValues().keySet();
for (String key : this.keys)
{
if (!responseKeys.contains(key))
{
throw new InvalidResponseException(RequestFailureReason.BAD_DATA,
"The server responded with an incomplete key set, missing key [" + key + "]");
}
}
}
}
private void validate(Set<String> keys)
{
if (keys == null || keys.isEmpty())
{
throw new InvalidRequestException(RequestFailureReason.BAD_PARAMS, "Cannot request an empty set");
}
for (String key : keys)
{
this.validateKey(key);
}
}
}