Search in sources :

Example 1 with HttpMethodReleaseInputStream

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

the class BrickReadFeature method read.

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        final FileEntity entity = new FilesApi(new BrickApiClient(session)).download(StringUtils.removeStart(file.getAbsolute(), String.valueOf(Path.DELIMITER)), null, null, null, null);
        final HttpUriRequest request = new HttpGet(entity.getDownloadUri());
        if (status.isAppend()) {
            final HttpRange range = HttpRange.withStatus(status);
            final String header;
            if (TransferStatus.UNKNOWN_LENGTH == range.getEnd()) {
                header = String.format("bytes=%d-", range.getStart());
            } else {
                header = String.format("bytes=%d-%d", range.getStart(), range.getEnd());
            }
            if (log.isDebugEnabled()) {
                log.debug(String.format("Add range header %s for file %s", header, file));
            }
            request.addHeader(new BasicHeader(HttpHeaders.RANGE, header));
            // Disable compression
            request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity"));
        }
        final HttpResponse response = session.getClient().execute(request);
        switch(response.getStatusLine().getStatusCode()) {
            case HttpStatus.SC_OK:
            case HttpStatus.SC_PARTIAL_CONTENT:
                return new HttpMethodReleaseInputStream(response, status);
            default:
                throw new DefaultHttpResponseExceptionMappingService().map("Download {0} failed", new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()), file);
        }
    } catch (ApiException e) {
        throw new BrickExceptionMappingService().map("Download {0} failed", e, file);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) FileEntity(ch.cyberduck.core.brick.io.swagger.client.model.FileEntity) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) HttpMethodReleaseInputStream(ch.cyberduck.core.http.HttpMethodReleaseInputStream) FilesApi(ch.cyberduck.core.brick.io.swagger.client.api.FilesApi) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) BasicHeader(org.apache.http.message.BasicHeader) HttpRange(ch.cyberduck.core.http.HttpRange) ApiException(ch.cyberduck.core.brick.io.swagger.client.ApiException)

Example 2 with HttpMethodReleaseInputStream

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

the class SDSReadFeature method read.

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        final SDSApiClient client = session.getClient();
        final DownloadTokenGenerateResponse token = new NodesApi(session.getClient()).generateDownloadUrl(Long.valueOf(nodeid.getVersionId(file, new DisabledListProgressListener())), StringUtils.EMPTY);
        final HttpUriRequest request = new HttpGet(token.getDownloadUrl());
        request.addHeader("X-Sds-Auth-Token", StringUtils.EMPTY);
        if (status.isAppend()) {
            final HttpRange range = HttpRange.withStatus(status);
            final String header;
            if (TransferStatus.UNKNOWN_LENGTH == range.getEnd()) {
                header = String.format("bytes=%d-", range.getStart());
            } else {
                header = String.format("bytes=%d-%d", range.getStart(), range.getEnd());
            }
            if (log.isDebugEnabled()) {
                log.debug(String.format("Add range header %s for file %s", header, file));
            }
            request.addHeader(new BasicHeader(HttpHeaders.RANGE, header));
            // Disable compression
            request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity"));
        }
        final HttpResponse response = client.getClient().execute(request);
        switch(response.getStatusLine().getStatusCode()) {
            case HttpStatus.SC_OK:
            case HttpStatus.SC_PARTIAL_CONTENT:
                return new HttpMethodReleaseInputStream(response, status);
            case HttpStatus.SC_NOT_FOUND:
                nodeid.cache(file, null);
            // Break through
            default:
                throw new DefaultHttpResponseExceptionMappingService().map("Download {0} failed", new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()), file);
        }
    } catch (ApiException e) {
        throw new SDSExceptionMappingService(nodeid).map("Download {0} failed", e, file);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) HttpMethodReleaseInputStream(ch.cyberduck.core.http.HttpMethodReleaseInputStream) NodesApi(ch.cyberduck.core.sds.io.swagger.client.api.NodesApi) DownloadTokenGenerateResponse(ch.cyberduck.core.sds.io.swagger.client.model.DownloadTokenGenerateResponse) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) BasicHeader(org.apache.http.message.BasicHeader) HttpRange(ch.cyberduck.core.http.HttpRange) ApiException(ch.cyberduck.core.sds.io.swagger.client.ApiException)

Example 3 with HttpMethodReleaseInputStream

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

the class EueReadFeature method read.

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        final String resourceId = fileid.getFileId(file, new DisabledListProgressListener());
        final UiFsModel uiFsModel = new ListResourceApi(new EueApiClient(session)).resourceResourceIdGet(resourceId, null, null, null, null, null, null, Collections.singletonList(EueAttributesFinderFeature.OPTION_DOWNLOAD), null);
        final HttpUriRequest request = new HttpGet(uiFsModel.getUilink().getDownloadURI());
        if (status.isAppend()) {
            final HttpRange range = HttpRange.withStatus(status);
            final String header;
            if (TransferStatus.UNKNOWN_LENGTH == range.getEnd()) {
                header = String.format("bytes=%d-", range.getStart());
            } else {
                header = String.format("bytes=%d-%d", range.getStart(), range.getEnd());
            }
            if (log.isDebugEnabled()) {
                log.debug(String.format("Add range header %s for file %s", header, file));
            }
            request.addHeader(new BasicHeader(HttpHeaders.RANGE, header));
            // Disable compression
            request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity"));
        }
        final HttpResponse response = session.getClient().execute(request);
        switch(response.getStatusLine().getStatusCode()) {
            case HttpStatus.SC_OK:
            case HttpStatus.SC_PARTIAL_CONTENT:
                return new HttpMethodReleaseInputStream(response);
            default:
                throw new DefaultHttpResponseExceptionMappingService().map("Download {0} failed", new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()), file);
        }
    } catch (ApiException e) {
        throw new EueExceptionMappingService().map("Download {0} failed", e, file);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) DefaultHttpResponseExceptionMappingService(ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) HttpMethodReleaseInputStream(ch.cyberduck.core.http.HttpMethodReleaseInputStream) ListResourceApi(ch.cyberduck.core.eue.io.swagger.client.api.ListResourceApi) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) UiFsModel(ch.cyberduck.core.eue.io.swagger.client.model.UiFsModel) BasicHeader(org.apache.http.message.BasicHeader) HttpRange(ch.cyberduck.core.http.HttpRange) ApiException(ch.cyberduck.core.eue.io.swagger.client.ApiException)

Example 4 with HttpMethodReleaseInputStream

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

the class S3ReadFeature method read.

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        if (file.getType().contains(Path.Type.upload)) {
            return new NullInputStream(0L);
        }
        final HttpRange range = HttpRange.withStatus(status);
        final RequestEntityRestStorageService client = session.getClient();
        final Map<String, Object> requestHeaders = new HashMap<>();
        final Map<String, String> requestParameters = new HashMap<>();
        if (file.attributes().getVersionId() != null) {
            requestParameters.put("versionId", file.attributes().getVersionId());
        }
        if (status.isAppend()) {
            final String header;
            if (TransferStatus.UNKNOWN_LENGTH == range.getEnd()) {
                header = String.format("bytes=%d-", range.getStart());
            } else {
                header = String.format("bytes=%d-%d", range.getStart(), range.getEnd());
            }
            if (log.isDebugEnabled()) {
                log.debug(String.format("Add range header %s for file %s", header, file));
            }
            requestHeaders.put(HttpHeaders.RANGE, header);
        }
        final Path bucket = containerService.getContainer(file);
        final HttpResponse response = client.performRestGet(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName(), containerService.getKey(file), requestParameters, requestHeaders, new int[] { HttpStatus.SC_PARTIAL_CONTENT, HttpStatus.SC_OK });
        return new HttpMethodReleaseInputStream(response, status);
    } catch (ServiceException e) {
        throw new S3ExceptionMappingService().map("Download {0} failed", e, file);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file);
    }
}
Also used : Path(ch.cyberduck.core.Path) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpMethodReleaseInputStream(ch.cyberduck.core.http.HttpMethodReleaseInputStream) ServiceException(org.jets3t.service.ServiceException) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) NullInputStream(org.apache.commons.io.input.NullInputStream) HttpRange(ch.cyberduck.core.http.HttpRange)

Example 5 with HttpMethodReleaseInputStream

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

the class DAVReadFeature method read.

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    final List<Header> headers = new ArrayList<Header>(this.headers());
    if (status.isAppend()) {
        final HttpRange range = HttpRange.withStatus(status);
        final String header;
        if (TransferStatus.UNKNOWN_LENGTH == range.getEnd()) {
            header = String.format("bytes=%d-", range.getStart());
        } else {
            header = String.format("bytes=%d-%d", range.getStart(), range.getEnd());
        }
        if (log.isDebugEnabled()) {
            log.debug(String.format("Add range header %s for file %s", header, file));
        }
        headers.add(new BasicHeader(HttpHeaders.RANGE, header));
        // Disable compression
        headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity"));
    }
    try {
        final StringBuilder resource = new StringBuilder(new DAVPathEncoder().encode(file));
        if (!status.getParameters().isEmpty()) {
            resource.append("?");
        }
        for (Map.Entry<String, String> parameter : status.getParameters().entrySet()) {
            if (!resource.toString().endsWith("?")) {
                resource.append("&");
            }
            resource.append(URIEncoder.encode(parameter.getKey())).append("=").append(URIEncoder.encode(parameter.getValue()));
        }
        final HttpGet request = new HttpGet(resource.toString());
        for (Header header : headers) {
            request.addHeader(header);
        }
        final HttpResponse response = session.getClient().execute(request);
        final VoidResponseHandler handler = new VoidResponseHandler();
        try {
            handler.handleResponse(response);
            // Will abort the read when closed before EOF.
            final ContentLengthStatusInputStream stream = new ContentLengthStatusInputStream(new HttpMethodReleaseInputStream(response, status), response.getEntity().getContentLength(), response.getStatusLine().getStatusCode());
            if (status.isAppend()) {
                if (stream.getCode() == HttpStatus.SC_OK) {
                    if (TransferStatus.UNKNOWN_LENGTH != status.getLength()) {
                        if (stream.getLength() != status.getLength()) {
                            log.warn(String.format("Range header not supported. Skipping %d bytes in file %s.", status.getOffset(), file));
                            stream.skip(status.getOffset());
                        }
                    }
                }
            }
            return stream;
        } catch (IOException ex) {
            request.abort();
            throw ex;
        }
    } catch (SardineException e) {
        throw new DAVExceptionMappingService().map("Download {0} failed", e, file);
    } catch (IOException e) {
        throw new HttpExceptionMappingService().map("Download {0} failed", e, file);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) VoidResponseHandler(com.github.sardine.impl.handler.VoidResponseHandler) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpMethodReleaseInputStream(ch.cyberduck.core.http.HttpMethodReleaseInputStream) SardineException(com.github.sardine.impl.SardineException) HttpExceptionMappingService(ch.cyberduck.core.http.HttpExceptionMappingService) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) Map(java.util.Map) BasicHeader(org.apache.http.message.BasicHeader) HttpRange(ch.cyberduck.core.http.HttpRange)

Aggregations

HttpMethodReleaseInputStream (ch.cyberduck.core.http.HttpMethodReleaseInputStream)11 IOException (java.io.IOException)10 HttpRange (ch.cyberduck.core.http.HttpRange)9 BasicHeader (org.apache.http.message.BasicHeader)9 HttpResponse (org.apache.http.HttpResponse)7 HttpGet (org.apache.http.client.methods.HttpGet)7 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)6 DefaultHttpResponseExceptionMappingService (ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService)5 HttpResponseException (org.apache.http.client.HttpResponseException)5 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 HashMap (java.util.HashMap)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 HttpExceptionMappingService (ch.cyberduck.core.http.HttpExceptionMappingService)2 List (java.util.List)2 Path (ch.cyberduck.core.Path)1 ApiException (ch.cyberduck.core.brick.io.swagger.client.ApiException)1 FilesApi (ch.cyberduck.core.brick.io.swagger.client.api.FilesApi)1 FileEntity (ch.cyberduck.core.brick.io.swagger.client.model.FileEntity)1 ApiException (ch.cyberduck.core.eue.io.swagger.client.ApiException)1