1 28 29 package HTTPClient; 30 31 import java.net.URL; 32 import java.net.ProtocolException; 33 import java.net.UnknownHostException; 34 import java.io.IOException; 35 import java.io.InterruptedIOException; 36 import java.io.InputStream; 37 import java.io.OutputStream; 38 import java.io.BufferedInputStream; 39 import java.io.ByteArrayOutputStream; 40 import java.util.Date; 41 import java.util.Hashtable; 42 import java.util.Enumeration; 43 44 45 91 92 public class HttpURLConnection extends java.net.HttpURLConnection 93 implements GlobalConstants 94 { 95 96 private static Hashtable connections = new Hashtable(); 97 98 99 private HTTPConnection con; 100 101 102 private String resource; 103 104 105 private String method; 106 107 108 private boolean method_set; 109 110 111 private static NVPair[] default_headers = new NVPair[0]; 112 113 114 private NVPair[] headers; 115 116 117 private HTTPResponse resp; 118 119 120 private boolean do_redir; 121 122 123 private static Class redir_mod; 124 125 126 private OutputStream output_stream; 127 128 129 static 130 { 131 try 134 { 135 if (Boolean.getBoolean("HTTPClient.HttpURLConnection.AllowUI")) 136 setDefaultAllowUserInteraction(true); 137 } 138 catch (SecurityException se) 139 { } 140 141 try 143 { redir_mod = Class.forName("HTTPClient.RedirectionModule"); } 144 catch (ClassNotFoundException cnfe) 145 { throw new NoClassDefFoundError(cnfe.getMessage()); } 146 147 try 149 { 150 String agent = System.getProperty("http.agent"); 151 if (agent != null) 152 setDefaultRequestProperty("User-Agent", agent); 153 } 154 catch (SecurityException se) 155 { } 156 } 157 158 159 161 private static String non_proxy_hosts = ""; 162 private static String proxy_host = ""; 163 private static int proxy_port = -1; 164 165 175 public HttpURLConnection(URL url) 176 throws ProtocolNotSuppException, IOException 177 { 178 super(url); 179 180 try 182 { 183 String hosts = System.getProperty("http.nonProxyHosts", ""); 184 if (!hosts.equalsIgnoreCase(non_proxy_hosts)) 185 { 186 connections.clear(); 187 non_proxy_hosts = hosts; 188 String[] list = Util.splitProperty(hosts); 189 for (int idx=0; idx<list.length; idx++) 190 HTTPConnection.dontProxyFor(list[idx]); 191 } 192 } 193 catch (ParseException pe) 194 { throw new IOException(pe.toString()); } 195 catch (SecurityException se) 196 { } 197 198 try 199 { 200 String host = System.getProperty("http.proxyHost", ""); 201 int port = Integer.getInteger("http.proxyPort", -1).intValue(); 202 if (!host.equalsIgnoreCase(proxy_host) || port != proxy_port) 203 { 204 connections.clear(); 205 proxy_host = host; 206 proxy_port = port; 207 HTTPConnection.setProxyServer(host, port); 208 } 209 } 210 catch (SecurityException se) 211 { } 212 213 con = getConnection(url); 215 method = "GET"; 216 method_set = false; 217 resource = url.getFile(); 218 headers = default_headers; 219 do_redir = getFollowRedirects(); 220 output_stream = null; 221 } 222 223 224 233 private HTTPConnection getConnection(URL url) 234 throws ProtocolNotSuppException 235 { 236 238 String php = url.getProtocol() + ":" + url.getHost() + ":" + 239 ((url.getPort() != -1) ? url.getPort() : 240 URI.defaultPort(url.getProtocol())); 241 php = php.toLowerCase(); 242 243 HTTPConnection con = (HTTPConnection) connections.get(php); 244 if (con != null) return con; 245 246 247 249 con = new HTTPConnection(url); 250 connections.put(php, con); 251 252 return con; 253 } 254 255 256 258 265 public void setRequestMethod(String method) throws ProtocolException 266 { 267 if (connected) 268 throw new ProtocolException("Already connected!"); 269 270 if (DebugURLC) 271 System.err.println("URLC: (" + url + ") Setting request method: " + 272 method); 273 274 this.method = method.trim().toUpperCase(); 275 method_set = true; 276 } 277 278 279 284 public String getRequestMethod() 285 { 286 return method; 287 } 288 289 290 295 public int getResponseCode() throws IOException 296 { 297 if (!connected) connect(); 298 299 try 300 { return resp.getStatusCode(); } 301 catch (ModuleException me) 302 { throw new IOException(me.toString()); } 303 } 304 305 306 312 public String getResponseMessage() throws IOException 313 { 314 if (!connected) connect(); 315 316 try 317 { return resp.getReasonLine(); } 318 catch (ModuleException me) 319 { throw new IOException(me.toString()); } 320 } 321 322 323 329 public String getHeaderField(String name) 330 { 331 try 332 { 333 if (!connected) connect(); 334 return resp.getHeader(name); 335 } 336 catch (Exception e) 337 { return null; } 338 } 339 340 341 350 public int getHeaderFieldInt(String name, int def) 351 { 352 try 353 { 354 if (!connected) connect(); 355 return resp.getHeaderAsInt(name); 356 } 357 catch (Exception e) 358 { return def; } 359 } 360 361 362 372 public long getHeaderFieldDate(String name, long def) 373 { 374 try 375 { 376 if (!connected) connect(); 377 return resp.getHeaderAsDate(name).getTime(); 378 } 379 catch (Exception e) 380 { return def; } 381 } 382 383 384 private String[] hdr_keys, hdr_values; 385 386 394 public String getHeaderFieldKey(int n) 395 { 396 if (hdr_keys == null) 397 fill_hdr_arrays(); 398 399 if (n >= 0 && n < hdr_keys.length) 400 return hdr_keys[n]; 401 else 402 return null; 403 } 404 405 406 413 public String getHeaderField(int n) 414 { 415 if (hdr_values == null) 416 fill_hdr_arrays(); 417 418 if (n >= 0 && n < hdr_values.length) 419 return hdr_values[n]; 420 else 421 return null; 422 } 423 424 425 428 private void fill_hdr_arrays() 429 { 430 try 431 { 432 if (!connected) connect(); 433 434 int num = 1; 436 Enumeration enum = resp.listHeaders(); 437 while (enum.hasMoreElements()) 438 { 439 num++; 440 enum.nextElement(); 441 } 442 443 hdr_keys = new String[num]; 445 hdr_values = new String[num]; 446 447 enum = resp.listHeaders(); 449 for (int idx=1; idx<num; idx++) 450 { 451 hdr_keys[idx] = (String) enum.nextElement(); 452 hdr_values[idx] = resp.getHeader(hdr_keys[idx]); 453 } 454 455 hdr_values[0] = resp.getVersion() + " " + resp.getStatusCode() + 457 " " + resp.getReasonLine(); 458 } 459 catch (Exception e) 460 { hdr_keys = hdr_values = new String[0]; } 461 } 462 463 464 472 public InputStream getInputStream() throws IOException 473 { 474 if (!doInput) 475 throw new ProtocolException("Input not enabled! (use setDoInput(true))"); 476 477 if (!connected) connect(); 478 479 InputStream stream; 480 try 481 { stream = resp.getInputStream(); } 482 catch (ModuleException e) 483 { throw new IOException(e.toString()); } 484 485 return stream; 486 } 487 488 489 500 public InputStream getErrorStream() 501 { 502 try 503 { 504 if (!doInput || !connected || resp.getStatusCode() < 300 || 505 resp.getHeaderAsInt("Content-length") <= 0) 506 return null; 507 508 return resp.getInputStream(); 509 } 510 catch (Exception e) 511 { return null; } 512 } 513 514 515 536 public synchronized OutputStream getOutputStream() throws IOException 537 { 538 if (connected) 539 throw new ProtocolException("Already connected!"); 540 541 if (!doOutput) 542 throw new ProtocolException("Output not enabled! (use setDoOutput(true))"); 543 if (!method_set) 544 method = "POST"; 545 else if (method.equals("HEAD") || method.equals("GET") || 546 method.equals("TRACE")) 547 throw new ProtocolException("Method "+method+" does not support output!"); 548 549 if (getRequestProperty("Content-type") == null) 550 setRequestProperty("Content-type", "application/x-www-form-urlencoded"); 551 552 if (output_stream == null) 553 { 554 if (DebugURLC) 555 System.err.println("URLC: (" +url+ ") creating output stream"); 556 557 String cl = getRequestProperty("Content-Length"); 558 if (cl != null) 559 output_stream = new HttpOutputStream(Integer.parseInt(cl)); 560 else 561 { 562 if (getRequestProperty("Content-type").equals( 567 "application/x-www-form-urlencoded")) 568 output_stream = new ByteArrayOutputStream(300); 569 else 570 output_stream = new HttpOutputStream(); 571 } 572 573 if (output_stream instanceof HttpOutputStream) 574 connect(); 575 } 576 577 return output_stream; 578 } 579 580 581 587 public URL getURL() 588 { 589 if (connected) 590 { 591 try 592 { 593 if (resp.getEffectiveURL() != null) 594 return resp.getEffectiveURL(); 595 } 596 catch (Exception e) 597 { return null; } 598 } 599 600 return url; 601 } 602 603 604 609 public void setIfModifiedSince(long time) 610 { 611 super.setIfModifiedSince(time); 612 setRequestProperty("If-Modified-Since", Util.httpDate(new Date(time))); 613 } 614 615 616 622 public void setRequestProperty(String name, String value) 623 { 624 if (DebugURLC) 625 System.err.println("URLC: (" +url+ ") Setting request property: " + 626 name + " : " + value); 627 628 int idx; 629 for (idx=0; idx<headers.length; idx++) 630 { 631 if (headers[idx].getName().equalsIgnoreCase(name)) 632 break; 633 } 634 635 if (idx == headers.length) 636 headers = Util.resizeArray(headers, idx+1); 637 638 headers[idx] = new NVPair(name, value); 639 } 640 641 642 648 public String getRequestProperty(String name) 649 { 650 for (int idx=0; idx<headers.length; idx++) 651 { 652 if (headers[idx].getName().equalsIgnoreCase(name)) 653 return headers[idx].getValue(); 654 } 655 656 return null; 657 } 658 659 660 667 public static void setDefaultRequestProperty(String name, String value) 668 { 669 if (DebugURLC) 670 System.err.println("URLC: Setting default request property: " + 671 name + " : " + value); 672 673 int idx; 674 for (idx=0; idx<default_headers.length; idx++) 675 { 676 if (default_headers[idx].getName().equalsIgnoreCase(name)) 677 break; 678 } 679 680 if (idx == default_headers.length) 681 default_headers = Util.resizeArray(default_headers, idx+1); 682 683 default_headers[idx] = new NVPair(name, value); 684 } 685 686 687 693 public static String getDefaultRequestProperty(String name) 694 { 695 for (int idx=0; idx<default_headers.length; idx++) 696 { 697 if (default_headers[idx].getName().equalsIgnoreCase(name)) 698 return default_headers[idx].getValue(); 699 } 700 701 return null; 702 } 703 704 705 711 public void setInstanceFollowRedirects(boolean set) 712 { 713 if (connected) 714 throw new IllegalStateException("Already connected!"); 715 716 do_redir = set; 717 } 718 719 720 724 public boolean getInstanceFollowRedirects() 725 { 726 return do_redir; 727 } 728 729 730 734 public synchronized void connect() throws IOException 735 { 736 if (connected) return; 737 738 if (DebugURLC) 739 System.err.println("URLC: (" + url + ") Connecting ..."); 740 741 743 synchronized(con) 744 { 745 con.setAllowUserInteraction(allowUserInteraction); 746 if (do_redir) 747 con.addModule(redir_mod, 2); 748 else 749 con.removeModule(redir_mod); 750 751 try 752 { 753 if (output_stream instanceof ByteArrayOutputStream) 754 resp = con.ExtensionMethod(method, resource, 755 ((ByteArrayOutputStream) output_stream).toByteArray(), 756 headers); 757 else 758 resp = con.ExtensionMethod(method, resource, 759 (HttpOutputStream) output_stream, headers); 760 } 761 catch (ModuleException e) 762 { throw new IOException(e.toString()); } 763 } 764 765 connected = true; 766 } 767 768 769 772 public void disconnect() 773 { 774 if (DebugURLC) 775 System.err.println("URLC: (" + url + ") Disconnecting ..."); 776 777 con.stop(); 778 } 779 780 781 786 public boolean usingProxy() 787 { 788 return (con.getProxyHost() != null); 789 } 790 791 792 796 public String toString() 797 { 798 return getClass().getName() + "[" + url + "]"; 799 } 800 } 801 802 | Popular Tags |