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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations