public void handleError(HttpCommand command, HttpResponse response) {
   String content;
   try {
     content =
         response.getContent() != null ? Utils.toStringAndClose(response.getContent()) : null;
     if (content != null) {
       try {
         if (content.indexOf('<') >= 0) {
           AzureStorageError error =
               utils.parseAzureStorageErrorFromContent(command, response, content);
           command.setException(new AzureStorageResponseException(command, response, error));
         } else {
           command.setException(new HttpResponseException(command, response, content));
         }
       } catch (Exception he) {
         command.setException(new HttpResponseException(command, response, content));
         Utils.rethrowIfRuntime(he);
       }
     } else {
       command.setException(new HttpResponseException(command, response));
     }
   } catch (Exception e) {
     command.setException(new HttpResponseException(command, response));
     Utils.rethrowIfRuntime(e);
   }
 }
  private static void verifyMetadata(String metadataValue, AtmosObject getBlob) {
    assertEquals(getBlob.getContentMetadata().getContentLength(), new Long(16));
    assert getBlob.getContentMetadata().getContentType().startsWith("text/plain");
    assertEquals(getBlob.getUserMetadata().getMetadata().get("Metadata"), metadataValue);
    SystemMetadata md = getBlob.getSystemMetadata();
    assertEquals(md.getSize(), 16);
    assert md.getGroupID() != null;
    assertEquals(md.getHardLinkCount(), 1);
    assert md.getInceptionTime() != null;
    assert md.getLastAccessTime() != null;
    assert md.getLastMetadataModification() != null;
    assert md.getLastUserDataModification() != null;
    assert md.getObjectID() != null;
    assertEquals(md.getObjectName(), "object");
    assert md.getPolicyName() != null;
    assertEquals(md.getType(), FileType.REGULAR);
    assert md.getUserID() != null;

    try {
      Utils.toStringAndClose(
          URI.create(
                  "http://accesspoint.emccis.com/rest/objects/"
                      + getBlob.getSystemMetadata().getObjectID())
              .toURL()
              .openStream());
      assert false : "shouldn't have worked, since it is private";
    } catch (IOException e) {

    }
  }
  public void testAddNodeOptions() throws SecurityException, NoSuchMethodException, IOException {
    Method method =
        TerremarkVCloudAsyncClient.class.getMethod(
            "addNode",
            int.class,
            String.class,
            String.class,
            int.class,
            Array.newInstance(AddNodeOptions.class, 0).getClass());
    GeneratedHttpRequest<TerremarkVCloudAsyncClient> httpMethod =
        processor.createRequest(
            method,
            12,
            "10.2.2.2",
            "name",
            22,
            AddNodeOptions.Builder.disabled().withDescription("yahoo"));

    assertRequestLineEquals(
        httpMethod, "POST http://vcloud/extensions/internetService/12/nodeServices HTTP/1.1");
    assertHeadersEqual(
        httpMethod,
        "Accept: application/vnd.tmrk.vCloud.nodeService+xml\nContent-Length: 333\nContent-Type: application/vnd.tmrk.vCloud.nodeService+xml\n");

    assertPayloadEquals(
        httpMethod,
        Utils.toStringAndClose(
            getClass().getResourceAsStream("/terremark/CreateNodeService-options-test.xml")));
    assertResponseParserClassEquals(method, httpMethod, ParseSax.class);
    assertSaxResponseParserClassEquals(method, NodeHandler.class);
    assertExceptionParserClassEquals(method, null);

    checkFilters(httpMethod);
  }
  public void testAddInternetServiceToExistingIp()
      throws SecurityException, NoSuchMethodException, IOException {
    Method method =
        TerremarkVCloudAsyncClient.class.getMethod(
            "addInternetServiceToExistingIp",
            int.class,
            String.class,
            Protocol.class,
            int.class,
            Array.newInstance(AddInternetServiceOptions.class, 0).getClass());
    GeneratedHttpRequest<TerremarkVCloudAsyncClient> httpMethod =
        processor.createRequest(method, 12, "name", Protocol.TCP, 22);

    assertRequestLineEquals(
        httpMethod, "POST http://vcloud/extensions/publicIp/12/internetServices HTTP/1.1");
    assertHeadersEqual(
        httpMethod,
        "Accept: application/vnd.tmrk.vCloud.internetService+xml\nContent-Length: 298\nContent-Type: application/vnd.tmrk.vCloud.internetService+xml\n");
    assertPayloadEquals(
        httpMethod,
        Utils.toStringAndClose(
            getClass().getResourceAsStream("/terremark/CreateInternetService-test2.xml")));

    assertResponseParserClassEquals(method, httpMethod, ParseSax.class);
    assertSaxResponseParserClassEquals(method, InternetServiceHandler.class);
    assertExceptionParserClassEquals(method, null);

    checkFilters(httpMethod);
  }
  public void testInstantiateVAppTemplate()
      throws SecurityException, NoSuchMethodException, IOException {
    Method method =
        TerremarkVCloudAsyncClient.class.getMethod(
            "instantiateVAppTemplateInVDC",
            String.class,
            String.class,
            String.class,
            Array.newInstance(InstantiateVAppTemplateOptions.class, 0).getClass());
    GeneratedHttpRequest<TerremarkVCloudAsyncClient> httpMethod =
        processor.createRequest(method, "1", "name", 3 + "");

    assertRequestLineEquals(
        httpMethod, "POST http://vcloud/vdc/1/action/instantiateVAppTemplate HTTP/1.1");
    assertHeadersEqual(
        httpMethod,
        "Accept: application/vnd.vmware.vcloud.vApp+xml\nContent-Length: 687\nContent-Type: application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml\n");
    assertPayloadEquals(
        httpMethod,
        Utils.toStringAndClose(
            getClass().getResourceAsStream("/terremark/InstantiateVAppTemplateParams-test.xml")));

    assertResponseParserClassEquals(method, httpMethod, ParseSax.class);
    assertSaxResponseParserClassEquals(method, VAppHandler.class);
    assertExceptionParserClassEquals(method, null);

    checkFilters(httpMethod);
  }
 AtmosStorageError parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) {
   if (response.getContent() != null) {
     try {
       String content = Utils.toStringAndClose(response.getContent());
       if (content != null && content.indexOf('<') >= 0)
         return utils.parseAtmosStorageErrorFromContent(
             command, response, Utils.toInputStream(content));
     } catch (IOException e) {
       logger.warn(e, "exception reading error from response", response);
     }
   }
   return null;
 }
  public void testComplete() throws IOException {
    CreateDriveRequest input =
        new CreateDriveRequest.Builder()
            .name("Ubuntu 10.10 Server Edition Linux 64bit Preinstalled System")
            //
            .size(8589934592l) //
            .claimType(ClaimType.SHARED) //
            .readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff")) //
            .tags(ImmutableSet.of("tag1", "tag2"))
            .userMetadata(ImmutableMap.of("foo", "bar", "baz", "raz")) //
            .encryptionCipher("aes-xts-plain")
            .avoid(ImmutableSet.of("avoid1"))
            .build();

    HttpRequest request = new HttpRequest("POST", URI.create("https://host/drives/create"));
    FN.bindToRequest(request, input);
    assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.TEXT_PLAIN);
    assertEquals(
        request.getPayload().getRawContent(),
        Utils.toStringAndClose(
            BindDriveToPlainTextStringTest.class.getResourceAsStream("/create_drive.txt")));
  }
  @Test(
      timeOut = 5 * 60 * 1000,
      dependsOnMethods = {"testCreateContainer", "testCreatePublicContainer"})
  public void testObjectOperations() throws Exception {
    String data = "Here is my data";

    // Test PUT with string data, ETag hash, and a piece of metadata
    AzureBlob object = client.newBlob();
    object.getProperties().setName("object");
    object.setPayload(data);
    Payloads.calculateMD5(object);
    object.getProperties().getContentMetadata().setContentType("text/plain");
    object.getProperties().getMetadata().put("mykey", "metadata-value");
    byte[] md5 = object.getProperties().getContentMetadata().getContentMD5();
    String newEtag = client.putBlob(privateContainer, object);
    assertEquals(
        CryptoStreams.hex(md5),
        CryptoStreams.hex(object.getProperties().getContentMetadata().getContentMD5()));

    // Test HEAD of missing object
    assert client.getBlobProperties(privateContainer, "non-existent-object") == null;

    // Test HEAD of object
    BlobProperties metadata =
        client.getBlobProperties(privateContainer, object.getProperties().getName());
    // TODO assertEquals(metadata.getName(), object.getProperties().getName());
    // we can't check this while hacking around lack of content-md5, as GET of the first byte will
    // show incorrect length 1, the returned size, as opposed to the real length. This is an ok
    // tradeoff, as a container list will contain the correct size of the objects in an
    // inexpensive fashion
    // http://code.google.com/p/jclouds/issues/detail?id=92
    // assertEquals(metadata.getSize(), data.length());
    assertEquals(metadata.getContentMetadata().getContentType(), "text/plain");
    // Azure doesn't return the Content-MD5 on head request..
    assertEquals(
        CryptoStreams.hex(md5),
        CryptoStreams.hex(object.getProperties().getContentMetadata().getContentMD5()));
    assertEquals(metadata.getETag(), newEtag);
    assertEquals(metadata.getMetadata().entrySet().size(), 1);
    assertEquals(metadata.getMetadata().get("mykey"), "metadata-value");

    // // Test POST to update object's metadata
    // Multimap<String, String> userMetadata = LinkedHashMultimap.create();
    // userMetadata.put("New-Metadata-1", "value-1");
    // userMetadata.put("New-Metadata-2", "value-2");
    // assertTrue(client.setBlobProperties(privateContainer, object.getProperties().getName(),
    // userMetadata));

    // Test GET of missing object
    assert client.getBlob(privateContainer, "non-existent-object") == null;

    // Test GET of object (including updated metadata)
    AzureBlob getBlob = client.getBlob(privateContainer, object.getProperties().getName());
    assertEquals(Utils.toStringAndClose(getBlob.getPayload().getInput()), data);
    // TODO assertEquals(getBlob.getName(), object.getProperties().getName());
    assertEquals(
        getBlob.getPayload().getContentMetadata().getContentLength(), new Long(data.length()));
    assertEquals(getBlob.getProperties().getContentMetadata().getContentType(), "text/plain");
    assertEquals(
        CryptoStreams.hex(md5),
        CryptoStreams.hex(getBlob.getProperties().getContentMetadata().getContentMD5()));
    assertEquals(newEtag, getBlob.getProperties().getETag());
    // wait until we can update metadata
    // assertEquals(getBlob.getProperties().getMetadata().entries().size(), 2);
    // assertEquals(
    // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-1")),
    // "value-1");
    // assertEquals(
    // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-2")),
    // "value-2");
    assertEquals(metadata.getMetadata().entrySet().size(), 1);
    assertEquals(metadata.getMetadata().get("mykey"), "metadata-value");

    // test listing
    ListBlobsResponse response =
        client.listBlobs(
            privateContainer,
            ListBlobsOptions.Builder.prefix(
                    object
                        .getProperties()
                        .getName()
                        .substring(0, object.getProperties().getName().length() - 1))
                .maxResults(1)
                .includeMetadata());
    assertEquals(response.size(), 1);
    assertEquals(Iterables.getOnlyElement(response).getName(), object.getProperties().getName());
    assertEquals(
        Iterables.getOnlyElement(response).getMetadata(),
        ImmutableMap.of("mykey", "metadata-value"));

    // Test PUT with invalid ETag (as if object's data was corrupted in transit)
    String correctEtag = newEtag;
    String incorrectEtag = "0" + correctEtag.substring(1);
    object.getProperties().setETag(incorrectEtag);
    try {
      client.putBlob(privateContainer, object);
    } catch (Throwable e) {
      assertEquals(e.getCause().getClass(), HttpResponseException.class);
      assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422);
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes("UTF-8"));
    object = client.newBlob();
    object.getProperties().setName("chunked-object");
    object.setPayload(bais);
    object.getPayload().getContentMetadata().setContentLength(new Long(data.getBytes().length));
    newEtag = client.putBlob(privateContainer, object);
    assertEquals(
        CryptoStreams.hex(md5),
        CryptoStreams.hex(getBlob.getProperties().getContentMetadata().getContentMD5()));

    // Test GET with options
    // Non-matching ETag
    try {
      client.getBlob(
          privateContainer,
          object.getProperties().getName(),
          GetOptions.Builder.ifETagDoesntMatch(newEtag));
    } catch (Exception e) {
      assertEquals(e.getCause().getClass(), HttpResponseException.class);
      assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 304);
    }

    // Matching ETag TODO this shouldn't fail!!!
    try {
      getBlob =
          client.getBlob(
              privateContainer,
              object.getProperties().getName(),
              GetOptions.Builder.ifETagMatches(newEtag));
      assertEquals(getBlob.getProperties().getETag(), newEtag);
    } catch (HttpResponseException e) {
      assertEquals(e.getResponse().getStatusCode(), 412);
    }

    // Range
    // doesn't work per
    // http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/479fa63f-51df-4b66-96b5-33ae362747b6
    // getBlob = client
    // .getBlob(privateContainer, object.getProperties().getName(),
    // GetOptions.Builder.startAt(8)).get(120,
    // TimeUnit.SECONDS);
    // assertEquals(Utils.toStringAndClose((InputStream) getBlob.getData()), data.substring(8));

    client.deleteBlob(privateContainer, "object");
    client.deleteBlob(privateContainer, "chunked-object");
  }