1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.activation;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.IOException;
  11. /**
  12. * The URLDataSource class provides an object that wraps a <code>URL</code>
  13. * object in a DataSource interface. URLDataSource simplifies the handling
  14. * of data described by URLs within the JavaBeans Activation Framework
  15. * because this class can be used to create new DataHandlers. <i>NOTE: The
  16. * DataHandler object creates a URLDataSource internally,
  17. * when it is constructed with a URL.</i>
  18. *
  19. * @see javax.activation.DataSource
  20. * @see javax.activation.DataHandler
  21. */
  22. public class URLDataSource implements DataSource {
  23. private URL url = null;
  24. private URLConnection url_conn = null;
  25. /**
  26. * URLDataSource constructor. The URLDataSource class will
  27. * not open a connection to the URL until a method requiring it
  28. * to do so is called.
  29. *
  30. * @param url The URL to be encapsulated in this object.
  31. */
  32. public URLDataSource(URL url) {
  33. this.url = url;
  34. }
  35. /**
  36. * Returns the value of the URL content-type header field.
  37. * It calls the URL's <code>URLConnection.getContentType</code> method
  38. * after retrieving a URLConnection object.
  39. * <i>Note: this method attempts to call the <code>openConnection</code>
  40. * method on the URL. If this method fails, or if a content type is not
  41. * returned from the URLConnection, getContentType returns
  42. * "application/octet-stream" as the content type.</i>
  43. *
  44. * @return the content type.
  45. */
  46. public String getContentType() {
  47. String type = null;
  48. try {
  49. if (url_conn == null)
  50. url_conn = url.openConnection();
  51. } catch (IOException e) { }
  52. if (url_conn != null)
  53. type = url_conn.getContentType();
  54. if (type == null)
  55. type = "application/octet-stream";
  56. return type;
  57. }
  58. /**
  59. * Calls the <code>getFile</code> method on the URL used to
  60. * instantiate the object.
  61. *
  62. * @return the result of calling the URL's getFile method.
  63. */
  64. public String getName() {
  65. return url.getFile();
  66. }
  67. /**
  68. * The getInputStream method from the URL. Calls the
  69. * <code>openStream</code> method on the URL.
  70. *
  71. * @return the InputStream.
  72. */
  73. public InputStream getInputStream() throws IOException {
  74. return url.openStream();
  75. }
  76. /**
  77. * The getOutputStream method from the URL. First an attempt is
  78. * made to get the URLConnection object for the URL. If that
  79. * succeeds, the getOutputStream method on the URLConnection
  80. * is returned.
  81. *
  82. * @return the OutputStream.
  83. */
  84. public OutputStream getOutputStream() throws IOException {
  85. // get the url connection if it is available
  86. url_conn = url.openConnection();
  87. if (url_conn != null)
  88. return url_conn.getOutputStream();
  89. else
  90. return null;
  91. }
  92. /**
  93. * Return the URL used to create this DataSource.
  94. *
  95. * @return The URL.
  96. */
  97. public URL getURL() {
  98. return url;
  99. }
  100. }