Search in sources :

Example 1 with Checksum

use of ch.cyberduck.core.io.Checksum in project cyberduck by iterate-ch.

the class BoxWriteFeature method write.

@Override
public HttpResponseOutputStream<File> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    final DelayedHttpEntityCallable<File> command = new DelayedHttpEntityCallable<File>() {

        @Override
        public File call(final AbstractHttpEntity entity) throws BackgroundException {
            try {
                final HttpPost request;
                if (status.isExists()) {
                    request = new HttpPost(String.format("%s/files/%s/content?fields=%s", client.getBasePath(), fileid.getFileId(file, new DisabledListProgressListener()), String.join(",", BoxAttributesFinderFeature.DEFAULT_FIELDS)));
                } else {
                    request = new HttpPost(String.format("%s/files/content?fields=%s", client.getBasePath(), String.join(",", BoxAttributesFinderFeature.DEFAULT_FIELDS)));
                }
                final Checksum checksum = status.getChecksum();
                if (Checksum.NONE != checksum) {
                    switch(checksum.algorithm) {
                        case sha1:
                            request.addHeader(HttpHeaders.CONTENT_MD5, checksum.hash);
                    }
                }
                final ByteArrayOutputStream content = new ByteArrayOutputStream();
                new JSON().getContext(null).writeValue(content, new FilescontentAttributes().name(file.getName()).parent(new FilescontentAttributesParent().id(fileid.getFileId(file.getParent(), new DisabledListProgressListener()))).contentModifiedAt(status.getTimestamp() != null ? new DateTime(status.getTimestamp()) : null));
                final MultipartEntityBuilder multipart = MultipartEntityBuilder.create();
                multipart.addBinaryBody("attributes", content.toByteArray());
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                entity.writeTo(out);
                multipart.addBinaryBody("file", out.toByteArray(), null == status.getMime() ? ContentType.APPLICATION_OCTET_STREAM : ContentType.create(status.getMime()), file.getName());
                request.setEntity(multipart.build());
                if (status.isExists()) {
                    if (StringUtils.isNotBlank(status.getRemote().getETag())) {
                        request.addHeader(new BasicHeader(HttpHeaders.IF_MATCH, status.getRemote().getETag()));
                    } else {
                        log.warn(String.format("Missing remote attributes in transfer status to read current ETag for %s", file));
                    }
                }
                final Files files = session.getClient().execute(request, new BoxClientErrorResponseHandler<Files>() {

                    @Override
                    public Files handleEntity(final HttpEntity entity) throws IOException {
                        return new JSON().getContext(null).readValue(entity.getContent(), Files.class);
                    }
                });
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Received response %s for upload of %s", files, file));
                }
                if (files.getEntries().stream().findFirst().isPresent()) {
                    return files.getEntries().stream().findFirst().get();
                }
                throw new NotfoundException(file.getAbsolute());
            } catch (HttpResponseException e) {
                throw new DefaultHttpResponseExceptionMappingService().map(e);
            } catch (IOException e) {
                throw new DefaultIOExceptionMappingService().map("Upload {0} failed", e, file);
            }
        }

        @Override
        public long getContentLength() {
            return -1L;
        }
    };
    return this.write(file, status, command);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) NotfoundException(ch.cyberduck.core.exception.NotfoundException) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) JSON(ch.cyberduck.core.box.io.swagger.client.JSON) FilescontentAttributes(ch.cyberduck.core.box.io.swagger.client.model.FilescontentAttributes) HttpResponseException(org.apache.http.client.HttpResponseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FilescontentAttributesParent(ch.cyberduck.core.box.io.swagger.client.model.FilescontentAttributesParent) DelayedHttpEntityCallable(ch.cyberduck.core.http.DelayedHttpEntityCallable) DateTime(org.joda.time.DateTime) Checksum(ch.cyberduck.core.io.Checksum) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) Files(ch.cyberduck.core.box.io.swagger.client.model.Files) File(ch.cyberduck.core.box.io.swagger.client.model.File) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BasicHeader(org.apache.http.message.BasicHeader)

Example 2 with Checksum

use of ch.cyberduck.core.io.Checksum 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 3 with Checksum

use of ch.cyberduck.core.io.Checksum in project cyberduck by iterate-ch.

the class PathAttributesDictionary method deserialize.

public <T> PathAttributes deserialize(T serialized) {
    final Deserializer dict = factory.create(serialized);
    final PathAttributes attributes = new PathAttributes();
    final String sizeObj = dict.stringForKey("Size");
    if (sizeObj != null) {
        attributes.setSize(Long.parseLong(sizeObj));
    }
    final String quotaObj = dict.stringForKey("Quota");
    if (quotaObj != null) {
        attributes.setQuota(Long.parseLong(quotaObj));
    }
    final String modifiedObj = dict.stringForKey("Modified");
    if (modifiedObj != null) {
        attributes.setModificationDate(Long.parseLong(modifiedObj));
    }
    final String createdObj = dict.stringForKey("Created");
    if (createdObj != null) {
        attributes.setCreationDate(Long.parseLong(createdObj));
    }
    final String revisionObj = dict.stringForKey("Revision");
    if (revisionObj != null) {
        attributes.setRevision(Long.parseLong(revisionObj));
    }
    final List<T> versionsObj = dict.listForKey("Versions");
    if (versionsObj != null) {
        final AttributedList<Path> versions = new AttributedList<>();
        for (T versionDict : versionsObj) {
            versions.add(new PathDictionary(factory).deserialize(versionDict));
        }
        attributes.setVersions(versions);
    }
    final String etagObj = dict.stringForKey("ETag");
    if (etagObj != null) {
        attributes.setETag(etagObj);
    }
    final Object permissionObj = dict.objectForKey("Permission");
    if (permissionObj != null) {
        attributes.setPermission(new PermissionDictionary().deserialize(permissionObj));
    }
    final Object aclObj = dict.objectForKey("Acl");
    if (aclObj != null) {
        attributes.setAcl(new AclDictionary().deserialize(aclObj));
    }
    if (dict.mapForKey("Link") != null) {
        final Map<String, String> link = dict.mapForKey("Link");
        attributes.setLink(new DescriptiveUrl(URI.create(link.get("Url")), DescriptiveUrl.Type.valueOf(link.get("Type"))));
    } else {
        final String linkObj = dict.stringForKey("Link");
        if (linkObj != null) {
            attributes.setLink(new DescriptiveUrl(URI.create(linkObj), DescriptiveUrl.Type.http));
        }
    }
    if (dict.mapForKey("Checksum") != null) {
        final Map<String, String> checksum = dict.mapForKey("Checksum");
        attributes.setChecksum(new Checksum(HashAlgorithm.valueOf(checksum.get("Algorithm")), checksum.get("Hash")));
    } else {
        attributes.setChecksum(Checksum.parse(dict.stringForKey("Checksum")));
    }
    attributes.setVersionId(dict.stringForKey("Version"));
    attributes.setFileId(dict.stringForKey("File Id"));
    attributes.setLockId(dict.stringForKey("Lock Id"));
    final String duplicateObj = dict.stringForKey("Duplicate");
    if (duplicateObj != null) {
        attributes.setDuplicate(Boolean.parseBoolean(duplicateObj));
    }
    final String hiddenObj = dict.stringForKey("Hidden");
    if (hiddenObj != null) {
        attributes.setHidden(Boolean.parseBoolean(hiddenObj));
    }
    attributes.setMetadata(Collections.emptyMap());
    attributes.setRegion(dict.stringForKey("Region"));
    attributes.setStorageClass(dict.stringForKey("Storage Class"));
    final Object vaultObj = dict.objectForKey("Vault");
    if (vaultObj != null) {
        attributes.setVault(new PathDictionary(factory).deserialize(vaultObj));
    }
    final Map<String, String> customObj = dict.mapForKey("Custom");
    if (customObj != null) {
        attributes.setCustom(customObj);
    }
    return attributes;
}
Also used : Path(ch.cyberduck.core.Path) PathAttributes(ch.cyberduck.core.PathAttributes) DescriptiveUrl(ch.cyberduck.core.DescriptiveUrl) AttributedList(ch.cyberduck.core.AttributedList) Checksum(ch.cyberduck.core.io.Checksum)

Example 4 with Checksum

use of ch.cyberduck.core.io.Checksum in project cyberduck by iterate-ch.

the class ChecksumProfileMatcherTest method testEqual.

@Test
public void testEqual() throws Exception {
    // Managed profile
    final ProfileDescription remote = new ProfileDescription(ProtocolFactory.get(), new Checksum(HashAlgorithm.md5, "d41d8cd98f00b204e9800998ecf8427e"), null) {

        @Override
        public boolean isLatest() {
            return true;
        }
    };
    final ProfileDescription local = new ProfileDescription(ProtocolFactory.get(), new Checksum(HashAlgorithm.md5, "d41d8cd98f00b204e9800998ecf8427e"), null);
    assertFalse(new ChecksumProfileMatcher(Stream.of(remote).collect(Collectors.toSet())).compare(local).isPresent());
}
Also used : Checksum(ch.cyberduck.core.io.Checksum) Test(org.junit.Test)

Example 5 with Checksum

use of ch.cyberduck.core.io.Checksum in project cyberduck by iterate-ch.

the class ChecksumProfileMatcherTest method testLocalOnly.

@Test
public void testLocalOnly() throws Exception {
    // Local only profile
    final ProfileDescription local = new ProfileDescription(ProtocolFactory.get(), new Checksum(HashAlgorithm.md5, "d41d8cd98f00b204e9800998ecf8427e"), null);
    assertFalse(new ChecksumProfileMatcher(Stream.<ProfileDescription>empty().collect(Collectors.toSet())).compare(local).isPresent());
}
Also used : Checksum(ch.cyberduck.core.io.Checksum) Test(org.junit.Test)

Aggregations

Checksum (ch.cyberduck.core.io.Checksum)40 Test (org.junit.Test)19 Path (ch.cyberduck.core.Path)17 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)17 IntegrationTest (ch.cyberduck.test.IntegrationTest)13 DisabledConnectionCallback (ch.cyberduck.core.DisabledConnectionCallback)12 DisabledLoginCallback (ch.cyberduck.core.DisabledLoginCallback)12 PathAttributes (ch.cyberduck.core.PathAttributes)11 Delete (ch.cyberduck.core.features.Delete)11 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 IOException (java.io.IOException)9 InputStream (java.io.InputStream)9 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)8 Local (ch.cyberduck.core.Local)8 StreamCopier (ch.cyberduck.core.io.StreamCopier)7 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)6 DefaultFindFeature (ch.cyberduck.core.shared.DefaultFindFeature)6 HashMap (java.util.HashMap)6 Host (ch.cyberduck.core.Host)5