Search in sources :

Example 1 with DefaultHttpResponseExceptionMappingService

use of ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService in project cyberduck by iterate-ch.

the class BoxUploadHelper method commitUploadSession.

public Files commitUploadSession(final Path file, final String uploadSessionId, final TransferStatus overall, final List<UploadPart> uploadParts) throws BackgroundException {
    try {
        final HttpPost request = new HttpPost(String.format("%s/files/upload_sessions/%s/commit", client.getBasePath(), uploadSessionId));
        if (!Checksum.NONE.equals(overall.getChecksum())) {
            request.addHeader(new BasicHeader("Digest", String.format("sha=%s", overall.getChecksum().hash)));
        }
        final ByteArrayOutputStream content = new ByteArrayOutputStream();
        final UploadSessionIdCommitBody body = new UploadSessionIdCommitBody().parts(uploadParts);
        new JSON().getContext(null).writeValue(content, body);
        request.setEntity(new ByteArrayEntity(content.toByteArray()));
        if (overall.isExists()) {
            if (StringUtils.isNotBlank(overall.getRemote().getETag())) {
                request.addHeader(new BasicHeader(HttpHeaders.IF_MATCH, overall.getRemote().getETag()));
            } else {
                log.warn(String.format("Missing remote attributes in transfer status to read current ETag for %s", file));
            }
        }
        return session.getClient().execute(request, new BoxClientErrorResponseHandler<Files>() {

            @Override
            public Files handleResponse(final HttpResponse response) throws IOException {
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_ACCEPTED) {
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Wait for server to process chunks with response %s", response));
                    }
                    this.flush(file, response, uploadSessionId);
                    return session.getClient().execute(request, this);
                }
                return super.handleResponse(response);
            }

            @Override
            public Files handleEntity(final HttpEntity entity) throws IOException {
                return new JSON().getContext(null).readValue(entity.getContent(), Files.class);
            }

            /**
             * Wait for server processing all pending chunks
             */
            private void flush(final Path file, final HttpResponse response, final String uploadSessionId) throws IOException {
                UploadSession uploadSession;
                do {
                    final HttpGet request = new HttpGet(String.format("%s/files/upload_sessions/%s", client.getBasePath(), uploadSessionId));
                    uploadSession = new JSON().getContext(null).readValue(session.getClient().execute(request).getEntity().getContent(), UploadSession.class);
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Server processed %d of %d parts", uploadSession.getNumPartsProcessed(), uploadSession.getTotalParts()));
                    }
                } while (!Objects.equals(uploadSession.getNumPartsProcessed(), uploadSession.getTotalParts()));
            }
        });
    } catch (HttpResponseException e) {
        throw new DefaultHttpResponseExceptionMappingService().map("Upload {0} failed", e, file);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map("Upload {0} failed", e, file);
    }
}
Also used : Path(ch.cyberduck.core.Path) HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) JSON(ch.cyberduck.core.box.io.swagger.client.JSON) HttpResponseException(org.apache.http.client.HttpResponseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UploadSession(ch.cyberduck.core.box.io.swagger.client.model.UploadSession) UploadSessionIdCommitBody(ch.cyberduck.core.box.io.swagger.client.model.UploadSessionIdCommitBody) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) Files(ch.cyberduck.core.box.io.swagger.client.model.Files) BasicHeader(org.apache.http.message.BasicHeader)

Example 2 with DefaultHttpResponseExceptionMappingService

use of ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService in project cyberduck by iterate-ch.

the class BoxWriteFeature 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 HttpPost request;
                if (status.isExists()) {
                    request = new HttpPost(String.format("%s/files/%s/content?fields=%s", client.getBasePath(), fileid.getFileId(file, new DisabledListProgressListener()), String.join(",", BoxAttributesFinderFeature.DEFAULT_FIELDS)));
                } else {
                    request = new HttpPost(String.format("%s/files/content?fields=%s", client.getBasePath(), String.join(",", BoxAttributesFinderFeature.DEFAULT_FIELDS)));
                }
                final Checksum checksum = status.getChecksum();
                if (Checksum.NONE != checksum) {
                    switch(checksum.algorithm) {
                        case sha1:
                            request.addHeader(HttpHeaders.CONTENT_MD5, checksum.hash);
                    }
                }
                final ByteArrayOutputStream content = new ByteArrayOutputStream();
                new JSON().getContext(null).writeValue(content, new FilescontentAttributes().name(file.getName()).parent(new FilescontentAttributesParent().id(fileid.getFileId(file.getParent(), new DisabledListProgressListener()))).contentModifiedAt(status.getTimestamp() != null ? new DateTime(status.getTimestamp()) : null));
                final MultipartEntityBuilder multipart = MultipartEntityBuilder.create();
                multipart.addBinaryBody("attributes", content.toByteArray());
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                entity.writeTo(out);
                multipart.addBinaryBody("file", out.toByteArray(), null == status.getMime() ? ContentType.APPLICATION_OCTET_STREAM : ContentType.create(status.getMime()), file.getName());
                request.setEntity(multipart.build());
                if (status.isExists()) {
                    if (StringUtils.isNotBlank(status.getRemote().getETag())) {
                        request.addHeader(new BasicHeader(HttpHeaders.IF_MATCH, status.getRemote().getETag()));
                    } else {
                        log.warn(String.format("Missing remote attributes in transfer status to read current ETag for %s", file));
                    }
                }
                final Files files = session.getClient().execute(request, new BoxClientErrorResponseHandler<Files>() {

                    @Override
                    public Files handleEntity(final HttpEntity entity) throws IOException {
                        return new JSON().getContext(null).readValue(entity.getContent(), Files.class);
                    }
                });
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Received response %s for upload of %s", files, file));
                }
                if (files.getEntries().stream().findFirst().isPresent()) {
                    return files.getEntries().stream().findFirst().get();
                }
                throw new NotfoundException(file.getAbsolute());
            } 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 : HttpPost(org.apache.http.client.methods.HttpPost) NotfoundException(ch.cyberduck.core.exception.NotfoundException) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) JSON(ch.cyberduck.core.box.io.swagger.client.JSON) FilescontentAttributes(ch.cyberduck.core.box.io.swagger.client.model.FilescontentAttributes) HttpResponseException(org.apache.http.client.HttpResponseException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FilescontentAttributesParent(ch.cyberduck.core.box.io.swagger.client.model.FilescontentAttributesParent) DelayedHttpEntityCallable(ch.cyberduck.core.http.DelayedHttpEntityCallable) DateTime(org.joda.time.DateTime) Checksum(ch.cyberduck.core.io.Checksum) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) Files(ch.cyberduck.core.box.io.swagger.client.model.Files) File(ch.cyberduck.core.box.io.swagger.client.model.File) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BasicHeader(org.apache.http.message.BasicHeader)

Example 3 with DefaultHttpResponseExceptionMappingService

use of ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService 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)

Example 4 with DefaultHttpResponseExceptionMappingService

use of ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService in project cyberduck by iterate-ch.

the class BrickWriteFeature method write.

@Override
public HttpResponseOutputStream<FileEntity> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    final String uploadUri;
    FileUploadPartEntity uploadPartEntity = null;
    if (StringUtils.isBlank(status.getUrl())) {
        uploadPartEntity = new BrickUploadFeature(session, this).startUpload(file);
        uploadUri = uploadPartEntity.getUploadUri();
    } else {
        uploadUri = status.getUrl();
    }
    final HttpResponseOutputStream<FileEntity> stream = this.write(file, status, new DelayedHttpEntityCallable<FileEntity>() {

        @Override
        public FileEntity call(final AbstractHttpEntity entity) throws BackgroundException {
            try {
                final HttpPut request = new HttpPut(uploadUri);
                request.setEntity(entity);
                request.setHeader(HttpHeaders.CONTENT_TYPE, MimeTypeService.DEFAULT_CONTENT_TYPE);
                final HttpResponse response = session.getClient().execute(request);
                // Validate response
                try {
                    switch(response.getStatusLine().getStatusCode()) {
                        case HttpStatus.SC_OK:
                            // Upload complete
                            if (response.containsHeader("ETag")) {
                                if (log.isInfoEnabled()) {
                                    log.info(String.format("Received response %s for part number %d", response, status.getPart()));
                                }
                                if (HashAlgorithm.md5.equals(status.getChecksum().algorithm)) {
                                    final Checksum etag = Checksum.parse(StringUtils.remove(response.getFirstHeader("ETag").getValue(), '"'));
                                    if (!status.getChecksum().equals(etag)) {
                                        throw new ChecksumException(MessageFormat.format(LocaleFactory.localizedString("Upload {0} failed", "Error"), file.getName()), MessageFormat.format("Mismatch between {0} hash {1} of uploaded data and ETag {2} returned by the server", etag.algorithm.toString(), status.getChecksum().hash, etag.hash));
                                    }
                                }
                                return null;
                            } else {
                                log.error(String.format("Missing ETag in response %s", response));
                                throw new InteroperabilityException(response.getStatusLine().getReasonPhrase());
                            }
                        default:
                            EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity()));
                            throw new DefaultHttpResponseExceptionMappingService().map(new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
                    }
                } finally {
                    EntityUtils.consume(response.getEntity());
                }
            } catch (HttpResponseException e) {
                throw new DefaultHttpResponseExceptionMappingService().map(e);
            } catch (IOException e) {
                throw new DefaultIOExceptionMappingService().map(e);
            }
        }

        @Override
        public long getContentLength() {
            return status.getLength();
        }
    });
    if (StringUtils.isBlank(status.getUrl())) {
        final String ref = uploadPartEntity.getRef();
        return new HttpResponseOutputStream<FileEntity>(new ProxyOutputStream(stream), new BrickAttributesFinderFeature(session), status) {

            private final AtomicBoolean close = new AtomicBoolean();

            @Override
            public FileEntity getStatus() throws BackgroundException {
                return stream.getStatus();
            }

            @Override
            public void close() throws IOException {
                if (close.get()) {
                    log.warn(String.format("Skip double close of stream %s", this));
                    return;
                }
                super.close();
                try {
                    new BrickUploadFeature(session, BrickWriteFeature.this).completeUpload(file, ref, status, Collections.singletonList(status));
                } catch (BackgroundException e) {
                    throw new IOException(e.getMessage(), e);
                } finally {
                    close.set(true);
                }
            }
        };
    }
    return stream;
}
Also used : FileEntity(ch.cyberduck.core.brick.io.swagger.client.model.FileEntity) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) ChecksumException(ch.cyberduck.core.exception.ChecksumException) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) FileUploadPartEntity(ch.cyberduck.core.brick.io.swagger.client.model.FileUploadPartEntity) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) Checksum(ch.cyberduck.core.io.Checksum) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) HttpResponseOutputStream(ch.cyberduck.core.http.HttpResponseOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BackgroundException(ch.cyberduck.core.exception.BackgroundException)

Example 5 with DefaultHttpResponseExceptionMappingService

use of ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService in project cyberduck by iterate-ch.

the class BrickExceptionMappingService method map.

@Override
public BackgroundException map(final ApiException failure) {
    switch(failure.getCode()) {
        case HttpStatus.SC_UNPROCESSABLE_ENTITY:
            return new LockedException(StringUtils.EMPTY, failure);
    }
    for (Throwable cause : ExceptionUtils.getThrowableList(failure)) {
        if (cause instanceof SocketException) {
            // Map Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Broken pipe
            return new DefaultSocketExceptionMappingService().map((SocketException) cause);
        }
        if (cause instanceof HttpResponseException) {
            return new DefaultHttpResponseExceptionMappingService().map((HttpResponseException) cause);
        }
        if (cause instanceof IOException) {
            return new DefaultIOExceptionMappingService().map((IOException) cause);
        }
        if (cause instanceof IllegalStateException) {
            // Caused by: ch.cyberduck.core.sds.io.swagger.client.ApiException: javax.ws.rs.ProcessingException: java.lang.IllegalStateException: Connection pool shut down
            return new ConnectionCanceledException(cause);
        }
    }
    final StringBuilder buffer = new StringBuilder();
    this.parse(buffer, failure.getResponseBody());
    return new DefaultHttpResponseExceptionMappingService().map(failure, buffer, failure.getCode());
}
Also used : SocketException(java.net.SocketException) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) LockedException(ch.cyberduck.core.exception.LockedException) DefaultSocketExceptionMappingService(ch.cyberduck.core.DefaultSocketExceptionMappingService) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService)

Aggregations

DefaultHttpResponseExceptionMappingService (ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService)34 HttpResponseException (org.apache.http.client.HttpResponseException)30 IOException (java.io.IOException)27 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)24 HttpResponse (org.apache.http.HttpResponse)15 HttpPost (org.apache.http.client.methods.HttpPost)11 HttpEntity (org.apache.http.HttpEntity)9 BasicHeader (org.apache.http.message.BasicHeader)8 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)7 HttpRange (ch.cyberduck.core.http.HttpRange)7 HttpPut (org.apache.http.client.methods.HttpPut)7 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)6 LoginFailureException (ch.cyberduck.core.exception.LoginFailureException)5 RetriableAccessDeniedException (ch.cyberduck.core.exception.RetriableAccessDeniedException)5 HttpMethodReleaseInputStream (ch.cyberduck.core.http.HttpMethodReleaseInputStream)5 HttpGet (org.apache.http.client.methods.HttpGet)5 BackgroundException (ch.cyberduck.core.exception.BackgroundException)4 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)4 LoginCanceledException (ch.cyberduck.core.exception.LoginCanceledException)4 DelayedHttpEntityCallable (ch.cyberduck.core.http.DelayedHttpEntityCallable)4