| 1 7 8 package java.net; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.util.Hashtable ; 14 import java.util.Date ; 15 import java.util.StringTokenizer ; 16 import java.util.Collections ; 17 import java.util.Map ; 18 import java.util.List ; 19 import java.security.Permission ; 20 import java.security.AccessController ; 21 import sun.security.util.SecurityConstants; 22 23 151 public abstract class URLConnection { 152 153 166 protected URL url; 167 168 181 protected boolean doInput = true; 182 183 196 protected boolean doOutput = false; 197 198 private static boolean defaultAllowUserInteraction = false; 199 200 217 protected boolean allowUserInteraction = defaultAllowUserInteraction; 218 219 private static boolean defaultUseCaches = true; 220 221 236 protected boolean useCaches = defaultUseCaches; 237 238 256 protected long ifModifiedSince = 0; 257 258 263 protected boolean connected = false; 264 265 268 private int connectTimeout; 269 private int readTimeout; 270 271 274 private static FileNameMap fileNameMap; 275 276 279 private static boolean fileNameMapLoaded = false; 280 281 292 public static synchronized FileNameMap getFileNameMap() { 293 if ((fileNameMap == null) && !fileNameMapLoaded) { 294 fileNameMap = sun.net.www.MimeTable.loadTable(); 295 fileNameMapLoaded = true; 296 } 297 298 return new FileNameMap () { 299 private FileNameMap map = fileNameMap; 300 public String getContentTypeFor(String fileName) { 301 return map.getContentTypeFor(fileName); 302 } 303 }; 304 } 305 306 321 public static void setFileNameMap(FileNameMap map) { 322 SecurityManager sm = System.getSecurityManager(); 323 if (sm != null) sm.checkSetFactory(); 324 fileNameMap = map; 325 } 326 327 351 abstract public void connect() throws IOException ; 352 353 373 public void setConnectTimeout(int timeout) { 374 if (timeout < 0) { 375 throw new IllegalArgumentException ("timeout can not be negative"); 376 } 377 connectTimeout = timeout; 378 } 379 380 392 public int getConnectTimeout() { 393 return connectTimeout; 394 } 395 396 416 public void setReadTimeout(int timeout) { 417 if (timeout < 0) { 418 throw new IllegalArgumentException ("timeout can not be negative"); 419 } 420 readTimeout = timeout; 421 } 422 423 434 public int getReadTimeout() { 435 return readTimeout; 436 } 437 438 444 protected URLConnection(URL url) { 445 this.url = url; 446 } 447 448 456 public URL getURL() { 457 return url; 458 } 459 460 467 public int getContentLength() { 468 return getHeaderFieldInt("content-length", -1); 469 } 470 471 478 public String getContentType() { 479 return getHeaderField("content-type"); 480 } 481 482 489 public String getContentEncoding() { 490 return getHeaderField("content-encoding"); 491 } 492 493 501 public long getExpiration() { 502 return getHeaderFieldDate("expires", 0); 503 } 504 505 513 public long getDate() { 514 return getHeaderFieldDate("date", 0); 515 } 516 517 525 public long getLastModified() { 526 return getHeaderFieldDate("last-modified", 0); 527 } 528 529 540 public String getHeaderField(String name) { 541 return null; 542 } 543 544 554 public Map <String ,List <String >> getHeaderFields() { 555 return Collections.EMPTY_MAP; 556 } 557 558 572 public int getHeaderFieldInt(String name, int Default) { 573 String value = getHeaderField(name); 574 try { 575 return Integer.parseInt(value); 576 } catch (Exception e) { } 577 return Default; 578 } 579 580 596 public long getHeaderFieldDate(String name, long Default) { 597 String value = getHeaderField(name); 598 try { 599 return Date.parse(value); 600 } catch (Exception e) { } 601 return Default; 602 } 603 604 613 public String getHeaderFieldKey(int n) { 614 return null; 615 } 616 617 631 public String getHeaderField(int n) { 632 return null; 633 } 634 635 678 public Object getContent() throws IOException { 679 getInputStream(); 683 return getContentHandler().getContent(this); 684 } 685 686 705 public Object getContent(Class [] classes) throws IOException { 706 getInputStream(); 710 return getContentHandler().getContent(this, classes); 711 } 712 713 752 public Permission getPermission() throws IOException { 753 return SecurityConstants.ALL_PERMISSION; 754 } 755 756 771 public InputStream getInputStream() throws IOException { 772 throw new UnknownServiceException ("protocol doesn't support input"); 773 } 774 775 784 public OutputStream getOutputStream() throws IOException { 785 throw new UnknownServiceException ("protocol doesn't support output"); 786 } 787 788 793 public String toString() { 794 return this.getClass().getName() + ":" + url; 795 } 796 797 810 public void setDoInput(boolean doinput) { 811 if (connected) 812 throw new IllegalStateException ("Already connected"); 813 doInput = doinput; 814 } 815 816 824 public boolean getDoInput() { 825 return doInput; 826 } 827 828 840 public void setDoOutput(boolean dooutput) { 841 if (connected) 842 throw new IllegalStateException ("Already connected"); 843 doOutput = dooutput; 844 } 845 846 854 public boolean getDoOutput() { 855 return doOutput; 856 } 857 858 866 public void setAllowUserInteraction(boolean allowuserinteraction) { 867 if (connected) 868 throw new IllegalStateException ("Already connected"); 869 allowUserInteraction = allowuserinteraction; 870 } 871 872 880 public boolean getAllowUserInteraction() { 881 return allowUserInteraction; 882 } 883 884 892 public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) { 893 defaultAllowUserInteraction = defaultallowuserinteraction; 894 } 895 896 908 public static boolean getDefaultAllowUserInteraction() { 909 return defaultAllowUserInteraction; 910 } 911 912 929 public void setUseCaches(boolean usecaches) { 930 if (connected) 931 throw new IllegalStateException ("Already connected"); 932 useCaches = usecaches; 933 } 934 935 943 public boolean getUseCaches() { 944 return useCaches; 945 } 946 947 955 public void setIfModifiedSince(long ifmodifiedsince) { 956 if (connected) 957 throw new IllegalStateException ("Already connected"); 958 ifModifiedSince = ifmodifiedsince; 959 } 960 961 967 public long getIfModifiedSince() { 968 return ifModifiedSince; 969 } 970 971 983 public boolean getDefaultUseCaches() { 984 return defaultUseCaches; 985 } 986 987 994 public void setDefaultUseCaches(boolean defaultusecaches) { 995 defaultUseCaches = defaultusecaches; 996 } 997 998 1014 public void setRequestProperty(String key, String value) { 1015 if (connected) 1016 throw new IllegalStateException ("Already connected"); 1017 if (key == null) 1018 throw new NullPointerException ("key is null"); 1019 } 1020 1021 |