Blame view

src/client/java/com/mumfrey/webprefs/framework/WebPreferencesRequestSet.java 2.61 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
package com.mumfrey.webprefs.framework;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
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 WebPreferencesRequestSet extends WebPreferencesRequestAbstract
{
    private static final long serialVersionUID = 1L;

    @Expose @SerializedName("set")
    private final Map<String, String> map = new HashMap<String, String>();

    @Expose @SerializedName("private")
    private boolean isPrivate;

    public WebPreferencesRequestSet(IWebPreferencesServiceDelegate delegate, String uuid, Map<String, String> values)
    {
        this(delegate, uuid, values, false);
    }

    public WebPreferencesRequestSet(IWebPreferencesServiceDelegate delegate, String uuid, Map<String, String> values, 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(values);

        this.map.putAll(values);
        this.isPrivate = isPrivate;
    }

    @Override
    protected String getPath()
    {
        return "/set";
    }

    @Override
    public boolean isValidationRequired()
    {
        return true;
    }

    @Override
    public Set<String> getKeys()
    {
        return this.map.keySet();
    }

    public Map<String, String> getMap()
    {
        return this.map;
    }

    @Override
    protected void validateResponse(IWebPreferencesResponse response)
    {
        if (response.hasSetters())
        {
            Set<String> responseKeys = response.getSetters();
            for (String key : this.map.keySet())
            {
                if (!responseKeys.contains(key))
                {
                    throw new InvalidResponseException(RequestFailureReason.BAD_DATA,
                            "The server responded with an incomplete key set, missing key [" + key + "]");
                }
            }
        }
    }

    private void validate(Map<String, String> set)
    {
        for (Entry<String, String> entry : set.entrySet())
        {
            this.validateKey(entry.getKey());
            this.validateValue(entry.getKey(), entry.getValue());
        }
    }
}