Search in sources :

Example 1 with BackgroundException

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

the class BrickWriteFeature method write.

@Override
public HttpResponseOutputStream<FileEntity> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    final String uploadUri;
    FileUploadPartEntity uploadPartEntity = null;
    if (StringUtils.isBlank(status.getUrl())) {
        uploadPartEntity = new BrickUploadFeature(session, this).startUpload(file);
        uploadUri = uploadPartEntity.getUploadUri();
    } else {
        uploadUri = status.getUrl();
    }
    final HttpResponseOutputStream<FileEntity> stream = this.write(file, status, new DelayedHttpEntityCallable<FileEntity>() {

        @Override
        public FileEntity call(final AbstractHttpEntity entity) throws BackgroundException {
            try {
                final HttpPut request = new HttpPut(uploadUri);
                request.setEntity(entity);
                request.setHeader(HttpHeaders.CONTENT_TYPE, MimeTypeService.DEFAULT_CONTENT_TYPE);
                final HttpResponse response = session.getClient().execute(request);
                // Validate response
                try {
                    switch(response.getStatusLine().getStatusCode()) {
                        case HttpStatus.SC_OK:
                            // Upload complete
                            if (response.containsHeader("ETag")) {
                                if (log.isInfoEnabled()) {
                                    log.info(String.format("Received response %s for part number %d", response, status.getPart()));
                                }
                                if (HashAlgorithm.md5.equals(status.getChecksum().algorithm)) {
                                    final Checksum etag = Checksum.parse(StringUtils.remove(response.getFirstHeader("ETag").getValue(), '"'));
                                    if (!status.getChecksum().equals(etag)) {
                                        throw new ChecksumException(MessageFormat.format(LocaleFactory.localizedString("Upload {0} failed", "Error"), file.getName()), MessageFormat.format("Mismatch between {0} hash {1} of uploaded data and ETag {2} returned by the server", etag.algorithm.toString(), status.getChecksum().hash, etag.hash));
                                    }
                                }
                                return null;
                            } else {
                                log.error(String.format("Missing ETag in response %s", response));
                                throw new InteroperabilityException(response.getStatusLine().getReasonPhrase());
                            }
                        default:
                            EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity()));
                            throw new DefaultHttpResponseExceptionMappingService().map(new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
                    }
                } finally {
                    EntityUtils.consume(response.getEntity());
                }
            } catch (HttpResponseException e) {
                throw new DefaultHttpResponseExceptionMappingService().map(e);
            } catch (IOException e) {
                throw new DefaultIOExceptionMappingService().map(e);
            }
        }

        @Override
        public long getContentLength() {
            return status.getLength();
        }
    });
    if (StringUtils.isBlank(status.getUrl())) {
        final String ref = uploadPartEntity.getRef();
        return new HttpResponseOutputStream<FileEntity>(new ProxyOutputStream(stream), new BrickAttributesFinderFeature(session), status) {

            private final AtomicBoolean close = new AtomicBoolean();

            @Override
            public FileEntity getStatus() throws BackgroundException {
                return stream.getStatus();
            }

            @Override
            public void close() throws IOException {
                if (close.get()) {
                    log.warn(String.format("Skip double close of stream %s", this));
                    return;
                }
                super.close();
                try {
                    new BrickUploadFeature(session, BrickWriteFeature.this).completeUpload(file, ref, status, Collections.singletonList(status));
                } catch (BackgroundException e) {
                    throw new IOException(e.getMessage(), e);
                } finally {
                    close.set(true);
                }
            }
        };
    }
    return stream;
}
Also used : FileEntity(ch.cyberduck.core.brick.io.swagger.client.model.FileEntity) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) ChecksumException(ch.cyberduck.core.exception.ChecksumException) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) FileUploadPartEntity(ch.cyberduck.core.brick.io.swagger.client.model.FileUploadPartEntity) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) Checksum(ch.cyberduck.core.io.Checksum) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) HttpResponseOutputStream(ch.cyberduck.core.http.HttpResponseOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BackgroundException(ch.cyberduck.core.exception.BackgroundException)

Example 2 with BackgroundException

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

the class BrickPairingSchedulerFeature method repeat.

public void repeat(final PasswordCallback callback) {
    final long timeout = new HostPreferences(session.getHost()).getLong("brick.pairing.interrupt.ms");
    final long start = System.currentTimeMillis();
    scheduler.repeat(() -> {
        try {
            if (System.currentTimeMillis() - start > timeout) {
                throw new ConnectionCanceledException(String.format("Interrupt polling for pairing key after %d", timeout));
            }
            this.operate(callback);
        } catch (ConnectionCanceledException e) {
            log.warn(String.format("Cancel processing scheduled task. %s", e.getMessage()), e);
            callback.close(null);
            this.shutdown();
        } catch (BackgroundException e) {
            log.warn(String.format("Failure processing scheduled task. %s", e.getMessage()), e);
            callback.close(null);
            this.shutdown();
        }
    }, new HostPreferences(session.getHost()).getLong("brick.pairing.interval.ms"), TimeUnit.MILLISECONDS);
}
Also used : ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) BackgroundException(ch.cyberduck.core.exception.BackgroundException) HostPreferences(ch.cyberduck.core.preferences.HostPreferences)

Example 3 with BackgroundException

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

the class AbstractUploadFilter method complete.

@Override
public void complete(final Path file, final Local local, final TransferOptions options, final TransferStatus status, final ProgressListener listener) throws BackgroundException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Complete %s with status %s", file.getAbsolute(), status));
    }
    if (status.isComplete()) {
        if (!Permission.EMPTY.equals(status.getPermission())) {
            final UnixPermission feature = session.getFeature(UnixPermission.class);
            if (feature != null) {
                try {
                    listener.message(MessageFormat.format(LocaleFactory.localizedString("Changing permission of {0} to {1}", "Status"), file.getName(), status.getPermission()));
                    feature.setUnixPermission(file, status.getPermission());
                } catch (BackgroundException e) {
                    // Ignore
                    log.warn(e.getMessage());
                }
            }
        }
        if (!Acl.EMPTY.equals(status.getAcl())) {
            final AclPermission feature = session.getFeature(AclPermission.class);
            if (feature != null) {
                try {
                    listener.message(MessageFormat.format(LocaleFactory.localizedString("Changing permission of {0} to {1}", "Status"), file.getName(), status.getAcl()));
                    feature.setPermission(file, status.getAcl());
                } catch (BackgroundException e) {
                    // Ignore
                    log.warn(e.getMessage());
                }
            }
        }
        if (status.getTimestamp() != null) {
            if (!session.getFeature(Write.class).timestamp()) {
                final Timestamp feature = session.getFeature(Timestamp.class);
                if (feature != null) {
                    try {
                        listener.message(MessageFormat.format(LocaleFactory.localizedString("Changing timestamp of {0} to {1}", "Status"), file.getName(), UserDateFormatterFactory.get().getShortFormat(status.getTimestamp())));
                        feature.setTimestamp(file, status);
                    } catch (BackgroundException e) {
                        // Ignore
                        log.warn(e.getMessage());
                    }
                }
            }
        }
        if (file.isFile()) {
            if (status.getDisplayname().remote != null) {
                final Move move = session.getFeature(Move.class);
                if (log.isInfoEnabled()) {
                    log.info(String.format("Rename file %s to %s", file, status.getDisplayname().remote));
                }
                move.move(file, status.getDisplayname().remote, status, new Delete.DisabledCallback(), new DisabledConnectionCallback());
            }
        }
    }
}
Also used : Delete(ch.cyberduck.core.features.Delete) AclPermission(ch.cyberduck.core.features.AclPermission) Move(ch.cyberduck.core.features.Move) Timestamp(ch.cyberduck.core.features.Timestamp) BackgroundException(ch.cyberduck.core.exception.BackgroundException) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) UnixPermission(ch.cyberduck.core.features.UnixPermission)

Example 4 with BackgroundException

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

the class MoveWorker method run.

@Override
public Map<Path, Path> run(final Session<?> session) throws BackgroundException {
    final Session<?> destination = target.borrow(new BackgroundActionState() {

        @Override
        public boolean isCanceled() {
            return MoveWorker.this.isCanceled();
        }

        @Override
        public boolean isRunning() {
            return true;
        }
    });
    try {
        final Move feature = session.getFeature(Move.class).withTarget(destination);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Run with feature %s", feature));
        }
        final ListService list = session.getFeature(ListService.class);
        // sort ascending by timestamp to move older versions first
        final Map<Path, Path> sorted = new TreeMap<>(new VersionsComparator(true));
        sorted.putAll(files);
        final Map<Path, Path> result = new HashMap<>();
        for (Map.Entry<Path, Path> entry : sorted.entrySet()) {
            if (this.isCanceled()) {
                throw new ConnectionCanceledException();
            }
            final Map<Path, Path> recursive = this.compile(feature, list, entry.getKey(), entry.getValue());
            if (log.isDebugEnabled()) {
                log.debug(String.format("Compiled recursive list %s", recursive));
            }
            for (Map.Entry<Path, Path> r : recursive.entrySet()) {
                if (r.getKey().isDirectory() && !feature.isRecursive(r.getKey(), r.getValue())) {
                    log.warn(String.format("Move operation is not recursive. Create directory %s", r.getValue()));
                    // Create directory unless copy implementation is recursive
                    result.put(r.getKey(), session.getFeature(Directory.class).mkdir(r.getValue(), new TransferStatus().withRegion(r.getKey().attributes().getRegion())));
                } else {
                    final TransferStatus status = this.status(session, r);
                    result.put(r.getKey(), feature.move(r.getKey(), r.getValue(), status, new Delete.Callback() {

                        @Override
                        public void delete(final Path file) {
                            listener.message(MessageFormat.format(LocaleFactory.localizedString("Deleting {0}", "Status"), file.getName()));
                        }
                    }, callback));
                }
            }
            // Find previous folders to be deleted
            final List<Path> folders = recursive.entrySet().stream().filter(f -> !feature.isRecursive(f.getKey(), f.getValue())).collect(Collectors.toCollection(ArrayList::new)).stream().map(Map.Entry::getKey).filter(Path::isDirectory).collect(Collectors.toCollection(ArrayList::new));
            if (!folders.isEmpty()) {
                // Must delete inverse
                Collections.reverse(folders);
                final Delete delete = session.getFeature(Delete.class);
                for (Path folder : folders) {
                    log.warn(String.format("Delete source directory %s", folder));
                    final TransferStatus status = new TransferStatus().withLockId(this.getLockId(folder));
                    delete.delete(Collections.singletonMap(folder, status), callback, new Delete.DisabledCallback());
                }
            }
        }
        return result;
    } finally {
        target.release(destination, null);
    }
}
Also used : Path(ch.cyberduck.core.Path) CachingFindFeature(ch.cyberduck.core.CachingFindFeature) Move(ch.cyberduck.core.features.Move) Delete(ch.cyberduck.core.features.Delete) CachingAttributesFinderFeature(ch.cyberduck.core.CachingAttributesFinderFeature) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) HashMap(java.util.HashMap) ListService(ch.cyberduck.core.ListService) StringUtils(org.apache.commons.lang3.StringUtils) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) Cache(ch.cyberduck.core.Cache) Map(java.util.Map) Find(ch.cyberduck.core.features.Find) AttributesFinder(ch.cyberduck.core.features.AttributesFinder) Session(ch.cyberduck.core.Session) AttributedList(ch.cyberduck.core.AttributedList) LocaleFactory(ch.cyberduck.core.LocaleFactory) MappingMimeTypeService(ch.cyberduck.core.MappingMimeTypeService) TimestampComparator(ch.cyberduck.ui.comparator.TimestampComparator) BackgroundException(ch.cyberduck.core.exception.BackgroundException) DefaultAttributesFinderFeature(ch.cyberduck.core.shared.DefaultAttributesFinderFeature) Collectors(java.util.stream.Collectors) BackgroundActionState(ch.cyberduck.core.threading.BackgroundActionState) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TreeMap(java.util.TreeMap) SessionPool(ch.cyberduck.core.pool.SessionPool) Path(ch.cyberduck.core.Path) ProgressListener(ch.cyberduck.core.ProgressListener) DefaultFindFeature(ch.cyberduck.core.shared.DefaultFindFeature) Directory(ch.cyberduck.core.features.Directory) ConnectionCallback(ch.cyberduck.core.ConnectionCallback) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) Delete(ch.cyberduck.core.features.Delete) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) BackgroundActionState(ch.cyberduck.core.threading.BackgroundActionState) TreeMap(java.util.TreeMap) ListService(ch.cyberduck.core.ListService) ConnectionCallback(ch.cyberduck.core.ConnectionCallback) Move(ch.cyberduck.core.features.Move) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 5 with BackgroundException

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

the class DecryptingListProgressListener method visit.

@Override
public void visit(final AttributedList<Path> list, final int index, final Path f) {
    try {
        f.getType().add(Path.Type.encrypted);
        if (f.attributes().getVersions().isEmpty()) {
            list.set(index, vault.decrypt(session, f));
        } else {
            final AttributedList<Path> versions = new AttributedList<>();
            for (Path version : f.attributes().getVersions()) {
                versions.add(vault.decrypt(session, version));
            }
            list.set(index, vault.decrypt(session, f).withAttributes(new PathAttributes(f.attributes()).withVersions(versions)));
        }
    } catch (BackgroundException e) {
        log.error(String.format("Failure %s decrypting %s", e, f));
        list.remove(index);
    }
}
Also used : Path(ch.cyberduck.core.Path) AttributedList(ch.cyberduck.core.AttributedList) PathAttributes(ch.cyberduck.core.PathAttributes) BackgroundException(ch.cyberduck.core.exception.BackgroundException)

Aggregations

BackgroundException (ch.cyberduck.core.exception.BackgroundException)119 Test (org.junit.Test)47 Path (ch.cyberduck.core.Path)43 IOException (java.io.IOException)36 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)34 IntegrationTest (ch.cyberduck.test.IntegrationTest)21 Host (ch.cyberduck.core.Host)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 DisabledConnectionCallback (ch.cyberduck.core.DisabledConnectionCallback)18 Delete (ch.cyberduck.core.features.Delete)18 ConnectionCanceledException (ch.cyberduck.core.exception.ConnectionCanceledException)17 NotfoundException (ch.cyberduck.core.exception.NotfoundException)17 DisabledPasswordCallback (ch.cyberduck.core.DisabledPasswordCallback)16 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)16 ArrayList (java.util.ArrayList)15 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)14 Local (ch.cyberduck.core.Local)13 TestProtocol (ch.cyberduck.core.TestProtocol)13 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)12 DisabledLoginCallback (ch.cyberduck.core.DisabledLoginCallback)12