Example usage for com.google.common.net HttpHeaders RANGE

List of usage examples for com.google.common.net HttpHeaders RANGE

Introduction

In this page you can find the example usage for com.google.common.net HttpHeaders RANGE.

Prototype

String RANGE

To view the source code for com.google.common.net HttpHeaders RANGE.

Click Source Link

Document

The HTTP Range header field name.

Usage

From source file:org.apache.druid.segment.realtime.firehose.HttpFirehoseFactory.java

@Override
protected InputStream openObjectStream(URI object, long start) throws IOException {
    if (supportContentRange) {
        final URLConnection connection = object.toURL().openConnection();
        // Set header for range request.
        // Since we need to set only the start offset, the header is "bytes=<range-start>-".
        // See https://tools.ietf.org/html/rfc7233#section-2.1
        connection.addRequestProperty(HttpHeaders.RANGE, StringUtils.format("bytes=%d-", start));
        return connection.getInputStream();
    } else {/*from  ww  w  .  j av a 2  s  .c  om*/
        log.warn(
                "Since the input source doesn't support range requests, the object input stream is opened from the start and "
                        + "then skipped. This may make the ingestion speed slower. Consider enabling prefetch if you see this message"
                        + " a lot.");
        final InputStream in = openObjectStream(object);
        in.skip(start);
        return in;
    }
}

From source file:org.sonatype.nexus.repository.http.PartialFetchHandler.java

private String getRangeHeader(final Context context) {
    final Request request = context.getRequest();
    return request.getHeaders().get(HttpHeaders.RANGE);
}

From source file:org.elasticlib.common.client.RepositoryTarget.java

/**
 * Partially downloads a content from this repository. Supplied range [offset, offset + length[ is expected to be a
 * included in [0, totalContentLength]./*from w w  w .j  a  v a 2  s  .c  o  m*/
 *
 * @param hash Content hash.
 * @param offset The position of first byte to return, inclusive. Expected to be positive or zero.
 * @param length The amount of bytes to returns. Expected to be positive or zero.
 * @return Corresponding content.
 */
public Content getContent(Hash hash, long offset, long length) {
    checkArgument(offset >= 0, "Offset is negative");
    checkArgument(length >= 0, "Length is negative");

    return getContent(target.path(CONTENTS_TEMPLATE).resolveTemplate(HASH, hash).request()
            .header(HttpHeaders.RANGE, range(offset, length)));
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handleGetBlob(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore,
        String containerName, String blobName) throws IOException, S3Exception {
    int status = HttpServletResponse.SC_OK;
    GetOptions options = new GetOptions();
    String range = request.getHeader(HttpHeaders.RANGE);
    if (range != null && range.startsWith("bytes=") &&
    // ignore multiple ranges
            range.indexOf(',') == -1) {
        range = range.substring("bytes=".length());
        String[] ranges = range.split("-", 2);
        if (ranges[0].isEmpty()) {
            options.tail(Long.parseLong(ranges[1]));
        } else if (ranges[1].isEmpty()) {
            options.startAt(Long.parseLong(ranges[0]));
        } else {//from w w  w .j a  va 2  s .c  o  m
            options.range(Long.parseLong(ranges[0]), Long.parseLong(ranges[1]));
        }
        status = HttpServletResponse.SC_PARTIAL_CONTENT;
    }

    Blob blob;
    try {
        blob = blobStore.getBlob(containerName, blobName, options);
    } catch (IllegalArgumentException iae) {
        throw new S3Exception(S3ErrorCode.INVALID_RANGE);
    }
    if (blob == null) {
        throw new S3Exception(S3ErrorCode.NO_SUCH_KEY);
    }

    response.setStatus(status);

    addMetadataToResponse(response, blob.getMetadata());
    Collection<String> contentRanges = blob.getAllHeaders().get(HttpHeaders.CONTENT_RANGE);
    if (!contentRanges.isEmpty()) {
        response.addHeader(HttpHeaders.CONTENT_RANGE, contentRanges.iterator().next());
    }

    try (InputStream is = blob.getPayload().openStream(); OutputStream os = response.getOutputStream()) {
        ByteStreams.copy(is, os);
        os.flush();
    }
}