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