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 package org.sentrysoftware.wbem.sblim.slp;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 import java.io.Serializable;
51
52
53
54
55
56
57
58 public class ServiceURL implements Serializable {
59
60 private static final long serialVersionUID = 8998115518853094365L;
61
62
63
64
65
66 public static final int NO_PORT = 0;
67
68
69
70
71
72
73 public static final int LIFETIME_NONE = 0;
74
75
76
77
78 public static final int LIFETIME_DEFAULT = 10800;
79
80
81
82
83 public static final int LIFETIME_MAXIMUM = 65535;
84
85
86
87
88
89 public static final int LIFETIME_PERMANENT = -1;
90
91 static final int PORT_MAXIMUM = 65535;
92
93 private ServiceType iServiceType = null;
94
95 private String iTransport = null;
96
97 private String iHost = null;
98
99 private int iPort = 0;
100
101 private String iURLPath = null;
102
103 private int iLifetime = LIFETIME_DEFAULT;
104
105
106
107
108
109
110
111
112
113
114
115
116 public ServiceURL(String pServiceURL, int pLifetime) {
117
118 if (pLifetime > LIFETIME_MAXIMUM || pLifetime < LIFETIME_PERMANENT) throw new IllegalArgumentException(
119 "lifetime:" + pLifetime);
120
121 for (int i = 0; i < pServiceURL.length(); i++) {
122 char c = pServiceURL.charAt(i);
123 if ("/:-.%_\'*()$!,+\\;@?&=[]".indexOf(c) == -1 && !Character.isLetterOrDigit(c)) { throw new IllegalArgumentException(
124 "invalid character: '" + c + "' on string \"" + pServiceURL + "\""); }
125 }
126
127 parseURL(pServiceURL);
128
129 this.iLifetime = (pLifetime == LIFETIME_PERMANENT) ? LIFETIME_MAXIMUM : pLifetime;
130 }
131
132
133
134
135
136
137
138 public ServiceType getServiceType() {
139 return this.iServiceType;
140 }
141
142
143
144
145
146
147
148
149 public void setServiceType(ServiceType pServicetype) {
150 if (!this.iServiceType.isServiceURL()) this.iServiceType = pServicetype;
151 }
152
153
154
155
156
157
158
159 public String getTransport() {
160
161 return "";
162 }
163
164
165
166
167
168
169
170 public String getHost() {
171 return this.iHost;
172 }
173
174
175
176
177
178
179
180 public int getPort() {
181 return this.iPort;
182 }
183
184
185
186
187
188
189 public String getURLPath() {
190 return this.iURLPath;
191 }
192
193
194
195
196
197
198
199 public int getLifetime() {
200 return this.iLifetime;
201 }
202
203
204
205
206
207
208
209
210
211
212 @Override
213 public boolean equals(Object obj) {
214 if (obj == this) return true;
215 if (!(obj instanceof ServiceURL)) return false;
216
217 ServiceURL that = (ServiceURL) obj;
218
219 return equalObjs(this.iServiceType, that.iServiceType)
220 && equalStrs(this.iTransport, that.iTransport) && equalStrs(this.iHost, that.iHost)
221 && this.iPort == that.iPort;
222 }
223
224
225
226
227
228
229
230
231
232
233 @Override
234 public String toString() {
235 StringBuffer buf = new StringBuffer();
236 if (this.iServiceType != null) buf.append(this.iServiceType);
237 if (this.iURLPath != null) {
238 if (buf.length() > 0) buf.append("://");
239 buf.append(this.iURLPath);
240 }
241 return buf.toString();
242 }
243
244 private int iHashCode = 0;
245
246
247
248
249
250
251
252
253
254
255 @Override
256 public int hashCode() {
257 if (this.iHashCode == 0) {
258 this.iHashCode = toString().hashCode();
259 }
260 return this.iHashCode;
261 }
262
263 private static final String DELIM = "://";
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 private void parseURL(String pUrlStr) throws IllegalArgumentException {
279 int srvTypeEndIdx = pUrlStr.indexOf(DELIM);
280 String addrStr;
281 if (srvTypeEndIdx >= 0) {
282 this.iServiceType = new ServiceType(pUrlStr.substring(0, srvTypeEndIdx));
283 addrStr = pUrlStr.substring(srvTypeEndIdx + DELIM.length());
284 } else {
285 if (pUrlStr.startsWith("service:")) {
286 this.iServiceType = new ServiceType(pUrlStr);
287 addrStr = null;
288 } else {
289 addrStr = pUrlStr;
290 }
291 }
292 if (addrStr == null) return;
293 this.iURLPath = addrStr;
294 if (addrStr.charAt(0) == '[') {
295 parseIPv6Address(addrStr);
296 } else {
297 parseIPv4Address(addrStr);
298 }
299 }
300
301 private void parseIPv6Address(String pAddrStr) throws IllegalArgumentException {
302 int hostEndIdx = pAddrStr.indexOf(']');
303 if (hostEndIdx < 0) throw new IllegalArgumentException("']' is not found for IPv6 address");
304 int colonIdx = hostEndIdx + 1;
305 this.iHost = pAddrStr.substring(0, colonIdx);
306 if (colonIdx < pAddrStr.length()) {
307 if (pAddrStr.charAt(colonIdx) != ':') throw new IllegalArgumentException(
308 "':' expected in \"" + pAddrStr + "\" at position " + colonIdx + " !");
309 parsePort(pAddrStr.substring(colonIdx + 1), pAddrStr);
310 }
311 }
312
313 private void parseIPv4Address(String pAddrStr) {
314 int colonIdx = pAddrStr.indexOf(':');
315 if (colonIdx > 0) {
316 this.iHost = pAddrStr.substring(0, colonIdx);
317 parsePort(pAddrStr.substring(colonIdx + 1), pAddrStr);
318 } else {
319 this.iHost = pAddrStr;
320 }
321 }
322
323 private void parsePort(String pPortStr, String pAddrStr) throws IllegalArgumentException {
324 try {
325 this.iPort = Integer.parseInt(pPortStr);
326 } catch (NumberFormatException e) {
327 throw new IllegalArgumentException("Port field : " + pPortStr + " in " + pAddrStr
328 + " is invalid!");
329 }
330 }
331
332 private static boolean equalObjs(Object pThis, Object pThat) {
333 return pThis == null ? pThat == null : pThis.equals(pThat);
334 }
335
336 private static boolean equalStrs(String pThis, String pThat) {
337 return (pThis == null || pThis.length() == 0) ? (pThat == null || pThat.length() == 0)
338 : pThis.equals(pThat);
339 }
340
341 }