Example #1
0
 @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;
 }
  /** {@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);
  }