AccountConfiguration.java

 1package eu.siacs.conversations.entities;
 2
 3import com.google.common.base.Preconditions;
 4import com.google.gson.Gson;
 5import com.google.gson.GsonBuilder;
 6import com.google.gson.JsonSyntaxException;
 7import com.google.gson.annotations.SerializedName;
 8import eu.siacs.conversations.xmpp.Jid;
 9
10public class AccountConfiguration {
11
12    private static final Gson GSON = new GsonBuilder().create();
13
14    public Protocol protocol;
15    public String address;
16    public String password;
17
18    public Jid getJid() {
19        return Jid.of(address);
20    }
21
22    public static AccountConfiguration parse(final String input) {
23        final AccountConfiguration c;
24        try {
25            c = GSON.fromJson(input, AccountConfiguration.class);
26        } catch (JsonSyntaxException e) {
27            throw new IllegalArgumentException("Not a valid JSON string", e);
28        }
29        Preconditions.checkArgument(c.protocol == Protocol.XMPP, "Protocol must be XMPP");
30        Preconditions.checkArgument(
31                c.address != null && c.getJid().isBareJid() && !c.getJid().isDomainJid(),
32                "Invalid XMPP address");
33        Preconditions.checkArgument(
34                c.password != null && !c.password.isEmpty(), "No password specified");
35        return c;
36    }
37
38    public enum Protocol {
39        @SerializedName("xmpp")
40        XMPP,
41    }
42}