Search in sources :

Example 1 with NotfoundException

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);
}
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 NotfoundException

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());
}
Also used : Path(ch.cyberduck.core.Path) Delete(ch.cyberduck.core.features.Delete) NotfoundException(ch.cyberduck.core.exception.NotfoundException) PathAttributes(ch.cyberduck.core.PathAttributes) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 3 with NotfoundException

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());
}
Also used : Path(ch.cyberduck.core.Path) Delete(ch.cyberduck.core.features.Delete) NotfoundException(ch.cyberduck.core.exception.NotfoundException) PathAttributes(ch.cyberduck.core.PathAttributes) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 4 with NotfoundException

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());
}
Also used : Path(ch.cyberduck.core.Path) Delete(ch.cyberduck.core.features.Delete) NotfoundException(ch.cyberduck.core.exception.NotfoundException) PathAttributes(ch.cyberduck.core.PathAttributes) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 5 with NotfoundException

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());
}
Also used : Path(ch.cyberduck.core.Path) Delete(ch.cyberduck.core.features.Delete) NotfoundException(ch.cyberduck.core.exception.NotfoundException) PathAttributes(ch.cyberduck.core.PathAttributes) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Aggregations

NotfoundException (ch.cyberduck.core.exception.NotfoundException)140 Path (ch.cyberduck.core.Path)95 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)55 Test (org.junit.Test)55 IntegrationTest (ch.cyberduck.test.IntegrationTest)48 PathAttributes (ch.cyberduck.core.PathAttributes)45 Delete (ch.cyberduck.core.features.Delete)41 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)38 DisabledLoginCallback (ch.cyberduck.core.DisabledLoginCallback)34 IOException (java.io.IOException)27 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)22 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)17 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)17 BackgroundException (ch.cyberduck.core.exception.BackgroundException)16 AttributedList (ch.cyberduck.core.AttributedList)15 AccessDeniedException (ch.cyberduck.core.exception.AccessDeniedException)14 ArrayList (java.util.ArrayList)14 SimplePathPredicate (ch.cyberduck.core.SimplePathPredicate)13 StorageException (com.microsoft.azure.storage.StorageException)12 URISyntaxException (java.net.URISyntaxException)12