Search in sources :

Example 1 with UploadedPart

use of ch.cyberduck.core.box.io.swagger.client.model.UploadedPart in project cyberduck by iterate-ch.

the class BoxChunkedWriteFeature method write.

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

        @Override
        public File call(final AbstractHttpEntity entity) throws BackgroundException {
            try {
                final HttpRange range = HttpRange.withStatus(new TransferStatus().withLength(status.getLength()).withOffset(status.getOffset()));
                final String uploadSessionId = status.getParameters().get(BoxLargeUploadService.UPLOAD_SESSION_ID);
                final String overall_length = status.getParameters().get(BoxLargeUploadService.OVERALL_LENGTH);
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Send range %s for file %s", range, file));
                }
                final HttpPut request = new HttpPut(String.format("%s/files/upload_sessions/%s", client.getBasePath(), uploadSessionId));
                // Must not overlap with the range of a part already uploaded this session.
                request.addHeader(new BasicHeader(HttpHeaders.CONTENT_RANGE, String.format("bytes %d-%d/%d", range.getStart(), range.getEnd(), Long.valueOf(overall_length))));
                request.addHeader(new BasicHeader("Digest", String.format("sha=%s", status.getChecksum())));
                request.setEntity(entity);
                final UploadedPart uploadedPart = session.getClient().execute(request, new BoxClientErrorResponseHandler<UploadedPart>() {

                    @Override
                    public UploadedPart handleEntity(final HttpEntity entity1) throws IOException {
                        return new JSON().getContext(null).readValue(entity1.getContent(), UploadedPart.class);
                    }
                });
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Received response %s for upload of %s", uploadedPart, file));
                }
                return new File().size(status.getLength()).sha1(uploadedPart.getPart().getSha1());
            } catch (HttpResponseException e) {
                throw new DefaultHttpResponseExceptionMappingService().map(e);
            } catch (IOException e) {
                throw new DefaultIOExceptionMappingService().map("Upload {0} failed", e, file);
            }
        }

        @Override
        public long getContentLength() {
            return -1L;
        }
    };
    return this.write(file, status, command);
}
Also used : UploadedPart(ch.cyberduck.core.box.io.swagger.client.model.UploadedPart) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) JSON(ch.cyberduck.core.box.io.swagger.client.JSON) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) DelayedHttpEntityCallable(ch.cyberduck.core.http.DelayedHttpEntityCallable) HttpPut(org.apache.http.client.methods.HttpPut) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) File(ch.cyberduck.core.box.io.swagger.client.model.File) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BasicHeader(org.apache.http.message.BasicHeader) HttpRange(ch.cyberduck.core.http.HttpRange)

Aggregations

DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)1 JSON (ch.cyberduck.core.box.io.swagger.client.JSON)1 File (ch.cyberduck.core.box.io.swagger.client.model.File)1 UploadedPart (ch.cyberduck.core.box.io.swagger.client.model.UploadedPart)1 DefaultHttpResponseExceptionMappingService (ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService)1 DelayedHttpEntityCallable (ch.cyberduck.core.http.DelayedHttpEntityCallable)1 HttpRange (ch.cyberduck.core.http.HttpRange)1 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)1 IOException (java.io.IOException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponseException (org.apache.http.client.HttpResponseException)1 HttpPut (org.apache.http.client.methods.HttpPut)1 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)1 BasicHeader (org.apache.http.message.BasicHeader)1