URLConnection.java 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /* URLConnection.java -- Abstract superclass for reading from URL's
  2. Copyright (C) 1998, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.net;
  32. import gnu.classpath.SystemProperties;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.io.OutputStream;
  36. import java.security.AllPermission;
  37. import java.security.Permission;
  38. import java.text.ParsePosition;
  39. import java.text.SimpleDateFormat;
  40. import java.util.Collections;
  41. import java.util.Date;
  42. import java.util.List;
  43. import java.util.Locale;
  44. import java.util.Map;
  45. import java.util.StringTokenizer;
  46. /**
  47. * Written using on-line Java Platform 1.2 API Specification, as well
  48. * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
  49. * Status: One guessContentTypeFrom... methods not implemented.
  50. * getContent method assumes content type from response; see comment there.
  51. */
  52. /**
  53. * This class models a connection that retrieves the information pointed
  54. * to by a URL object. This is typically a connection to a remote node
  55. * on the network, but could be a simple disk read.
  56. * <p>
  57. * A URLConnection object is normally created by calling the openConnection()
  58. * method of a URL object. This method is somewhat misnamed because it does
  59. * not actually open the connection. Instead, it return an unconnected
  60. * instance of this object. The caller then has the opportunity to set
  61. * various connection options prior to calling the actual connect() method.
  62. * <p>
  63. * After the connection has been opened, there are a number of methods in
  64. * this class that access various attributes of the data, typically
  65. * represented by headers sent in advance of the actual data itself.
  66. * <p>
  67. * Also of note are the getInputStream and getContent() methods which allow
  68. * the caller to retrieve the actual data from the connection. Note that
  69. * for some types of connections, writing is also allowed. The setDoOutput()
  70. * method must be called prior to connecing in order to enable this, then
  71. * the getOutputStream method called after the connection in order to
  72. * obtain a stream to write the output to.
  73. * <p>
  74. * The getContent() method is of particular note. This method returns an
  75. * Object that encapsulates the data returned. There is no way do determine
  76. * the type of object that will be returned in advance. This is determined
  77. * by the actual content handlers as described in the description of that
  78. * method.
  79. *
  80. * @author Aaron M. Renn (arenn@urbanophile.com)
  81. * @author Warren Levy (warrenl@cygnus.com)
  82. */
  83. public abstract class URLConnection
  84. {
  85. /**
  86. * This is an object that maps filenames to MIME types. The interface
  87. * to do this is implemented by this class, so just create an empty
  88. * instance and store it here.
  89. */
  90. private static FileNameMap fileNameMap;
  91. /**
  92. * This is the ContentHandlerFactory set by the caller, if any
  93. */
  94. private static ContentHandlerFactory factory;
  95. /**
  96. * This is the default value that will be used to determine whether or
  97. * not user interaction should be allowed.
  98. */
  99. private static boolean defaultAllowUserInteraction;
  100. /**
  101. * This is the default flag indicating whether or not to use caches to
  102. * store the data returned from a server
  103. */
  104. private static boolean defaultUseCaches = true;
  105. /**
  106. * Default internal content handler factory.
  107. */
  108. private static ContentHandlerFactory defaultFactory
  109. = new gnu.java.net.DefaultContentHandlerFactory();
  110. /**
  111. * This variable determines whether or not interaction is allowed with
  112. * the user. For example, to prompt for a username and password.
  113. */
  114. protected boolean allowUserInteraction;
  115. /**
  116. * Indicates whether or not a connection has been established to the
  117. * destination specified in the URL
  118. */
  119. protected boolean connected;
  120. /**
  121. * Indicates whether or not input can be read from this URL
  122. */
  123. protected boolean doInput = true;
  124. /**
  125. * Indicates whether or not output can be sent to this URL
  126. */
  127. protected boolean doOutput;
  128. /**
  129. * If this flag is set, the protocol is allowed to cache data whenever
  130. * it can (caching is not guaranteed). If it is not set, the protocol
  131. * must a get a fresh copy of the data.
  132. * <p>
  133. * This field is set by the setUseCaches method and returned by the
  134. * getUseCaches method.
  135. *
  136. * Its default value is that determined by the last invocation of
  137. * setDefaultUseCaches
  138. */
  139. protected boolean useCaches;
  140. /**
  141. * If this value is non-zero, then the connection will only attempt to
  142. * fetch the document pointed to by the URL if the document has been
  143. * modified more recently than the date set in this variable. That date
  144. * should be specified as the number of seconds since 1/1/1970 GMT.
  145. */
  146. protected long ifModifiedSince;
  147. /**
  148. * This is the URL associated with this connection
  149. */
  150. protected URL url;
  151. private static SimpleDateFormat[] dateFormats;
  152. private static boolean dateformats_initialized;
  153. /**
  154. * The connection timeout period.
  155. */
  156. private int connectTimeout;
  157. /**
  158. * The read timeout period.
  159. */
  160. private int readTimeout;
  161. /* Cached ParsePosition, used when parsing dates. */
  162. private ParsePosition position;
  163. /**
  164. * Creates a URL connection to a given URL. A real connection is not made.
  165. * Use <code>connect()</code> to do this.
  166. *
  167. * @param url The Object to create the URL connection to
  168. *
  169. * @see URLConnection#connect()
  170. */
  171. protected URLConnection(URL url)
  172. {
  173. // Set up all our instance variables
  174. this.url = url;
  175. allowUserInteraction = defaultAllowUserInteraction;
  176. useCaches = defaultUseCaches;
  177. }
  178. /**
  179. * Establishes the actual connection to the URL associated with this
  180. * connection object
  181. *
  182. * @exception IOException if an error occurs
  183. */
  184. public abstract void connect() throws IOException;
  185. /**
  186. * Returns the URL object associated with this connection
  187. *
  188. * @return The URL for this connection.
  189. */
  190. public URL getURL()
  191. {
  192. return url;
  193. }
  194. /**
  195. * Returns the connection timeout speed, in milliseconds, or zero if
  196. * the timeout is infinite or not set.
  197. *
  198. * @return The timeout.
  199. *
  200. * @since 1.5
  201. */
  202. public int getConnectTimeout()
  203. {
  204. return connectTimeout;
  205. }
  206. /**
  207. * Set the connection timeout speed, in milliseconds, or zero if the timeout
  208. * is to be considered infinite. Note that in certain socket
  209. * implementations/platforms this method may not have any effect.
  210. *
  211. * Throws an <code>IllegalArgumentException</code> if timeout < 0.
  212. *
  213. * @param timeout the timeout, in milliseconds.
  214. *
  215. * @since 1.5
  216. */
  217. public void setConnectTimeout(int timeout)
  218. throws IllegalArgumentException
  219. {
  220. if( timeout < 0 )
  221. throw new IllegalArgumentException("Timeout must be 0 or positive.");
  222. connectTimeout = timeout;
  223. }
  224. /**
  225. * Returns the read timeout, in milliseconds, or zero if the timeout
  226. * is infinite or not set.
  227. *
  228. * @return The timeout.
  229. *
  230. * @see #setReadTimeout
  231. *
  232. * @since 1.5
  233. */
  234. public int getReadTimeout()
  235. {
  236. return readTimeout;
  237. }
  238. /**
  239. * Set the read timeout, in milliseconds, or zero if the timeout
  240. * is to be considered infinite. Note that in certain socket
  241. * implementations/platforms this method may not have any effect.
  242. *
  243. * Throws an <code>IllegalArgumentException</code> if timeout < 0.
  244. *
  245. * @param timeout - The timeout, in milliseconds.
  246. *
  247. * @throws IllegalArgumentException if timeout is negative.
  248. *
  249. * @see #getReadTimeout
  250. *
  251. * @since 1.5
  252. */
  253. public void setReadTimeout(int timeout)
  254. throws IllegalArgumentException
  255. {
  256. if( timeout < 0 )
  257. throw new IllegalArgumentException("Timeout must be 0 or positive.");
  258. readTimeout = timeout;
  259. }
  260. /**
  261. * Returns the value of the content-length header field or -1 if the value
  262. * is not known or not present.
  263. *
  264. * @return The content-length field
  265. */
  266. public int getContentLength()
  267. {
  268. return getHeaderFieldInt("content-length", -1);
  269. }
  270. /**
  271. * Returns the the content-type of the data pointed to by the URL. This
  272. * method first tries looking for a content-type header. If that is not
  273. * present, it attempts to use the file name to determine the content's
  274. * MIME type. If that is unsuccessful, the method returns null. The caller
  275. * may then still attempt to determine the MIME type by a call to
  276. * guessContentTypeFromStream()
  277. *
  278. * @return The content MIME type
  279. */
  280. public String getContentType()
  281. {
  282. return getHeaderField("content-type");
  283. }
  284. /**
  285. * Returns the value of the content-encoding field or null if it is not
  286. * known or not present.
  287. *
  288. * @return The content-encoding field
  289. */
  290. public String getContentEncoding()
  291. {
  292. return getHeaderField("content-encoding");
  293. }
  294. /**
  295. * Returns the value of the expires header or 0 if not known or present.
  296. * If populated, the return value is number of seconds since midnight
  297. * on 1/1/1970 GMT.
  298. *
  299. * @return The expiration time.
  300. */
  301. public long getExpiration()
  302. {
  303. return getHeaderFieldDate("expires", 0L);
  304. }
  305. /**
  306. * Returns the date of the document pointed to by the URL as reported in
  307. * the date field of the header or 0 if the value is not present or not
  308. * known. If populated, the return value is number of seconds since
  309. * midnight on 1/1/1970 GMT.
  310. *
  311. * @return The document date
  312. */
  313. public long getDate()
  314. {
  315. return getHeaderFieldDate("date", 0L);
  316. }
  317. /**
  318. * Returns the value of the last-modified header field or 0 if not known known
  319. * or not present. If populated, the return value is the number of seconds
  320. * since midnight on 1/1/1970.
  321. *
  322. * @return The last modified time
  323. */
  324. public long getLastModified()
  325. {
  326. return getHeaderFieldDate("last-modified", 0L);
  327. }
  328. /**
  329. * Return a String representing the header value at the specified index.
  330. * This allows the caller to walk the list of header fields. The analogous
  331. * {@link #getHeaderField(int)} method allows access to the corresponding
  332. * key for this header field
  333. *
  334. * @param index The index into the header field list to retrieve the value for
  335. *
  336. * @return The header value or null if index is past the end of the headers
  337. */
  338. public String getHeaderField(int index)
  339. {
  340. // Subclasses for specific protocols override this.
  341. return null;
  342. }
  343. /**
  344. * Returns a String representing the value of the header field having
  345. * the named key. Returns null if the header field does not exist.
  346. *
  347. * @param name The key of the header field
  348. *
  349. * @return The value of the header field as a String
  350. */
  351. public String getHeaderField(String name)
  352. {
  353. // Subclasses for specific protocols override this.
  354. return null;
  355. }
  356. /**
  357. * Returns an unmodifiable Map containing all sent header fields.
  358. *
  359. * @return The map of header fields. The map consists of String keys with
  360. * an unmodifiable List of String objects as value.
  361. *
  362. * @since 1.4
  363. */
  364. public Map<String,List<String>> getHeaderFields()
  365. {
  366. // Subclasses for specific protocols override this.
  367. return Collections.emptyMap();
  368. }
  369. /**
  370. * Returns the value of the named header field as an int. If the field
  371. * is not present or cannot be parsed as an integer, the default value
  372. * will be returned.
  373. *
  374. * @param name The header field key to lookup
  375. * @param defaultValue The defaule value if the header field is not found
  376. * or can't be parsed.
  377. *
  378. * @return The value of the header field or the default value if the field
  379. * is missing or malformed
  380. */
  381. public int getHeaderFieldInt(String name, int defaultValue)
  382. {
  383. String value = getHeaderField(name);
  384. if (value == null)
  385. return defaultValue;
  386. try
  387. {
  388. return Integer.parseInt(value);
  389. }
  390. catch (NumberFormatException e)
  391. {
  392. return defaultValue;
  393. }
  394. }
  395. /**
  396. * Returns the value of the named header field as a date. This date will
  397. * be the number of seconds since midnight 1/1/1970 GMT or the default
  398. * value if the field is not present or cannot be converted to a date.
  399. *
  400. * @param name The name of the header field
  401. * @param defaultValue The default date if the header field is not found
  402. * or can't be converted.
  403. *
  404. * @return The date value of the header filed or the default value
  405. * if the field is missing or malformed
  406. */
  407. public long getHeaderFieldDate(String name, long defaultValue)
  408. {
  409. if (! dateformats_initialized)
  410. initializeDateFormats();
  411. if (position == null)
  412. position = new ParsePosition(0);
  413. long result = defaultValue;
  414. String str = getHeaderField(name);
  415. if (str != null)
  416. {
  417. for (int i = 0; i < dateFormats.length; i++)
  418. {
  419. SimpleDateFormat df = dateFormats[i];
  420. position.setIndex(0);
  421. position.setErrorIndex(0);
  422. Date date = df.parse(str, position);
  423. if (date != null)
  424. return date.getTime();
  425. }
  426. }
  427. return result;
  428. }
  429. /**
  430. * Returns a String representing the header key at the specified index.
  431. * This allows the caller to walk the list of header fields. The analogous
  432. * {@link #getHeaderField(int)} method allows access to the corresponding
  433. * value for this tag.
  434. *
  435. * @param index The index into the header field list to retrieve the key for.
  436. *
  437. * @return The header field key or null if index is past the end
  438. * of the headers.
  439. */
  440. public String getHeaderFieldKey(int index)
  441. {
  442. // Subclasses for specific protocols override this.
  443. return null;
  444. }
  445. /**
  446. * This method returns the content of the document pointed to by the
  447. * URL as an Object. The type of object depends on the MIME type of
  448. * the object and particular content hander loaded. Most text type
  449. * content handlers will return a subclass of
  450. * <code>InputStream</code>. Images usually return a class that
  451. * implements <code>ImageProducer</code>. There is not guarantee
  452. * what type of object will be returned, however.
  453. *
  454. * <p>This class first determines the MIME type of the content, then
  455. * creates a ContentHandler object to process the input. If the
  456. * <code>ContentHandlerFactory</code> is set, then that object is
  457. * called to load a content handler, otherwise a class called
  458. * gnu.java.net.content.&lt;content_type&gt; is tried. If this
  459. * handler does not exist, the method will simple return the
  460. * <code>InputStream</code> returned by
  461. * <code>getInputStream()</code>. Note that the default
  462. * implementation of <code>getInputStream()</code> throws a
  463. * <code>UnknownServiceException</code> so subclasses are encouraged
  464. * to override this method.</p>
  465. *
  466. * @return the content
  467. *
  468. * @exception IOException If an error with the connection occurs.
  469. * @exception UnknownServiceException If the protocol does not support the
  470. * content type at all.
  471. */
  472. public Object getContent() throws IOException
  473. {
  474. if (!connected)
  475. connect();
  476. // FIXME: Doc indicates that other criteria should be applied as
  477. // heuristics to determine the true content type, e.g. see
  478. // guessContentTypeFromName() and guessContentTypeFromStream methods
  479. // as well as FileNameMap class & fileNameMap field & get/set methods.
  480. String type = getContentType();
  481. ContentHandler ch = getContentHandler(type);
  482. if (ch != null)
  483. return ch.getContent(this);
  484. return getInputStream();
  485. }
  486. /**
  487. * Retrieves the content of this URLConnection
  488. *
  489. * @param classes The allowed classes for the content
  490. *
  491. * @return the content
  492. *
  493. * @exception IOException If an error occurs
  494. * @exception UnknownServiceException If the protocol does not support the
  495. * content type
  496. */
  497. public Object getContent(Class[] classes)
  498. throws IOException
  499. {
  500. if (! connected)
  501. connect();
  502. String type = getContentType();
  503. ContentHandler ch = getContentHandler(type);
  504. if (ch != null)
  505. return ch.getContent(this, classes);
  506. throw new UnknownServiceException("protocol does not support the content type");
  507. }
  508. /**
  509. * This method returns a <code>Permission</code> object representing the
  510. * permissions required to access this URL. This method returns
  511. * <code>java.security.AllPermission</code> by default. Subclasses should
  512. * override it to return a more specific permission. For example, an
  513. * HTTP URL should return an instance of <code>SocketPermission</code>
  514. * for the appropriate host and port.
  515. * <p>
  516. * Note that because of items such as HTTP redirects, the permission
  517. * object returned might be different before and after connecting.
  518. *
  519. * @return A Permission object
  520. *
  521. * @exception IOException If the computation of the permission requires
  522. * network or file I/O and an exception occurs while computing it
  523. */
  524. public Permission getPermission() throws IOException
  525. {
  526. // Subclasses may override this.
  527. return new AllPermission();
  528. }
  529. /**
  530. * Returns an InputStream for this connection. As this default
  531. * implementation returns null, subclasses should override this method
  532. *
  533. * @return An InputStream for this connection
  534. *
  535. * @exception IOException If an error occurs
  536. * @exception UnknownServiceException If the protocol does not support input
  537. */
  538. public InputStream getInputStream() throws IOException
  539. {
  540. // Subclasses for specific protocols override this.
  541. throw new UnknownServiceException("Protocol " + url.getProtocol()
  542. + " does not support input.");
  543. }
  544. /**
  545. * Returns an OutputStream for this connection. As this default
  546. * implementation returns null, subclasses should override this method
  547. *
  548. * @return An OutputStream for this connection
  549. *
  550. * @exception IOException If an error occurs
  551. * @exception UnknownServiceException If the protocol does not support output
  552. */
  553. public OutputStream getOutputStream() throws IOException
  554. {
  555. // Subclasses for specific protocols override this.
  556. throw new UnknownServiceException("Protocol " + url.getProtocol()
  557. + " does not support output.");
  558. }
  559. /**
  560. * The methods prints the value of this object as a String by calling the
  561. * toString() method of its associated URL. Overrides Object.toString()
  562. *
  563. * @return A String representation of this object
  564. */
  565. public String toString()
  566. {
  567. return this.getClass().getName() + ":" + url.toString();
  568. }
  569. /**
  570. * Sets the value of a flag indicating whether or not input is going
  571. * to be done for this connection. This default to true unless the
  572. * doOutput flag is set to false, in which case this defaults to false.
  573. *
  574. * @param input <code>true</code> if input is to be done,
  575. * <code>false</code> otherwise
  576. *
  577. * @exception IllegalStateException If already connected
  578. */
  579. public void setDoInput(boolean input)
  580. {
  581. if (connected)
  582. throw new IllegalStateException("Already connected");
  583. doInput = input;
  584. }
  585. /**
  586. * Returns the value of a flag indicating whether or not input is going
  587. * to be done for this connection. This default to true unless the
  588. * doOutput flag is set to false, in which case this defaults to false.
  589. *
  590. * @return true if input is to be done, false otherwise
  591. */
  592. public boolean getDoInput()
  593. {
  594. return doInput;
  595. }
  596. /**
  597. * Sets a boolean flag indicating whether or not output will be done
  598. * on this connection. The default value is false, so this method can
  599. * be used to override the default
  600. *
  601. * @param output ture if output is to be done, false otherwise
  602. *
  603. * @exception IllegalStateException If already connected
  604. */
  605. public void setDoOutput(boolean output)
  606. {
  607. if (connected)
  608. throw new IllegalStateException("Already connected");
  609. doOutput = output;
  610. }
  611. /**
  612. * Returns a boolean flag indicating whether or not output will be done
  613. * on this connection. This defaults to false.
  614. *
  615. * @return true if output is to be done, false otherwise
  616. */
  617. public boolean getDoOutput()
  618. {
  619. return doOutput;
  620. }
  621. /**
  622. * Sets a boolean flag indicating whether or not user interaction is
  623. * allowed for this connection. (For example, in order to prompt for
  624. * username and password info.
  625. *
  626. * @param allow true if user interaction should be allowed, false otherwise.
  627. *
  628. * @exception IllegalStateException If already connected
  629. */
  630. public void setAllowUserInteraction(boolean allow)
  631. {
  632. if (connected)
  633. throw new IllegalStateException("Already connected");
  634. allowUserInteraction = allow;
  635. }
  636. /**
  637. * Returns a boolean flag indicating whether or not user interaction is
  638. * allowed for this connection. (For example, in order to prompt for
  639. * username and password info.
  640. *
  641. * @return true if user interaction is allowed, false otherwise
  642. */
  643. public boolean getAllowUserInteraction()
  644. {
  645. return allowUserInteraction;
  646. }
  647. /**
  648. * Sets the default flag for whether or not interaction with a user
  649. * is allowed. This will be used for all connections unless overridden
  650. *
  651. * @param allow true to allow user interaction, false otherwise
  652. */
  653. public static void setDefaultAllowUserInteraction(boolean allow)
  654. {
  655. defaultAllowUserInteraction = allow;
  656. }
  657. /**
  658. * Returns the default flag for whether or not interaction with a user
  659. * is allowed. This will be used for all connections unless overridden
  660. *
  661. * @return true if user interaction is allowed, false otherwise
  662. */
  663. public static boolean getDefaultAllowUserInteraction()
  664. {
  665. return defaultAllowUserInteraction;
  666. }
  667. /**
  668. * Sets a boolean flag indicating whether or not caching will be used
  669. * (if possible) to store data downloaded via the connection.
  670. *
  671. * @param usecaches The new value
  672. *
  673. * @exception IllegalStateException If already connected
  674. */
  675. public void setUseCaches(boolean usecaches)
  676. {
  677. if (connected)
  678. throw new IllegalStateException("Already connected");
  679. useCaches = usecaches;
  680. }
  681. /**
  682. * Returns a boolean flag indicating whether or not caching will be used
  683. * (if possible) to store data downloaded via the connection.
  684. *
  685. * @return true if caching should be used if possible, false otherwise
  686. */
  687. public boolean getUseCaches()
  688. {
  689. return useCaches;
  690. }
  691. /**
  692. * Sets the ifModified since instance variable. If this value is non
  693. * zero and the underlying protocol supports it, the actual document will
  694. * not be fetched unless it has been modified since this time. The value
  695. * passed should be 0 if this feature is to be disabled or the time expressed
  696. * as the number of seconds since midnight 1/1/1970 GMT otherwise.
  697. *
  698. * @param ifmodifiedsince The new value in milliseconds
  699. * since January 1, 1970 GMT
  700. *
  701. * @exception IllegalStateException If already connected
  702. */
  703. public void setIfModifiedSince(long ifmodifiedsince)
  704. {
  705. if (connected)
  706. throw new IllegalStateException("Already connected");
  707. ifModifiedSince = ifmodifiedsince;
  708. }
  709. /**
  710. * Returns the ifModified since instance variable. If this value is non
  711. * zero and the underlying protocol supports it, the actual document will
  712. * not be fetched unless it has been modified since this time. The value
  713. * returned will be 0 if this feature is disabled or the time expressed
  714. * as the number of seconds since midnight 1/1/1970 GMT otherwise
  715. *
  716. * @return The ifModifiedSince value
  717. */
  718. public long getIfModifiedSince()
  719. {
  720. return ifModifiedSince;
  721. }
  722. /**
  723. * Returns the default value used to determine whether or not caching
  724. * of documents will be done when possible.
  725. *
  726. * @return true if caches will be used, false otherwise
  727. */
  728. public boolean getDefaultUseCaches()
  729. {
  730. return defaultUseCaches;
  731. }
  732. /**
  733. * Sets the default value used to determine whether or not caching
  734. * of documents will be done when possible.
  735. *
  736. * @param use true to use caches if possible by default, false otherwise
  737. */
  738. public void setDefaultUseCaches(boolean use)
  739. {
  740. defaultUseCaches = use;
  741. }
  742. /**
  743. * Sets the value of the named request property.
  744. * This method does overwrite the value of existing properties with
  745. * the new value.
  746. *
  747. * @param key The name of the property
  748. * @param value The value of the property
  749. *
  750. * @exception IllegalStateException If already connected
  751. * @exception NullPointerException If key is null
  752. *
  753. * @see URLConnection#getRequestProperty(String key)
  754. * @see URLConnection#addRequestProperty(String key, String value)
  755. *
  756. * @since 1.4
  757. */
  758. public void setRequestProperty(String key, String value)
  759. {
  760. if (connected)
  761. throw new IllegalStateException("Already connected");
  762. if (key == null)
  763. throw new NullPointerException("key is null");
  764. // Do nothing unless overridden by subclasses that support setting
  765. // header fields in the request.
  766. }
  767. /**
  768. * Adds a new request property by a key/value pair.
  769. * This method does not overwrite existing properties with the same key.
  770. *
  771. * @param key Key of the property to add
  772. * @param value Value of the Property to add
  773. *
  774. * @exception IllegalStateException If already connected
  775. * @exception NullPointerException If key is null
  776. *
  777. * @see URLConnection#getRequestProperty(String)
  778. * @see URLConnection#setRequestProperty(String, String)
  779. *
  780. * @since 1.4
  781. */
  782. public void addRequestProperty(String key, String value)
  783. {
  784. if (connected)
  785. throw new IllegalStateException("Already connected");
  786. if (key == null)
  787. throw new NullPointerException("key is null");
  788. // Do nothing unless overridden by subclasses that support adding
  789. // header fields in the request.
  790. }
  791. /**
  792. * Returns the value of the named request property.
  793. *
  794. * @param key The name of the property
  795. *
  796. * @return Value of the property, or <code>null</code> if key is null.
  797. *
  798. * @exception IllegalStateException If already connected
  799. *
  800. * @see URLConnection#setRequestProperty(String, String)
  801. * @see URLConnection#addRequestProperty(String, String)
  802. */
  803. public String getRequestProperty(String key)
  804. {
  805. if (connected)
  806. throw new IllegalStateException("Already connected");
  807. // Overridden by subclasses that support reading header fields from the
  808. // request.
  809. return null;
  810. }
  811. /**
  812. * Returns an unmodifiable Map containing the request properties.
  813. *
  814. * @return The map of properties. The map consists of String keys with an
  815. * unmodifiable List of String objects as value.
  816. *
  817. * @exception IllegalStateException If already connected
  818. *
  819. * @since 1.4
  820. */
  821. public Map<String,List<String>> getRequestProperties()
  822. {
  823. if (connected)
  824. throw new IllegalStateException("Already connected");
  825. // Overridden by subclasses that support reading header fields from the
  826. // request.
  827. return Collections.emptyMap();
  828. }
  829. /**
  830. * Sets the default value of a request property. This will be used
  831. * for all connections unless the value of the property is manually
  832. * overridden.
  833. *
  834. * @param key The request property name the default is being set for
  835. * @param value The value to set the default to
  836. *
  837. * @deprecated 1.3 The method setRequestProperty should be used instead.
  838. * This method does nothing now.
  839. *
  840. * @see URLConnection#setRequestProperty(String, String)
  841. */
  842. public static void setDefaultRequestProperty(String key, String value)
  843. {
  844. // This method does nothing since JDK 1.3.
  845. }
  846. /**
  847. * Returns the default value of a request property. This will be used
  848. * for all connections unless the value of the property is manually
  849. * overridden.
  850. *
  851. * @param key The request property to return the default value of
  852. *
  853. * @return The value of the default property or null if not available
  854. *
  855. * @deprecated 1.3 The method getRequestProperty should be used instead.
  856. * This method does nothing now.
  857. *
  858. * @see URLConnection#getRequestProperty(String)
  859. */
  860. public static String getDefaultRequestProperty(String key)
  861. {
  862. // This method does nothing since JDK 1.3.
  863. return null;
  864. }
  865. /**
  866. * Sets the ContentHandlerFactory for an application. This can be called
  867. * once and only once. If it is called again, then an Error is thrown.
  868. * Unlike for other set factory methods, this one does not do a security
  869. * check prior to setting the factory.
  870. *
  871. * @param factory The ContentHandlerFactory for this application
  872. *
  873. * @exception Error If the factory has already been defined
  874. * @exception SecurityException If a security manager exists and its
  875. * checkSetFactory method doesn't allow the operation
  876. */
  877. public static synchronized void setContentHandlerFactory(ContentHandlerFactory factory)
  878. {
  879. if (URLConnection.factory != null)
  880. throw new Error("ContentHandlerFactory already set");
  881. // Throw an exception if an extant security mgr precludes
  882. // setting the factory.
  883. SecurityManager s = System.getSecurityManager();
  884. if (s != null)
  885. s.checkSetFactory();
  886. URLConnection.factory = factory;
  887. }
  888. /**
  889. * Returns the MIME type of a file based on the name of the file. This
  890. * works by searching for the file's extension in a list of file extensions
  891. * and returning the MIME type associated with it. If no type is found,
  892. * then a MIME type of "application/octet-stream" will be returned.
  893. *
  894. * @param filename The filename to determine the MIME type for
  895. *
  896. * @return The MIME type String
  897. *
  898. * @specnote public since JDK 1.4
  899. */
  900. public static String guessContentTypeFromName(String filename)
  901. {
  902. return getFileNameMap().getContentTypeFor(filename.toLowerCase());
  903. }
  904. /**
  905. * Returns the MIME type of a stream based on the first few characters
  906. * at the beginning of the stream. This routine can be used to determine
  907. * the MIME type if a server is believed to be returning an incorrect
  908. * MIME type. This method returns "application/octet-stream" if it
  909. * cannot determine the MIME type.
  910. * <p>
  911. * NOTE: Overriding MIME types sent from the server can be obnoxious
  912. * to user's. See Internet Exploder 4 if you don't believe me.
  913. *
  914. * @param is The InputStream to determine the MIME type from
  915. *
  916. * @return The MIME type
  917. *
  918. * @exception IOException If an error occurs
  919. */
  920. public static String guessContentTypeFromStream(InputStream is)
  921. throws IOException
  922. {
  923. String result = VMURLConnection.guessContentTypeFromStream(is);
  924. if (result == null)
  925. return "application/octet-stream";
  926. return result;
  927. }
  928. /**
  929. * This method returns the <code>FileNameMap</code> object being used
  930. * to decode MIME types by file extension.
  931. *
  932. * @return The <code>FileNameMap</code>.
  933. *
  934. * @since 1.2
  935. */
  936. public static synchronized FileNameMap getFileNameMap()
  937. {
  938. // Delayed initialization.
  939. if (fileNameMap == null)
  940. fileNameMap = new MimeTypeMapper();
  941. return fileNameMap;
  942. }
  943. /**
  944. * This method sets the <code>FileNameMap</code> object being used
  945. * to decode MIME types by file extension.
  946. *
  947. * @param map The <code>FileNameMap</code>.
  948. *
  949. * @exception SecurityException If a security manager exists and its
  950. * checkSetFactory method doesn't allow the operation
  951. *
  952. * @since 1.2
  953. */
  954. public static synchronized void setFileNameMap(FileNameMap map)
  955. {
  956. // Throw an exception if an extant security manager precludes
  957. // setting the factory.
  958. SecurityManager s = System.getSecurityManager();
  959. if (s != null)
  960. s.checkSetFactory();
  961. fileNameMap = map;
  962. }
  963. private ContentHandler getContentHandler(String contentType)
  964. {
  965. // No content type so just handle it as the default.
  966. if (contentType == null || contentType.equals(""))
  967. return null;
  968. ContentHandler handler = null;
  969. // If a non-default factory has been set, use it.
  970. if (factory != null)
  971. handler = factory.createContentHandler(contentType);
  972. // Now try default factory. Using this factory to instantiate built-in
  973. // content handlers is preferable
  974. if (handler == null)
  975. handler = defaultFactory.createContentHandler(contentType);
  976. // User-set factory has not returned a handler. Use the default search
  977. // algorithm.
  978. if (handler == null)
  979. {
  980. // Get the list of packages to check and append our default handler
  981. // to it, along with the JDK specified default as a last resort.
  982. // Except in very unusual environments the JDK specified one shouldn't
  983. // ever be needed (or available).
  984. String propVal = SystemProperties.getProperty("java.content.handler.pkgs");
  985. propVal = (((propVal == null) ? "" : (propVal + "|"))
  986. + "gnu.java.net.content|sun.net.www.content");
  987. // Deal with "Content-Type: text/html; charset=ISO-8859-1".
  988. int parameterBegin = contentType.indexOf(';');
  989. if (parameterBegin >= 1)
  990. contentType = contentType.substring(0, parameterBegin);
  991. contentType = contentType.trim();
  992. // Replace the '/' character in the content type with '.' and
  993. // all other non-alphabetic, non-numeric characters with '_'.
  994. char[] cArray = contentType.toCharArray();
  995. for (int i = 0; i < cArray.length; i++)
  996. {
  997. if (cArray[i] == '/')
  998. cArray[i] = '.';
  999. else if (! ((cArray[i] >= 'A' && cArray[i] <= 'Z') ||
  1000. (cArray[i] >= 'a' && cArray[i] <= 'z') ||
  1001. (cArray[i] >= '0' && cArray[i] <= '9')))
  1002. cArray[i] = '_';
  1003. }
  1004. String contentClass = new String(cArray);
  1005. // See if a class of this content type exists in any of the packages.
  1006. StringTokenizer pkgPrefix = new StringTokenizer(propVal, "|");
  1007. do
  1008. {
  1009. String facName = pkgPrefix.nextToken() + "." + contentClass;
  1010. try
  1011. {
  1012. handler =
  1013. (ContentHandler) Class.forName(facName).newInstance();
  1014. }
  1015. catch (Exception e)
  1016. {
  1017. // Can't instantiate; handler still null, go on to next element.
  1018. }
  1019. } while (handler == null && pkgPrefix.hasMoreTokens());
  1020. }
  1021. return handler;
  1022. }
  1023. // We don't put these in a static initializer, because it creates problems
  1024. // with initializer co-dependency: SimpleDateFormat's constructors
  1025. // eventually depend on URLConnection (via the java.text.*Symbols classes).
  1026. private static synchronized void initializeDateFormats()
  1027. {
  1028. if (dateformats_initialized)
  1029. return;
  1030. Locale locale = new Locale("En", "Us", "Unix");
  1031. dateFormats = new SimpleDateFormat[3];
  1032. dateFormats[0] =
  1033. new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'", locale);
  1034. dateFormats[1] =
  1035. new SimpleDateFormat("EEEE, dd-MMM-yy hh:mm:ss 'GMT'", locale);
  1036. dateFormats[2] = new SimpleDateFormat("EEE MMM d hh:mm:ss yyyy", locale);
  1037. dateformats_initialized = true;
  1038. }
  1039. }