@Override public void init() throws Exception { if (username == null) { throw new IllegalArgumentException("Rackspace 'username' must not be null"); } if (apiKey == null) { throw new IllegalArgumentException("Rackspace 'apiKey' must not be null"); } if (region == null || (!region.equals("UK") && !region.equals("US"))) { throw new IllegalArgumentException("Invalid 'region', must be UK or US"); } URL authURL = new URL(region.equals("UK") ? UKService : USService); rackspaceClient = new RackspaceClient(authURL, username, apiKey); super.init(); }
/** * Create an object (=file) * * @param containerName Name of the container * @param objectName Name of the file * @param contents Binary content of the file */ public void createObject(String containerName, String objectName, byte[] contents) { HttpURLConnection conn = new ConnBuilder(credentials, containerName, objectName) .method("PUT") .addHeader(CONTENT_LENGTH_HEADER, String.valueOf(contents.length)) .getConnection(); Response response = doSendOperation(conn, contents); if (!response.isSuccessCode()) { if (response.isAuthDenied()) { log.warn("Refreshing credentials and retrying"); authenticate(); createObject(containerName, objectName, contents); } else { log.error(Util.getMessage("ErrorCreatingObject") + objectName + " in container " + containerName + ",code = " + response.code); } } }
/** * Authenticate */ public void authenticate() { HttpURLConnection urlConnection = new ConnBuilder(apiEndpoint) .addHeader(AUTH_HEADER, username) .addHeader(AUTH_KEY_HEADER, apiKey) .getConnection(); Response response = doAuthOperation(urlConnection); if (response.isSuccessCode()) { credentials = new Credentials( response.getHeader(STORAGE_TOKEN_HEADER), response.getHeader(STORAGE_URL_HEADER) ); log.trace("Authentication successful"); } else { throw new IllegalStateException("Error authenticating to the service. Please check your credentials. Code = " + response.code); } }
/** * Create an object (=file) * * @param containerName Name of the container * @param objectName Name of the file * @param contents Binary content of the file */ public void createObject(String containerName, String objectName, byte[] contents) { HttpURLConnection conn = new ConnBuilder(credentials, containerName, objectName) .method("PUT") .addHeader(CONTENT_LENGTH_HEADER, String.valueOf(contents.length)) .getConnection(); Response response = doSendOperation(conn, contents); if (!response.isSuccessCode()) { if (response.isAuthDenied()) { log.warn("Refreshing credentials and retrying"); authenticate(); createObject(containerName, objectName, contents); } else { log.error(Util.getMessage("ErrorCreatingObject") + objectName + " in container " + containerName + ",code = " + response.code); } } }
/** * List files in a folder * * @param containerName Folder name * @return List of file names */ public List<String> listObjects(String containerName) { HttpURLConnection urlConnection = new ConnBuilder(credentials, containerName, null).getConnection(); Response response = doReadOperation(urlConnection); if (!response.isSuccessCode()) { if (response.isAuthDenied()) { log.warn("Refreshing credentials and retrying"); authenticate(); return listObjects(containerName); } else { log.error(Util.getMessage("ErrorListingContainer") + containerName + ", code = " + response.code); } } return response.payloadAsLines(); }
/** * Authenticate */ public void authenticate() { HttpURLConnection urlConnection = new ConnBuilder(apiEndpoint) .addHeader(AUTH_HEADER, username) .addHeader(AUTH_KEY_HEADER, apiKey) .getConnection(); Response response = doAuthOperation(urlConnection); if (response.isSuccessCode()) { credentials = new Credentials( response.getHeader(STORAGE_TOKEN_HEADER), response.getHeader(STORAGE_URL_HEADER) ); log.trace("Authentication successful"); } else { throw new IllegalStateException("Error authenticating to the service. Please check your credentials. Code = " + response.code); } }
/** * Read the content of a file * * @param containerName Name of the folder * @param objectName name of the file * @return Content of the files */ public byte[] readObject(String containerName, String objectName) { HttpURLConnection urlConnection = new ConnBuilder(credentials, containerName, objectName).getConnection(); Response response = doReadOperation(urlConnection); if (!response.isSuccessCode()) { if (response.isAuthDenied()) { log.warn("Refreshing credentials and retrying"); authenticate(); return readObject(containerName, objectName); } else { log.error(Util.getMessage("ErrorReadingObject") + objectName + " from container " + containerName + ", code = " + response.code); } } return response.payload; }
/** * Delete a object (=file) from the storage * * @param containerName Folder name * @param objectName File name */ public void deleteObject(String containerName, String objectName) { HttpURLConnection urlConnection = new ConnBuilder(credentials, containerName, objectName) .method("DELETE") .getConnection(); Response response = doVoidOperation(urlConnection); if (!response.isSuccessCode()) { if (response.isAuthDenied()) { log.warn("Refreshing credentials and retrying"); authenticate(); deleteObject(containerName, objectName); } else { log.error(Util.getMessage("ErrorDeletingObject") + objectName + " from container " + containerName + ",code = " + response.code); } } }
/** * Create a container, which is equivalent to a bucket * * @param containerName Name of the container */ public void createContainer(String containerName) { HttpURLConnection urlConnection = new ConnBuilder(credentials, containerName, null) .method("PUT") .getConnection(); Response response = doVoidOperation(urlConnection); if (!response.isSuccessCode()) { if (response.isAuthDenied()) { log.warn("Refreshing credentials and retrying"); authenticate(); createContainer(containerName); } else { log.error(Util.getMessage("ErrorCreatingContainer") + containerName + " ,code = " + response.code); } } }
@Override public void init() throws Exception { if (username == null) { throw new IllegalArgumentException("Rackspace 'username' must not be null"); } if (apiKey == null) { throw new IllegalArgumentException("Rackspace 'apiKey' must not be null"); } if (region == null || (!region.equals("UK") && !region.equals("US"))) { throw new IllegalArgumentException("Invalid 'region', must be UK or US"); } URL authURL = new URL(region.equals("UK") ? UKService : USService); rackspaceClient = new RackspaceClient(authURL, username, apiKey); super.init(); }
@Override protected void write(List<PingData> list, String clustername) { try { String filename = clustername + "/" + addressToFilename(local_addr); ByteArrayOutputStream out=new ByteArrayOutputStream(4096); write(list, out); byte[] data=out.toByteArray(); rackspaceClient.createObject(container, filename, data); } catch (Exception e) { log.error(Util.getMessage("ErrorMarshallingObject"), e); } }
@Override protected void remove(String clustername, Address addr) { String fileName = clustername + "/" + addressToFilename(addr); rackspaceClient.deleteObject(container, fileName); }
/** * Do a operation that reads from the httpconnection * * @param urlConnection The connections * @return Response */ private Response doReadOperation(HttpURLConnection urlConnection) { return doOperation(urlConnection, null, true); }
/** * Do a http auth operation, will not handle 401 permission denied errors * * @param urlConnection the HttpURLConnection to be used * @return Response Response */ private Response doAuthOperation(HttpURLConnection urlConnection) { return doOperation(urlConnection, null, false); }
@Override protected void removeAll(String clustername) { List<String> objects = rackspaceClient.listObjects(container); for(String objName: objects) { rackspaceClient.deleteObject(container, objName); } }
/** * Do a operation that writes content to the HttpURLConnection * * @param urlConnection the connection * @param content The content to send * @return Response */ private Response doSendOperation(HttpURLConnection urlConnection, byte[] content) { return doOperation(urlConnection, content, false); }
/** * Do a operation that does not write or read from HttpURLConnection, except for the headers * * @param urlConnection the connection * @return Response */ private Response doVoidOperation(HttpURLConnection urlConnection) { return doOperation(urlConnection, null, false); }
@Override protected void createRootDir() { rackspaceClient.authenticate(); rackspaceClient.createContainer(container); }
/** * Read the content of a file * * @param containerName Name of the folder * @param objectName name of the file * @return Content of the files */ public byte[] readObject(String containerName, String objectName) { HttpURLConnection urlConnection = new ConnBuilder(credentials, containerName, objectName).getConnection(); Response response = doReadOperation(urlConnection); if (!response.isSuccessCode()) { if (response.isAuthDenied()) { log.warn("Refreshing credentials and retrying"); authenticate(); return readObject(containerName, objectName); } else { log.error(Util.getMessage("ErrorReadingObject") + objectName + " from container " + containerName + ", code = " + response.code); } } return response.payload; }
/** * Delete a object (=file) from the storage * * @param containerName Folder name * @param objectName File name */ public void deleteObject(String containerName, String objectName) { HttpURLConnection urlConnection = new ConnBuilder(credentials, containerName, objectName) .method("DELETE") .getConnection(); Response response = doVoidOperation(urlConnection); if (!response.isSuccessCode()) { if (response.isAuthDenied()) { log.warn("Refreshing credentials and retrying"); authenticate(); deleteObject(containerName, objectName); } else { log.error(Util.getMessage("ErrorDeletingObject") + objectName + " from container " + containerName + ",code = " + response.code); } } }