1package eu.siacs.conversations.xmpp.jid;
2
3import net.java.otr4j.session.SessionID;
4
5import java.net.IDN;
6
7import gnu.inet.encoding.Stringprep;
8import gnu.inet.encoding.StringprepException;
9
10/**
11 * The `Jid' class provides an immutable representation of a JID.
12 */
13public final class Jid {
14
15 private final String localpart;
16 private final String domainpart;
17 private final String resourcepart;
18
19 // It's much more efficient to store the ful JID as well as the parts instead of figuring them
20 // all out every time (since some characters are displayed but aren't used for comparisons).
21 private final String displayjid;
22
23 public String getLocalpart() {
24 return localpart;
25 }
26
27 public String getDomainpart() {
28 return IDN.toUnicode(domainpart);
29 }
30
31 public String getResourcepart() {
32 return resourcepart;
33 }
34
35 public static Jid fromSessionID(SessionID id) throws InvalidJidException{
36 if (id.getUserID().isEmpty()) {
37 return Jid.fromString(id.getAccountID());
38 } else {
39 return Jid.fromString(id.getAccountID()+"/"+id.getUserID());
40 }
41 }
42
43 public static Jid fromString(final String jid) throws InvalidJidException {
44 return new Jid(jid);
45 }
46
47 public static Jid fromParts(final String localpart,
48 final String domainpart,
49 final String resourcepart) throws InvalidJidException {
50 String out;
51 if (localpart == null || localpart.isEmpty()) {
52 out = domainpart;
53 } else {
54 out = localpart + "@" + domainpart;
55 }
56 if (resourcepart != null && !resourcepart.isEmpty()) {
57 out = out + "/" + resourcepart;
58 }
59 return new Jid(out);
60 }
61
62 private Jid(final String jid) throws InvalidJidException {
63 // Hackish Android way to count the number of chars in a string... should work everywhere.
64 final int atCount = jid.length() - jid.replace("@", "").length();
65 final int slashCount = jid.length() - jid.replace("/", "").length();
66
67 // Throw an error if there's anything obvious wrong with the JID...
68 if (jid.isEmpty() || jid.length() > 3071) {
69 throw new InvalidJidException(InvalidJidException.INVALID_LENGTH);
70 }
71 if (atCount > 1 || slashCount > 1 ||
72 jid.startsWith("@") || jid.endsWith("@") ||
73 jid.startsWith("/") || jid.endsWith("/")) {
74 throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER);
75 }
76
77 String finaljid;
78
79 final int domainpartStart;
80 if (atCount == 1) {
81 final int atLoc = jid.indexOf("@");
82 final String lp = jid.substring(0, atLoc);
83 try {
84 localpart = Stringprep.nodeprep(lp);
85 } catch (final StringprepException e) {
86 throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
87 }
88 if (localpart.isEmpty() || localpart.length() > 1023) {
89 throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
90 }
91 domainpartStart = atLoc + 1;
92 finaljid = lp + "@";
93 } else {
94 localpart = "";
95 finaljid = "";
96 domainpartStart = 0;
97 }
98
99 final String dp;
100 if (slashCount == 1) {
101 final int slashLoc = jid.indexOf("/");
102 final String rp = jid.substring(slashLoc + 1, jid.length());
103 try {
104 resourcepart = Stringprep.resourceprep(rp);
105 } catch (final StringprepException e) {
106 throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
107 }
108 if (resourcepart.isEmpty() || resourcepart.length() > 1023) {
109 throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
110 }
111 dp = jid.substring(domainpartStart, slashLoc);
112 finaljid = finaljid + dp + "/" + rp;
113 } else {
114 resourcepart = "";
115 dp = jid.substring(domainpartStart, jid.length());
116 finaljid = finaljid + dp;
117 }
118
119 // Remove trailling "." before storing the domain part.
120 if (dp.endsWith(".")) {
121 try {
122 domainpart = IDN.toASCII(dp.substring(0, dp.length() - 1), IDN.USE_STD3_ASCII_RULES);
123 } catch (final IllegalArgumentException e) {
124 throw new InvalidJidException(e);
125 }
126 } else {
127 try {
128 domainpart = IDN.toASCII(dp, IDN.USE_STD3_ASCII_RULES);
129 } catch (final IllegalArgumentException e) {
130 throw new InvalidJidException(e);
131 }
132 }
133
134 // TODO: Find a proper domain validation library; validate individual parts, separators, etc.
135 if (domainpart.isEmpty() || domainpart.length() > 1023) {
136 throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
137 }
138
139 this.displayjid = finaljid;
140 }
141
142 public Jid toBareJid() {
143 try {
144 return resourcepart.isEmpty() ? this : fromParts(localpart, domainpart, "");
145 } catch (final InvalidJidException e) {
146 // This should never happen.
147 return null;
148 }
149 }
150
151 public Jid toDomainJid() {
152 try {
153 return resourcepart.isEmpty() && localpart.isEmpty() ? this : fromString(getDomainpart());
154 } catch (final InvalidJidException e) {
155 // This should never happen.
156 return null;
157 }
158 }
159
160 @Override
161 public String toString() {
162 return displayjid;
163 }
164
165 @Override
166 public boolean equals(final Object o) {
167 if (this == o) return true;
168 if (o == null || getClass() != o.getClass()) return false;
169
170 final Jid jid = (Jid) o;
171
172 return jid.hashCode() == this.hashCode();
173 }
174
175 @Override
176 public int hashCode() {
177 int result = localpart.hashCode();
178 result = 31 * result + domainpart.hashCode();
179 result = 31 * result + resourcepart.hashCode();
180 return result;
181 }
182
183 public boolean hasLocalpart() {
184 return !localpart.isEmpty();
185 }
186
187 public boolean isBareJid() {
188 return this.resourcepart.isEmpty();
189 }
190}