Example 3: Sending object or version data with an annotation (Java)

Here’s the partial implementation of a Java class named WholeIO. The implementation shows the WriteToHCP method, which uses a single HTTP PUT request to store data and an annotation for an object (or version).

This example assumes that the applicable imports are included in the full class implementation.

public class WholeIO {
  .
  .
  .
void WriteToHCP() throws Exception {
    /*
     * Set up the PUT request to store both object data and an
     * annotation.
     * This method assumes that the HTTP client has already been
     * initialized.
     */
    HttpPut httpRequest = new HttpPut(sHCPURLFilePath +
      "?type=whole-object");

    // Construct the Whole I/O Sequenced Stream with the object data
    // and annotation.
    FileInputStream dataFileStream = new FileInputStream(sBaseFileName);


    FileInputStream customMetadataStream =
      new FileInputStream(sBaseFileName + ".cm");

    SequenceInputStream wholeIOStream = new SequenceInputStream(
        dataFileStream, customMetadataStream);

    // Point the HttpRequest to the input stream.
    httpRequest.setEntity(new InputStreamEntity(wholeIOStream, -1));

    // Put the size of the data object data into the X-HCP-Size header.
    httpRequest.setHeader("X-HCP-Size",
      String.valueOf(dataFileStream.available()));

    // Create the HTTP Authorization Header.
    httpRequest.setHeader(HCPUtils.HTTP_AUTH_HEADER, "HCP " +
       sEncodedUserName + ":" + sEncodedPassword);

    /*
     * Now execute the PUT request.
     */
    HttpResponse httpResponse = mHttpClient.execute(httpRequest);

    // If the return code is anything BUT 200 range indicating success,
    // throw an exception.
    if (2 != (int)(httpResponse.getStatusLine().getStatusCode() / 100))
    {
      // Clean up after ourselves and release the HTTP connection to the
      // connection manager.
      EntityUtils.consume(httpResponse.getEntity())

      wholeIOStream.close();
      dataFileStream.close();
      customMetadataStream.close();

      throw new HttpResponseException(
         httpResponse.getStatusLine().getStatusCode(),
         "Unexpected status returned from " + httpRequest.getMethod()
         + " (" + httpResponse.getStatusLine().getStatusCode() + ": "
         + httpResponse.getStatusLine().getReasonPhrase() + ")");
    }

    // Clean up after ourselves and release the HTTP connection to the
    // connection manager.
    EntityUtils.consume(httpResponse.getEntity());

    wholeIOStream.close();
    dataFileStream.close();
    customMetadataStream.close();

  }
  .
  .
  .
}

Trademarks and Legal Disclaimer

© 2017 Hitachi Data Systems Corporation. All rights reserved.