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