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 1034 public void addRequestProperty(String key, String value) { 1035 if (connected) 1036 throw new IllegalStateException ("Already connected"); 1037 if (key == null) 1038 throw new NullPointerException ("key is null"); 1039 } 1040 1041 1042 1052 public String getRequestProperty(String key) { 1053 if (connected) 1054 throw new IllegalStateException ("Already connected"); 1055 return null; 1056 } 1057 1058 1070 public Map <String ,List <String >> getRequestProperties() { 1071 if (connected) 1072 throw new IllegalStateException ("Already connected"); 1073 return Collections.EMPTY_MAP; 1074 } 1075 1076 1093 @Deprecated 1094 public static void setDefaultRequestProperty(String key, String value) { 1095 } 1096 1097 1113 @Deprecated 1114 public static String getDefaultRequestProperty(String key) { 1115 return null; 1116 } 1117 1118 1121 static ContentHandlerFactory factory; 1122 1123 1143 public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) { 1144 if (factory != null) { 1145 throw new Error ("factory already defined"); 1146 } 1147 SecurityManager security = System.getSecurityManager(); 1148 if (security != null) { 1149 security.checkSetFactory(); 1150 } 1151 factory = fac; 1152 } 1153 1154 private static Hashtable handlers = new Hashtable (); 1155 private static final ContentHandler UnknownContentHandlerP = new UnknownContentHandler(); 1156 1157 1161 synchronized ContentHandler getContentHandler() 1162 throws UnknownServiceException 1163 { 1164 String contentType = stripOffParameters(getContentType()); 1165 ContentHandler handler = null; 1166 if (contentType == null) 1167 throw new UnknownServiceException ("no content-type"); 1168 try { 1169 handler = (ContentHandler ) handlers.get(contentType); 1170 if (handler != null) 1171 return handler; 1172 } catch(Exception e) { 1173 } 1174 1175 if (factory != null) 1176 handler = factory.createContentHandler(contentType); 1177 if (handler == null) { 1178 try { 1179 handler = lookupContentHandlerClassFor(contentType); 1180 } catch(Exception e) { 1181 e.printStackTrace(); 1182 handler = UnknownContentHandlerP; 1183 } 1184 handlers.put(contentType, handler); 1185 } 1186 return handler; 1187 } 1188 1189 1194 private String stripOffParameters(String contentType) 1195 { 1196 if (contentType == null) 1197 return null; 1198 int index = contentType.indexOf(';'); 1199 1200 if (index > 0) 1201 return contentType.substring(0, index); 1202 else 1203 return contentType; 1204 } 1205 1206 private static final String contentClassPrefix = "sun.net.www.content"; 1207 private static final String contentPathProp = "java.content.handler.pkgs"; 1208 1209 1221 private ContentHandler lookupContentHandlerClassFor(String contentType) 1222 throws InstantiationException , IllegalAccessException , ClassNotFoundException { 1223 String contentHandlerClassName = typeToPackageName(contentType); 1224 1225 String contentHandlerPkgPrefixes =getContentHandlerPkgPrefixes(); 1226 1227 StringTokenizer packagePrefixIter = 1228 new StringTokenizer (contentHandlerPkgPrefixes, "|"); 1229 1230 while (packagePrefixIter.hasMoreTokens()) { 1231 String packagePrefix = packagePrefixIter.nextToken().trim(); 1232 1233 try { 1234 String clsName = packagePrefix + "." + contentHandlerClassName; 1235 Class cls = null; 1236 try { 1237 cls = Class.forName(clsName); 1238 } catch (ClassNotFoundException e) { 1239 ClassLoader cl = ClassLoader.getSystemClassLoader(); 1240 if (cl != null) { 1241 cls = cl.loadClass(clsName); 1242 } 1243 } 1244 if (cls != null) { 1245 ContentHandler handler = 1246 (ContentHandler )cls.newInstance(); 1247 return handler; 1248 } 1249 } catch(Exception e) { 1250 } 1251 } 1252 1253 return UnknownContentHandlerP; 1254 } 1255 1256 1261 private String typeToPackageName(String contentType) { 1262 contentType = contentType.toLowerCase(); 1264 int len = contentType.length(); 1265 char nm[] = new char[len]; 1266 contentType.getChars(0, len, nm, 0); 1267 for (int i = 0; i < len; i++) { 1268 char c = nm[i]; 1269 if (c == '/') { 1270 nm[i] = '.'; 1271 } else if (!('A' <= c && c <= 'Z' || 1272 'a' <= c && c <= 'z' || 1273 '0' <= c && c <= '9')) { 1274 nm[i] = '_'; 1275 } 1276 } 1277 return new String (nm); 1278 } 1279 1280 1281 1288 private String getContentHandlerPkgPrefixes() { 1289 String packagePrefixList = (String ) AccessController.doPrivileged( 1290 new sun.security.action.GetPropertyAction(contentPathProp, "")); 1291 1292 if (packagePrefixList != "") { 1293 packagePrefixList += "|"; 1294 } 1295 1296 return packagePrefixList + contentClassPrefix; 1297 } 1298 1299 1310 public static String guessContentTypeFromName(String fname) { 1311 return getFileNameMap().getContentTypeFor(fname); 1312 } 1313 1314 1335 static public String guessContentTypeFromStream(InputStream is) 1336 throws IOException { 1337 if (!is.markSupported()) 1339 return null; 1340 1341 is.mark(12); 1342 int c1 = is.read(); 1343 int c2 = is.read(); 1344 int c3 = is.read(); 1345 int c4 = is.read(); 1346 int c5 = is.read(); 1347 int c6 = is.read(); 1348 int c7 = is.read(); 1349 int c8 = is.read(); 1350 int c9 = is.read(); 1351 int c10 = is.read(); 1352 int c11 = is.read(); 1353 is.reset(); 1354 1355 if (c1 == 0xCA && c2 == 0xFE && c3 == 0xBA && c4 == 0xBE) { 1356 return "application/java-vm"; 1357 } 1358 1359 if (c1 == 0xAC && c2 == 0xED) { 1360 return "application/x-java-serialized-object"; 1362 } 1363 1364 if (c1 == '<') { 1365 if (c2 == '!' 1366 || ((c2 == 'h' && (c3 == 't' && c4 == 'm' && c5 == 'l' || 1367 c3 == 'e' && c4 == 'a' && c5 == 'd') || 1368 (c2 == 'b' && c3 == 'o' && c4 == 'd' && c5 == 'y'))) || 1369 ((c2 == 'H' && (c3 == 'T' && c4 == 'M' && c5 == 'L' || 1370 c3 == 'E' && c4 == 'A' && c5 == 'D') || 1371 (c2 == 'B' && c3 == 'O' && c4 == 'D' && c5 == 'Y')))) { 1372 return "text/html"; 1373 } 1374 1375 if (c2 == '?' && c3 == 'x' && c4 == 'm' && c5 == 'l' && c6 == ' ') { 1376 return "application/xml"; 1377 } 1378 } 1379 1380 if (c1 == 0xfe && c2 == 0xff) { 1382 if (c3 == 0 && c4 == '<' && c5 == 0 && c6 == '?' && 1383 c7 == 0 && c8 == 'x') { 1384 return "application/xml"; 1385 } 1386 } 1387 1388 if (c1 == 0xff && c2 == 0xfe) { 1389 if (c3 == '<' && c4 == 0 && c5 == '?' && c6 == 0 && 1390 c7 == 'x' && c8 == 0) { 1391 return "application/xml"; 1392 } 1393 } 1394 1395 if (c1 == 'G' && c2 == 'I' && c3 == 'F' && c4 == '8') { 1396 return "image/gif"; 1397 } 1398 1399 if (c1 == '#' && c2 == 'd' && c3 == 'e' && c4 == 'f') { 1400 return "image/x-bitmap"; 1401 } 1402 1403 if (c1 == '!' && c2 == ' ' && c3 == 'X' && c4 == 'P' && 1404 c5 == 'M' && c6 == '2') { 1405 return "image/x-pixmap"; 1406 } 1407 1408 if (c1 == 137 && c2 == 80 && c3 == 78 && 1409 c4 == 71 && c5 == 13 && c6 == 10 && 1410 c7 == 26 && c8 == 10) { 1411 return "image/png"; 1412 } 1413 1414 if (c1 == 0xFF && c2 == 0xD8 && c3 == 0xFF) { 1415 if (c4 == 0xE0) { 1416 return "image/jpeg"; 1417 } 1418 1419 1425 if ((c4 == 0xE1) && 1426 (c7 == 'E' && c8 == 'x' && c9 == 'i' && c10 =='f' && 1427 c11 == 0)) { 1428 return "image/jpeg"; 1429 } 1430 1431 if (c4 == 0xEE) { 1432 return "image/jpg"; 1433 } 1434 } 1435 1436 if (c1 == 0xD0 && c2 == 0xCF && c3 == 0x11 && c4 == 0xE0 && 1437 c5 == 0xA1 && c6 == 0xB1 && c7 == 0x1A && c8 == 0xE1) { 1438 1439 1443 if (checkfpx(is)) { 1444 return "image/vnd.fpx"; 1445 } 1446 } 1447 1448 if (c1 == 0x2E && c2 == 0x73 && c3 == 0x6E && c4 == 0x64) { 1449 return "audio/basic"; } 1451 1452 if (c1 == 0x64 && c2 == 0x6E && c3 == 0x73 && c4 == 0x2E) { 1453 return "audio/basic"; } 1455 1456 if (c1 == 'R' && c2 == 'I' && c3 == 'F' && c4 == 'F') { 1457 1460 return "audio/x-wav"; 1461 } 1462 return null; 1463 } 1464 1465 1471 static private boolean checkfpx(InputStream is) throws IOException { 1472 1473 1496 1497 is.mark(0x100); 1502 1503 long toSkip = (long)0x1C; 1506 long posn; 1507 1508 if ((posn = skipForward(is, toSkip)) < toSkip) { 1509 is.reset(); 1510 return false; 1511 } 1512 1513 int c[] = new int[16]; 1514 if (readBytes(c, 2, is) < 0) { 1515 is.reset(); 1516 return false; 1517 } 1518 1519 int byteOrder = c[0]; 1520 1521 posn+=2; 1522 int uSectorShift; 1523 if (readBytes(c, 2, is) < 0) { 1524 is.reset(); 1525 return false; 1526 } 1527 1528 if(byteOrder == 0xFE) { 1529 uSectorShift = c[0]; 1530 uSectorShift += c[1] << 8; 1531 } 1532 else { 1533 uSectorShift = c[0] << 8; 1534 uSectorShift += c[1]; 1535 } 1536 1537 posn += 2; 1538 toSkip = (long)0x30 - posn; 1539 long skipped = 0; 1540 if ((skipped = skipForward(is, toSkip)) < toSkip) { 1541 is.reset(); 1542 return false; 1543 } 1544 posn += skipped; 1545 1546 if (readBytes(c, 4, is) < 0) { 1547 is.reset(); 1548 return false; 1549 } 1550 1551 int sectDirStart; 1552 if(byteOrder == 0xFE) { 1553 sectDirStart = c[0]; 1554 sectDirStart += c[1] << 8; 1555 sectDirStart += c[2] << 16; 1556 sectDirStart += c[3] << 24; 1557 } else { 1558 sectDirStart = c[0] << 24; 1559 sectDirStart += c[1] << 16; 1560 sectDirStart += c[2] << 8; 1561 sectDirStart += c[3]; 1562 } 1563 posn += 4; 1564 is.reset(); 1566 toSkip = (long)0x200 + 1567 (long)((int)1<<uSectorShift)*sectDirStart + (long)0x50; 1568 1569 if (toSkip < 0) { 1571 return false; 1572 } 1573 1574 1580 is.mark((int)toSkip+0x30); 1581 1582 if ((skipForward(is, toSkip)) < toSkip) { 1583 is.reset(); 1584 return false; 1585 } 1586 1587 1598 1599 if (readBytes(c, 16, is) < 0) { 1600 is.reset(); 1601 return false; 1602 } 1603 1604 if (byteOrder == 0xFE && 1606 c[0] == 0x00 && c[2] == 0x61 && c[3] == 0x56 && 1607 c[4] == 0x54 && c[5] == 0xC1 && c[6] == 0xCE && 1608 c[7] == 0x11 && c[8] == 0x85 && c[9] == 0x53 && 1609 c[10]== 0x00 && c[11]== 0xAA && c[12]== 0x00 && 1610 c[13]== 0xA1 && c[14]== 0xF9 && c[15]== 0x5B) { 1611 is.reset(); 1612 return true; 1613 } 1614 1615 else if (c[3] == 0x00 && c[1] == 0x61 && c[0] == 0x56 && 1617 c[5] == 0x54 && c[4] == 0xC1 && c[7] == 0xCE && 1618 c[6] == 0x11 && c[8] == 0x85 && c[9] == 0x53 && 1619 c[10]== 0x00 && c[11]== 0xAA && c[12]== 0x00 && 1620 c[13]== 0xA1 && c[14]== 0xF9 && c[15]== 0x5B) { 1621 is.reset(); 1622 return true; 1623 } 1624 is.reset(); 1625 return false; 1626 } 1627 1628 1633 static private int readBytes(int c[], int len, InputStream is) 1634 throws IOException { 1635 1636 byte buf[] = new byte[len]; 1637 if (is.read(buf, 0, len) < len) { 1638 return -1; 1639 } 1640 1641 for (int i = 0; i < len; i++) { 1643 c[i] = buf[i] & 0xff; 1644 } 1645 return 0; 1646 } 1647 1648 1649 1654 static private long skipForward(InputStream is, long toSkip) 1655 throws IOException { 1656 1657 long eachSkip = 0; 1658 long skipped = 0; 1659 1660 while (skipped != toSkip) { 1661 eachSkip = is.skip(toSkip - skipped); 1662 1663 if (eachSkip <= 0) { 1665 if (is.read() == -1) { 1666 return skipped ; 1667 } else { 1668 skipped++; 1669 } 1670 } 1671 skipped += eachSkip; 1672 } 1673 return skipped; 1674 } 1675 1676} 1677 1678 1679class UnknownContentHandler extends ContentHandler { 1680 public Object getContent(URLConnection uc) throws IOException { 1681 return uc.getInputStream(); 1682 } 1683} 1684 | Popular Tags |