Search in sources :

Example 26 with ConnectionCanceledException

use of ch.cyberduck.core.exception.ConnectionCanceledException in project cyberduck by iterate-ch.

the class EueWriteFeatureTest method testWriteCancel.

@Test(expected = TransferCanceledException.class)
public void testWriteCancel() throws Exception {
    final EueResourceIdProvider fileid = new EueResourceIdProvider(session);
    final EueWriteFeature writer = new EueWriteFeature(session, fileid);
    final byte[] content = RandomUtils.nextBytes(32769);
    final Path test = new Path(String.format("{%s", new AlphanumericRandomStringService().random()), EnumSet.of(Path.Type.file));
    {
        final BytecountStreamListener count = new BytecountStreamListener();
        final TransferStatus status = new TransferStatus() {

            @Override
            public void validate() throws ConnectionCanceledException {
                if (count.getSent() >= 32768) {
                    throw new TransferCanceledException();
                }
                super.validate();
            }
        };
        status.setLength(content.length);
        final StatusOutputStream<EueWriteFeature.Chunk> out = writer.write(test, status, new DisabledConnectionCallback());
        assertNotNull(out);
        new StreamCopier(status, status).withListener(count).transfer(new ByteArrayInputStream(content), out);
        assertFalse(new DefaultFindFeature(session).find(test));
        try {
            out.getStatus();
            fail();
        } catch (TransferCanceledException e) {
        // 
        }
    }
    // Rewrite
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);
    final StatusOutputStream<EueWriteFeature.Chunk> out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
    assertFalse(new DefaultFindFeature(session).find(test));
    new EueDeleteFeature(session, fileid).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
Also used : AbstractPath(ch.cyberduck.core.AbstractPath) Path(ch.cyberduck.core.Path) Delete(ch.cyberduck.core.features.Delete) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) StatusOutputStream(ch.cyberduck.core.io.StatusOutputStream) TransferCanceledException(ch.cyberduck.core.exception.TransferCanceledException) BytecountStreamListener(ch.cyberduck.core.BytecountStreamListener) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultFindFeature(ch.cyberduck.core.shared.DefaultFindFeature) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 27 with ConnectionCanceledException

use of ch.cyberduck.core.exception.ConnectionCanceledException in project cyberduck by iterate-ch.

the class IRODSUploadFeatureTest method testInterruptStatus.

@Test
public void testInterruptStatus() throws Exception {
    final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new IRODSProtocol())));
    final Profile profile = new ProfilePlistReader(factory).read(new Local("../profiles/iRODS (iPlant Collaborative).cyberduckprofile"));
    final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials(System.getProperties().getProperty("irods.key"), System.getProperties().getProperty("irods.secret")));
    final IRODSSession session = new IRODSSession(host);
    session.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
    session.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
    final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    final int length = 32770;
    final byte[] content = RandomUtils.nextBytes(length);
    final OutputStream out = local.getOutputStream(false);
    IOUtils.write(content, out);
    out.close();
    final Path test = new Path(new IRODSHomeFinderService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final TransferStatus status = new TransferStatus().withLength(content.length);
    final Checksum checksum = new IRODSUploadFeature(session).upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener() {

        @Override
        public void sent(final long bytes) {
            super.sent(bytes);
            status.setCanceled();
        }
    }, status, new DisabledConnectionCallback());
    try {
        status.validate();
        fail();
    } catch (ConnectionCanceledException e) {
    // 
    }
    assertFalse(status.isComplete());
    session.close();
}
Also used : OutputStream(java.io.OutputStream) ProfilePlistReader(ch.cyberduck.core.serializer.impl.dd.ProfilePlistReader) Profile(ch.cyberduck.core.Profile) Checksum(ch.cyberduck.core.io.Checksum) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) Path(ch.cyberduck.core.Path) DisabledStreamListener(ch.cyberduck.core.io.DisabledStreamListener) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) Local(ch.cyberduck.core.Local) Host(ch.cyberduck.core.Host) ProtocolFactory(ch.cyberduck.core.ProtocolFactory) DisabledCancelCallback(ch.cyberduck.core.DisabledCancelCallback) BandwidthThrottle(ch.cyberduck.core.io.BandwidthThrottle) DisabledHostKeyCallback(ch.cyberduck.core.DisabledHostKeyCallback) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) Credentials(ch.cyberduck.core.Credentials) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 28 with ConnectionCanceledException

use of ch.cyberduck.core.exception.ConnectionCanceledException in project cyberduck by iterate-ch.

the class OpenSSHHostKeyVerifier method verify.

@Override
public boolean verify(final Host host, final PublicKey key) throws ConnectionCanceledException, ChecksumException {
    if (null == database) {
        log.warn(String.format("Missing database %s", database));
        return super.verify(host, key);
    }
    final KeyType type = KeyType.fromKey(key);
    if (type == KeyType.UNKNOWN) {
        return false;
    }
    boolean foundApplicableHostEntry = false;
    for (OpenSSHKnownHosts.KnownHostEntry entry : database.entries()) {
        try {
            if (entry.appliesTo(type, format(host))) {
                foundApplicableHostEntry = true;
                if (entry.verify(key)) {
                    return true;
                }
            }
        } catch (IOException e) {
            log.error(String.format("Failure verifying host key entry %s. %s", entry, e.getMessage()));
            return false;
        }
    }
    if (foundApplicableHostEntry) {
        try {
            return this.isChangedKeyAccepted(host, key);
        } catch (ConnectionCanceledException | ChecksumException e) {
            return false;
        }
    }
    try {
        return this.isUnknownKeyAccepted(host, key);
    } catch (ConnectionCanceledException | ChecksumException e) {
        return false;
    }
}
Also used : KeyType(net.schmizz.sshj.common.KeyType) OpenSSHKnownHosts(net.schmizz.sshj.transport.verification.OpenSSHKnownHosts) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) ChecksumException(ch.cyberduck.core.exception.ChecksumException) IOException(java.io.IOException)

Example 29 with ConnectionCanceledException

use of ch.cyberduck.core.exception.ConnectionCanceledException in project cyberduck by iterate-ch.

the class S3VersionedObjectListService method list.

@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
    final ThreadPool pool = ThreadPoolFactory.get("list", concurrency);
    try {
        final String prefix = this.createPrefix(directory);
        final Path bucket = containerService.getContainer(directory);
        final AttributedList<Path> children = new AttributedList<>();
        final List<Future<Path>> folders = new ArrayList<>();
        String priorLastKey = null;
        String priorLastVersionId = null;
        long revision = 0L;
        String lastKey = null;
        boolean hasDirectoryPlaceholder = bucket.isRoot() || containerService.isContainer(directory);
        do {
            final VersionOrDeleteMarkersChunk chunk = session.getClient().listVersionedObjectsChunked(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName(), prefix, String.valueOf(Path.DELIMITER), new HostPreferences(session.getHost()).getInteger("s3.listing.chunksize"), priorLastKey, priorLastVersionId, false);
            // Amazon S3 returns object versions in the order in which they were stored, with the most recently stored returned first.
            for (BaseVersionOrDeleteMarker marker : chunk.getItems()) {
                final String key = URIEncoder.decode(marker.getKey());
                if (String.valueOf(Path.DELIMITER).equals(PathNormalizer.normalize(key))) {
                    log.warn(String.format("Skipping prefix %s", key));
                    continue;
                }
                if (new SimplePathPredicate(PathNormalizer.compose(bucket, key)).test(directory)) {
                    // Placeholder object, skip
                    hasDirectoryPlaceholder = true;
                    continue;
                }
                final PathAttributes attr = new PathAttributes();
                attr.setVersionId("null".equals(marker.getVersionId()) ? null : marker.getVersionId());
                if (!StringUtils.equals(lastKey, key)) {
                    // Reset revision for next file
                    revision = 0L;
                }
                attr.setRevision(++revision);
                attr.setDuplicate(marker.isDeleteMarker() && marker.isLatest() || !marker.isLatest());
                if (marker.isDeleteMarker()) {
                    attr.setCustom(Collections.singletonMap(KEY_DELETE_MARKER, String.valueOf(true)));
                }
                attr.setModificationDate(marker.getLastModified().getTime());
                attr.setRegion(bucket.attributes().getRegion());
                if (marker instanceof S3Version) {
                    final S3Version object = (S3Version) marker;
                    attr.setSize(object.getSize());
                    if (StringUtils.isNotBlank(object.getEtag())) {
                        attr.setETag(StringUtils.remove(object.getEtag(), "\""));
                        // The ETag will only be the MD5 of the object data when the object is stored as plaintext or encrypted
                        // using SSE-S3. If the object is encrypted using another method (such as SSE-C or SSE-KMS) the ETag is
                        // not the MD5 of the object data.
                        attr.setChecksum(Checksum.parse(StringUtils.remove(object.getEtag(), "\"")));
                    }
                    if (StringUtils.isNotBlank(object.getStorageClass())) {
                        attr.setStorageClass(object.getStorageClass());
                    }
                }
                final Path f = new Path(directory.isDirectory() ? directory : directory.getParent(), PathNormalizer.name(key), EnumSet.of(Path.Type.file), attr);
                if (metadata) {
                    f.withAttributes(attributes.find(f));
                }
                children.add(f);
                lastKey = key;
            }
            if (references) {
                for (Path f : children) {
                    if (f.attributes().isDuplicate()) {
                        final Path latest = children.find(new LatestVersionPathPredicate(f));
                        if (latest != null) {
                            // Reference version
                            final AttributedList<Path> versions = new AttributedList<>(latest.attributes().getVersions());
                            versions.add(f);
                            latest.attributes().setVersions(versions);
                        } else {
                            log.warn(String.format("No current version found for %s", f));
                        }
                    }
                }
            }
            final String[] prefixes = chunk.getCommonPrefixes();
            for (String common : prefixes) {
                if (String.valueOf(Path.DELIMITER).equals(common)) {
                    log.warn(String.format("Skipping prefix %s", common));
                    continue;
                }
                final String key = PathNormalizer.normalize(URIEncoder.decode(common));
                if (new SimplePathPredicate(new Path(bucket, key, EnumSet.of(Path.Type.directory))).test(directory)) {
                    continue;
                }
                folders.add(this.submit(pool, bucket, directory, URIEncoder.decode(common)));
            }
            priorLastKey = null != chunk.getNextKeyMarker() ? URIEncoder.decode(chunk.getNextKeyMarker()) : null;
            priorLastVersionId = chunk.getNextVersionIdMarker();
            listener.chunk(directory, children);
        } while (priorLastKey != null);
        for (Future<Path> future : folders) {
            try {
                children.add(future.get());
            } catch (InterruptedException e) {
                log.error("Listing versioned objects failed with interrupt failure");
                throw new ConnectionCanceledException(e);
            } catch (ExecutionException e) {
                log.warn(String.format("Listing versioned objects failed with execution failure %s", e.getMessage()));
                if (e.getCause() instanceof BackgroundException) {
                    throw (BackgroundException) e.getCause();
                }
                throw new BackgroundException(e.getCause());
            }
        }
        listener.chunk(directory, children);
        if (!hasDirectoryPlaceholder && children.isEmpty()) {
            // Only for AWS
            if (S3Session.isAwsHostname(session.getHost().getHostname())) {
                if (StringUtils.isEmpty(RequestEntityRestStorageService.findBucketInHostname(session.getHost()))) {
                    throw new NotfoundException(directory.getAbsolute());
                }
            } else {
                // Handle missing prefix for directory placeholders in Minio
                final VersionOrDeleteMarkersChunk chunk = session.getClient().listVersionedObjectsChunked(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName(), String.format("%s%s", this.createPrefix(directory.getParent()), directory.getName()), String.valueOf(Path.DELIMITER), 1, null, null, false);
                if (Arrays.stream(chunk.getCommonPrefixes()).map(URIEncoder::decode).noneMatch(common -> common.equals(prefix))) {
                    throw new NotfoundException(directory.getAbsolute());
                }
            }
        }
        return children;
    } catch (ServiceException e) {
        throw new S3ExceptionMappingService().map("Listing directory {0} failed", e, directory);
    } finally {
        // Cancel future tasks
        pool.shutdown(false);
    }
}
Also used : S3Version(org.jets3t.service.model.S3Version) ThreadPool(ch.cyberduck.core.threading.ThreadPool) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) Path(ch.cyberduck.core.Path) NotfoundException(ch.cyberduck.core.exception.NotfoundException) BaseVersionOrDeleteMarker(org.jets3t.service.model.BaseVersionOrDeleteMarker) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) VersionOrDeleteMarkersChunk(org.jets3t.service.VersionOrDeleteMarkersChunk) PathAttributes(ch.cyberduck.core.PathAttributes) HostPreferences(ch.cyberduck.core.preferences.HostPreferences) AttributedList(ch.cyberduck.core.AttributedList) ServiceException(org.jets3t.service.ServiceException) Future(java.util.concurrent.Future) SimplePathPredicate(ch.cyberduck.core.SimplePathPredicate) BackgroundException(ch.cyberduck.core.exception.BackgroundException)

Example 30 with ConnectionCanceledException

use of ch.cyberduck.core.exception.ConnectionCanceledException in project cyberduck by iterate-ch.

the class S3MultipartCopyFeature method copy.

@Override
protected String copy(final Path source, final S3Object destination, final TransferStatus status, final StreamListener listener) throws BackgroundException {
    try {
        final List<MultipartPart> completed = new ArrayList<>();
        // ID for the initiated multipart upload.
        final MultipartUpload multipart = session.getClient().multipartStartUpload(destination.getBucketName(), destination);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Multipart upload started for %s with ID %s", multipart.getObjectKey(), multipart.getUploadId()));
        }
        final long size = status.getLength();
        long remaining = size;
        long offset = 0;
        final List<Future<MultipartPart>> parts = new ArrayList<>();
        for (int partNumber = 1; remaining > 0; partNumber++) {
            // Last part can be less than 5 MB. Adjust part size.
            final Long length = Math.min(Math.max((size / S3DefaultMultipartService.MAXIMUM_UPLOAD_PARTS), partsize), remaining);
            // Submit to queue
            parts.add(this.submit(source, multipart, partNumber, offset, length));
            remaining -= length;
            offset += length;
        }
        for (Future<MultipartPart> future : parts) {
            try {
                final MultipartPart part = future.get();
                completed.add(part);
                listener.sent(part.getSize());
            } catch (InterruptedException e) {
                log.error("Part upload failed with interrupt failure");
                throw new ConnectionCanceledException(e);
            } catch (ExecutionException e) {
                log.warn(String.format("Part upload failed with execution failure %s", e.getMessage()));
                if (e.getCause() instanceof BackgroundException) {
                    throw (BackgroundException) e.getCause();
                }
                throw new BackgroundException(e.getCause());
            }
        }
        // Combining all the given parts into the final object. Processing of a Complete Multipart Upload request
        // could take several minutes to complete. Because a request could fail after the initial 200 OK response
        // has been sent, it is important that you check the response body to determine whether the request succeeded.
        final MultipartCompleted complete = session.getClient().multipartCompleteUpload(multipart, completed);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Completed multipart upload for %s with checksum %s", complete.getObjectKey(), complete.getEtag()));
        }
        return complete.getVersionId();
    } catch (ServiceException e) {
        throw new S3ExceptionMappingService().map("Cannot copy {0}", e, source);
    } finally {
        pool.shutdown(false);
    }
}
Also used : MultipartPart(org.jets3t.service.model.MultipartPart) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) ArrayList(java.util.ArrayList) MultipartUpload(org.jets3t.service.model.MultipartUpload) ServiceException(org.jets3t.service.ServiceException) S3ServiceException(org.jets3t.service.S3ServiceException) Future(java.util.concurrent.Future) MultipartCompleted(org.jets3t.service.model.MultipartCompleted) ExecutionException(java.util.concurrent.ExecutionException) BackgroundException(ch.cyberduck.core.exception.BackgroundException)

Aggregations

ConnectionCanceledException (ch.cyberduck.core.exception.ConnectionCanceledException)66 Path (ch.cyberduck.core.Path)28 BackgroundException (ch.cyberduck.core.exception.BackgroundException)17 ArrayList (java.util.ArrayList)16 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)12 IOException (java.io.IOException)11 ExecutionException (java.util.concurrent.ExecutionException)11 Future (java.util.concurrent.Future)11 ThreadPool (ch.cyberduck.core.threading.ThreadPool)10 Test (org.junit.Test)9 ListService (ch.cyberduck.core.ListService)8 PathContainerService (ch.cyberduck.core.PathContainerService)8 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)7 HashMap (java.util.HashMap)7 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)6 IntegrationTest (ch.cyberduck.test.IntegrationTest)6 AttributedList (ch.cyberduck.core.AttributedList)5 Host (ch.cyberduck.core.Host)5 LinkedHashMap (java.util.LinkedHashMap)5 ChecksumException (ch.cyberduck.core.exception.ChecksumException)4