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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1999 by Sun Microsystems, Inc.
* All rights reserved.
*
*/
// ServiceURL.java : The service URL.
// Author: James Kempf, Erik Guttman
//
package com.sun.slp;
import java.util.*;
import java.io.*;
import java.net.*;
/**
* The ServiceURL object models the SLP service URL. Both service: URLs
* and regular URLs are handled by this class.
*
* @author James Kempf, Erik Guttman
*/
public class ServiceURL extends Object implements Serializable {
// Recognized transports.
private final static String IPX = "ipx";
private final static String AT = "at";
/**
* Indicates that no port information is required or was returned
* for this service URL.
*/
public static final int NO_PORT = 0;
/**
* No life time parameter is given.
*/
public static final int LIFETIME_NONE = 0;
/**
* Default lifetime, 3 hours.
*/
public static final int LIFETIME_DEFAULT = 10800;
/**
* Maximum lifetime, approximately 18 hours.
*/
public static final int LIFETIME_MAXIMUM = 0xFFFF;
/**
* Reregister periodically.
*/
public static final int LIFETIME_PERMANENT = -1;
// Maximum port size.
static final int PORT_MAXIMUM = 0xFFFF;
//
// data fields
//
private ServiceType serviceType = null;
private ServiceType originalServiceType = null;
private String transport = "";
private String host = "";
private int port = NO_PORT;
private String URLPath = "";
private int lifetime = LIFETIME_DEFAULT;
private boolean isPermanent = false;
private boolean noDoubleSlash = false;
/**
* Construct a service URL object.
*
* @param URL The service URL as a string.
* @param iLifetime The service advertisement lifetime.
* @exception IllegalArgumentException Thrown if parse
* errors occur in the
* parameter.
*/
public ServiceURL(String URL, int iLifetime)
throws IllegalArgumentException {
Assert.nonNullParameter(URL, "URL");
if ((iLifetime > LIFETIME_MAXIMUM) ||
(iLifetime < LIFETIME_PERMANENT)) {
throw
new IllegalArgumentException(
SLPConfig.getSLPConfig().formatMessage("lifetime_error",
new Object[0]));
}
checkURLString(URL);
parseURL(URL);
if (iLifetime == LIFETIME_PERMANENT) {
isPermanent = true;
iLifetime = LIFETIME_MAXIMUM;
}
lifetime = iLifetime;
}
//
// ------------------------------------------------------------------
// Accessors
// ------------------------------------------------------------------
//
/**
* @return The service type name.
*/
public ServiceType getServiceType() {
return serviceType;
}
/**
* Set service type and naming authority if this is not a service: URL.
*
* @param type The new ServiceType object.
* @exception IllegalArgumentException If the service type name or
* naming authority name is invalid.
*/
public void setServiceType(ServiceType type) {
if (!serviceType.isServiceURL()) {
serviceType = type;
}
}
/**
* @return The machine name or IP address.
*/
public String getHost() {
return host;
}
/**
* @return The port number, if any.
*/
public int getPort() {
return port;
}
/**
* @return The URL path description, if any.
*/
public String getURLPath() {
return URLPath;
}
/**
* @return The service advertisement lifetime.
*/
public int getLifetime() {
return lifetime;
}
/**
* Formats the service URL into standard URL form.
*
* @return Formatted string with the service URL.
*/
public String toString() { // Overrides Object.toString();
return
originalServiceType.toString() +
":/" + transport + (noDoubleSlash == false ? "/":"") +
host + (port != NO_PORT ? (":" + port) : "") +
URLPath;
}
public int hashCode() {
return
serviceType.hashCode() +
transport.hashCode() +
host.hashCode() +
port +
URLPath.hashCode();
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ServiceURL)) {
return false;
}
ServiceURL surl = (ServiceURL)obj;
return
serviceType.equals(surl.serviceType) &&
transport.equals(surl.transport) &&
host.equals(surl.host) &&
(port == surl.port) &&
(noDoubleSlash == surl.noDoubleSlash) &&
URLPath.equals(surl.URLPath);
}
// Return permanent status.
boolean getIsPermanent() {
return isPermanent;
}
// Check URL characters for correctness.
private void checkURLString(String s)
throws IllegalArgumentException {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// allowed by RFC1738
if (c == '/' || c == ':' || c == '-' || c == ':' ||
c == '.' || c == '%' || c == '_' || c == '\'' ||
c == '*' || c == '(' || c == ')' || c == '$' ||
c == '!' || c == ',' || c == '+' || c == '\\') {
// defer to Windows
continue;
}
// reserved by RFC1738, and thus allowed, pg. 20
if (c == ';' || c == '@' || c == '?' || c == '&' || c == '=') {
continue;
}
if (Character.isLetterOrDigit(c)) {
continue;
}
SLPConfig conf = SLPConfig.getSLPConfig();
throw
new IllegalArgumentException(
conf.formatMessage("url_char_error",
new Object[] {
new Character(c)}));
}
}
// Parse the incoming service URL specification.
private void parseURL(String sURL)
throws IllegalArgumentException {
StringTokenizer st = new StringTokenizer(sURL, "/", true);
try {
// This loop is a kludgy way to break out of the parse so
// we only throw at one location in the code.
do {
String typeName = st.nextToken();
// First token must be service type name.
if (typeName.equals("/")) {
break; // error!
}
// Check for colon terminator, not part of service
// type name.
if (!typeName.endsWith(":")) {
break; // error!
}
// Create service type, remove trailing colon.
serviceType =
new ServiceType(typeName.substring(0,
typeName.length() - 1));
originalServiceType = serviceType;
// Separator between service type name and transport.
String slash1 = st.nextToken();
if (!slash1.equals("/")) {
break; // error!
}
String slash2 = st.nextToken();
String sAddr = ""; // address...
// Check for abstract type or alternate transport.
if (!slash2.equals("/")) {
// If this is an abstract type, then we could have
// something like: service:file-printer:file:/foo/bar.
// This is OK. Also, if this is a non-service: URL,
// something like file:/foo/bar is OK.
if (!serviceType.isServiceURL()) {
sAddr = slash2;
noDoubleSlash = true;
} else {
// We only recognize IPX and Appletalk at this point.
if (!slash2.equalsIgnoreCase(IPX) &&
!slash2.equalsIgnoreCase(AT)) {
// Abstract type is OK. We must check here because
// something like
// service:printing:lpr:/ipx/foo/bar
// is allowed.
if (serviceType.isAbstractType()) {
sAddr = slash2;
noDoubleSlash = true;
} else {
break; // error!
}
} else {
transport = slash2.toLowerCase();
// Check for separator between transport and host.
if (!st.nextToken().equals("/")) {
break; // error!
}
sAddr = st.nextToken();
}
}
} else {
// Not abstract type, no alternate transport. Get host.
sAddr = st.nextToken();
}
if (sAddr.equals("/")) {// no host part
URLPath = "/" + st.nextToken("");
return; // we're done!
}
host = sAddr;
// Need to check for port number if this is an IP transport.
if (transport.equals("")) {
StringTokenizer tk = new StringTokenizer(host, ":");
host = tk.nextToken();
// Get port if any.
if (tk.hasMoreTokens()) {
String p = tk.nextToken();
if (tk.hasMoreTokens()) {
break; // error!
}
try {
port = Integer.parseInt(p);
} catch (NumberFormatException ex) {
break; // error!
}
if (port <= 0 || port > PORT_MAXIMUM) {
break; // error!
}
}
}
//
// after this point we have to check if there is a token
// remaining before we read it: It is legal to stop at any
// point now. Before all the tokens were required, so
// missing any was an error.
//
if (st.hasMoreTokens() == false) {
// minimal url service:t:// a
return; // we're done!
}
String sSep = st.nextToken();
if (!sSep.equals("/")) {
break; // error!
}
// there is a URL path
// URLPath is all remaining tokens
URLPath = sSep;
if (st.hasMoreTokens()) {
URLPath += st.nextToken("");
}
URLPath = URLPath.trim();
return; // done!
} while (false); // done with parse.
} catch (NoSuchElementException ex) {
throw
new IllegalArgumentException(
SLPConfig.getSLPConfig().formatMessage("url_syntax_error",
new Object[] {sURL}));
}
// The only way to get here is if there was an error in the
// parse.
throw
new IllegalArgumentException(
SLPConfig.getSLPConfig().formatMessage("url_syntax_error",
new Object[] {sURL}));
}
}
|