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;
8
9import eu.siacs.conversations.xmpp.Jid;
10
11public class AccountConfiguration {
12
13 private static final Gson GSON = new GsonBuilder().create();
14
15 public Protocol protocol;
16 public String address;
17 public String password;
18
19 public Jid getJid() {
20 return Jid.of(address);
21 }
22
23 public static AccountConfiguration parse(final String input) {
24 final AccountConfiguration c;
25 try {
26 c = GSON.fromJson(input, AccountConfiguration.class);
27 } catch (JsonSyntaxException e) {
28 throw new IllegalArgumentException("Not a valid JSON string", e);
29 }
30 Preconditions.checkArgument(
31 c.protocol == Protocol.XMPP,
32 "Protocol must be XMPP"
33 );
34 Preconditions.checkArgument(
35 c.address != null && c.getJid().isBareJid() && !c.getJid().isDomainJid(),
36 "Invalid XMPP address"
37 );
38 Preconditions.checkArgument(
39 c.password != null && c.password.length() > 0,
40 "No password specified"
41 );
42 return c;
43 }
44
45 public enum Protocol {
46 @SerializedName("xmpp") XMPP,
47 }
48
49}
50