Search in sources :

Example 21 with AccessDeniedException

use of ch.cyberduck.core.exception.AccessDeniedException in project cyberduck by iterate-ch.

the class B2LargeUploadServiceTest method testAppendNoPartCompleted.

@Test
public void testAppendNoPartCompleted() throws Exception {
    final Path bucket = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path test = new Path(bucket, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final String name = new AlphanumericRandomStringService().random();
    final Local local = new Local(System.getProperty("java.io.tmpdir"), name);
    final int length = 102 * 1024 * 1024;
    final byte[] content = RandomUtils.nextBytes(length);
    IOUtils.write(content, local.getOutputStream(false));
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);
    final AtomicBoolean interrupt = new AtomicBoolean();
    final BytecountStreamListener count = new BytecountStreamListener();
    final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
    final B2LargeUploadService service = new B2LargeUploadService(session, fileid, new B2WriteFeature(session, fileid), 100 * 1024L * 1024L, 1);
    try {
        service.upload(test, new Local(System.getProperty("java.io.tmpdir"), name) {

            @Override
            public InputStream getInputStream() throws AccessDeniedException {
                return new CountingInputStream(super.getInputStream()) {

                    @Override
                    protected void beforeRead(int n) throws IOException {
                        if (count.getSent() >= 5 * 1024L * 1024L) {
                            throw new IOException();
                        }
                    }
                };
            }
        }, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), count, status, new DisabledLoginCallback());
    } catch (BackgroundException e) {
        // Expected
        interrupt.set(true);
    }
    assertTrue(interrupt.get());
    assertEquals(5 * 1024L * 1024L, count.getSent(), 0L);
    assertFalse(status.isComplete());
    final TransferStatus append = new TransferStatus().append(true).withLength(content.length);
    service.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), append, new DisabledLoginCallback());
    assertTrue(new B2FindFeature(session, fileid).find(test));
    assertEquals(content.length, new B2AttributesFinderFeature(session, fileid).find(test).getSize());
    assertTrue(append.isComplete());
    final byte[] buffer = new byte[content.length];
    final InputStream in = new B2ReadFeature(session, fileid).read(test, new TransferStatus(), new DisabledConnectionCallback());
    IOUtils.readFully(in, buffer);
    in.close();
    assertArrayEquals(content, buffer);
    new B2DeleteFeature(session, fileid).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
    local.delete();
}
Also used : Delete(ch.cyberduck.core.features.Delete) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) Path(ch.cyberduck.core.Path) DisabledStreamListener(ch.cyberduck.core.io.DisabledStreamListener) ByteArrayInputStream(java.io.ByteArrayInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) InputStream(java.io.InputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) Local(ch.cyberduck.core.Local) IOException(java.io.IOException) BytecountStreamListener(ch.cyberduck.core.BytecountStreamListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BandwidthThrottle(ch.cyberduck.core.io.BandwidthThrottle) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) BackgroundException(ch.cyberduck.core.exception.BackgroundException) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 22 with AccessDeniedException

use of ch.cyberduck.core.exception.AccessDeniedException in project cyberduck by iterate-ch.

the class ProtocolFactory method register.

/**
 * Register profile and copy to application support directory
 *
 * @param file Connection profile to install
 * @return Installation location in application support directory
 */
public Local register(final Local file) {
    try {
        final Profile profile = new ProfilePlistReader(this).read(file);
        if (null == profile) {
            log.error("Attempt to register unknown protocol");
            return null;
        }
        if (log.isInfoEnabled()) {
            log.info(String.format("Register profile %s", profile));
        }
        registered.add(profile);
        preferences.setProperty(StringUtils.lowerCase(String.format("profiles.%s.%s.enabled", profile.getProtocol(), profile.getProvider())), true);
        if (!profiles.exists()) {
            new DefaultLocalDirectoryFeature().mkdir(profiles);
        }
        if (log.isDebugEnabled()) {
            log.debug(String.format("Save profile %s to %s", profile, profiles));
        }
        if (!file.isChild(profiles)) {
            final Local target = LocalFactory.get(profiles, file.getName());
            file.copy(target);
            return target;
        }
        return file;
    } catch (AccessDeniedException e) {
        log.error(String.format("Failure %s reading profile %s", e, file));
        return null;
    }
}
Also used : AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) DefaultLocalDirectoryFeature(ch.cyberduck.core.local.DefaultLocalDirectoryFeature) ProfilePlistReader(ch.cyberduck.core.serializer.impl.dd.ProfilePlistReader)

Example 23 with AccessDeniedException

use of ch.cyberduck.core.exception.AccessDeniedException in project cyberduck by iterate-ch.

the class FlashFxpBookmarkCollection method parse.

@Override
protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException {
    try (final BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
        Host current = null;
        String line;
        while ((line = in.readLine()) != null) {
            if (line.startsWith("[")) {
                current = new Host(protocols.forScheme(Scheme.ftp));
                current.getCredentials().setUsername(PreferencesFactory.get().getProperty("connection.login.anon.name"));
                Pattern pattern = Pattern.compile("\\[(.*)\\]");
                Matcher matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    current.setNickname(matcher.group(1));
                }
            } else if (StringUtils.isBlank(line)) {
                this.add(current);
                current = null;
            } else {
                if (null == current) {
                    log.warn("Failed to detect start of bookmark");
                    continue;
                }
                Scanner scanner = new Scanner(line);
                scanner.useDelimiter("=");
                if (!scanner.hasNext()) {
                    log.warn("Missing key in line:" + line);
                    continue;
                }
                String name = scanner.next().toLowerCase(Locale.ROOT);
                if (!scanner.hasNext()) {
                    log.warn("Missing value in line:" + line);
                    continue;
                }
                String value = scanner.next();
                switch(name) {
                    case "ip":
                        current.setHostname(StringUtils.substringBefore(value, "\u0001"));
                        break;
                    case "port":
                        try {
                            current.setPort(Integer.parseInt(value));
                        } catch (NumberFormatException e) {
                            log.warn("Invalid Port:" + e.getMessage());
                        }
                        break;
                    case "path":
                        current.setDefaultPath(value);
                        break;
                    case "notes":
                        current.setComment(value);
                        break;
                    case "user":
                        current.getCredentials().setUsername(value);
                        break;
                }
            }
        }
    } catch (IOException e) {
        throw new AccessDeniedException(e.getMessage(), e);
    }
}
Also used : Pattern(java.util.regex.Pattern) Scanner(java.util.Scanner) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) Host(ch.cyberduck.core.Host) IOException(java.io.IOException)

Example 24 with AccessDeniedException

use of ch.cyberduck.core.exception.AccessDeniedException in project cyberduck by iterate-ch.

the class S3BrowserBookmarkCollection method parse.

@Override
protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException {
    try (final BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
        Host current = null;
        String line;
        while ((line = in.readLine()) != null) {
            if (line.startsWith("[account_")) {
                current = new Host(protocols.forScheme(Scheme.s3));
            } else if (StringUtils.isBlank(line)) {
                this.add(current);
                current = null;
            } else {
                if (null == current) {
                    log.warn("Failed to detect start of bookmark");
                    continue;
                }
                Scanner scanner = new Scanner(line);
                scanner.useDelimiter(" = ");
                if (!scanner.hasNext()) {
                    log.warn("Missing key in line:" + line);
                    continue;
                }
                String name = scanner.next().toLowerCase(Locale.ROOT);
                if (!scanner.hasNext()) {
                    log.warn("Missing value in line:" + line);
                    continue;
                }
                String value = scanner.next();
                switch(name) {
                    case "name":
                        current.setNickname(value);
                        break;
                    case "comment":
                        current.setComment(value);
                        break;
                    case "access_key":
                        current.getCredentials().setUsername(value);
                        break;
                    case "secret_key":
                        current.getCredentials().setPassword(value);
                        break;
                }
            }
        }
    } catch (IOException e) {
        throw new AccessDeniedException(e.getMessage(), e);
    }
}
Also used : Scanner(java.util.Scanner) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Host(ch.cyberduck.core.Host) IOException(java.io.IOException)

Example 25 with AccessDeniedException

use of ch.cyberduck.core.exception.AccessDeniedException in project cyberduck by iterate-ch.

the class WinScpBookmarkCollection method parse.

@Override
protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException {
    try (final BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
        Host current = null;
        String line;
        while ((line = in.readLine()) != null) {
            if (line.startsWith("[Sessions\\")) {
                current = new Host(protocols.forScheme(Scheme.sftp));
                current.getCredentials().setUsername(PreferencesFactory.get().getProperty("connection.login.anon.name"));
                Pattern pattern = Pattern.compile("\\[Session\\\\(.*)\\]");
                Matcher matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    current.setNickname(matcher.group(1));
                }
            } else if (StringUtils.isBlank(line)) {
                this.add(current);
                current = null;
            } else {
                if (null == current) {
                    log.warn("Failed to detect start of bookmark");
                    continue;
                }
                Scanner scanner = new Scanner(line);
                scanner.useDelimiter("=");
                if (!scanner.hasNext()) {
                    log.warn("Missing key in line:" + line);
                    continue;
                }
                String name = scanner.next().toLowerCase(Locale.ROOT);
                if (!scanner.hasNext()) {
                    log.warn("Missing value in line:" + line);
                    continue;
                }
                String value = scanner.next();
                switch(name) {
                    case "hostname":
                        current.setHostname(value);
                        break;
                    case "username":
                        current.getCredentials().setUsername(value);
                        break;
                    case "portnumber":
                        try {
                            current.setPort(Integer.parseInt(value));
                        } catch (NumberFormatException e) {
                            log.warn("Invalid Port:" + e.getMessage());
                        }
                        break;
                    case "fsprotocol":
                        try {
                            switch(Integer.parseInt(value)) {
                                case 0:
                                case 1:
                                case 2:
                                    current.setProtocol(protocols.forScheme(Scheme.sftp));
                                    break;
                                case 5:
                                    current.setProtocol(protocols.forScheme(Scheme.ftp));
                                    break;
                            }
                            // Reset port to default
                            current.setPort(-1);
                        } catch (NumberFormatException e) {
                            log.warn("Unknown Protocol:" + e.getMessage());
                        }
                        break;
                }
            }
        }
    } catch (IOException e) {
        throw new AccessDeniedException(e.getMessage(), e);
    }
}
Also used : Pattern(java.util.regex.Pattern) Scanner(java.util.Scanner) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) Host(ch.cyberduck.core.Host) IOException(java.io.IOException)

Aggregations

AccessDeniedException (ch.cyberduck.core.exception.AccessDeniedException)71 Path (ch.cyberduck.core.Path)27 IOException (java.io.IOException)23 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)19 Local (ch.cyberduck.core.Local)18 Test (org.junit.Test)18 NotfoundException (ch.cyberduck.core.exception.NotfoundException)14 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)14 BackgroundException (ch.cyberduck.core.exception.BackgroundException)12 LoginFailureException (ch.cyberduck.core.exception.LoginFailureException)11 IntegrationTest (ch.cyberduck.test.IntegrationTest)9 DisabledConnectionCallback (ch.cyberduck.core.DisabledConnectionCallback)8 DisabledLoginCallback (ch.cyberduck.core.DisabledLoginCallback)8 Delete (ch.cyberduck.core.features.Delete)8 InputStream (java.io.InputStream)8 ServiceException (org.jets3t.service.ServiceException)8 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)7 PathAttributes (ch.cyberduck.core.PathAttributes)7 BandwidthThrottle (ch.cyberduck.core.io.BandwidthThrottle)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7