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

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

Introduction

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

Prototype

String ACCEPT_RANGES

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

Click Source Link

Document

The HTTP Accept-Ranges header field name.

Usage

From source file:io.druid.segment.realtime.firehose.HttpFirehoseFactory.java

@JsonCreator
public HttpFirehoseFactory(@JsonProperty("uris") List<URI> uris,
        @JsonProperty("maxCacheCapacityBytes") Long maxCacheCapacityBytes,
        @JsonProperty("maxFetchCapacityBytes") Long maxFetchCapacityBytes,
        @JsonProperty("prefetchTriggerBytes") Long prefetchTriggerBytes,
        @JsonProperty("fetchTimeout") Long fetchTimeout, @JsonProperty("maxFetchRetry") Integer maxFetchRetry)
        throws IOException {
    super(maxCacheCapacityBytes, maxFetchCapacityBytes, prefetchTriggerBytes, fetchTimeout, maxFetchRetry);
    this.uris = uris;

    Preconditions.checkArgument(uris.size() > 0, "Empty URIs");
    final URLConnection connection = uris.get(0).toURL().openConnection();
    final String acceptRanges = connection.getHeaderField(HttpHeaders.ACCEPT_RANGES);
    this.supportContentRange = acceptRanges != null && acceptRanges.equalsIgnoreCase("bytes");
}

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

@JsonCreator
public HttpFirehoseFactory(@JsonProperty("uris") List<URI> uris,
        @JsonProperty("maxCacheCapacityBytes") Long maxCacheCapacityBytes,
        @JsonProperty("maxFetchCapacityBytes") Long maxFetchCapacityBytes,
        @JsonProperty("prefetchTriggerBytes") Long prefetchTriggerBytes,
        @JsonProperty("fetchTimeout") Long fetchTimeout, @JsonProperty("maxFetchRetry") Integer maxFetchRetry)
        throws IOException {
    super(maxCacheCapacityBytes, maxFetchCapacityBytes, prefetchTriggerBytes, fetchTimeout, maxFetchRetry);
    this.uris = uris;

    Preconditions.checkArgument(uris.size() > 0, "Empty URIs");
    final URLConnection connection = uris.get(0).toURL().openConnection();
    final String acceptRanges = connection.getHeaderField(HttpHeaders.ACCEPT_RANGES);
    this.supportContentRange = acceptRanges != null && "bytes".equalsIgnoreCase(acceptRanges);
}

From source file:org.eclipse.hawkbit.rest.util.RestResourceConversionHelper.java

/**
 * <p>//from ww w.  j a  va 2s  .  c  o m
 * Write response with target relation and publishes events concerning the
 * download progress based on given update action status.
 * </p>
 *
 * <p>
 * The request supports RFC7233 range requests.
 * </p>
 *
 * @param artifact
 *            the artifact
 * @param response
 *            to be sent back to the requesting client
 * @param request
 *            from the client
 * @param file
 *            to be write to the client response
 * @param controllerManagement
 *            to write progress updates to
 * @param statusId
 *            of the {@link ActionStatus}
 *
 * @return http code
 *
 * @see <a href="https://tools.ietf.org/html/rfc7233">https://tools.ietf.org
 *      /html/rfc7233</a>
 */
public static ResponseEntity<InputStream> writeFileResponse(final Artifact artifact,
        final HttpServletResponse response, final HttpServletRequest request, final DbArtifact file,
        final ControllerManagement controllerManagement, final Long statusId) {

    ResponseEntity<InputStream> result;

    final String etag = artifact.getSha1Hash();
    final Long lastModified = artifact.getLastModifiedAt() != null ? artifact.getLastModifiedAt()
            : artifact.getCreatedAt();
    final long length = file.getSize();

    response.reset();
    response.setBufferSize(BUFFER_SIZE);
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + artifact.getFilename());
    response.setHeader(HttpHeaders.ETAG, etag);
    response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
    response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModified);
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

    final ByteRange full = new ByteRange(0, length - 1, length);
    final List<ByteRange> ranges = new ArrayList<>();

    // Validate and process Range and If-Range headers.
    final String range = request.getHeader("Range");
    if (range != null) {
        LOG.debug("range header for filename ({}) is: {}", artifact.getFilename(), range);

        // Range header matches"bytes=n-n,n-n,n-n..."
        if (!range.matches("^bytes=\\d*-\\d*(,\\d*-\\d*)*$")) {
            response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + length);
            LOG.debug("range header for filename ({}) is not satisfiable: ", artifact.getFilename());
            return new ResponseEntity<>(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE);
        }

        // RFC: if the representation is unchanged, send me the part(s) that
        // I am requesting in
        // Range; otherwise, send me the entire representation.
        checkForShortcut(request, etag, lastModified, full, ranges);

        // it seems there are valid ranges
        result = extractRange(response, length, ranges, range);
        // return if range extraction turned out to be invalid
        if (result != null) {
            return result;
        }
    }

    // full request - no range
    if (ranges.isEmpty() || ranges.get(0).equals(full)) {
        LOG.debug("filename ({}) results into a full request: ", artifact.getFilename());
        handleFullFileRequest(artifact, response, file, controllerManagement, statusId, full);
        result = new ResponseEntity<>(HttpStatus.OK);
    }
    // standard range request
    else if (ranges.size() == 1) {
        LOG.debug("filename ({}) results into a standard range request: ", artifact.getFilename());
        handleStandardRangeRequest(artifact, response, file, controllerManagement, statusId, ranges);
        result = new ResponseEntity<>(HttpStatus.PARTIAL_CONTENT);
    }
    // multipart range request
    else {
        LOG.debug("filename ({}) results into a multipart range request: ", artifact.getFilename());
        handleMultipartRangeRequest(artifact, response, file, controllerManagement, statusId, ranges);
        result = new ResponseEntity<>(HttpStatus.PARTIAL_CONTENT);
    }

    return result;
}