DirectoryProductSource.java

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

  5. import gov.usgs.earthquake.product.Content;
  6. import gov.usgs.earthquake.product.Product;
  7. import gov.usgs.earthquake.product.FileContent;
  8. import gov.usgs.earthquake.product.URLContent;

  9. import gov.usgs.util.StreamUtils;

  10. import java.io.File;
  11. import java.io.InputStream;
  12. import java.util.Map;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;

  15. /**
  16.  * Load a product from a Directory.
  17.  *
  18.  * Usually a directory is created using DirectoryProductOutput. It should
  19.  * contain a product xml file named "product.xml". All other files are treated
  20.  * as attachments.
  21.  */
  22. public class DirectoryProductSource implements ProductSource {

  23.     /** The directory this product input references. */
  24.     private File directory;

  25.     private static final Logger LOGGER = Logger.getLogger(DirectoryProductSource.class.getName());

  26.     /**
  27.      * Construct a new DirectoryProductSource object.
  28.      *
  29.      * @param directory the directory containing a product.
  30.      */
  31.     public DirectoryProductSource(final File directory) {
  32.         this.directory = directory;
  33.     }

  34.     /**
  35.      * Load Product from a directory, then send product events to the ProductOutput.
  36.      *
  37.      * @param out the ProductOutput that will receive the product.
  38.      */
  39.     public void streamTo(ProductHandler out) throws Exception {
  40.         InputStream in = null;

  41.         try {
  42.             in = StreamUtils.getInputStream(new File(directory, DirectoryProductHandler.PRODUCT_XML_FILENAME));

  43.             // load product from xml
  44.             Product product = ObjectProductHandler.getProduct(new XmlProductSource(in));

  45.             // Convert URLContent to FileContent
  46.             Map<String, Content> contents = product.getContents();
  47.             Content urlContent;
  48.             boolean foundURLContent = false;
  49.             for (String key : contents.keySet()) {
  50.                 urlContent = contents.get(key);
  51.                 if (urlContent instanceof URLContent) {
  52.                     foundURLContent = true;
  53.                     if (!"".equals(key)) {
  54.                         File filePath = new File(directory, key);
  55.                         if (filePath.exists()) {
  56.                             FileContent fileContent = new FileContent(filePath);
  57.                             fileContent.setContentType(urlContent.getContentType());
  58.                             fileContent.setLastModified(urlContent.getLastModified());
  59.                             fileContent.setLength(urlContent.getLength());
  60.                             // go direct to file based on key
  61.                             contents.put(key, fileContent);
  62.                         } else {
  63.                             // old way
  64.                             contents.put(key, new FileContent((URLContent) urlContent));
  65.                         }
  66.                     }
  67.                 }
  68.             }

  69.             if (!foundURLContent) {
  70.                 LOGGER.log(Level.FINER,
  71.                         "[DirectoryProductSource] Product does not have any " + " URLContent. Scraping directory for files.");

  72.                 // load contents from directory
  73.                 contents.putAll(FileContent.getDirectoryContents(directory));

  74.                 // except for product.xml which is the product, not its content
  75.                 contents.remove(DirectoryProductHandler.PRODUCT_XML_FILENAME);
  76.             }

  77.             // now use ObjectProductInput to send loaded product
  78.             new ObjectProductSource(product).streamTo(out);
  79.         } finally {
  80.             StreamUtils.closeStream(in);
  81.         }
  82.     }

  83.     /**
  84.      * Free any resources associated with this source.
  85.      */
  86.     @Override
  87.     public void close() {
  88.         directory = null;
  89.     }

  90. }