@Override public GetOptions apply(org.jclouds.blobstore.options.GetOptions from) { GetOptions httpOptions = new GetOptions(); if (from != null && from != org.jclouds.blobstore.options.GetOptions.NONE) { if (from.getIfMatch() != null) { httpOptions.ifETagMatches(from.getIfMatch()); } if (from.getIfModifiedSince() != null) { httpOptions.ifModifiedSince(from.getIfModifiedSince()); } if (from.getIfNoneMatch() != null) { httpOptions.ifETagDoesntMatch(from.getIfNoneMatch()); } if (from.getIfUnmodifiedSince() != null) { httpOptions.ifUnmodifiedSince(from.getIfUnmodifiedSince()); } for (String range : from.getRanges()) { String[] firstLast = range.split("\\-"); if (firstLast.length == 2) httpOptions.range(Long.parseLong(firstLast[0]), Long.parseLong(firstLast[1])); else if (range.startsWith("-")) httpOptions.tail(Long.parseLong(firstLast[0])); else httpOptions.startAt(Long.parseLong(firstLast[0])); } } return httpOptions; }
private static ArchiveRetrievalJobRequest buildArchiveRetrievalRequest( String key, GetOptions getOptions) { ArchiveRetrievalJobRequest.Builder requestBuilder = ArchiveRetrievalJobRequest.builder().archiveId(key); if (getOptions != null) { int size = getOptions.getRanges().size(); checkArgument(size <= 1, "The number of ranges should be zero or one"); if (size == 1) { requestBuilder.range(ContentRange.fromString(getOptions.getRanges().get(0))); } } return requestBuilder.build(); }
public void testRanges() throws IOException { blobStore.createContainerInLocation(null, CONTAINER_NAME); String input = "abcdefgh"; Payload payload; Blob blob = blobStore.blobBuilder("test").payload(new StringPayload(input)).build(); blobStore.putBlob(CONTAINER_NAME, blob); GetOptions getOptionsRangeStartAt = new GetOptions(); getOptionsRangeStartAt.startAt(1); Blob blobRangeStartAt = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeStartAt); payload = blobRangeStartAt.getPayload(); try { assertEquals(input.substring(1), Strings2.toString(payload)); } finally { Closeables.closeQuietly(payload); } GetOptions getOptionsRangeTail = new GetOptions(); getOptionsRangeTail.tail(3); Blob blobRangeTail = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeTail); payload = blobRangeTail.getPayload(); try { assertEquals(input.substring(5), Strings2.toString(payload)); } finally { Closeables.closeQuietly(payload); } GetOptions getOptionsFragment = new GetOptions(); getOptionsFragment.range(4, 6); Blob blobFragment = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsFragment); payload = blobFragment.getPayload(); try { assertEquals(input.substring(4, 7), Strings2.toString(payload)); } finally { Closeables.closeQuietly(payload); } }
/** {@inheritDoc} */ @Override public ListenableFuture<Blob> getBlob( final String containerName, final 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 (!containerExistsSyncImpl(containerName)) { logger.debug("Container %s does not exist", containerName); return immediateFailedFuture(cnfe(containerName)); } // 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 immediateFuture(null); } Blob blob = loadFileBlob(containerName, key); if (options != null) { if (options.getIfMatch() != null) { if (!blob.getMetadata().getETag().equals(options.getIfMatch())) return immediateFailedFuture(returnResponseException(412)); } if (options.getIfNoneMatch() != null) { if (blob.getMetadata().getETag().equals(options.getIfNoneMatch())) return immediateFailedFuture(returnResponseException(304)); } if (options.getIfModifiedSince() != null) { Date modifiedSince = options.getIfModifiedSince(); if (blob.getMetadata().getLastModified().before(modifiedSince)) { HttpResponse response = new HttpResponse(304, null, null); return immediateFailedFuture( 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 = new HttpResponse(412, null, null); return immediateFailedFuture( new HttpResponseException( String.format( "%1$s is after %2$s", blob.getMetadata().getLastModified(), unmodifiedSince), null, response)); } } if (options.getRanges() != null && options.getRanges().size() > 0) { byte[] data; try { data = toByteArray(blob.getPayload().getInput()); } catch (IOException e) { return immediateFailedFuture(new RuntimeException(e)); } ByteArrayOutputStream out = new ByteArrayOutputStream(); for (String s : options.getRanges()) { if (s.startsWith("-")) { int length = Integer.parseInt(s.substring(1)); out.write(data, data.length - length, length); } else if (s.endsWith("-")) { int offset = Integer.parseInt(s.substring(0, s.length() - 1)); out.write(data, offset, data.length - offset); } else if (s.contains("-")) { String[] firstLast = s.split("\\-"); int offset = Integer.parseInt(firstLast[0]); int last = Integer.parseInt(firstLast[1]); int length = last - offset + 1; // the range end is included out.write(data, offset, length); } else { return immediateFailedFuture(new IllegalArgumentException("first and last were null!")); } } blob.setPayload(out.toByteArray()); blob.getMetadata().getContentMetadata().setContentLength(new Long(data.length)); } } checkNotNull(blob.getPayload(), "payload " + blob); return immediateFuture(blob); }