1 31 32 package org.apache.commons.httpclient; 33 34 import org.apache.commons.httpclient.util.URIUtil; 35 36 42 public class HttpURL extends URI { 43 44 46 47 protected HttpURL() { 48 } 49 50 51 61 public HttpURL(char[] escaped, String charset) 62 throws URIException, NullPointerException { 63 protocolCharset = charset; 64 parseUriReference(new String (escaped), true); 65 checkValid(); 66 } 67 68 69 77 public HttpURL(char[] escaped) throws URIException, NullPointerException { 78 parseUriReference(new String (escaped), true); 79 checkValid(); 80 } 81 82 83 92 public HttpURL(String original, String charset) throws URIException { 93 protocolCharset = charset; 94 parseUriReference(original, false); 95 checkValid(); 96 } 97 98 99 106 public HttpURL(String original) throws URIException { 107 parseUriReference(original, false); 108 checkValid(); 109 } 110 111 112 121 public HttpURL(String host, int port, String path) throws URIException { 122 this(null, host, port, path, null, null); 123 checkValid(); 124 } 125 126 127 137 public HttpURL(String host, int port, String path, String query) 138 throws URIException { 139 140 this(null, host, port, path, query, null); 141 checkValid(); 142 } 143 144 145 154 public HttpURL(String user, String password, String host) 155 throws URIException { 156 157 this((user == null) ? null : user 158 + ((password == null) ? "" : ':' + password), 159 host, -1, null, null, null); 160 checkValid(); 161 } 162 163 164 174 public HttpURL(String user, String password, String host, int port) 175 throws URIException { 176 177 this((user == null) ? null : user 178 + ((password == null) ? "" : ':' + password), 179 host, port, null, null, null); 180 checkValid(); 181 } 182 183 184 195 public HttpURL(String user, String password, String host, int port, 196 String path) throws URIException { 197 198 this((user == null) ? null : user 199 + ((password == null) ? "" : ':' + password), 200 host, port, path, null, null); 201 checkValid(); 202 } 203 204 205 217 public HttpURL(String user, String password, String host, int port, 218 String path, String query) throws URIException { 219 220 this((user == null) ? null : user 221 + ((password == null) ? "" : ':' + password), 222 host, port, path, query, null); 223 checkValid(); 224 } 225 226 227 237 public HttpURL(String host, String path, String query, String fragment) 238 throws URIException { 239 240 this(null, host, -1, path, query, fragment); 241 checkValid(); 242 } 243 244 245 256 public HttpURL(String userinfo, String host, String path, String query, 257 String fragment) throws URIException { 258 259 this(userinfo, host, -1, path, query, fragment); 260 checkValid(); 261 } 262 263 264 274 public HttpURL(String userinfo, String host, int port, String path) 275 throws URIException { 276 277 this(userinfo, host, port, path, null, null); 278 checkValid(); 279 } 280 281 282 293 public HttpURL(String userinfo, String host, int port, String path, 294 String query) throws URIException { 295 296 this(userinfo, host, port, path, query, null); 297 checkValid(); 298 } 299 300 301 313 public HttpURL(String userinfo, String host, int port, String path, 314 String query, String fragment) throws URIException { 315 316 StringBuffer buff = new StringBuffer (); 318 if (userinfo != null || host != null || port != -1) { 319 _scheme = DEFAULT_SCHEME; buff.append(_default_scheme); 321 buff.append("://"); 322 if (userinfo != null) { 323 buff.append(URIUtil.encode(userinfo, URI.allowed_userinfo)); 324 buff.append('@'); 325 } 326 if (host != null) { 327 buff.append(URIUtil.encode(host, URI.allowed_host)); 328 if (port != -1 || port != DEFAULT_PORT) { 329 buff.append(':'); 330 buff.append(port); 331 } 332 } 333 } 334 if (path != null) { if (scheme != null && !path.startsWith("/")) { 336 throw new URIException(URIException.PARSING, 337 "abs_path requested"); 338 } 339 buff.append(URIUtil.encode(path, URI.allowed_abs_path)); 340 } 341 if (query != null) { 342 buff.append('?'); 343 buff.append(URIUtil.encode(query, URI.allowed_query)); 344 } 345 if (fragment != null) { 346 buff.append('#'); 347 buff.append(URIUtil.encode(fragment, URI.allowed_fragment)); 348 } 349 parseUriReference(buff.toString(), true); 350 checkValid(); 351 } 352 353 354 361 public HttpURL(HttpURL base, String relative) throws URIException { 362 this(base, new HttpURL(relative)); 363 } 364 365 366 373 public HttpURL(HttpURL base, HttpURL relative) throws URIException { 374 super(base, relative); 375 checkValid(); 376 } 377 378 380 383 public static final char[] DEFAULT_SCHEME = { 'h', 't', 't', 'p' }; 384 385 390 public static final char[] _default_scheme = DEFAULT_SCHEME; 391 392 395 public static final int DEFAULT_PORT = 80; 396 397 402 public static final int _default_port = DEFAULT_PORT; 403 404 407 static final long serialVersionUID = -7158031098595039459L; 408 409 411 416 public char[] getRawScheme() { 417 return (_scheme == null) ? null : HttpURL.DEFAULT_SCHEME; 418 } 419 420 421 426 public String getScheme() { 427 return (_scheme == null) ? null : new String (HttpURL.DEFAULT_SCHEME); 428 } 429 430 432 436 public int getPort() { 437 return (_port == -1) ? HttpURL.DEFAULT_PORT : _port; 438 } 439 440 442 450 public void setRawUserinfo(char[] escapedUser, char[] escapedPassword) 451 throws URIException { 452 453 if (escapedUser == null || escapedUser.length == 0) { 454 throw new URIException(URIException.PARSING, "user required"); 455 } 456 if (!validate(escapedUser, within_userinfo) 457 || ((escapedPassword != null) 458 && !validate(escapedPassword, within_userinfo))) { 459 throw new URIException(URIException.ESCAPING, 460 "escaped userinfo not valid"); 461 } 462 String username = new String (escapedUser); 463 String password = (escapedPassword == null) 464 ? null : new String (escapedPassword); 465 String userinfo = username + ((password == null) ? "" : ":" + password); 466 String hostname = new String (getRawHost()); 467 String hostport = (_port == -1) ? hostname : hostname + ":" + _port; 468 String authority = userinfo + "@" + hostport; 469 _userinfo = userinfo.toCharArray(); 470 _authority = authority.toCharArray(); 471 setURI(); 472 } 473 474 475 484 public void setEscapedUserinfo(String escapedUser, String escapedPassword) 485 throws URIException, NullPointerException { 486 487 setRawUserinfo(escapedUser.toCharArray(), (escapedPassword == null) 488 ? null : escapedPassword.toCharArray()); 489 } 490 491 492 500 public void setUserinfo(String user, String password) 501 throws URIException, NullPointerException { 502 String charset = getProtocolCharset(); 504 setRawUserinfo(encode(user, within_userinfo, charset), 505 (password == null) 506 ? null 507 : encode(password, within_userinfo, charset)); 508 } 509 510 511 517 public void setRawUser(char[] escapedUser) throws URIException { 518 if (escapedUser == null || escapedUser.length == 0) { 519 throw new URIException(URIException.PARSING, "user required"); 520 } 521 if (!validate(escapedUser, within_userinfo)) { 522 throw new URIException(URIException.ESCAPING, 523 "escaped user not valid"); 524 } 525 String username = new String (escapedUser); 526 String password = new String (getRawPassword()); 527 String userinfo = username + ((password == null) ? "" : ":" + password); 528 String hostname = new String (getRawHost()); 529 String hostport = (_port == -1) ? hostname : hostname + ":" + _port; 530 String authority = userinfo + "@" + hostport; 531 _userinfo = userinfo.toCharArray(); 532 _authority = authority.toCharArray(); 533 setURI(); 534 } 535 536 537 544 public void setEscapedUser(String escapedUser) 545 throws URIException, NullPointerException { 546 setRawUser(escapedUser.toCharArray()); 547 } 548 549 550 557 public void setUser(String user) throws URIException, NullPointerException { 558 setRawUser(encode(user, allowed_within_userinfo, getProtocolCharset())); 559 } 560 561 562 567 public char[] getRawUser() { 568 if (_userinfo == null || _userinfo.length == 0) { 569 return null; 570 } 571 int to = indexFirstOf(_userinfo, ':'); 572 if (to == -1) { 574 return _userinfo; } 576 char[] result = new char[to]; 577 System.arraycopy(_userinfo, 0, result, 0, to); 578 return result; 579 } 580 581 582 587 public String getEscapedUser() { 588 char[] user = getRawUser(); 589 return (user == null) ? null : new String (user); 590 } 591 592 593 599 public String getUser() throws URIException { 600 char[] user = getRawUser(); 601 return (user == null) ? null : decode(user, getProtocolCharset()); 602 } 603 604 605 611 public void setRawPassword(char[] escapedPassword) throws URIException { 612 if (escapedPassword != null 613 && !validate(escapedPassword, within_userinfo)) { 614 throw new URIException(URIException.ESCAPING, 615 "escaped password not valid"); 616 } 617 if (getRawUser() == null || getRawUser().length == 0) { 618 throw new URIException(URIException.PARSING, "username required"); 619 } 620 String username = new String (getRawUser()); 621 String password = new String (escapedPassword); 622 String userinfo = username + ((password == null) ? "" : ":" + password); 624 String hostname = new String (getRawHost()); 625 String hostport = (_port == -1) ? hostname : hostname + ":" + _port; 626 String authority = userinfo + "@" + hostport; 627 _userinfo = userinfo.toCharArray(); 628 _authority = authority.toCharArray(); 629 setURI(); 630 } 631 632 633 639 public void setEscapedPassword(String escapedPassword) throws URIException { 640 setRawPassword((escapedPassword == null) ? null 641 : escapedPassword.toCharArray()); 642 } 643 644 645 651 public void setPassword(String password) throws URIException { 652 setRawPassword((password == null) ? null : encode(password, 653 allowed_within_userinfo, getProtocolCharset())); 654 } 655 656 657 662 public char[] getRawPassword() { 663 int from = indexFirstOf(_userinfo, ':'); 664 if (from == -1) { 665 return null; } 667 int len = _userinfo.length - from - 1; 668 char[] result = new char[len]; 669 System.arraycopy(_userinfo, from + 1, result, 0, len); 670 return result; 671 } 672 673 674 679 public String getEscapedPassword() { 680 char[] password = getRawPassword(); 681 return (password == null) ? null : new String (password); 682 } 683 684 685 691 public String getPassword() throws URIException { 692 char[] password = getRawPassword(); 693 return (password == null) ? null : decode(password, 694 getProtocolCharset()); 695 } 696 697 699 705 public char[] getRawCurrentHierPath() throws URIException { 706 return (_path == null || _path.length == 0) ? rootPath 707 : super.getRawCurrentHierPath(_path); 708 } 709 710 711 717 public char[] getRawAboveHierPath() throws URIException { 718 char[] path = getRawCurrentHierPath(); 719 return (path == null || path.length == 0) ? rootPath : getRawCurrentHierPath(path); 720 } 721 722 723 728 public char[] getRawPath() { 729 char[] path = super.getRawPath(); 730 return (path == null || path.length == 0) ? rootPath : path; 731 } 732 733 735 745 public void setQuery(String queryName, String queryValue) 746 throws URIException, NullPointerException { 747 748 StringBuffer buff = new StringBuffer (); 749 String charset = getProtocolCharset(); 751 buff.append(encode(queryName, allowed_within_query, charset)); 752 buff.append('='); 753 buff.append(encode(queryValue, allowed_within_query, charset)); 754 _query = buff.toString().toCharArray(); 755 setURI(); 756 } 757 758 759 769 public void setQuery(String [] queryName, String [] queryValue) 770 throws URIException, NullPointerException { 771 772 int length = queryName.length; 773 if (length != queryValue.length) { 774 throw new URIException("wrong array size of query"); 775 } 776 777 StringBuffer buff = new StringBuffer (); 778 String charset = getProtocolCharset(); 780 for (int i = 0; i < length; i++) { 781 buff.append(encode(queryName[i], allowed_within_query, charset)); 782 buff.append('='); 783 buff.append(encode(queryValue[i], allowed_within_query, charset)); 784 if (i + 1 < length) { 785 buff.append('&'); 786 } 787 } 788 _query = buff.toString().toCharArray(); 789 setURI(); 790 } 791 792 794 799 protected void checkValid() throws URIException { 800 if (!(equals(_scheme, DEFAULT_SCHEME) || _scheme == null)) { 802 throw new URIException(URIException.PARSING, "wrong class use"); 803 } 804 } 805 806 } 807 808 | Popular Tags |