1 21 22 27 28 package javax.mail; 29 30 import java.net.*; 31 32 import java.io.ByteArrayOutputStream ; 33 import java.io.OutputStreamWriter ; 34 import java.io.IOException ; 35 import java.io.UnsupportedEncodingException ; 36 import java.util.BitSet ; 37 38 39 52 53 public class URLName { 54 55 58 protected String fullURL; 59 60 63 private String protocol; 64 65 68 private String username; 69 70 73 private String password; 74 75 78 private String host; 79 80 84 private InetAddress hostAddress; 85 private boolean hostAddressKnown = false; 86 87 90 private int port = -1; 91 92 95 private String file; 96 97 100 private String ref; 101 102 105 private int hashCode = 0; 106 107 110 private static boolean doEncode = true; 111 112 static { 113 try { 114 doEncode = !Boolean.getBoolean("mail.URLName.dontencode"); 115 } catch (Exception ex) { 116 } 118 } 119 120 126 public URLName( 127 String protocol, 128 String host, 129 int port, 130 String file, 131 String username, 132 String password 133 ) 134 { 135 this.protocol = protocol; 136 this.host = host; 137 this.port = port; 138 int refStart; 139 if (file != null && (refStart = file.indexOf('#')) != -1) { 140 this.file = file.substring(0, refStart); 141 this.ref = file.substring(refStart + 1); 142 } else { 143 this.file = file; 144 this.ref = null; 145 } 146 this.username = doEncode ? encode(username) : username; 147 this.password = doEncode ? encode(password) : password; 148 } 149 150 153 public URLName(URL url) { 154 this(url.toString()); 155 } 156 157 161 public URLName(String url) { 162 parseString(url); 163 } 164 165 168 public String toString() { 169 if (fullURL == null) { 170 StringBuffer tempURL = new StringBuffer (); 172 if (protocol != null) { 173 tempURL.append(protocol); 174 tempURL.append(":"); 175 } 176 177 if (username != null || host != null) { 178 tempURL.append("//"); 180 181 if (username != null) { 184 tempURL.append(username); 185 186 if (password != null){ 187 tempURL.append(":"); 188 tempURL.append(password); 189 } 190 191 tempURL.append("@"); 192 } 193 194 if (host != null) { 196 tempURL.append(host); 197 } 198 199 if (port != -1) { 201 tempURL.append(":"); 202 tempURL.append(Integer.toString(port)); 203 } 204 if (file != null) 205 tempURL.append("/"); 206 } 207 208 if (file != null) { 210 tempURL.append(file); 211 } 212 213 if (ref != null) { 215 tempURL.append("#"); 216 tempURL.append(ref); 217 } 218 219 fullURL = tempURL.toString(); 221 } 222 223 return fullURL; 224 } 225 226 229 protected void parseString(String url) { 230 protocol = file = ref = host = username = password = null; 233 port = -1; 234 235 int len = url.length(); 236 237 int protocolEnd = url.indexOf(':'); 241 if (protocolEnd != -1) 242 protocol = url.substring(0, protocolEnd); 243 244 if (url.regionMatches(protocolEnd + 1, "//", 0, 2)) { 246 String fullhost = null; 248 int fileStart = url.indexOf('/', protocolEnd + 3); 249 if (fileStart != -1) { 250 fullhost = url.substring(protocolEnd + 3, fileStart); 251 if (fileStart + 1 < len) 252 file = url.substring(fileStart + 1); 253 else 254 file = ""; 255 } else 256 fullhost = url.substring(protocolEnd + 3); 257 258 int i = fullhost.indexOf('@'); 260 if (i != -1) { 261 String fulluserpass = fullhost.substring(0, i); 262 fullhost = fullhost.substring(i + 1); 263 264 int passindex = fulluserpass.indexOf(':'); 266 if (passindex != -1) { 267 username = fulluserpass.substring(0, passindex); 268 password = fulluserpass.substring(passindex + 1); 269 } else { 270 username = fulluserpass; 271 } 272 } 273 274 int portindex; 276 if (fullhost.length() > 0 && fullhost.charAt(0) == '[') { 277 portindex = fullhost.indexOf(':', fullhost.indexOf(']')); 279 } else { 280 portindex = fullhost.indexOf(':'); 281 } 282 if (portindex != -1) { 283 String portstring = fullhost.substring(portindex + 1); 284 if (portstring.length() > 0) { 285 try { 286 port = Integer.parseInt(portstring); 287 } catch (NumberFormatException nfex) { 288 port = -1; 289 } 290 } 291 292 host = fullhost.substring(0, portindex); 293 } else { 294 host = fullhost; 295 } 296 } else { 297 if (protocolEnd + 1 < len) 298 file = url.substring(protocolEnd + 1); 299 } 300 301 int refStart; 303 if (file != null && (refStart = file.indexOf('#')) != -1) { 304 ref = file.substring(refStart + 1); 305 file = file.substring(0, refStart); 306 } 307 } 308 309 313 public int getPort() { 314 return port; 315 } 316 317 321 public String getProtocol() { 322 return protocol; 323 } 324 325 329 public String getFile() { 330 return file; 331 } 332 333 337 public String getRef() { 338 return ref; 339 } 340 341 345 public String getHost() { 346 return host; 347 } 348 349 353 public String getUsername() { 354 return doEncode ? decode(username) : username; 355 } 356 357 361 public String getPassword() { 362 return doEncode ? decode(password) : password; 363 } 364 365 368 public URL getURL() throws MalformedURLException { 369 return new URL(getProtocol(), getHost(), getPort(), getFile()); 370 } 371 372 393 public boolean equals(Object obj) { 394 if (!(obj instanceof URLName )) 395 return false; 396 URLName u2 = (URLName )obj; 397 398 if (u2.protocol == null || !u2.protocol.equals(protocol)) 400 return false; 401 402 InetAddress a1 = getHostAddress(), a2 = u2.getHostAddress(); 404 if (a1 != null && a2 != null) { 406 if (!a1.equals(a2)) 407 return false; 408 } else if (host != null && u2.host != null) { 410 if (!host.equalsIgnoreCase(u2.host)) 411 return false; 412 } else if (host != u2.host) { 414 return false; 415 } 416 418 if (!(username == u2.username || 420 (username != null && username.equals(u2.username)))) 421 return false; 422 423 426 String f1 = file == null ? "" : file; 428 String f2 = u2.file == null ? "" : u2.file; 429 430 if (!f1.equals(f2)) 431 return false; 432 433 if (port != u2.port) 435 return false; 436 437 return true; 439 } 440 441 444 public int hashCode() { 445 if (hashCode != 0) 446 return hashCode; 447 if (protocol != null) 448 hashCode += protocol.hashCode(); 449 InetAddress addr = getHostAddress(); 450 if (addr != null) 451 hashCode += addr.hashCode(); 452 else if (host != null) 453 hashCode += host.toLowerCase().hashCode(); 454 if (username != null) 455 hashCode += username.hashCode(); 456 if (file != null) 457 hashCode += file.hashCode(); 458 hashCode += port; 459 return hashCode; 460 } 461 462 467 private synchronized InetAddress getHostAddress() { 468 if (hostAddressKnown) 469 return hostAddress; 470 if (host == null) 471 return null; 472 try { 473 hostAddress = InetAddress.getByName(host); 474 } catch (UnknownHostException ex) { 475 hostAddress = null; 476 } 477 hostAddressKnown = true; 478 return hostAddress; 479 } 480 481 503 static BitSet dontNeedEncoding; 504 static final int caseDiff = ('a' - 'A'); 505 506 508 509 static { 510 dontNeedEncoding = new BitSet (256); 511 int i; 512 for (i = 'a'; i <= 'z'; i++) { 513 dontNeedEncoding.set(i); 514 } 515 for (i = 'A'; i <= 'Z'; i++) { 516 dontNeedEncoding.set(i); 517 } 518 for (i = '0'; i <= '9'; i++) { 519 dontNeedEncoding.set(i); 520 } 521 522 dontNeedEncoding.set(' '); 523 dontNeedEncoding.set('-'); 524 dontNeedEncoding.set('_'); 525 dontNeedEncoding.set('.'); 526 dontNeedEncoding.set('*'); 527 } 528 529 535 static String encode(String s) { 536 if (s == null) 537 return null; 538 for (int i = 0; i < s.length(); i++) { 540 int c = (int)s.charAt(i); 541 if (c == ' ' || !dontNeedEncoding.get(c)) 542 return _encode(s); 543 } 544 return s; 545 } 546 547 private static String _encode(String s) { 548 int maxBytesPerChar = 10; 549 StringBuffer out = new StringBuffer (s.length()); 550 ByteArrayOutputStream buf = new ByteArrayOutputStream (maxBytesPerChar); 551 OutputStreamWriter writer = new OutputStreamWriter (buf); 552 553 for (int i = 0; i < s.length(); i++) { 554 int c = (int)s.charAt(i); 555 if (dontNeedEncoding.get(c)) { 556 if (c == ' ') { 557 c = '+'; 558 } 559 out.append((char)c); 560 } else { 561 try { 563 writer.write(c); 564 writer.flush(); 565 } catch(IOException e) { 566 buf.reset(); 567 continue; 568 } 569 byte[] ba = buf.toByteArray(); 570 for (int j = 0; j < ba.length; j++) { 571 out.append('%'); 572 char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16); 573 if (Character.isLetter(ch)) { 576 ch -= caseDiff; 577 } 578 out.append(ch); 579 ch = Character.forDigit(ba[j] & 0xF, 16); 580 if (Character.isLetter(ch)) { 581 ch -= caseDiff; 582 } 583 out.append(ch); 584 } 585 buf.reset(); 586 } 587 } 588 589 return out.toString(); 590 } 591 592 593 616 617 623 static String decode(String s) { 624 if (s == null) 625 return null; 626 if (indexOfAny(s, "+%") == -1) 627 return s; 629 StringBuffer sb = new StringBuffer (); 630 for (int i = 0; i < s.length(); i++) { 631 char c = s.charAt(i); 632 switch (c) { 633 case '+': 634 sb.append(' '); 635 break; 636 case '%': 637 try { 638 sb.append((char)Integer.parseInt( 639 s.substring(i+1,i+3),16)); 640 } catch (NumberFormatException e) { 641 throw new IllegalArgumentException (); 642 } 643 i += 2; 644 break; 645 default: 646 sb.append(c); 647 break; 648 } 649 } 650 String result = sb.toString(); 652 try { 653 byte[] inputBytes = result.getBytes("8859_1"); 654 result = new String (inputBytes); 655 } catch (UnsupportedEncodingException e) { 656 } 658 return result; 659 } 660 661 667 private static int indexOfAny(String s, String any) { 668 return indexOfAny(s, any, 0); 669 } 670 671 private static int indexOfAny(String s, String any, int start) { 672 try { 673 int len = s.length(); 674 for (int i = start; i < len; i++) { 675 if (any.indexOf(s.charAt(i)) >= 0) 676 return i; 677 } 678 return -1; 679 } catch (StringIndexOutOfBoundsException e) { 680 return -1; 681 } 682 } 683 684 757 } 758 | Popular Tags |