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