URLNotificationJSONConverter.java

  1. package gov.usgs.earthquake.distribution;

  2. import gov.usgs.earthquake.product.ProductId;
  3. import gov.usgs.util.XmlUtils;

  4. import java.io.InputStream;
  5. import java.net.URL;
  6. import java.util.Date;
  7. import javax.json.Json;
  8. import javax.json.JsonObject;
  9. import javax.json.JsonReader;

  10. public class URLNotificationJSONConverter {

  11.   /** attribute for product id */
  12.   public static final String ATTRIBUTE_PRODUCT_ID = "id";
  13.   /** attribute for source */
  14.   public static final String ATTRIBUTE_SOURCE = "source";
  15.   /** attribute for type */
  16.   public static final String ATTRIBUTE_TYPE = "type";
  17.   /** attribute for code */
  18.   public static final String ATTRIBUTE_CODE = "code";
  19.   /** attribute for updatetime */
  20.   public static final String ATTRIBUTE_UPDATE_TIME = "updatetime";
  21.   /** attribute for trackerURL */
  22.   public static final String ATTRIBUTE_TRACKER_URL = "trackerURL";
  23.   /** attribute for expires */
  24.   public static final String ATTRIBUTE_EXPIRES = "expires";
  25.   /** attribute for url */
  26.   public static final String ATTRIBUTE_URL = "url";

  27.   /**
  28.    * Converts a URLNotification into a JSON string
  29.    * @param notification URLNotification to convert
  30.    * @return JSON string
  31.    */
  32.   public static String toJSON(final URLNotification notification) {
  33.     //id
  34.     ProductId id = notification.getProductId();

  35.     JsonObject json = Json.createObjectBuilder()
  36.       .add(ATTRIBUTE_PRODUCT_ID,Json.createObjectBuilder()
  37.         .add(ATTRIBUTE_SOURCE,id.getSource())
  38.         .add(ATTRIBUTE_TYPE,id.getType())
  39.         .add(ATTRIBUTE_CODE,id.getCode())
  40.         .add(ATTRIBUTE_UPDATE_TIME, XmlUtils.formatDate(id.getUpdateTime())))
  41.       .add(ATTRIBUTE_TRACKER_URL,notification.getTrackerURL().toString())
  42.       .add(ATTRIBUTE_EXPIRES,XmlUtils.formatDate(notification.getExpirationDate()))
  43.       .add(ATTRIBUTE_URL,notification.getProductURL().toString())
  44.       .build();

  45.     return json.toString();
  46.   }

  47.   /**
  48.    * parse a message from the input stream
  49.    * @param message InputStream message
  50.    * @return URLNotification
  51.    * @throws Exception if error occurs
  52.    */
  53.   public static URLNotification parseJSON(final InputStream message) throws Exception{
  54.     JsonReader jsonReader = Json.createReader(message);
  55.     JsonObject json = jsonReader.readObject();
  56.     jsonReader.close();

  57.     return parseJSON(json);
  58.   }

  59.   /**
  60.    * Parse a JsonObject
  61.    * @param json JsonObject
  62.    * @return URLNotification
  63.    * @throws Exception if error occurs
  64.    */
  65.   public static URLNotification parseJSON(final JsonObject json) throws Exception{
  66.     JsonObject idJson = json.getJsonObject(ATTRIBUTE_PRODUCT_ID);

  67.     ProductId id = new ProductId(
  68.             idJson.getString(ATTRIBUTE_SOURCE),
  69.             idJson.getString(ATTRIBUTE_TYPE),
  70.             idJson.getString(ATTRIBUTE_CODE),
  71.             XmlUtils.getDate(idJson.getString(ATTRIBUTE_UPDATE_TIME)));

  72.     return new URLNotification(
  73.             id,
  74.             XmlUtils.getDate(json.getString(ATTRIBUTE_EXPIRES)),
  75.             new URL(json.getString(ATTRIBUTE_TRACKER_URL)),
  76.             new URL(json.getString(ATTRIBUTE_URL)));
  77.   }

  78.   /**
  79.    * Main function to run a test JSON-ifying a URLnotification
  80.    * @param args arguments
  81.    * @throws Exception if error occurs
  82.    */
  83.   public static void main(String[] args) throws Exception{
  84.     URLNotification testNotification = new URLNotification(new ProductId("testSource","testType","testCode"), new Date(),
  85.             new URL("http://localhost/tracker/"), new URL("http://localhost/product/"));

  86.     String JSON = toJSON(testNotification);

  87.     System.out.println(JSON);
  88.   }

  89. }