Search in sources :

Example 6 with ApiException

use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class StoregateWriteFeature method write.

@Override
public HttpResponseOutputStream<FileMetadata> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    final DelayedHttpEntityCallable<FileMetadata> command = new DelayedHttpEntityCallable<FileMetadata>() {

        @Override
        public FileMetadata call(final AbstractHttpEntity entity) throws BackgroundException {
            // Initiate a resumable upload
            String location;
            try {
                location = start(file, status);
            } catch (InteroperabilityException e) {
                if (null == status.getLockId()) {
                    throw e;
                }
                location = start(file, status.withLockId(null));
            }
            final StoregateApiClient client = session.getClient();
            try {
                // Upload the file
                final HttpPut put = new HttpPut(location);
                put.setEntity(entity);
                final String header;
                if (status.getLength() == 0) {
                    // Touch
                    header = "*/0";
                } else {
                    final HttpRange range = HttpRange.byLength(0, status.getLength());
                    header = String.format("%d-%d/%d", range.getStart(), range.getEnd(), status.getLength());
                }
                put.addHeader(HttpHeaders.CONTENT_RANGE, String.format("bytes %s", header));
                final HttpResponse putResponse = client.getClient().execute(put);
                try {
                    switch(putResponse.getStatusLine().getStatusCode()) {
                        case HttpStatus.SC_OK:
                        case HttpStatus.SC_CREATED:
                            final FileMetadata result = new JSON().getContext(FileMetadata.class).readValue(new InputStreamReader(putResponse.getEntity().getContent(), StandardCharsets.UTF_8), FileMetadata.class);
                            fileid.cache(file, result.getId());
                            return result;
                        default:
                            throw new StoregateExceptionMappingService(fileid).map(new ApiException(putResponse.getStatusLine().getStatusCode(), putResponse.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(putResponse.getEntity())));
                    }
                } catch (BackgroundException e) {
                    // Cancel upload on error reply
                    cancel(file, location);
                    throw e;
                } finally {
                    EntityUtils.consume(putResponse.getEntity());
                }
            } catch (IOException e) {
                // Cancel upload on I/O failure
                cancel(file, location);
                throw new HttpExceptionMappingService().map("Upload {0} failed", e, file);
            }
        }

        @Override
        public long getContentLength() {
            return status.getLength();
        }
    };
    return this.write(file, status, command);
}
Also used : InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) InputStreamReader(java.io.InputStreamReader) FileMetadata(ch.cyberduck.core.storegate.io.swagger.client.model.FileMetadata) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) JSON(ch.cyberduck.core.storegate.io.swagger.client.JSON) IOException(java.io.IOException) DelayedHttpEntityCallable(ch.cyberduck.core.http.DelayedHttpEntityCallable) HttpPut(org.apache.http.client.methods.HttpPut) HttpExceptionMappingService(ch.cyberduck.core.http.HttpExceptionMappingService) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BackgroundException(ch.cyberduck.core.exception.BackgroundException) HttpRange(ch.cyberduck.core.http.HttpRange) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Example 7 with ApiException

use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class StoregateListService method list.

@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
    if (directory.isRoot()) {
        final AttributedList<Path> list = new AttributedList<>();
        for (RootFolder root : session.roots()) {
            switch(root.getRootFolderType()) {
                // My Files
                case 0:
                case // Common
                1:
                    final PathAttributes attr = new PathAttributes().withFileId(root.getId());
                    attr.setModificationDate(root.getModified().getMillis());
                    attr.setCreationDate(root.getCreated().getMillis());
                    list.add(new Path(PathNormalizer.normalize(root.getName()), EnumSet.of(Path.Type.directory, Path.Type.volume), attr));
                    break;
            }
        }
        listener.chunk(directory, list);
        return list;
    } else {
        try {
            final AttributedList<Path> children = new AttributedList<>();
            final StoregateAttributesFinderFeature attributes = new StoregateAttributesFinderFeature(session, fileid);
            int pageIndex = 0;
            FileContents files;
            do {
                files = new FilesApi(this.session.getClient()).filesGet(URIEncoder.encode(fileid.getPrefixedPath(directory)), pageIndex, chunksize, "Name asc", // All
                0, true, false, false);
                for (File f : files.getFiles()) {
                    final PathAttributes attrs = attributes.toAttributes(f);
                    final EnumSet<Path.Type> type = (f.getFlags() & 1) == 1 ? EnumSet.of(Path.Type.directory) : EnumSet.of(Path.Type.file);
                    final Path p = new Path(directory, f.getName(), type, attrs);
                    children.add(p);
                    listener.chunk(directory, children);
                }
                pageIndex++;
            } while (children.size() < files.getTotalRowCount());
            return children;
        } catch (ApiException e) {
            throw new StoregateExceptionMappingService(fileid).map("Listing directory {0} failed", e, directory);
        }
    }
}
Also used : Path(ch.cyberduck.core.Path) PathAttributes(ch.cyberduck.core.PathAttributes) FileContents(ch.cyberduck.core.storegate.io.swagger.client.model.FileContents) FilesApi(ch.cyberduck.core.storegate.io.swagger.client.api.FilesApi) AttributedList(ch.cyberduck.core.AttributedList) RootFolder(ch.cyberduck.core.storegate.io.swagger.client.model.RootFolder) File(ch.cyberduck.core.storegate.io.swagger.client.model.File) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Example 8 with ApiException

use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class StoregateMoveFeature method move.

@Override
public Path move(final Path file, final Path renamed, final TransferStatus status, final Delete.Callback delete, final ConnectionCallback callback) throws BackgroundException {
    try {
        final StoregateApiClient client = session.getClient();
        final MoveFileRequest move = new MoveFileRequest().name(renamed.getName()).parentID(fileid.getFileId(renamed.getParent(), new DisabledListProgressListener())).mode(// Overwrite
        1);
        final HttpEntityEnclosingRequestBase request;
        request = new HttpPost(String.format("%s/v4/files/%s/move", client.getBasePath(), fileid.getFileId(file, new DisabledListProgressListener())));
        if (status.getLockId() != null) {
            request.addHeader("X-Lock-Id", status.getLockId().toString());
        }
        request.setEntity(new StringEntity(new JSON().getContext(move.getClass()).writeValueAsString(move), ContentType.create("application/json", StandardCharsets.UTF_8.name())));
        request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE);
        final HttpResponse response = client.getClient().execute(request);
        try {
            switch(response.getStatusLine().getStatusCode()) {
                case HttpStatus.SC_NO_CONTENT:
                    final PathAttributes attr = new PathAttributes(file.attributes());
                    fileid.cache(file, null);
                    fileid.cache(renamed, file.attributes().getFileId());
                    return renamed.withAttributes(attr);
                default:
                    throw new StoregateExceptionMappingService(fileid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
            }
        } finally {
            EntityUtils.consume(response.getEntity());
        }
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) PathAttributes(ch.cyberduck.core.PathAttributes) HttpResponse(org.apache.http.HttpResponse) JSON(ch.cyberduck.core.storegate.io.swagger.client.JSON) MoveFileRequest(ch.cyberduck.core.storegate.io.swagger.client.model.MoveFileRequest) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Example 9 with ApiException

use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class StoregateQuotaFeature method get.

@Override
public Space get() throws BackgroundException {
    try {
        final AccountSettingsApi account = new AccountSettingsApi(session.getClient());
        final AccountStorage quota = account.accountSettingsGetAccountStorage();
        return new Space(quota.getUsed(), quota.getAvailable());
    } catch (ApiException e) {
        throw new StoregateExceptionMappingService(fileid).map("Failure to read attributes of {0}", e, new DefaultHomeFinderService(session).find());
    }
}
Also used : AccountSettingsApi(ch.cyberduck.core.storegate.io.swagger.client.api.AccountSettingsApi) DefaultHomeFinderService(ch.cyberduck.core.shared.DefaultHomeFinderService) AccountStorage(ch.cyberduck.core.storegate.io.swagger.client.model.AccountStorage) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Example 10 with ApiException

use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.

the class StoregateCopyFeature method copy.

@Override
public Path copy(final Path source, final Path target, final TransferStatus status, final ConnectionCallback callback, final StreamListener listener) throws BackgroundException {
    try {
        final CopyFileRequest copy = new CopyFileRequest().name(target.getName()).parentID(fileid.getFileId(target.getParent(), new DisabledListProgressListener())).mode(// Overwrite
        1);
        final File file = new FilesApi(session.getClient()).filesCopy(fileid.getFileId(source, new DisabledListProgressListener()), copy);
        listener.sent(status.getLength());
        fileid.cache(target, file.getId());
        return target.withAttributes(new StoregateAttributesFinderFeature(session, fileid).toAttributes(file));
    } catch (ApiException e) {
        throw new StoregateExceptionMappingService(fileid).map("Cannot copy {0}", e, source);
    }
}
Also used : FilesApi(ch.cyberduck.core.storegate.io.swagger.client.api.FilesApi) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) CopyFileRequest(ch.cyberduck.core.storegate.io.swagger.client.model.CopyFileRequest) File(ch.cyberduck.core.storegate.io.swagger.client.model.File) 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