Search in sources :

Example 1 with ApiException

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HostUrlProvider(ch.cyberduck.core.HostUrlProvider) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) ExtendedUser(ch.cyberduck.core.storegate.io.swagger.client.model.ExtendedUser) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) URI(java.net.URI) CYBERDUCK_REDIRECT_URI(ch.cyberduck.core.oauth.OAuth2AuthorizationService.CYBERDUCK_REDIRECT_URI) LoginFailureException(ch.cyberduck.core.exception.LoginFailureException) JsonElement(com.google.gson.JsonElement) UsersApi(ch.cyberduck.core.storegate.io.swagger.client.api.UsersApi) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SettingsApi(ch.cyberduck.core.storegate.io.swagger.client.api.SettingsApi) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) BackgroundException(ch.cyberduck.core.exception.BackgroundException) Credentials(ch.cyberduck.core.Credentials) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Example 2 with ApiException

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);
    }
}
Also used : LoginOptions(ch.cyberduck.core.LoginOptions) DescriptiveUrl(ch.cyberduck.core.DescriptiveUrl) FileSharesApi(ch.cyberduck.core.storegate.io.swagger.client.api.FileSharesApi) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) LoginCanceledException(ch.cyberduck.core.exception.LoginCanceledException) Host(ch.cyberduck.core.Host) CreateFileShareRequest(ch.cyberduck.core.storegate.io.swagger.client.model.CreateFileShareRequest) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Example 3 with ApiException

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);
    }
}
Also used : LoginOptions(ch.cyberduck.core.LoginOptions) DescriptiveUrl(ch.cyberduck.core.DescriptiveUrl) FileSharesApi(ch.cyberduck.core.storegate.io.swagger.client.api.FileSharesApi) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) LoginCanceledException(ch.cyberduck.core.exception.LoginCanceledException) Host(ch.cyberduck.core.Host) CreateFileShareRequest(ch.cyberduck.core.storegate.io.swagger.client.model.CreateFileShareRequest) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Example 4 with ApiException

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);
    }
}
Also used : UpdateFilePropertiesRequest(ch.cyberduck.core.storegate.io.swagger.client.model.UpdateFilePropertiesRequest) FilesApi(ch.cyberduck.core.storegate.io.swagger.client.api.FilesApi) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) DateTime(org.joda.time.DateTime) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Example 5 with ApiException

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) FileMetadata(ch.cyberduck.core.storegate.io.swagger.client.model.FileMetadata) JSON(ch.cyberduck.core.storegate.io.swagger.client.JSON) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) StringEntity(org.apache.http.entity.StringEntity) HttpExceptionMappingService(ch.cyberduck.core.http.HttpExceptionMappingService) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Aggregations

ApiException (ch.cyberduck.core.storegate.io.swagger.client.ApiException)15 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)9 FilesApi (ch.cyberduck.core.storegate.io.swagger.client.api.FilesApi)5 IOException (java.io.IOException)5 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)3 JSON (ch.cyberduck.core.storegate.io.swagger.client.JSON)3 File (ch.cyberduck.core.storegate.io.swagger.client.model.File)3 HttpResponse (org.apache.http.HttpResponse)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 HttpPost (org.apache.http.client.methods.HttpPost)3 DateTime (org.joda.time.DateTime)3 DescriptiveUrl (ch.cyberduck.core.DescriptiveUrl)2 Host (ch.cyberduck.core.Host)2 LoginOptions (ch.cyberduck.core.LoginOptions)2 Path (ch.cyberduck.core.Path)2 PathAttributes (ch.cyberduck.core.PathAttributes)2 BackgroundException (ch.cyberduck.core.exception.BackgroundException)2 LoginCanceledException (ch.cyberduck.core.exception.LoginCanceledException)2 HttpExceptionMappingService (ch.cyberduck.core.http.HttpExceptionMappingService)2 FileSharesApi (ch.cyberduck.core.storegate.io.swagger.client.api.FileSharesApi)2