Search in sources :

Example 1 with Read

use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.

the class RemoteProfilesFinder method find.

@Override
public Set<ProfileDescription> find(final Visitor visitor) throws BackgroundException {
    if (log.isInfoEnabled()) {
        log.info(String.format("Fetch profiles from %s", session.getHost()));
    }
    final ProfileFilter filter = new ProfileFilter();
    final AttributedList<Path> list = session.getFeature(ListService.class).list(new DelegatingHomeFeature(new DefaultPathHomeFeature(session.getHost())).find(), new DisabledListProgressListener());
    return list.filter(filter).toStream().map(file -> visitor.visit(new RemoteProfileDescription(protocols, file, new LazyInitializer<Local>() {

        @Override
        protected Local initialize() throws ConcurrentException {
            try {
                final Read read = session.getFeature(Read.class);
                if (log.isInfoEnabled()) {
                    log.info(String.format("Download profile %s", file));
                }
                final InputStream in = read.read(file.withAttributes(new PathAttributes(file.attributes()).withVersionId(null)), new TransferStatus().withLength(TransferStatus.UNKNOWN_LENGTH), new DisabledConnectionCallback());
                final Local temp = TemporaryFileServiceFactory.get().create(file.getName());
                new DefaultLocalTouchFeature().touch(temp);
                final OutputStream out = temp.getOutputStream(false);
                try {
                    IOUtils.copy(in, out);
                } finally {
                    in.close();
                    out.close();
                }
                return temp;
            } catch (BackgroundException | IOException e) {
                throw new ConcurrentException(e);
            }
        }
    }))).collect(Collectors.toSet());
}
Also used : Path(ch.cyberduck.core.Path) Read(ch.cyberduck.core.features.Read) DelegatingHomeFeature(ch.cyberduck.core.shared.DelegatingHomeFeature) DefaultLocalTouchFeature(ch.cyberduck.core.local.DefaultLocalTouchFeature) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ListService(ch.cyberduck.core.ListService) TemporaryFileServiceFactory(ch.cyberduck.core.local.TemporaryFileServiceFactory) ProtocolFactory(ch.cyberduck.core.ProtocolFactory) Filter(ch.cyberduck.core.Filter) Local(ch.cyberduck.core.Local) ConcurrentException(org.apache.commons.lang3.concurrent.ConcurrentException) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) PathAttributes(ch.cyberduck.core.PathAttributes) OutputStream(java.io.OutputStream) Session(ch.cyberduck.core.Session) AttributedList(ch.cyberduck.core.AttributedList) Set(java.util.Set) IOException(java.io.IOException) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) BackgroundException(ch.cyberduck.core.exception.BackgroundException) Collectors(java.util.stream.Collectors) LazyInitializer(org.apache.commons.lang3.concurrent.LazyInitializer) DefaultPathHomeFeature(ch.cyberduck.core.shared.DefaultPathHomeFeature) IOUtils(org.apache.commons.io.IOUtils) Logger(org.apache.logging.log4j.Logger) Path(ch.cyberduck.core.Path) Pattern(java.util.regex.Pattern) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) LazyInitializer(org.apache.commons.lang3.concurrent.LazyInitializer) DelegatingHomeFeature(ch.cyberduck.core.shared.DelegatingHomeFeature) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) InputStream(java.io.InputStream) PathAttributes(ch.cyberduck.core.PathAttributes) OutputStream(java.io.OutputStream) Local(ch.cyberduck.core.Local) DefaultPathHomeFeature(ch.cyberduck.core.shared.DefaultPathHomeFeature) IOException(java.io.IOException) ListService(ch.cyberduck.core.ListService) Read(ch.cyberduck.core.features.Read) DefaultLocalTouchFeature(ch.cyberduck.core.local.DefaultLocalTouchFeature) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) BackgroundException(ch.cyberduck.core.exception.BackgroundException) ConcurrentException(org.apache.commons.lang3.concurrent.ConcurrentException)

Example 2 with Read

use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.

the class CryptoVaultTest method testLoadInvalidPassphrase.

@Test
public void testLoadInvalidPassphrase() throws Exception {
    final NullSession session = new NullSession(new Host(new TestProtocol())) {

        @Override
        @SuppressWarnings("unchecked")
        public <T> T _getFeature(final Class<T> type) {
            if (type == Read.class) {
                return (T) new Read() {

                    @Override
                    public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) {
                        final String masterKey = "{\n" + "  \"version\": 999,\n" + "  \"scryptSalt\": \"RVAAirkArDU=\",\n" + "  \"scryptCostParam\": 32768,\n" + "  \"scryptBlockSize\": 8,\n" + "  \"primaryMasterKey\": \"+03NkJNWVsJ9Tb1CTpKhXyfINzjDirFFI+iJLOWIOySyxB+abpx34Q==\",\n" + "  \"hmacMasterKey\": \"aMoDtn7Y6kIXxyHo2zl47p5jCYTlRnfx3l3AMgULmIDSYAxVAraSgg==\",\n" + "  \"versionMac\": \"FzirA8UhwCmS5RsC4JvxbO+ZBxaCbIkzqD2Ocagd+A8=\"\n" + "}";
                        return IOUtils.toInputStream(masterKey, Charset.defaultCharset());
                    }

                    @Override
                    public boolean offset(final Path file) {
                        return false;
                    }
                };
            }
            return super._getFeature(type);
        }
    };
    final AtomicBoolean prompt = new AtomicBoolean();
    final CryptoVault vault = new CryptoVault(new Path("/", EnumSet.of(Path.Type.directory)));
    try {
        vault.load(session, new DisabledPasswordCallback() {

            @Override
            public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) throws LoginCanceledException {
                if (!prompt.get()) {
                    assertEquals("Provide your passphrase to unlock the Cryptomator Vault /", reason);
                    prompt.set(true);
                    return new VaultCredentials("null");
                } else {
                    assertEquals("Failure to decrypt master key file. Provide your passphrase to unlock the Cryptomator Vault /.", reason);
                    throw new LoginCanceledException();
                }
            }
        }, new DisabledPasswordStore());
        fail();
    } catch (LoginCanceledException e) {
    // 
    }
    assertTrue(prompt.get());
}
Also used : Path(ch.cyberduck.core.Path) TestProtocol(ch.cyberduck.core.TestProtocol) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) NullInputStream(org.apache.commons.io.input.NullInputStream) InputStream(java.io.InputStream) LoginCanceledException(ch.cyberduck.core.exception.LoginCanceledException) NullSession(ch.cyberduck.core.NullSession) Host(ch.cyberduck.core.Host) Read(ch.cyberduck.core.features.Read) LoginOptions(ch.cyberduck.core.LoginOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ConnectionCallback(ch.cyberduck.core.ConnectionCallback) DisabledPasswordCallback(ch.cyberduck.core.DisabledPasswordCallback) DisabledPasswordStore(ch.cyberduck.core.DisabledPasswordStore) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) Credentials(ch.cyberduck.core.Credentials) Test(org.junit.Test)

Example 3 with Read

use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.

the class CryptoVaultTest method testLoadEmptyPassword.

@Test
public void testLoadEmptyPassword() throws Exception {
    final NullSession session = new NullSession(new Host(new TestProtocol())) {

        @Override
        @SuppressWarnings("unchecked")
        public <T> T _getFeature(final Class<T> type) {
            if (type == Read.class) {
                return (T) new Read() {

                    @Override
                    public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) {
                        final String masterKey = "{\n" + "  \"scryptSalt\": \"NrC7QGG/ouc=\",\n" + "  \"scryptCostParam\": 16384,\n" + "  \"scryptBlockSize\": 8,\n" + "  \"primaryMasterKey\": \"Q7pGo1l0jmZssoQh9rXFPKJE9NIXvPbL+HcnVSR9CHdkeR8AwgFtcw==\",\n" + "  \"hmacMasterKey\": \"xzBqT4/7uEcQbhHFLC0YmMy4ykVKbuvJEA46p1Xm25mJNuTc20nCbw==\",\n" + "  \"versionMac\": \"hlNr3dz/CmuVajhaiGyCem9lcVIUjDfSMLhjppcXOrM=\",\n" + "  \"version\": 5\n" + "}";
                        return IOUtils.toInputStream(masterKey, Charset.defaultCharset());
                    }

                    @Override
                    public boolean offset(final Path file) {
                        return false;
                    }
                };
            }
            return super._getFeature(type);
        }
    };
    final CryptoVault vault = new CryptoVault(new Path("/", EnumSet.of(Path.Type.directory)));
    try {
        vault.load(session, new DisabledPasswordCallback() {

            @Override
            public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) {
                return new VaultCredentials(null);
            }
        }, new DisabledPasswordStore());
        fail();
    } catch (LoginCanceledException e) {
    // 
    }
}
Also used : Path(ch.cyberduck.core.Path) TestProtocol(ch.cyberduck.core.TestProtocol) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) NullInputStream(org.apache.commons.io.input.NullInputStream) InputStream(java.io.InputStream) LoginCanceledException(ch.cyberduck.core.exception.LoginCanceledException) NullSession(ch.cyberduck.core.NullSession) Host(ch.cyberduck.core.Host) Read(ch.cyberduck.core.features.Read) LoginOptions(ch.cyberduck.core.LoginOptions) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ConnectionCallback(ch.cyberduck.core.ConnectionCallback) DisabledPasswordCallback(ch.cyberduck.core.DisabledPasswordCallback) DisabledPasswordStore(ch.cyberduck.core.DisabledPasswordStore) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) Credentials(ch.cyberduck.core.Credentials) Test(org.junit.Test)

Example 4 with Read

use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.

the class IRODSWriteFeatureTest method testWriteThreaded.

@Test
public void testWriteThreaded() throws Exception {
    final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new IRODSProtocol())));
    final Profile profile = new ProfilePlistReader(factory).read(new Local("../profiles/iRODS (iPlant Collaborative).cyberduckprofile"));
    final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials(System.getProperties().getProperty("irods.key"), System.getProperties().getProperty("irods.secret")));
    final IRODSSession session1 = new IRODSSession(host);
    session1.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
    session1.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
    final IRODSSession session2 = new IRODSSession(host);
    session2.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
    session2.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
    final CountDownLatch cw1 = new CountDownLatch(1);
    final CountDownLatch cw2 = new CountDownLatch(1);
    final Path test1 = new Path(new IRODSHomeFinderService(session1).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final Path test2 = new Path(new IRODSHomeFinderService(session2).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final byte[] content = RandomUtils.nextBytes(68400);
    final OutputStream out1 = new IRODSWriteFeature(session1).write(test1, new TransferStatus().append(false).withLength(content.length), new DisabledConnectionCallback());
    final OutputStream out2 = new IRODSWriteFeature(session2).write(test2, new TransferStatus().append(false).withLength(content.length), new DisabledConnectionCallback());
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out2);
            } catch (BackgroundException e) {
                fail();
            } finally {
                cw1.countDown();
            }
        }
    }).start();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out1);
            } catch (BackgroundException e) {
                fail();
            } finally {
                cw2.countDown();
            }
        }
    }).start();
    cw1.await();
    cw2.await();
    final CountDownLatch cr1 = new CountDownLatch(1);
    final CountDownLatch cr2 = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                final InputStream in1 = session1.getFeature(Read.class).read(test1, new TransferStatus(), new DisabledConnectionCallback());
                final byte[] buffer1 = new byte[content.length];
                IOUtils.readFully(in1, buffer1);
                in1.close();
                assertArrayEquals(content, buffer1);
            } catch (Exception e) {
                fail();
            } finally {
                cr1.countDown();
            }
        }
    }).start();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                final InputStream in2 = session2.getFeature(Read.class).read(test2, new TransferStatus(), new DisabledConnectionCallback());
                final byte[] buffer2 = new byte[content.length];
                IOUtils.readFully(in2, buffer2);
                in2.close();
                assertArrayEquals(content, buffer2);
            } catch (Exception e) {
                fail();
            } finally {
                cr2.countDown();
            }
        }
    }).start();
    cr1.await();
    cr2.await();
    session1.close();
    session2.close();
}
Also used : OutputStream(java.io.OutputStream) StatusOutputStream(ch.cyberduck.core.io.StatusOutputStream) ProfilePlistReader(ch.cyberduck.core.serializer.impl.dd.ProfilePlistReader) Profile(ch.cyberduck.core.Profile) Read(ch.cyberduck.core.features.Read) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) Path(ch.cyberduck.core.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Local(ch.cyberduck.core.Local) Host(ch.cyberduck.core.Host) CountDownLatch(java.util.concurrent.CountDownLatch) BackgroundException(ch.cyberduck.core.exception.BackgroundException) ProtocolFactory(ch.cyberduck.core.ProtocolFactory) DisabledCancelCallback(ch.cyberduck.core.DisabledCancelCallback) ByteArrayInputStream(java.io.ByteArrayInputStream) DisabledHostKeyCallback(ch.cyberduck.core.DisabledHostKeyCallback) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) Credentials(ch.cyberduck.core.Credentials) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) BackgroundException(ch.cyberduck.core.exception.BackgroundException) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 5 with Read

use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.

the class IRODSWriteFeatureTest method testWriteAppend.

@Test
public void testWriteAppend() throws Exception {
    final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new IRODSProtocol())));
    final Profile profile = new ProfilePlistReader(factory).read(new Local("../profiles/iRODS (iPlant Collaborative).cyberduckprofile"));
    final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials(System.getProperties().getProperty("irods.key"), System.getProperties().getProperty("irods.secret")));
    final IRODSSession session = new IRODSSession(host);
    session.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
    session.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
    final Path test = new Path(new IRODSHomeFinderService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    assertFalse(session.getFeature(Find.class).find(test));
    final byte[] content = RandomUtils.nextBytes(100);
    final TransferStatus status = new TransferStatus();
    status.setAppend(true);
    status.setLength(content.length);
    assertEquals(0L, new IRODSWriteFeature(session).append(test, status).size, 0L);
    final OutputStream out = new IRODSWriteFeature(session).write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
    assertTrue(session.getFeature(Find.class).find(test));
    final PathAttributes attributes = new IRODSAttributesFinderFeature(session).find(test);
    assertEquals(content.length, attributes.getSize());
    final InputStream in = session.getFeature(Read.class).read(test, new TransferStatus(), new DisabledConnectionCallback());
    final byte[] buffer = new byte[content.length];
    IOUtils.readFully(in, buffer);
    in.close();
    assertArrayEquals(content, buffer);
    // Append
    final byte[] content_append = RandomUtils.nextBytes(100);
    final TransferStatus status_append = new TransferStatus();
    status_append.setAppend(true);
    status_append.setLength(content_append.length);
    assertTrue(new IRODSWriteFeature(session).append(test, status_append).append);
    assertEquals(status.getLength(), new IRODSWriteFeature(session).append(test, status_append).size, 0L);
    final OutputStream out_append = new IRODSWriteFeature(session).write(test, status_append, new DisabledConnectionCallback());
    assertNotNull(out_append);
    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content_append), out_append);
    assertTrue(session.getFeature(Find.class).find(test));
    final PathAttributes attributes_complete = new IRODSAttributesFinderFeature(session).find(test);
    assertEquals(content.length + content_append.length, attributes_complete.getSize());
    final InputStream in_append = session.getFeature(Read.class).read(test, new TransferStatus(), new DisabledConnectionCallback());
    final byte[] buffer_complete = new byte[content.length + content_append.length];
    IOUtils.readFully(in_append, buffer_complete);
    in_append.close();
    byte[] complete = new byte[content.length + content_append.length];
    System.arraycopy(content, 0, complete, 0, content.length);
    System.arraycopy(content_append, 0, complete, content.length, content_append.length);
    assertArrayEquals(complete, buffer_complete);
    session.getFeature(Delete.class).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
    assertFalse(session.getFeature(Find.class).find(test));
    session.close();
}
Also used : Delete(ch.cyberduck.core.features.Delete) OutputStream(java.io.OutputStream) StatusOutputStream(ch.cyberduck.core.io.StatusOutputStream) ProfilePlistReader(ch.cyberduck.core.serializer.impl.dd.ProfilePlistReader) Profile(ch.cyberduck.core.Profile) Read(ch.cyberduck.core.features.Read) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) Path(ch.cyberduck.core.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PathAttributes(ch.cyberduck.core.PathAttributes) Local(ch.cyberduck.core.Local) Host(ch.cyberduck.core.Host) ProtocolFactory(ch.cyberduck.core.ProtocolFactory) DisabledCancelCallback(ch.cyberduck.core.DisabledCancelCallback) ByteArrayInputStream(java.io.ByteArrayInputStream) DisabledHostKeyCallback(ch.cyberduck.core.DisabledHostKeyCallback) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) Credentials(ch.cyberduck.core.Credentials) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Aggregations

Path (ch.cyberduck.core.Path)14 Read (ch.cyberduck.core.features.Read)14 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)14 InputStream (java.io.InputStream)14 Test (org.junit.Test)13 Host (ch.cyberduck.core.Host)12 Credentials (ch.cyberduck.core.Credentials)11 ConnectionCallback (ch.cyberduck.core.ConnectionCallback)10 NullSession (ch.cyberduck.core.NullSession)10 TestProtocol (ch.cyberduck.core.TestProtocol)10 DisabledPasswordCallback (ch.cyberduck.core.DisabledPasswordCallback)9 DisabledPasswordStore (ch.cyberduck.core.DisabledPasswordStore)9 LoginOptions (ch.cyberduck.core.LoginOptions)9 VaultCredentials (ch.cyberduck.core.vault.VaultCredentials)9 NullInputStream (org.apache.commons.io.input.NullInputStream)6 Local (ch.cyberduck.core.Local)5 DisabledConnectionCallback (ch.cyberduck.core.DisabledConnectionCallback)4 OutputStream (java.io.OutputStream)4 DisabledLoginCallback (ch.cyberduck.core.DisabledLoginCallback)3 ProtocolFactory (ch.cyberduck.core.ProtocolFactory)3