use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateSession method login.
@Override
public void login(final Proxy proxy, final LoginCallback controller, final CancelCallback cancel) throws BackgroundException {
authorizationService.setTokens(authorizationService.authorize(host, controller, cancel, OAuth2AuthorizationService.FlowType.AuthorizationCode));
try {
final HttpRequestBase request = new HttpPost(new HostUrlProvider().withUsername(false).withPath(true).get(host.getProtocol().getScheme(), host.getPort(), null, host.getHostname(), "/identity/core/connect/userinfo"));
request.addHeader(HttpHeaders.CONTENT_TYPE, MEDIA_TYPE);
final CloseableHttpResponse response = client.getClient().execute(request);
try {
switch(response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
try {
final JsonElement element = JsonParser.parseReader(new InputStreamReader(response.getEntity().getContent()));
if (element.isJsonObject()) {
final JsonObject json = element.getAsJsonObject();
final URI url = URI.create(json.getAsJsonPrimitive("web_url_api").getAsString());
if (log.isInfoEnabled()) {
log.info(String.format("Set base path to %s", url));
}
client.setBasePath(StringUtils.removeEnd(url.toString(), String.valueOf(Path.DELIMITER)));
}
} catch (JsonParseException | IllegalArgumentException e) {
log.warn(String.format("Ignore failure %s", e));
}
break;
case HttpStatus.SC_FORBIDDEN:
// Insufficient scope
final BackgroundException failure = new StoregateExceptionMappingService(fileid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(response.getEntity())));
throw new LoginFailureException(failure.getDetail(), failure);
default:
throw new StoregateExceptionMappingService(fileid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(response.getEntity())));
}
} finally {
EntityUtils.consume(response.getEntity());
}
final Credentials credentials = host.getCredentials();
// Get username
final ExtendedUser me = new UsersApi(client).usersGetMe();
if (log.isDebugEnabled()) {
log.debug(String.format("Authenticated for user %s", me));
}
credentials.setUsername(me.getUsername());
credentials.setSaved(true);
// Get root folders
roots = new SettingsApi(client).settingsGetRootfolders();
} catch (ApiException e) {
throw new StoregateExceptionMappingService(fileid).map(e);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map(e);
}
}
use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateShareFeature method toDownloadUrl.
@Override
public DescriptiveUrl toDownloadUrl(final Path file, final Void options, final PasswordCallback callback) throws BackgroundException {
try {
final Host bookmark = session.getHost();
final CreateFileShareRequest request = new CreateFileShareRequest().fileId(fileid.getFileId(file, new DisabledListProgressListener()));
try {
request.setPassword(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
}
return new DescriptiveUrl(URI.create(new FileSharesApi(session.getClient()).fileSharesPost(request).getUrl()), DescriptiveUrl.Type.signed);
} catch (ApiException e) {
throw new StoregateExceptionMappingService(fileid).map(e);
}
}
use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateShareFeature method toUploadUrl.
@Override
public DescriptiveUrl toUploadUrl(final Path file, final Void options, final PasswordCallback callback) throws BackgroundException {
try {
final Host bookmark = session.getHost();
final CreateFileShareRequest request = new CreateFileShareRequest().fileId(fileid.getFileId(file, new DisabledListProgressListener())).allowUpload(true);
try {
request.setPassword(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
}
return new DescriptiveUrl(URI.create(new FileSharesApi(session.getClient()).fileSharesPost(request).getUrl()), DescriptiveUrl.Type.signed);
} catch (ApiException e) {
throw new StoregateExceptionMappingService(fileid).map(e);
}
}
use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateTimestampFeature method setTimestamp.
@Override
public void setTimestamp(final Path file, final TransferStatus status) throws BackgroundException {
try {
final FilesApi files = new FilesApi(session.getClient());
files.filesUpdateFile(fileid.getFileId(file, new DisabledListProgressListener()), new UpdateFilePropertiesRequest().modified(new DateTime(status.getTimestamp())));
} catch (ApiException e) {
throw new StoregateExceptionMappingService(fileid).map("Failure to write attributes of {0}", e, file);
}
}
use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateWriteFeature method start.
protected String start(final Path file, final TransferStatus status) throws BackgroundException {
try {
final StoregateApiClient client = session.getClient();
final HttpEntityEnclosingRequestBase request = new HttpPost(String.format("%s/v4/upload/resumable", client.getBasePath()));
final FileMetadata meta = new FileMetadata();
meta.setId(StringUtils.EMPTY);
if (status.isHidden()) {
// Hidden
meta.setAttributes(2);
} else {
meta.setAttributes(0);
}
meta.setFlags(0);
if (status.getLockId() != null) {
request.addHeader("X-Lock-Id", status.getLockId().toString());
}
meta.setFileName(URIEncoder.encode(file.getName()));
meta.setParentId(fileid.getFileId(file.getParent(), new DisabledListProgressListener()));
meta.setFileSize(status.getLength() > 0 ? status.getLength() : null);
meta.setCreated(DateTime.now());
if (null != status.getTimestamp()) {
meta.setModified(new DateTime(status.getTimestamp()));
}
request.setEntity(new StringEntity(new JSON().getContext(meta.getClass()).writeValueAsString(meta), ContentType.create("application/json", StandardCharsets.UTF_8.name())));
request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE);
final CloseableHttpResponse response = client.getClient().execute(request);
try {
switch(response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
break;
default:
throw new StoregateExceptionMappingService(fileid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(response.getEntity())));
}
} finally {
EntityUtils.consume(response.getEntity());
}
if (response.containsHeader(HttpHeaders.LOCATION)) {
return response.getFirstHeader(HttpHeaders.LOCATION).getValue();
}
throw new StoregateExceptionMappingService(fileid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(response.getEntity())));
} catch (IOException e) {
throw new HttpExceptionMappingService().map("Upload {0} failed", e, file);
}
}
Aggregations