Search in sources :

Example 1 with CommandException

use of org.javaswift.joss.exception.CommandException in project alluxio by Alluxio.

the class SwiftUnderFileSystem method copyObject.

@Override
protected boolean copyObject(String source, String destination) {
    LOG.debug("copy from {} to {}", source, destination);
    // Retry copy for a few times, in case some Swift internal errors happened during copy.
    for (int i = 0; i < NUM_RETRIES; i++) {
        try {
            Container container = mAccount.getContainer(mContainerName);
            container.getObject(source).copyObject(container, container.getObject(destination));
            return true;
        } catch (CommandException e) {
            LOG.error("Source path {} does not exist", source);
            return false;
        } catch (Exception e) {
            LOG.error("Failed to copy file {} to {}", source, destination, e.getMessage());
            if (i != NUM_RETRIES - 1) {
                LOG.error("Retrying copying file {} to {}", source, destination);
            }
        }
    }
    LOG.error("Failed to copy file {} to {}, after {} retries", source, destination, NUM_RETRIES);
    return false;
}
Also used : Container(org.javaswift.joss.model.Container) CommandException(org.javaswift.joss.exception.CommandException) IOException(java.io.IOException) CommandException(org.javaswift.joss.exception.CommandException) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException)

Example 2 with CommandException

use of org.javaswift.joss.exception.CommandException in project alluxio by Alluxio.

the class SwiftUnderFileSystem method createEmptyObject.

@Override
protected boolean createEmptyObject(String key) {
    try {
        Container container = mAccount.getContainer(mContainerName);
        StoredObject object = container.getObject(key);
        object.uploadObject(new byte[0]);
        return true;
    } catch (CommandException e) {
        LOG.error("Failed to create object: {}", key, e);
        return false;
    }
}
Also used : Container(org.javaswift.joss.model.Container) StoredObject(org.javaswift.joss.model.StoredObject) CommandException(org.javaswift.joss.exception.CommandException)

Example 3 with CommandException

use of org.javaswift.joss.exception.CommandException in project alluxio by Alluxio.

the class SwiftUnderFileSystem method deleteObject.

@Override
protected boolean deleteObject(String path) throws IOException {
    try {
        Container container = mAccount.getContainer(mContainerName);
        StoredObject object = container.getObject(path);
        if (object != null) {
            object.delete();
            return true;
        }
    } catch (CommandException e) {
        LOG.debug("Object {} not found", path);
    }
    return false;
}
Also used : Container(org.javaswift.joss.model.Container) StoredObject(org.javaswift.joss.model.StoredObject) CommandException(org.javaswift.joss.exception.CommandException)

Example 4 with CommandException

use of org.javaswift.joss.exception.CommandException in project stocator by SparkTC.

the class PasswordScopeAccessProvider method authenticate.

/**
 * Authentication logic
 *
 * @return Access JOSS access object
 */
@Override
public Access authenticate() {
    try {
        JSONObject user = new JSONObject();
        user.put("id", mUserId);
        user.put("password", mPassword);
        JSONObject password = new JSONObject();
        password.put("user", user);
        JSONArray methods = new JSONArray();
        methods.add("password");
        JSONObject identity = new JSONObject();
        identity.put("methods", methods);
        identity.put("password", password);
        JSONObject project = new JSONObject();
        project.put("id", mProjectId);
        JSONObject scope = new JSONObject();
        scope.put("project", project);
        JSONObject auth = new JSONObject();
        auth.put("identity", identity);
        auth.put("scope", scope);
        JSONObject requestBody = new JSONObject();
        requestBody.put("auth", auth);
        HttpURLConnection connection = (HttpURLConnection) new URL(mAuthUrl).openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json");
        OutputStream output = connection.getOutputStream();
        output.write(requestBody.toString().getBytes());
        HttpStatusChecker.verifyCode(STATUS_CHECKERS, connection.getResponseCode());
        final String res;
        try (final BufferedReader bufReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            res = bufReader.readLine();
        }
        JSONParser parser = new JSONParser();
        JSONObject jsonResponse = (JSONObject) parser.parse(res);
        String token = connection.getHeaderField("X-Subject-Token");
        PasswordScopeAccess access = new PasswordScopeAccess(jsonResponse, token, mPrefferedRegion);
        connection.disconnect();
        return access;
    } catch (IOException | ParseException e) {
        LOG.error(e.getMessage());
        throw new CommandException("Unable to execute the HTTP call or to convert the HTTP Response", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) CommandException(org.javaswift.joss.exception.CommandException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.simple.JSONObject) BufferedReader(java.io.BufferedReader) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Aggregations

CommandException (org.javaswift.joss.exception.CommandException)4 Container (org.javaswift.joss.model.Container)3 IOException (java.io.IOException)2 StoredObject (org.javaswift.joss.model.StoredObject)2 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1