Exemplo n.º 1
0
 /**
  * This will delete an asset and all its attachments
  *
  * @param assetId The id of the asset
  * @return <code>true</code> if the asset and all its attachments are deleted. Note this is not
  *     atomic so some attachments may be deleted and <code>false</code> returned.
  * @throws IOException
  * @throws RequestFailureException
  */
 public void deleteAssetAndAttachments(String assetId)
     throws IOException, RequestFailureException {
   Asset ass = getUnverifiedAsset(assetId);
   List<Attachment> attachments = ass.getAttachments();
   if (attachments != null) {
     for (Attachment attachment : attachments) {
       deleteAttachment(assetId, attachment.get_id());
     }
   }
   // Now delete the asset
   deleteAsset(assetId);
 }
Exemplo n.º 2
0
  /**
   * Adds an asset into Massive. Note that Massive will set some fields (such as ID) so it is
   * important to switch to the returned object after calling this method.
   *
   * @param asset The asset to add, it will not be modified by this method
   * @return The asset with information added by Massive
   * @throws IOException
   */
  public Asset addAsset(Asset asset)
      throws IOException, BadVersionException, RequestFailureException {

    HttpURLConnection connection = createHttpURLConnectionToMassive("/assets");
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setDoOutput(true);
    JSONAssetConverter.writeValue(connection.getOutputStream(), asset);
    testResponseCode(connection);
    Asset returnedAsset = JSONAssetConverter.readValue(connection.getInputStream());
    returnedAsset = getAsset(returnedAsset.get_id());
    return returnedAsset;
  }
Exemplo n.º 3
0
  /**
   * Returns the meta data about an attachment
   *
   * @param assetId The ID of the asset owning the attachment
   * @param attachmentId The ID of the attachment
   * @return The attachment meta data
   * @throws IOException
   * @throws RequestFailureException
   */
  public Attachment getAttachmentMetaData(String assetId, String attachmentId)
      throws IOException, BadVersionException, RequestFailureException {
    // At the moment can only get all attachments
    Asset ass = getAsset(assetId);
    List<Attachment> allAttachments = ass.getAttachments();
    for (Attachment attachment : allAttachments) {
      if (attachmentId.equals(attachment.get_id())) {
        return attachment;
      }
    }

    // Didn't find it so just return null
    return null;
  }
Exemplo n.º 4
0
  /**
   * This method will update an existing attachment on an asset. Note that Massive currently doesn't
   * support update attachment so this will do a delete and an add.
   *
   * @param assetId The ID of the asset that the attachment is attached to
   * @param name The name of the attachment to update
   * @param file The file to attach
   * @param attach Attachment metadata
   * @return
   * @throws IOException
   * @throws RequestFailureException
   */
  public Attachment updateAttachment(String assetId, AttachmentSummary summary)
      throws IOException, BadVersionException, RequestFailureException {
    // First find the attachment to update
    // TODO update when massive supports proper updates to attachments
    Asset ass = getAsset(assetId);
    List<Attachment> attachments = ass.getAttachments();

    if (attachments != null) {
      for (Attachment attachment : attachments) {
        if (attachment.getName().equals(summary.getName())) {
          this.deleteAttachment(assetId, attachment.get_id());
          break;
        }
      }
    }
    return this.addAttachment(assetId, summary);
  }
Exemplo n.º 5
0
  /**
   * Updates an asset in Massive. The {@link Asset#get_id()} must return the correct ID for this
   * asset. Note that Massive will set some fields (such as last update date) so it is important to
   * switch to the returned object after calling this method.
   *
   * @param asset The asset to add, it will not be modified by this method
   * @return The asset with information added by Massive
   * @throws IOException
   * @throws RequestFailureException
   */
  public Asset updateAsset(Asset asset)
      throws IOException, BadVersionException, RequestFailureException {
    HttpURLConnection connection = createHttpURLConnectionToMassive("/assets/" + asset.get_id());
    connection.setRequestMethod("PUT");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setDoOutput(true);
    JSONAssetConverter.writeValue(connection.getOutputStream(), asset);

    /*
     * Force the PUT to take place by getting the response code and making sure it is ok
     */
    testResponseCode(connection);

    /*
     * PUTs don't return an (they just return "1" - not sure what that
     * means) so go and grab it so we have an updated one with the right
     * last update date in case they use it for optimistic locking
     */
    return getAsset(asset.get_id());
  }