@Test(groups = {"integration", "live"})
  public void testPutMoreThanSingleListing()
      throws InterruptedException, ExecutionException, TimeoutException {
    if (maxResultsForTestListings() == 0) return;
    String bucketName = getContainerName();
    try {
      Map<String, Blob> map = createMap(context, bucketName);
      Set<String> keySet = Sets.newHashSet();
      for (int i = 0; i < maxResultsForTestListings() + 1; i++) {
        keySet.add(i + "");
      }

      Map<String, Blob> newMap = new HashMap<String, Blob>();
      for (String key : keySet) {
        Blob blob = context.getBlobStore().newBlob(key);
        blob.setPayload(key);
        newMap.put(key, blob);
      }
      map.putAll(newMap);
      newMap.clear();

      assertConsistencyAwareMapSize(map, maxResultsForTestListings() + 1);
      assertConsistencyAwareKeySetEquals(map, keySet);
      map.clear();
      assertConsistencyAwareMapSize(map, 0);
    } finally {
      returnContainer(bucketName);
    }
  }
  @Override
  @Test(groups = {"integration", "live"})
  public void testEntrySet() throws IOException, InterruptedException {
    String bucketName = getContainerName();
    try {
      final BlobMap map = createMap(context, bucketName);
      putFiveStrings(map);
      assertConsistencyAwareMapSize(map, 5);
      Set<Entry<String, Blob>> entries = map.entrySet();
      assertEquals(entries.size(), 5);
      for (Entry<String, Blob> entry : entries) {
        assertEquals(
            fiveStrings.get(entry.getKey()), getContentAsStringOrNullAndClose(entry.getValue()));
        Blob blob = entry.getValue();
        blob.setPayload("");
        Payloads.calculateMD5(blob);
        entry.setValue(blob);
      }
      assertConsistencyAware(
          new Runnable() {
            public void run() {
              for (Blob blob : map.values()) {
                try {
                  assertEquals(getContentAsStringOrNullAndClose(blob), "");
                } catch (IOException e) {
                  Throwables.propagate(e);
                }
              }
            }
          });

    } finally {
      returnContainer(bucketName);
    }
  }
 public void execute(String containerName, String directory) {
   Blob blob = connection.newBlob(directory + directorySuffix);
   blob.setPayload(Payloads.newByteArrayPayload(new byte[] {}));
   blob.getPayload().setContentType("application/directory");
   blob.getMetadata().setType(StorageType.RELATIVE_PATH);
   connection.putBlob(containerName, blob);
 }
 @Override
 protected void putStringWithMD5(Map<String, Blob> map, String key, String text)
     throws IOException {
   Blob blob = context.getBlobStore().newBlob(key);
   blob.setPayload(text);
   Payloads.calculateMD5(blob);
   map.put(key, blob);
 }
 protected void putFiveStringsUnderPath(Map<String, Blob> map) {
   Map<String, Blob> newMap = new HashMap<String, Blob>();
   for (Map.Entry<String, String> entry : fiveStringsUnderPath.entrySet()) {
     Blob blob = context.getBlobStore().newBlob(entry.getKey());
     blob.setPayload(entry.getValue());
     newMap.put(entry.getKey(), blob);
   }
   map.putAll(newMap);
 }
 @Test(groups = {"integration", "live"})
 public void testPut() throws IOException, InterruptedException {
   String bucketName = getContainerName();
   try {
     Map<String, Blob> map = createMap(context, bucketName);
     Blob blob = context.getBlobStore().newBlob("one");
     blob.setPayload(Strings2.toInputStream("apple"));
     Payloads.calculateMD5(blob);
     Blob old = map.put(blob.getMetadata().getName(), blob);
     getOneReturnsAppleAndOldValueIsNull(map, old);
     blob.setPayload(Strings2.toInputStream("bear"));
     Payloads.calculateMD5(blob);
     Blob apple = map.put(blob.getMetadata().getName(), blob);
     getOneReturnsBearAndOldValueIsApple(map, apple);
   } finally {
     returnContainer(bucketName);
   }
 }
 @Test(groups = {"integration", "live"})
 public void testContains()
     throws InterruptedException, ExecutionException, TimeoutException, IOException {
   String bucketName = getContainerName();
   try {
     Map<String, Blob> map = createMap(context, bucketName);
     putStringWithMD5(map, "one", "apple");
     Blob blob = context.getBlobStore().newBlob("one");
     blob.setPayload("apple");
     Payloads.calculateMD5(blob);
     assertConsistencyAwareContainsValue(map, blob);
   } finally {
     returnContainer(bucketName);
   }
 }
 public Blob getBlob(String container, String name) {
   try {
     StorageObject storageObject = service.getStorageObject(container, name);
     Blob blob = new BlobImpl(generateJcloudsMetadata(storageObject.getMetadata()));
     if (storageObject.getStream() != null) {
       blob.setPayload(storageObject.getStream());
       blob.getMetadata()
           .getContentMetadata()
           .setContentLength(storageObject.getMetadata().getLength());
     }
     return blob;
   } catch (FileNotExistsException e) {
     e.printStackTrace();
     return null;
   }
 }
  public void testWritePayloadOnFile() throws IOException {
    String blobKey;
    File sourceFile;
    FilePayload filePayload;

    blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img");
    sourceFile = TestUtils.getImageForBlobPayload();
    filePayload = new FilePayload(sourceFile);
    Blob blob = storageStrategy.newBlob(blobKey);
    blob.setPayload(filePayload);
    // write files
    storageStrategy.putBlob(CONTAINER_NAME, blob);
    // verify that the files is equal
    File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
    InputSupplier<FileInputStream> expectedInput = Files.newInputStreamSupplier(sourceFile);
    InputSupplier<FileInputStream> actualInput = Files.newInputStreamSupplier(blobFullPath);
    assertTrue(ByteStreams.equal(expectedInput, actualInput), "Files are not equal");
  }
  /**
   * Retrieves the blob This operation will take several hours.
   *
   * @param container container name
   * @param key blob name
   * @return The blob to retrieve, or null if the blob doesn't exist or the archive retrieval fails
   */
  @Override
  public Blob getBlob(String container, String key, GetOptions getOptions) {
    String jobId = sync.initiateJob(container, buildArchiveRetrievalRequest(key, getOptions));
    try {
      if (pollingStrategy.get().waitForSuccess(container, jobId)) {
        MutableBlobMetadata blobMetadata = new MutableBlobMetadataImpl();
        blobMetadata.setContainer(container);
        blobMetadata.setName(key);

        Blob blob = new BlobImpl(blobMetadata);
        blob.setPayload(sync.getJobOutput(container, jobId));
        return blob;
      }
    } catch (InterruptedException e) {
      Throwables.propagate(e);
    }
    return null;
  }
  public void testSignPutBlob()
      throws ArrayIndexOutOfBoundsException, SecurityException, IllegalArgumentException,
          NoSuchMethodException, IOException {
    Blob blob = blobFactory.create(null);
    blob.getMetadata().setName("name");
    blob.setPayload("");
    blob.getPayload().getContentMetadata().setContentLength(2l);
    blob.getPayload().getContentMetadata().setContentMD5(new byte[] {0, 2, 4, 8});
    blob.getPayload().getContentMetadata().setContentType("text/plain");

    HttpRequest request = signer.signPutBlob("container", blob);

    assertRequestLineEquals(request, "PUT http://storageUrl/container/name HTTP/1.1");
    assertNonPayloadHeadersEqual(request, "X-Auth-Token: testtoken\n");
    assertContentHeadersEqual(
        request, "text/plain", null, null, null, (long) 2l, new byte[] {0, 2, 4, 8});

    assertEquals(request.getFilters().size(), 0);
  }
 @Test(groups = {"integration", "live"})
 public void testPutAll() throws InterruptedException, ExecutionException, TimeoutException {
   String bucketName = getContainerName();
   try {
     Map<String, Blob> map = createMap(context, bucketName);
     Map<String, Blob> newMap = new HashMap<String, Blob>();
     for (String key : fiveInputs.keySet()) {
       Blob blob = context.getBlobStore().newBlob(key);
       blob.setPayload(fiveInputs.get(key));
       blob.getPayload().getContentMetadata().setContentLength((long) fiveBytes.get(key).length);
       newMap.put(key, blob);
     }
     map.putAll(newMap);
     assertConsistencyAwareMapSize(map, 5);
     assertConsistencyAwareKeySetEquals(map, new HashSet<String>(fiveInputs.keySet()));
     fourLeftRemovingOne(map);
   } finally {
     returnContainer(bucketName);
   }
 }
  public void testSignPutBlob()
      throws ArrayIndexOutOfBoundsException, SecurityException, IllegalArgumentException,
          NoSuchMethodException, IOException {
    Blob blob = blobFactory.create(null);
    blob.getMetadata().setName("name");
    blob.setPayload("");
    blob.getPayload().getContentMetadata().setContentLength(2l);
    blob.getPayload().getContentMetadata().setContentMD5(new byte[] {0, 2, 4, 8});
    blob.getPayload().getContentMetadata().setContentType("text/plain");

    HttpRequest request = signer.signPutBlob("container", blob);

    assertRequestLineEquals(
        request, "POST https://accesspoint.atmosonline.com/rest/namespace/container/name HTTP/1.1");
    assertNonPayloadHeadersEqual(
        request,
        "Accept: */*\nDate: Thu, 05 Jun 2008 16:38:19 GMT\nx-emc-signature: aLpB1oQaCA27AXT6Nzam7s0f0pI=\nx-emc-uid: identity\n");

    assertContentHeadersEqual(
        request, "text/plain", null, null, null, (long) 2l, new byte[] {0, 2, 4, 8});

    assertEquals(request.getFilters().size(), 0);
  }
  /** {@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);
  }