use of ch.cyberduck.core.exception.NotfoundException 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.exception.NotfoundException in project cyberduck by iterate-ch.
the class BrickAttributesFinderFeatureTest method testFindDirectory.
@Test
public void testFindDirectory() throws Exception {
final Path folder = new BrickDirectoryFeature(session).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final Path test = new BrickDirectoryFeature(session).mkdir(new Path(folder, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final BrickAttributesFinderFeature f = new BrickAttributesFinderFeature(session);
final PathAttributes attributes = f.find(test);
assertEquals(-1L, attributes.getSize());
assertNotEquals(-1L, attributes.getModificationDate());
assertNull(attributes.getChecksum().algorithm);
assertTrue(attributes.getPermission().isReadable());
assertTrue(attributes.getPermission().isWritable());
assertTrue(attributes.getPermission().isExecutable());
// Test wrong type
try {
f.find(new Path(test.getAbsolute(), EnumSet.of(Path.Type.file)));
fail();
} catch (NotfoundException e) {
// Expected
}
new BrickDeleteFeature(session).delete(Collections.singletonList(folder), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of ch.cyberduck.core.exception.NotfoundException in project cyberduck by iterate-ch.
the class BrickAttributesFinderFeatureTest method testFindFile.
@Test
public void testFindFile() throws Exception {
final Path folder = new BrickDirectoryFeature(session).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final Path test = new BrickTouchFeature(session).touch(new Path(folder, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)), new TransferStatus());
final BrickAttributesFinderFeature f = new BrickAttributesFinderFeature(session);
final PathAttributes attributes = f.find(test);
assertEquals(0L, attributes.getSize());
assertNotEquals(-1L, attributes.getModificationDate());
assertNull(attributes.getChecksum().algorithm);
assertTrue(attributes.getPermission().isReadable());
assertTrue(attributes.getPermission().isWritable());
// Test wrong type
try {
f.find(new Path(test.getAbsolute(), EnumSet.of(Path.Type.directory)));
fail();
} catch (NotfoundException e) {
// Expected
}
new BrickDeleteFeature(session).delete(Collections.singletonList(folder), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of ch.cyberduck.core.exception.NotfoundException in project cyberduck by iterate-ch.
the class BoxAttributesFinderFeatureTest method testFindDirectory.
@Test
public void testFindDirectory() throws Exception {
final BoxFileidProvider fileid = new BoxFileidProvider(session);
final Path folder = new BoxDirectoryFeature(session, fileid).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final Path test = new BoxDirectoryFeature(session, fileid).mkdir(new Path(folder, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final BoxAttributesFinderFeature f = new BoxAttributesFinderFeature(session, fileid);
final PathAttributes attributes = f.find(test);
assertNotEquals(-1L, attributes.getSize());
assertNotEquals(-1L, attributes.getModificationDate());
assertNull(attributes.getChecksum().algorithm);
assertNull(attributes.getETag());
assertTrue(attributes.getPermission().isReadable());
assertTrue(attributes.getPermission().isWritable());
assertTrue(attributes.getPermission().isExecutable());
// Test wrong type
try {
f.find(new Path(test.getAbsolute(), EnumSet.of(Path.Type.file)));
fail();
} catch (NotfoundException e) {
// Expected
}
new BoxDeleteFeature(session, fileid).delete(Collections.singletonList(folder), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of ch.cyberduck.core.exception.NotfoundException in project cyberduck by iterate-ch.
the class BoxAttributesFinderFeatureTest method testFindFile.
@Test
public void testFindFile() throws Exception {
final BoxFileidProvider fileid = new BoxFileidProvider(session);
final Path folder = new BoxDirectoryFeature(session, fileid).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final Path test = new BoxTouchFeature(session, fileid).touch(new Path(folder, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)), new TransferStatus().withLength(0L));
final BoxAttributesFinderFeature f = new BoxAttributesFinderFeature(session, fileid);
final PathAttributes attributes = f.find(test);
assertEquals(0L, attributes.getSize());
assertNotEquals(-1L, attributes.getModificationDate());
assertNotNull(attributes.getFileId());
assertNotNull(attributes.getETag());
assertNotNull(attributes.getChecksum().algorithm);
assertTrue(attributes.getPermission().isReadable());
assertTrue(attributes.getPermission().isWritable());
// Test wrong type
try {
f.find(new Path(test.getAbsolute(), EnumSet.of(Path.Type.directory)));
fail();
} catch (NotfoundException e) {
// Expected
}
new BoxDeleteFeature(session, fileid).delete(Collections.singletonList(folder), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
Aggregations