use of ch.cyberduck.core.shared.DelegatingHomeFeature in project cyberduck by iterate-ch.
the class NextcloudShareProvider method toDownloadUrl.
/**
* int) 0 = user; 1 = group; 3 = public link; 6 = federated cloud share
*/
@Override
public DescriptiveUrl toDownloadUrl(final Path file, final Object options, final PasswordCallback callback) throws BackgroundException {
final Host bookmark = session.getHost();
final StringBuilder request = new StringBuilder(String.format("https://%s/ocs/v2.php/apps/files_sharing/api/v1/shares?path=%s&shareType=%d", bookmark.getHostname(), URIEncoder.encode(StringUtils.substringAfter(file.getAbsolute(), new DelegatingHomeFeature(new DefaultPathHomeFeature(session.getHost()), session.getFeature(Home.class)).find().getAbsolute())), // Public link
3));
try {
request.append(String.format("&password=%s", callback.prompt(bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), MessageFormat.format(LocaleFactory.localizedString("Create a passphrase required to access {0}", "Credentials"), file.getName()), new LoginOptions().keychain(false).icon(bookmark.getProtocol().disk())).getPassword()));
} catch (LoginCanceledException e) {
// Ignore no password set
}
final HttpPost resource = new HttpPost(request.toString());
resource.setHeader("OCS-APIRequest", "true");
resource.setHeader(HttpHeaders.ACCEPT, "application/xml");
try {
return new DescriptiveUrl(session.getClient().execute(resource, new AbstractResponseHandler<URI>() {
@Override
public URI handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
final StatusLine statusLine = response.getStatusLine();
final HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() >= 300) {
final StringAppender message = new StringAppender();
message.append(statusLine.getReasonPhrase());
final ocs error = new XmlMapper().readValue(entity.getContent(), ocs.class);
message.append(error.meta.message);
throw new HttpResponseException(statusLine.getStatusCode(), message.toString());
}
return super.handleResponse(response);
}
@Override
public URI handleEntity(final HttpEntity entity) throws IOException {
final XmlMapper mapper = new XmlMapper();
ocs value = mapper.readValue(entity.getContent(), ocs.class);
return URI.create(value.data.url);
}
}), DescriptiveUrl.Type.http);
} catch (HttpResponseException e) {
throw new DefaultHttpResponseExceptionMappingService().map(e);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map(e);
}
}
use of ch.cyberduck.core.shared.DelegatingHomeFeature in project cyberduck by iterate-ch.
the class S3Session method login.
@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
if (Scheme.isURL(host.getProtocol().getContext())) {
try {
final Credentials temporary = new AWSSessionCredentialsRetriever(trust, key, this, host.getProtocol().getContext()).get();
client.setProviderCredentials(new AWSSessionCredentials(temporary.getUsername(), temporary.getPassword(), temporary.getToken()));
} catch (ConnectionTimeoutException | ConnectionRefusedException | ResolveFailedException | NotfoundException | InteroperabilityException e) {
log.warn(String.format("Failure to retrieve session credentials from . %s", e.getMessage()));
throw new LoginFailureException(e.getDetail(false), e);
}
} else {
final Credentials credentials;
// Only for AWS
if (isAwsHostname(host.getHostname())) {
// Try auto-configure
credentials = new STSCredentialsConfigurator(new ThreadLocalHostnameDelegatingTrustManager(trust, host.getHostname()), key, prompt).configure(host);
} else {
credentials = host.getCredentials();
}
if (StringUtils.isNotBlank(credentials.getToken())) {
client.setProviderCredentials(credentials.isAnonymousLogin() ? null : new AWSSessionCredentials(credentials.getUsername(), credentials.getPassword(), credentials.getToken()));
} else {
client.setProviderCredentials(credentials.isAnonymousLogin() ? null : new AWSCredentials(credentials.getUsername(), credentials.getPassword()));
}
}
if (host.getCredentials().isPassed()) {
log.warn(String.format("Skip verifying credentials with previous successful authentication event for %s", this));
return;
}
try {
final Location.Name location = new S3PathStyleFallbackAdapter<>(this, new BackgroundExceptionCallable<Location.Name>() {
@Override
public Location.Name call() throws BackgroundException {
return new S3LocationFeature(S3Session.this, client.getRegionEndpointCache()).getLocation(new DelegatingHomeFeature(new DefaultPathHomeFeature(host)).find());
}
}).call();
if (log.isDebugEnabled()) {
log.debug(String.format("Retrieved region %s", location));
}
if (!Location.unknown.equals(location)) {
client.getConfiguration().setProperty("storage-service.default-region", location.getIdentifier());
}
} catch (AccessDeniedException | InteroperabilityException e) {
log.warn(String.format("Failure %s querying region", e));
final Path home = new DefaultHomeFinderService(this).find();
if (log.isDebugEnabled()) {
log.debug(String.format("Retrieved %s", home));
}
}
}
use of ch.cyberduck.core.shared.DelegatingHomeFeature 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());
}
use of ch.cyberduck.core.shared.DelegatingHomeFeature in project cyberduck by iterate-ch.
the class DAVSession method alert.
@Override
public boolean alert(final ConnectionCallback callback) throws BackgroundException {
if (super.alert(callback)) {
// Propose protocol change if HEAD request redirects to HTTPS
final Path home = new DelegatingHomeFeature(new DefaultPathHomeFeature(host)).find();
try {
final RequestConfig context = client.context().getRequestConfig();
final HttpHead request = new HttpHead(new DAVPathEncoder().encode(home));
request.setConfig(RequestConfig.copy(context).setRedirectsEnabled(false).build());
final Header location = client.execute(request, new ValidatingResponseHandler<Header>() {
@Override
public Header handleResponse(final HttpResponse response) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY) {
return response.getFirstHeader(HttpHeaders.LOCATION);
}
return null;
}
});
// Reset default redirect configuration in context
client.context().setRequestConfig(RequestConfig.copy(context).setRedirectsEnabled(true).build());
if (null != location) {
final URL url = new URL(location.getValue());
if (StringUtils.equals(Scheme.https.name(), url.getProtocol())) {
try {
callback.warn(host, MessageFormat.format(LocaleFactory.localizedString("Unsecured {0} connection", "Credentials"), host.getProtocol().getName()), MessageFormat.format("{0} {1}.", MessageFormat.format(LocaleFactory.localizedString("The server supports encrypted connections. Do you want to switch to {0}?", "Credentials"), new DAVSSLProtocol().getName()), LocaleFactory.localizedString("Please contact your web hosting service provider for assistance", "Support")), LocaleFactory.localizedString("Continue", "Credentials"), LocaleFactory.localizedString("Change", "Credentials"), String.format("connection.unsecure.%s", host.getHostname()));
// Continue chosen. Login using plain FTP.
} catch (LoginCanceledException e) {
// Protocol switch
host.setHostname(url.getHost());
host.setProtocol(ProtocolFactory.get().forScheme(Scheme.davs));
return false;
}
}
}
// Continue with default alert
} catch (SardineException e) {
// Ignore failure
log.warn(String.format("Ignore failed HEAD request to %s with %s.", host, e.getResponsePhrase()));
} catch (IOException e) {
throw new HttpExceptionMappingService().map(e);
}
return preferences.getBoolean("webdav.basic.preemptive");
}
return false;
}
use of ch.cyberduck.core.shared.DelegatingHomeFeature in project cyberduck by iterate-ch.
the class NextcloudShareProvider method toUploadUrl.
@Override
public DescriptiveUrl toUploadUrl(final Path file, final Object options, final PasswordCallback callback) throws BackgroundException {
final Host bookmark = session.getHost();
final StringBuilder request = new StringBuilder(String.format("https://%s/ocs/v2.php/apps/files_sharing/api/v1/shares?path=%s&shareType=%d&publicUpload=true", bookmark.getHostname(), URIEncoder.encode(StringUtils.substringAfter(file.getAbsolute(), new DelegatingHomeFeature(new DefaultPathHomeFeature(session.getHost()), session.getFeature(Home.class)).find().getAbsolute())), // Public link
3));
try {
request.append(String.format("&password=%s", callback.prompt(bookmark, LocaleFactory.localizedString("Passphrase", "Cryptomator"), MessageFormat.format(LocaleFactory.localizedString("Create a passphrase required to access {0}", "Credentials"), file.getName()), new LoginOptions().keychain(false).icon(bookmark.getProtocol().disk())).getPassword()));
} catch (LoginCanceledException e) {
// Ignore no password set
}
final HttpPost resource = new HttpPost(request.toString());
resource.setHeader("OCS-APIRequest", "true");
resource.setHeader(HttpHeaders.ACCEPT, "application/xml");
try {
return new DescriptiveUrl(session.getClient().execute(resource, new AbstractResponseHandler<URI>() {
@Override
public URI handleEntity(final HttpEntity entity) throws IOException {
final XmlMapper mapper = new XmlMapper();
ocs value = mapper.readValue(entity.getContent(), ocs.class);
return URI.create(value.data.url);
}
}), DescriptiveUrl.Type.http);
} catch (HttpResponseException e) {
throw new DefaultHttpResponseExceptionMappingService().map(e);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map(e);
}
}
Aggregations