URLContent.java

  1. /*
  2.  * URLContent
  3.  */
  4. package gov.usgs.earthquake.product;

  5. import gov.usgs.util.StreamUtils;

  6. import java.io.IOException;
  7. import java.io.InputStream;

  8. import java.net.MalformedURLException;
  9. import java.net.URISyntaxException;
  10. import java.net.URL;

  11. import javax.activation.MimetypesFileTypeMap;

  12. /**
  13.  * Content stored at a URL.
  14.  */
  15. public class URLContent extends AbstractContent {

  16.     /** Used to look up file types. */
  17.     private static MimetypesFileTypeMap MIME_TYPES = new MimetypesFileTypeMap();

  18.     /** The actual content. */
  19.     private URL content;

  20.     /**
  21.      * Create a new URLContent object.
  22.      *
  23.      * @param content
  24.      *            the content available at a URL.
  25.      * @throws URISyntaxException on URI error
  26.      */
  27.     public URLContent(final URL content) throws URISyntaxException {
  28.         this.setContentType(MIME_TYPES.getContentType(content.toURI()
  29.                 .toString()));
  30.         this.content = content;
  31.     }

  32.     /**
  33.      * Create a new URLContent object from a FileContent.
  34.      *
  35.      * @param fc
  36.      *            the file content.
  37.      * @throws MalformedURLException if URL error
  38.      */
  39.     public URLContent(final FileContent fc) throws MalformedURLException {
  40.         super(fc);
  41.         this.content = fc.getFile().toURI().toURL();
  42.     }

  43.     /**
  44.      * @return an InputStream for the wrapped content.
  45.      * @throws IOException on IO error
  46.      */
  47.     public InputStream getInputStream() throws IOException {
  48.         return StreamUtils.getURLInputStream(content);
  49.     }

  50.     /**
  51.      * @return the wrapped url.
  52.      */
  53.     public URL getURL() {
  54.         return content;
  55.     }

  56.     /**
  57.      * Free any resources associated with this content.
  58.      */
  59.     public void close() {
  60.         // nothing to free
  61.     }

  62. }