Search in sources :

Example 1 with UploadException

use of com.owncloud.android.operations.UploadException in project android by nextcloud.

the class EncryptionUtils method lockFolder.

public static String lockFolder(OCFile parentFile, OwnCloudClient client) throws UploadException {
    // Lock folder
    LockFileRemoteOperation lockFileOperation = new LockFileRemoteOperation(parentFile.getLocalId());
    RemoteOperationResult lockFileOperationResult = lockFileOperation.execute(client);
    if (lockFileOperationResult.isSuccess() && !TextUtils.isEmpty((String) lockFileOperationResult.getData().get(0))) {
        return (String) lockFileOperationResult.getData().get(0);
    } else if (lockFileOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
        throw new UploadException("Forbidden! Please try again later.)");
    } else {
        throw new UploadException("Could not lock folder");
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UploadException(com.owncloud.android.operations.UploadException) LockFileRemoteOperation(com.owncloud.android.lib.resources.e2ee.LockFileRemoteOperation)

Example 2 with UploadException

use of com.owncloud.android.operations.UploadException in project android by nextcloud.

the class EncryptionUtils method retrieveMetadata.

/**
 * @param parentFile file metadata should be retrieved for
 * @return Pair: boolean: true: metadata already exists, false: metadata new created
 */
public static Pair<Boolean, DecryptedFolderMetadata> retrieveMetadata(OCFile parentFile, OwnCloudClient client, String privateKey, String publicKey) throws UploadException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException, CertificateException {
    GetMetadataRemoteOperation getMetadataOperation = new GetMetadataRemoteOperation(parentFile.getLocalId());
    RemoteOperationResult getMetadataOperationResult = getMetadataOperation.execute(client);
    DecryptedFolderMetadata metadata;
    if (getMetadataOperationResult.isSuccess()) {
        // decrypt metadata
        String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
        EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
        });
        return new Pair<>(Boolean.TRUE, EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey));
    } else if (getMetadataOperationResult.getHttpCode() == HttpStatus.SC_NOT_FOUND) {
        // new metadata
        metadata = new DecryptedFolderMetadata();
        metadata.setMetadata(new DecryptedFolderMetadata.Metadata());
        metadata.getMetadata().setMetadataKeys(new HashMap<>());
        String metadataKey = EncryptionUtils.encodeBytesToBase64String(EncryptionUtils.generateKey());
        String encryptedMetadataKey = EncryptionUtils.encryptStringAsymmetric(metadataKey, publicKey);
        metadata.getMetadata().getMetadataKeys().put(0, encryptedMetadataKey);
        return new Pair<>(Boolean.FALSE, metadata);
    } else {
        // TODO error
        throw new UploadException("something wrong");
    }
}
Also used : HashMap(java.util.HashMap) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) GetMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation) UploadException(com.owncloud.android.operations.UploadException) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) KeyPair(java.security.KeyPair) Pair(android.util.Pair)

Example 3 with UploadException

use of com.owncloud.android.operations.UploadException in project android by nextcloud.

the class EncryptionUtils method uploadMetadata.

public static void uploadMetadata(OCFile parentFile, String serializedFolderMetadata, String token, OwnCloudClient client, boolean metadataExists) throws UploadException {
    RemoteOperationResult uploadMetadataOperationResult;
    if (metadataExists) {
        // update metadata
        UpdateMetadataRemoteOperation storeMetadataOperation = new UpdateMetadataRemoteOperation(parentFile.getLocalId(), serializedFolderMetadata, token);
        uploadMetadataOperationResult = storeMetadataOperation.execute(client);
    } else {
        // store metadata
        StoreMetadataRemoteOperation storeMetadataOperation = new StoreMetadataRemoteOperation(parentFile.getLocalId(), serializedFolderMetadata);
        uploadMetadataOperationResult = storeMetadataOperation.execute(client);
    }
    if (!uploadMetadataOperationResult.isSuccess()) {
        throw new UploadException("Storing/updating metadata was not successful");
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UpdateMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.UpdateMetadataRemoteOperation) UploadException(com.owncloud.android.operations.UploadException) StoreMetadataRemoteOperation(com.owncloud.android.lib.resources.e2ee.StoreMetadataRemoteOperation)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 UploadException (com.owncloud.android.operations.UploadException)3 Pair (android.util.Pair)1 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)1 GetMetadataRemoteOperation (com.owncloud.android.lib.resources.e2ee.GetMetadataRemoteOperation)1 LockFileRemoteOperation (com.owncloud.android.lib.resources.e2ee.LockFileRemoteOperation)1 StoreMetadataRemoteOperation (com.owncloud.android.lib.resources.e2ee.StoreMetadataRemoteOperation)1 UpdateMetadataRemoteOperation (com.owncloud.android.lib.resources.e2ee.UpdateMetadataRemoteOperation)1 KeyPair (java.security.KeyPair)1 HashMap (java.util.HashMap)1