Sample JOSS application
This appendix contains a sample JOSS application that uses the HCP HSwift API and and JOSS SDK to perform a series of operations in HCP.
Assumptions
The application makes these assumptions:
- The HCP system has a tenant named
europe
. - Keystone is installed and has a Keystone HCP tenant account named
europe
- The Keystone tenant has an admin user with the user name
lgreen
and the passwordp4ssw0rd
- The tenant has a user account with user name
lgreen
and passwordp4ssw0rd
. - By default, versioning is disabled for new containers.
- The local file system has folders named input and output that are located in the current working folder for the application.
- The input directory contains two files, Q4_2012.ppt and Q3_2012.ppt.
What the application does
The sample application shown in this appendix uses the HSwift API to:
- Create a container named
finance
in the context of the tenant namedeurope
(the service point) - List the container for the
europe
tenant that are owned by the userlgreen
- Add an ACL to the
finance
container - Store an object named quarterly_rpts/Q4_2012.ppt in the
finance
container, associating custom metadata with the object in the process - Store an object named quarterly_rpts/Q3_2012.ppt in the
finance
container - Retrieve the object named quarterly_rpts/Q4_2012.ppt and write its content to a new file on the local file system
- Add an ACL to the container named
finance
for the account namedrsilver
- Retrieve object quarterly_rpts/Q3_2012.ppt and write its content to a new file on the local file system
- Delete the quarterly_rpts/Q4_2012.ppt and quarterly_rpts/Q3_2012.ppt object from the
finance
container - Delete the quarterly_rpts folder from the
finance
container (HCP created this folder automatically when the first object was stored) - Delete the
finance
container
Required library
To run the sample application presented in this appendix, you need to have installed the following library:
- JOSS SDK 0.9.8 downloadable from: http://joss.javaswift.org/
JOSS application
Here's the same JOSS application.
/** * This sample Java application shows how to use the HCP *HSwift API, which is compatible with JOSS SDK. The *application uses the JOSS SDK. */ package com.hds.hcp.examples; import org.javaswift.joss.client.factory.AccountConfig; import org.javaswift.joss.client.factory.AccountFactory; import org.javaswift.joss.client.factory.AuthenticationMethod; import org.javaswift.joss.model.Account; import org.javaswift.joss.model.Container; import org.javaswift.joss.model.StoredObject; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class HSwiftSampleApp { /** * @param args */ public static void main(String[] args) { /* * Initialize access credentials for the HSwift client. */ // base64 of HCP user name: "lgreen" String accessKey = "bGdyZWVu"; // md5 of HCP user password: "p4ssw0rd" String secretKey = "2a9d119df47ff993b662a8ef36f9ea20"; /* * Build the JOSS account connection to be used * for communication with HCP. The Keystone must have *an admin user lgreen with password p4ssw0rd and *tenant europe. The AuthUrl must point to the keystone *server. */ AccountConfig accountConfig = new AccountConfig(); accountConfig.setAuthUrl("http://api.keystone.server.com:5000/v2.0/token"); accountConfig.setUsername(accessKey); accountConfig.setPassword(secretKey); accountConfig.setAuthenticationMethod(AuthenticationMethod.KEYSTONE); Account europe = new AccountFactor(accountConfig).createAccount(); /* * Now that the HSwift Client is created for HCP *usage, proceed with some operations. */ String containerName = "finance"; try { /* * Create a new container. With HCP, the container *name does not need to be globally unique. It needs *to be unique only within the HCP service point *that is, the HCP tenant. */ System.out.println("Creating container " + containerName + "\n"); Container finance = europe.getContainer(containerName); finance.create(); /* * List the containers you own at the service point. */ System.out.println("Containers:"); for (Container container : europe.list()) { System.out.println(" * " + container.getName()); } System.out.println(); /* * Add an ACL to the container to give read to a user with the specified user name. */ finance.setContainerRights(null, "pdgray"); /* * Upload a couple of objects to the container from *files on the local file system. */ String firstFileName = "input/Q4_2012.ppt"; String firstObjectName = "quarterly_rpts/Q4_2012.ppt"; StoredObject Q4_2012 = finance.getObject(firstObjectName); System.out.println("Uploading first object to HCP from a file\n"); Q4_2012.uploadObject(new FileInputStream(firstFileName)); // Add metadata for first object Q4_2012.setAndSaveMetadata("Author", "P.D. Gray"); Q4_2012.setAndSaveMetadata("Audit_Date", "2013-02-23"); // Write a second object without metadata. System.out.println("Uploading second object to HCP from a file\n"); String secondFileName = "input/Q3_2012.ppt"; String secondObjectName = "quarterly_rpts/Q3_2012.ppt"; StoredObject Q3_2012 = finance.getObject(secondObjectName); Q3_2012.uploadObject(new FileInputStream(secondFileName)); /* * List objects in the container with prefix *quarterly_rpts/Q. The objects listing is limited *to 1,000 items per request. */ System.out.println("Objects:"); for (StoredObject object : finance.list("quarterly_rpts/Q", null, 1000)) { System.out.println(" * " + object.getName() + " " + "(size = " + object.getContentLength() + ")"); } System.out.println(); /* * Download an object. When you download an object, * you get all the object metadata and a stream from * which to read the object content. */ System.out.println("Downloading the first object\n"); InputStream responseStream = Q4_2012.downloadObjectAsInputStream(); // Write the content to a file named Q4_2012.ppt in the output folder. FileOutputStream dataFile = new FileOutputStream("output/Q4_2012.ppt"); // Keep reading bytes until the end of stream is reached. byte buffer[] = new byte[2048]; int readSize; while (-1 != (readSize = responseStream.read(buffer))) { dataFile.write(buffer, 0, readSize); } dataFile.close(); /* * Add an ACL to the container which gives full control to the rsilver account. */ finance.setContainerRights("rsilver", "rsilver"); /* * Perform a download of the second object. */ System.out.println("Checking the second object"); responseStream = Q3_2012.downloadObjectAsInputStream(); // Download it to a new file. System.out.println("Downloading new revision\n"); FileOutputStream dataFile2 = new FileOutputStream("output/Q3_2012_Rev2.ppt"); // Keep reading bytes until the end of stream is reached. byte readBuffer[] = new byte[2048]; int conditionalReadSize; while (-1 != (conditionalReadSize = responseStream.read(readBuffer))) { dataFile2.write(readBuffer, 0, conditionalReadSize); } dataFile2.close(); /* * Delete the objects. */ System.out.println( "Deleting the objects created by this sample application\n"); Q4_2012.delete(); Q3_2012.delete(); /* * Delete the folder. */ System.out.println( "Deleting the folder created when the first object was stored\n"); StoredObject folder = finance.getObject("quarterly_rpts"); folder.delete(); /* * Delete the container. */ System.out.println("Deleting the finance container\n"); finance.delete(); } catch (IOException ioe) { System.out.println( "Caught an IOException while trying to create an object or read " + "from an internal buffer."); System.out.println("Error Message: " + ioe.getMessage()); } } }