Exemplo n.º 1
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());
  }
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;
  }