1 | package nu.fw.jeti.jabber; |
---|
2 | |
---|
3 | import nu.fw.jeti.util.I18N; |
---|
4 | |
---|
5 | |
---|
6 | /** |
---|
7 | * |
---|
8 | * @author E.S. de Boer |
---|
9 | * @version 1.0 |
---|
10 | */ |
---|
11 | |
---|
12 | public final class JID |
---|
13 | { |
---|
14 | private String domain; |
---|
15 | private String user; |
---|
16 | private String resource; |
---|
17 | |
---|
18 | private String domainPreped; |
---|
19 | private String userPreped; |
---|
20 | private String resourcePreped; |
---|
21 | |
---|
22 | public JID(String server) |
---|
23 | { |
---|
24 | // if(server == null || server.equals("")) throw new NullPointerException(I18N.gettext("main.error.Server_has_no_value")); |
---|
25 | // if(Utils.isAtleastJava(6)){ |
---|
26 | // domainPreped = JIDPrep.namePrep(server); |
---|
27 | // } |
---|
28 | // else |
---|
29 | domainPreped = server.toLowerCase(); |
---|
30 | this.domain = server; |
---|
31 | } |
---|
32 | |
---|
33 | public JID(String user,String server) |
---|
34 | { |
---|
35 | this(server); |
---|
36 | if(user == null) return; |
---|
37 | // if(Utils.isAtleastJava(6)){ |
---|
38 | // userPreped = JIDPrep.nodePrep(user); |
---|
39 | // } |
---|
40 | // else |
---|
41 | userPreped = user.toLowerCase(); |
---|
42 | this.user = user; |
---|
43 | } |
---|
44 | |
---|
45 | public JID(String user,String server,String resource) |
---|
46 | { |
---|
47 | this(user,server); |
---|
48 | // if(Utils.isAtleastJava(6)){ |
---|
49 | // resourcePreped = JIDPrep.resourcePrep(resource); |
---|
50 | // |
---|
51 | // } |
---|
52 | // else |
---|
53 | resourcePreped = resource; |
---|
54 | this.resource = resource; |
---|
55 | } |
---|
56 | |
---|
57 | public JID() |
---|
58 | { |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | public static JID jidFromString(String jid) |
---|
63 | { |
---|
64 | if(jid == null || jid.equals("")) return null; |
---|
65 | String node=null, domain=null, resource=null; |
---|
66 | int loc = jid.indexOf("@"); |
---|
67 | if (loc != -1) |
---|
68 | { |
---|
69 | node = jid.substring(0, loc); |
---|
70 | jid = jid.substring(loc + 1); |
---|
71 | } |
---|
72 | loc = jid.indexOf("/"); |
---|
73 | if (loc == -1) domain = jid; |
---|
74 | else |
---|
75 | { |
---|
76 | domain = jid.substring(0, loc); |
---|
77 | resource = jid.substring(loc + 1); |
---|
78 | } |
---|
79 | if(domain ==null) return null; |
---|
80 | return new JID(node,domain,resource); |
---|
81 | } |
---|
82 | |
---|
83 | public static JID checkedJIDFromString(String jid) throws InstantiationException |
---|
84 | { |
---|
85 | if(jid == null || jid.equals("")) return null; |
---|
86 | String node=null, domain=null, resource=null; |
---|
87 | int loc = jid.indexOf("@"); |
---|
88 | if (loc != -1) |
---|
89 | { |
---|
90 | node = jid.substring(0, loc); |
---|
91 | jid = jid.substring(loc + 1); |
---|
92 | } |
---|
93 | loc = jid.indexOf("/"); |
---|
94 | if (loc == -1) domain = jid; |
---|
95 | else |
---|
96 | { |
---|
97 | domain = jid.substring(0, loc); |
---|
98 | resource = jid.substring(loc + 1); |
---|
99 | } |
---|
100 | if (node != null && node.length() > 255) throw new InstantiationException (I18N.gettext("main.error.Username_>_255_Characters")); |
---|
101 | if(domain.indexOf("@") != -1) throw new InstantiationException (I18N.gettext("main.error.Server_or_Username_contains_a_'@'")); |
---|
102 | if(!isValidUser(node)) throw new InstantiationException(I18N.getTextWithAmp("main.error.Username_contains_illegal_chars_(see_english_translation)")); |
---|
103 | if(!isValidServer(domain)) throw new InstantiationException(I18N.gettext("main.error.Server_must_start_with_a_letter_(see_english_translation)")); |
---|
104 | return new JID(node,domain,resource); |
---|
105 | } |
---|
106 | |
---|
107 | public static boolean isValidUser(String user) |
---|
108 | { |
---|
109 | if(user == null) return true; |
---|
110 | int len = user.length(); |
---|
111 | if (len > 255) return false; |
---|
112 | char c; |
---|
113 | for(int i=0; i<len; i++) |
---|
114 | { |
---|
115 | c = user.charAt(i); |
---|
116 | if (c <= ' ') return false; |
---|
117 | if (c == ':') return false; |
---|
118 | if (c == '@') return false; |
---|
119 | if (c == '"') return false; |
---|
120 | if (c == '>') return false; |
---|
121 | if (c == '<') return false; |
---|
122 | if (c == '/') return false; |
---|
123 | if (c == '\'') return false; |
---|
124 | if (c == '&') return false; |
---|
125 | if (c == '\u077F') return false; |
---|
126 | if (c == '\u0FFE') return false; |
---|
127 | if (c == '\u0FFF') return false; |
---|
128 | } |
---|
129 | return true; |
---|
130 | } |
---|
131 | |
---|
132 | //check dns name |
---|
133 | public static boolean isValidServer(String server) |
---|
134 | { |
---|
135 | if(server == null || server.equals("")) return false; |
---|
136 | int len = server.length(); |
---|
137 | //if (len > 255) return false; |
---|
138 | //first leter must be alphanumeric |
---|
139 | if(!Character.isLetterOrDigit(server.charAt(0)))return false; |
---|
140 | char c; |
---|
141 | for(int i=0; i<len; i++) |
---|
142 | { |
---|
143 | c = server.charAt(i); |
---|
144 | if(!(Character.isLetterOrDigit(c) || c=='.' || c=='-')) return false; |
---|
145 | } |
---|
146 | return true; |
---|
147 | } |
---|
148 | |
---|
149 | public String getUser(){return user;} |
---|
150 | |
---|
151 | public String getDomain(){return domain;} |
---|
152 | |
---|
153 | public String getResource(){return resource;} |
---|
154 | |
---|
155 | public String toString() |
---|
156 | { |
---|
157 | StringBuffer jid = new StringBuffer(); |
---|
158 | if (user != null){ |
---|
159 | jid.append(user); |
---|
160 | jid.append("@"); |
---|
161 | } |
---|
162 | jid.append(domain); |
---|
163 | if (resource != null){ |
---|
164 | jid.append("/"); |
---|
165 | jid.append(resource); |
---|
166 | } |
---|
167 | return jid.toString(); |
---|
168 | } |
---|
169 | |
---|
170 | public String toStringNoResource() |
---|
171 | { |
---|
172 | StringBuffer jid = new StringBuffer(); |
---|
173 | if (user != null){ |
---|
174 | jid.append(user); |
---|
175 | jid.append("@"); |
---|
176 | } |
---|
177 | jid.append(domain); |
---|
178 | return jid.toString(); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | /** |
---|
184 | * equals doesn't look at resources |
---|
185 | * @param jid |
---|
186 | * @return boolean |
---|
187 | */ |
---|
188 | public boolean equals(JID jid){ |
---|
189 | if (jid == null) return false; |
---|
190 | return equalsNode(jid) && domainPreped.equals(jid.domainPreped); |
---|
191 | } |
---|
192 | |
---|
193 | private boolean equalsNode(JID jid){ |
---|
194 | if (userPreped == null ^ jid.userPreped == null ) return false; |
---|
195 | if(userPreped == null) return true; |
---|
196 | return userPreped.equals(jid.userPreped); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * equals doesn't look at resources |
---|
201 | * @param jid |
---|
202 | * @return boolean |
---|
203 | */ |
---|
204 | public boolean equals(Object o){ |
---|
205 | if(!(o instanceof JID)) return false; |
---|
206 | return equals((JID)o); |
---|
207 | } |
---|
208 | |
---|
209 | public int hashCode() |
---|
210 | { |
---|
211 | int hash = 0; |
---|
212 | if(userPreped != null) hash = userPreped.hashCode(); |
---|
213 | return hash ^ domainPreped.hashCode(); |
---|
214 | } |
---|
215 | } |
---|
216 | /* |
---|
217 | * Overrides for emacs |
---|
218 | * Local variables: |
---|
219 | * tab-width: 4 |
---|
220 | * End: |
---|
221 | */ |
---|