Search in sources :

Example 6 with UnknownVersionException

use of com.dracoon.sdk.crypto.error.UnknownVersionException in project cyberduck by iterate-ch.

the class PresignedMultipartOutputStream method close.

@Override
public void close() throws IOException {
    try {
        if (close.get()) {
            log.warn(String.format("Skip double close of stream %s", this));
            return;
        }
        if (null != canceled.get()) {
            return;
        }
        if (etags.isEmpty()) {
            new SDSTouchFeature(session, nodeid).touch(file, new TransferStatus());
        } else {
            try {
                final CompleteS3FileUploadRequest completeS3FileUploadRequest = new CompleteS3FileUploadRequest().keepShareLinks(overall.isExists() ? new HostPreferences(session.getHost()).getBoolean("sds.upload.sharelinks.keep") : false).resolutionStrategy(overall.isExists() ? CompleteS3FileUploadRequest.ResolutionStrategyEnum.OVERWRITE : CompleteS3FileUploadRequest.ResolutionStrategyEnum.FAIL);
                if (overall.getFilekey() != null) {
                    final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
                    final FileKey fileKey = reader.readValue(overall.getFilekey().array());
                    final EncryptedFileKey encryptFileKey = Crypto.encryptFileKey(TripleCryptConverter.toCryptoPlainFileKey(fileKey), TripleCryptConverter.toCryptoUserPublicKey(session.keyPair().getPublicKeyContainer()));
                    completeS3FileUploadRequest.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptFileKey));
                }
                etags.forEach((key, value) -> completeS3FileUploadRequest.addPartsItem(new S3FileUploadPart().partEtag(StringUtils.remove(value, '"')).partNumber(key)));
                new NodesApi(session.getClient()).completeS3FileUpload(completeS3FileUploadRequest, createFileUploadResponse.getUploadId(), StringUtils.EMPTY);
                // Polling
                final ScheduledThreadPool polling = new ScheduledThreadPool();
                final CountDownLatch done = new CountDownLatch(1);
                final AtomicReference<BackgroundException> failure = new AtomicReference<>();
                final ScheduledFuture f = polling.repeat(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            final S3FileUploadStatus uploadStatus = new NodesApi(session.getClient()).requestUploadStatusFiles(createFileUploadResponse.getUploadId(), StringUtils.EMPTY, null);
                            switch(uploadStatus.getStatus()) {
                                case "finishing":
                                    // Expected
                                    break;
                                case "transfer":
                                    failure.set(new InteroperabilityException(uploadStatus.getStatus()));
                                    done.countDown();
                                case "error":
                                    failure.set(new InteroperabilityException(uploadStatus.getErrorDetails().getMessage()));
                                    done.countDown();
                                case "done":
                                    nodeid.cache(file, String.valueOf(uploadStatus.getNode().getId()));
                                    done.countDown();
                                    break;
                            }
                        } catch (ApiException e) {
                            failure.set(new SDSExceptionMappingService(nodeid).map("Upload {0} failed", e, file));
                            done.countDown();
                        }
                    }
                }, new HostPreferences(session.getHost()).getLong("sds.upload.s3.status.period"), TimeUnit.MILLISECONDS);
                Uninterruptibles.awaitUninterruptibly(done);
                polling.shutdown();
                if (null != failure.get()) {
                    throw failure.get();
                }
            } catch (CryptoSystemException | InvalidFileKeyException | InvalidKeyPairException | UnknownVersionException e) {
                throw new TripleCryptExceptionMappingService().map("Upload {0} failed", e, file);
            } catch (ApiException e) {
                throw new SDSExceptionMappingService(nodeid).map("Upload {0} failed", e, file);
            }
        }
    } catch (BackgroundException e) {
        throw new IOException(e);
    } finally {
        close.set(true);
    }
}
Also used : EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) InvalidKeyPairException(com.dracoon.sdk.crypto.error.InvalidKeyPairException) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ScheduledThreadPool(ch.cyberduck.core.threading.ScheduledThreadPool) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) EncryptedFileKey(com.dracoon.sdk.crypto.model.EncryptedFileKey) InvalidFileKeyException(com.dracoon.sdk.crypto.error.InvalidFileKeyException) S3FileUploadStatus(ch.cyberduck.core.sds.io.swagger.client.model.S3FileUploadStatus) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledFuture(java.util.concurrent.ScheduledFuture) HostPreferences(ch.cyberduck.core.preferences.HostPreferences) S3FileUploadPart(ch.cyberduck.core.sds.io.swagger.client.model.S3FileUploadPart) TripleCryptExceptionMappingService(ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService) UnknownVersionException(com.dracoon.sdk.crypto.error.UnknownVersionException) CompleteS3FileUploadRequest(ch.cyberduck.core.sds.io.swagger.client.model.CompleteS3FileUploadRequest) BackgroundException(ch.cyberduck.core.exception.BackgroundException) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException) CryptoSystemException(com.dracoon.sdk.crypto.error.CryptoSystemException)

Aggregations

UnknownVersionException (com.dracoon.sdk.crypto.error.UnknownVersionException)6 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)5 TripleCryptExceptionMappingService (ch.cyberduck.core.sds.triplecrypt.TripleCryptExceptionMappingService)5 CryptoSystemException (com.dracoon.sdk.crypto.error.CryptoSystemException)5 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)5 IOException (java.io.IOException)5 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)4 ApiException (ch.cyberduck.core.sds.io.swagger.client.ApiException)4 HostPreferences (ch.cyberduck.core.preferences.HostPreferences)3 InvalidFileKeyException (com.dracoon.sdk.crypto.error.InvalidFileKeyException)3 InvalidKeyPairException (com.dracoon.sdk.crypto.error.InvalidKeyPairException)3 EncryptedFileKey (com.dracoon.sdk.crypto.model.EncryptedFileKey)3 BackgroundException (ch.cyberduck.core.exception.BackgroundException)2 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)2 NodesApi (ch.cyberduck.core.sds.io.swagger.client.api.NodesApi)2 CompleteS3FileUploadRequest (ch.cyberduck.core.sds.io.swagger.client.model.CompleteS3FileUploadRequest)2 S3FileUploadPart (ch.cyberduck.core.sds.io.swagger.client.model.S3FileUploadPart)2 S3FileUploadStatus (ch.cyberduck.core.sds.io.swagger.client.model.S3FileUploadStatus)2 ScheduledThreadPool (ch.cyberduck.core.threading.ScheduledThreadPool)2 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)2