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;
}
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);
}
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());
}
}
}
}
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);
}
}
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);
}
}
Aggregations