List of usage examples for com.google.common.net HttpHeaders CONTENT_RANGE
String CONTENT_RANGE
To view the source code for com.google.common.net HttpHeaders CONTENT_RANGE.
Click Source Link
From source file:org.eclipse.hawkbit.rest.util.RestResourceConversionHelper.java
private static void handleStandardRangeRequest(final Artifact artifact, final HttpServletResponse response, final DbArtifact file, final ControllerManagement controllerManagement, final Long statusId, final List<ByteRange> ranges) { final ByteRange r = ranges.get(0); response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes " + r.getStart() + "-" + r.getEnd() + "/" + r.getTotal()); response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(r.getLength())); response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); try (InputStream inputStream = file.getFileInputStream()) { copyStreams(inputStream, response.getOutputStream(), controllerManagement, statusId, r.getStart(), r.getLength());/*from w ww.j av a 2 s . co m*/ } catch (final IOException e) { LOG.error("standardRangeRequest of file ({}) failed!", artifact.getFilename(), e); throw new FileSteamingFailedException(artifact.getFilename()); } }
From source file:org.jclouds.b2.blobstore.B2BlobStore.java
@Override public Blob getBlob(String container, String name, GetOptions options) { if (options.getIfMatch() != null || options.getIfNoneMatch() != null || options.getIfModifiedSince() != null || options.getIfUnmodifiedSince() != null) { throw new UnsupportedOperationException("B2 does not support conditional get"); }/*from ww w .ja v a 2 s.c om*/ B2Object b2Object = api.getObjectApi().downloadFileByName(container, name, blob2ObjectGetOptions.apply(options)); if (b2Object == null) { return null; } MutableBlobMetadata metadata = toBlobMetadata(container, b2Object); Blob blob = new BlobImpl(metadata); blob.setPayload(b2Object.payload()); if (b2Object.contentRange() != null) { blob.getAllHeaders().put(HttpHeaders.CONTENT_RANGE, b2Object.contentRange()); } return blob; }
From source file:org.jclouds.blobstore.config.LocalBlobStore.java
@Override public Blob getBlob(String containerName, String key, GetOptions options) { logger.debug("Retrieving blob with key %s from container %s", key, containerName); // If the container doesn't exist, an exception is thrown if (!storageStrategy.containerExists(containerName)) { logger.debug("Container %s does not exist", containerName); throw cnfe(containerName); }/*from www . ja v a 2 s .c o m*/ // If the blob doesn't exist, a null object is returned if (!storageStrategy.blobExists(containerName, key)) { logger.debug("Item %s does not exist in container %s", key, containerName); return null; } Blob blob = loadBlob(containerName, key); if (options != null) { String eTag = blob.getMetadata().getETag(); if (eTag != null) { eTag = maybeQuoteETag(eTag); if (options.getIfMatch() != null) { if (!eTag.equals(maybeQuoteETag(options.getIfMatch()))) throw returnResponseException(412); } if (options.getIfNoneMatch() != null) { if (eTag.equals(maybeQuoteETag(options.getIfNoneMatch()))) throw returnResponseException(304); } } if (options.getIfModifiedSince() != null) { Date modifiedSince = options.getIfModifiedSince(); if (blob.getMetadata().getLastModified().before(modifiedSince)) { HttpResponse response = HttpResponse.builder().statusCode(304).build(); throw new HttpResponseException(String.format("%1$s is before %2$s", blob.getMetadata().getLastModified(), modifiedSince), null, response); } } if (options.getIfUnmodifiedSince() != null) { Date unmodifiedSince = options.getIfUnmodifiedSince(); if (blob.getMetadata().getLastModified().after(unmodifiedSince)) { HttpResponse response = HttpResponse.builder().statusCode(412).build(); throw new HttpResponseException(String.format("%1$s is after %2$s", blob.getMetadata().getLastModified(), unmodifiedSince), null, response); } } blob = copyBlob(blob); if (options.getRanges() != null && !options.getRanges().isEmpty()) { long size = 0; ImmutableList.Builder<ByteSource> streams = ImmutableList.builder(); // Try to convert payload to ByteSource, otherwise wrap it. ByteSource byteSource; try { byteSource = (ByteSource) blob.getPayload().getRawContent(); } catch (ClassCastException cce) { try { byteSource = ByteSource .wrap(ByteStreams2.toByteArrayAndClose(blob.getPayload().openStream())); } catch (IOException e) { throw new RuntimeException(e); } } for (String s : options.getRanges()) { // HTTP uses a closed interval while Java array indexing uses a // half-open interval. long offset = 0; long last = blob.getPayload().getContentMetadata().getContentLength() - 1; if (s.startsWith("-")) { offset = last - Long.parseLong(s.substring(1)) + 1; if (offset < 0) { offset = 0; } } else if (s.endsWith("-")) { offset = Long.parseLong(s.substring(0, s.length() - 1)); } else if (s.contains("-")) { String[] firstLast = s.split("\\-"); offset = Long.parseLong(firstLast[0]); last = Long.parseLong(firstLast[1]); } else { throw new IllegalArgumentException("illegal range: " + s); } if (offset >= blob.getPayload().getContentMetadata().getContentLength()) { throw new IllegalArgumentException("illegal range: " + s); } if (last + 1 > blob.getPayload().getContentMetadata().getContentLength()) { last = blob.getPayload().getContentMetadata().getContentLength() - 1; } streams.add(byteSource.slice(offset, last - offset + 1)); size += last - offset + 1; blob.getAllHeaders().put(HttpHeaders.CONTENT_RANGE, "bytes " + offset + "-" + last + "/" + blob.getPayload().getContentMetadata().getContentLength()); } ContentMetadata cmd = blob.getPayload().getContentMetadata(); blob.setPayload(ByteSource.concat(streams.build())); HttpUtils.copy(cmd, blob.getPayload().getContentMetadata()); blob.getPayload().getContentMetadata().setContentLength(size); blob.getMetadata().setSize(size); } } checkNotNull(blob.getPayload(), "payload " + blob); return blob; }
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 {//w ww . ja v a 2 s. co 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(); } }