ObjectProductHandler.java

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

  5. import gov.usgs.earthquake.product.Content;
  6. import gov.usgs.earthquake.product.ByteContent;
  7. import gov.usgs.earthquake.product.FileContent;
  8. import gov.usgs.earthquake.product.URLContent;
  9. import gov.usgs.util.CryptoUtils.Version;
  10. import gov.usgs.earthquake.product.Product;
  11. import gov.usgs.earthquake.product.ProductId;

  12. import java.net.URI;
  13. import java.net.URL;

  14. import java.util.List;
  15. import java.util.LinkedList;

  16. /**
  17.  * Convert ProductSource events into a java Product object.
  18.  *
  19.  * ObjectProductHandlers are not designed to handle multiple products
  20.  * simultaneously and separate objects must be created for each unique product
  21.  * id.
  22.  *
  23.  * The static method ObjectProductHandler.getProduct(ProductInput) should
  24.  * usually be used instead of constructing objects manually.
  25.  */
  26. public class ObjectProductHandler implements ProductHandler {

  27.     /** The Product being created. */
  28.     private Product product = null;

  29.     /** Whether onEndProduct has been called yet. */
  30.     private boolean complete = false;

  31.     /** Empty constructor */
  32.     public ObjectProductHandler() {
  33.     }

  34.     /**
  35.      * @return the product object that was created.
  36.      * @throws Exception if error occurs
  37.      */
  38.     public Product getProduct() throws Exception {
  39.         if (product == null) {
  40.             throw new IllegalArgumentException(
  41.                     "Called getProduct before onBeginProduct");
  42.         } else if (!complete) {
  43.             throw new IllegalArgumentException(
  44.                     "Called getProduct before onEndProduct");
  45.         }

  46.         return product;
  47.     }

  48.     public void onBeginProduct(final ProductId id, final String status,
  49.             final URL trackerURL) throws Exception {
  50.         if (product != null) {
  51.             throw new IllegalArgumentException(
  52.                     "Called onBeginProduct after onBeginProduct");
  53.         } else if (complete) {
  54.             throw new IllegalArgumentException(
  55.                     "Called onBeginProduct after onEndProduct");
  56.         }

  57.         // System.err.println("onBeginProduct(" + id.toString() + ", " + status
  58.         // + ", " + trackerURL + ")");

  59.         product = new Product(id, status);
  60.         product.setTrackerURL(trackerURL);
  61.     }

  62.     public void onContent(final ProductId id, final String path,
  63.             final Content content) throws Exception {
  64.         if (product == null) {
  65.             throw new IllegalArgumentException(
  66.                     "Called onContent before onBeginProduct");
  67.         } else if (complete) {
  68.             throw new IllegalArgumentException(
  69.                     "Called onContent after onEndProduct");
  70.         } else if (!product.getId().equals(id)) {
  71.             throw new IllegalArgumentException("ProductIds do not match");
  72.         }

  73.         // System.err.println("onContent(" + id.toString() + ", " + path + ", "
  74.         // + content.toString() + ")");

  75.         if (content instanceof FileContent || content instanceof ByteContent
  76.                 || content instanceof URLContent) {
  77.             // these types of content do not need to be read immediately
  78.             product.getContents().put(path, content);
  79.         } else {
  80.             // new ByteContent reads stream into byte array
  81.             product.getContents().put(path, new ByteContent(content));
  82.         }

  83.     }

  84.     public void onEndProduct(final ProductId id) throws Exception {
  85.         // System.err.println("onEndProduct(" + id.toString() + ")");

  86.         complete = true;
  87.     }

  88.     public void onLink(final ProductId id, final String relation, final URI href)
  89.             throws Exception {
  90.         if (product == null) {
  91.             throw new IllegalArgumentException(
  92.                     "Called onLink before onBeginProduct");
  93.         } else if (complete) {
  94.             throw new IllegalArgumentException(
  95.                     "Called onLink after onEndProduct");
  96.         } else if (!product.getId().equals(id)) {
  97.             throw new IllegalArgumentException("ProductIds do not match");
  98.         }

  99.         // get list of links for relation
  100.         List<URI> links = product.getLinks().get(relation);
  101.         if (links == null) {
  102.             // create if doesn't already exist
  103.             links = new LinkedList<URI>();
  104.             product.getLinks().put(relation, links);
  105.         }

  106.         // add if doesn't already contain
  107.         if (!links.contains(href)) {
  108.             links.add(href);
  109.         }
  110.     }

  111.     public void onProperty(final ProductId id, final String name,
  112.             final String value) throws Exception {
  113.         if (product == null) {
  114.             throw new IllegalArgumentException(
  115.                     "Called onProperty before onBeginProduct");
  116.         } else if (complete) {
  117.             throw new IllegalArgumentException(
  118.                     "Called onProperty after onEndProduct");
  119.         } else if (!product.getId().equals(id)) {
  120.             throw new IllegalArgumentException("ProductIds do not match");
  121.         }

  122.         // System.err.println("onProperty(" + id.toString() + ", " + name + ", "
  123.         // + value + ")");
  124.         product.getProperties().put(name, value);
  125.     }

  126.     public void onSignatureVersion(final ProductId id, final Version version)
  127.             throws Exception {
  128.         if (product == null) {
  129.             throw new IllegalArgumentException(
  130.                     "Called onSignature before onBeginProduct");
  131.         } else if (complete) {
  132.             throw new IllegalArgumentException(
  133.                     "Called onSignature after onEndProduct");
  134.         } else if (!product.getId().equals(id)) {
  135.             throw new IllegalArgumentException("ProductIds do not match");
  136.         }

  137.         product.setSignatureVersion(version);
  138.     }

  139.     public void onSignature(final ProductId id, final String signature)
  140.             throws Exception {
  141.         if (product == null) {
  142.             throw new IllegalArgumentException(
  143.                     "Called onSignature before onBeginProduct");
  144.         } else if (complete) {
  145.             throw new IllegalArgumentException(
  146.                     "Called onSignature after onEndProduct");
  147.         } else if (!product.getId().equals(id)) {
  148.             throw new IllegalArgumentException("ProductIds do not match");
  149.         }

  150.         product.setSignature(signature);
  151.     }

  152.     /**
  153.      * Convenience method to get a Product object from a ProductInput.
  154.      *
  155.      * @param in
  156.      *            the ProductInput to read.
  157.      * @return the Product read, or null if errors occur.
  158.      * @throws Exception if error occurs
  159.      */
  160.     public static Product getProduct(final ProductSource in) throws Exception {
  161.         ObjectProductHandler out = new ObjectProductHandler();
  162.         try {
  163.             in.streamTo(out);
  164.         } finally {
  165.             in.close();
  166.         }
  167.         return out.getProduct();
  168.     }


  169.     /**
  170.      * Free any resources associated with this handler.
  171.      */
  172.     @Override
  173.     public void close() {
  174.         // this.product = null;
  175.     }

  176. }