Пример #1
0
    /**
     * Create an object (=file)
     *
     * @param containerName Name of the container
     * @param objectName Name of the file
     * @param contents Binary content of the file
     * @throws IOException
     */
    public void createObject(String containerName, String objectName, byte[] contents)
        throws Exception {
      HttpURLConnection conn =
          getConnBuilder(containerName, objectName)
              .method("PUT")
              .addHeader(HttpHeaders.CONTENT_LENGTH_HEADER, String.valueOf(contents.length))
              .getConnection();

      HttpResponse response = Utils.doSendOperation(conn, contents);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          createObject(containerName, objectName, contents);
        } else {
          log.error(
              "Error creating object "
                  + objectName
                  + " in container "
                  + containerName
                  + ",code = "
                  + response.code);
        }
      }
    }
Пример #2
0
    /**
     * List files in a folder
     *
     * @param containerName Folder name
     * @return List of file names
     * @throws IOException
     */
    public List<String> listObjects(String containerName) throws Exception {
      HttpURLConnection urlConnection = getConnBuilder(containerName, null).getConnection();

      HttpResponse response = Utils.doReadOperation(urlConnection);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          return listObjects(containerName);
        } else {
          log.error("Error listing container " + containerName + ", code = " + response.code);
        }
      }
      return response.payloadAsLines();
    }
Пример #3
0
    /**
     * Create a container, which is equivalent to a bucket
     *
     * @param containerName Name of the container
     * @throws IOException
     */
    public void createContainer(String containerName) throws Exception {
      HttpURLConnection urlConnection =
          getConnBuilder(containerName, null).method("PUT").getConnection();

      HttpResponse response = Utils.doVoidOperation(urlConnection);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          createContainer(containerName);
        } else {
          log.error("Error creating container " + containerName + " ,code = " + response.code);
        }
      }
    }
Пример #4
0
    /**
     * Read the content of a file
     *
     * @param containerName Name of the folder
     * @param objectName name of the file
     * @return Content of the files
     * @throws IOException
     */
    public byte[] readObject(String containerName, String objectName) throws Exception {
      HttpURLConnection urlConnection = getConnBuilder(containerName, objectName).getConnection();

      HttpResponse response = Utils.doReadOperation(urlConnection);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          return readObject(containerName, objectName);
        } else {
          log.error(
              "Error reading object "
                  + objectName
                  + " from container "
                  + containerName
                  + ", code = "
                  + response.code);
        }
      }
      return response.payload;
    }
Пример #5
0
    /**
     * Delete a object (=file) from the storage
     *
     * @param containerName Folder name
     * @param objectName File name
     * @throws IOException
     */
    public void deleteObject(String containerName, String objectName) throws Exception {
      HttpURLConnection urlConnection =
          getConnBuilder(containerName, objectName).method("DELETE").getConnection();

      HttpResponse response = Utils.doVoidOperation(urlConnection);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          deleteObject(containerName, objectName);
        } else {
          log.error(
              "Error deleting object "
                  + objectName
                  + " from container "
                  + containerName
                  + ",code = "
                  + response.code);
        }
      }
    }
Пример #6
0
    public Credentials authenticate() throws Exception {
      HttpURLConnection urlConnection =
          new ConnBuilder(authUrl)
              .addHeader(HttpHeaders.CONTENT_TYPE_HEADER, "application/json")
              .addHeader(HttpHeaders.ACCEPT_HEADER, "application/xml")
              .getConnection();

      StringBuilder jsonBuilder = new StringBuilder();
      jsonBuilder
          .append("{\"auth\": {\"tenantName\": \"")
          .append(tenant)
          .append("\", \"passwordCredentials\": {\"username\": \"")
          .append(username)
          .append("\", \"password\": \"")
          .append(password)
          .append("\"}}}");

      HttpResponse response =
          Utils.doOperation(urlConnection, jsonBuilder.toString().getBytes(), true);

      if (response.isSuccessCode()) {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(response.payload));

        String authToken = (String) tokenIdExpression.evaluate(doc, XPathConstants.STRING);
        String storageUrl = (String) publicUrlExpression.evaluate(doc, XPathConstants.STRING);

        log.trace("Authentication successful");

        return new Credentials(authToken, storageUrl);
      } else {
        throw new IllegalStateException(
            "Error authenticating to the service. Please check your credentials. Code = "
                + response.code);
      }
    }