Package org.jclouds.openstack.swift.domain

Examples of org.jclouds.openstack.swift.domain.SwiftObject$Factory


       String contentType = getBlob.getPayload().getContentMetadata().getContentType();
       assert contentType.startsWith("text/plain") || "application/x-www-form-urlencoded".equals(contentType): contentType;
   }

   protected SwiftObject newSwiftObject(String data, String key) throws IOException {
      SwiftObject object = getApi().newSwiftObject();
      object.getInfo().setName(key);
      object.setPayload(data);
      Payloads.calculateMD5(object);
      object.getInfo().setContentType("text/plain");
      object.getInfo().getMetadata().put("Metadata", "metadata-value");
      return object;
   }
View Full Code Here


   @Override
   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
      checkArgument(checkNotNull(input, "input") instanceof SwiftObject, "this binder is only valid for SwiftObject!");
      checkNotNull(request, "request");

      SwiftObject object = (SwiftObject) input;
      if (object.getPayload().getContentMetadata().getContentType() == null)
         object.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);

      if (object.getPayload().getContentMetadata().getContentLength() != null
            && object.getPayload().getContentMetadata().getContentLength() >= 0) {
         checkArgument(object.getPayload().getContentMetadata().getContentLength() <= 5l * 1024 * 1024 * 1024,
               "maximum size for put object is 5GB");
      } else {
         // Enable "chunked"/"streamed" data, where the size needn't be known in advance.
         request = (R) request.toBuilder().replaceHeader("Transfer-Encoding", "chunked").build();
      }

      byte[] contentMD5 = object.getInfo().getHash();
      if (contentMD5 != null) {
         // Swizzle hash to ETag
         request = (R) request.toBuilder()
               .addHeader(HttpHeaders.ETAG,
                     BaseEncoding.base16().lowerCase().encode(contentMD5))
               .build();
      }

      Date expires = object.getPayload().getContentMetadata().getExpires();
      if (expires != null) {
         // Swizzle Expires to X-Delete-At
         request = (R) request.toBuilder()
               .addHeader("X-Delete-At", String.valueOf(
                     MILLISECONDS.toSeconds(expires.getTime())))
View Full Code Here

@Test(groups = "unit", testName = "BindSwiftObjectMetadataToRequestTest")
public class BindSwiftObjectMetadataToRequestTest extends CommonSwiftClientTest {

   @Test
   public void testPassWithMinimumDetailsAndPayload5GB() {
      SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(5 * 1024 * 1024 * 1024l);
      object.setPayload(payload);
      object.getInfo().setName("foo");

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class);

      assertEquals(binder.bindToRequest(request, object), HttpRequest.builder().method("PUT").endpoint(
View Full Code Here

               URI.create("http://localhost")).build());
   }

   @Test
   public void testExtendedPropertiesBind() {
      SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(5 * 1024 * 1024 * 1024l);
      long expiresMillis = 1000;
      payload.getContentMetadata().setExpires(new Date(expiresMillis));
      object.setPayload(payload);
      object.getInfo().setName("foo");
      object.getInfo().getMetadata().putAll(ImmutableMap.of("foo", "bar"));

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class);

      assertEquals(binder.bindToRequest(request, object), HttpRequest.builder().method("PUT")
View Full Code Here

                     MILLISECONDS.toSeconds(expiresMillis)))
               .endpoint("http://localhost").addHeader("X-Object-Meta-foo", "bar").build());
   }

   public void testNoContentLengthIsChunked() {
      SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(null);
      object.setPayload(payload);
      object.getInfo().setName("foo");

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class);

      assertEquals(binder.bindToRequest(request, object), HttpRequest.builder().method("PUT")
View Full Code Here

               .endpoint("http://localhost").addHeader("Transfer-Encoding", "chunked").build());
   }

   @Test(expectedExceptions = IllegalArgumentException.class)
   public void testNoNameIsBad() {
      SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(5368709120000l);
      object.setPayload(payload);

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class);
      binder.bindToRequest(request, object);
   }
View Full Code Here

      binder.bindToRequest(request, object);
   }

   @Test(expectedExceptions = IllegalArgumentException.class)
   public void testOver5GBIsBad() {
      SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(5 * 1024 * 1024 * 1024l + 1);
      object.setPayload(payload);
      object.getInfo().setName("foo");

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class);
      binder.bindToRequest(request, object);
   }
View Full Code Here

   public SwiftObject apply(HttpResponse from) {
      MutableObjectInfoWithMetadata metadata = infoParser.apply(from);
      if (metadata.getHash() != null)
         from.getPayload().getContentMetadata().setContentMD5(metadata.getHash());
      SwiftObject object = objectProvider.create(metadata);
      object.getAllHeaders().putAll(from.getHeaders());
      object.setPayload(from.getPayload());
      return object;
   }
View Full Code Here

   }

   public SwiftObject apply(Blob from) {
      if (from == null)
         return null;
      SwiftObject object = objectProvider.create(blob2ObjectMd.apply(from.getMetadata()));
      object.setPayload(checkNotNull(from.getPayload(), "payload: " + from));
      object.setAllHeaders(from.getAllHeaders());
      return object;
   }
View Full Code Here

      String containerName = getContainerName();
      try {
         // Test PUT with string data, ETag hash, and a piece of metadata
         String data = "Here is my data";
         String key = "object";
         SwiftObject object = newSwiftObject(data, key);
         byte[] md5 = object.getPayload().getContentMetadata().getContentMD5();
         String newEtag = getApi().putObject(containerName, object);
         assert newEtag != null;

         assertEquals(base16().lowerCase().encode(md5), base16().lowerCase().encode(object.getPayload().getContentMetadata()
                  .getContentMD5()));

         // Test HEAD of missing object
         assert getApi().getObjectInfo(containerName, "non-existent-object") == null;

         // Test HEAD of object
         MutableObjectInfoWithMetadata metadata = getApi().getObjectInfo(containerName, object.getInfo().getName());
         assertEquals(metadata.getName(), object.getInfo().getName());

         assertEquals(metadata.getBytes(), Long.valueOf(data.length()));
         assert metadata.getContentType().startsWith("text/plain") : metadata.getContentType();

         assertEquals(base16().lowerCase().encode(md5), base16().lowerCase().encode(metadata.getHash()));
         assertEquals(metadata.getHash(), base16().lowerCase().decode(newEtag));
         assertEquals(metadata.getMetadata().entrySet().size(), 1);
         assertEquals(metadata.getMetadata().get("metadata"), "metadata-value");

         // // Test POST to update object's metadata
         Map<String, String> userMetadata = Maps.newHashMap();
         userMetadata.put("New-Metadata-1", "value-1");
         userMetadata.put("New-Metadata-2", "value-2");
         assertTrue(getApi().setObjectInfo(containerName, object.getInfo().getName(), userMetadata));

         // Test GET of missing object
         assert getApi().getObject(containerName, "non-existent-object") == null;
         // Test GET of object (including updated metadata)
         SwiftObject getBlob = getApi().getObject(containerName, object.getInfo().getName());
         assertEquals(Strings2.toString(getBlob.getPayload()), data);
         // TODO assertEquals(getBlob.getName(),
         // object.getMetadata().getName());
         assertEquals(getBlob.getInfo().getBytes(), Long.valueOf(data.length()));
         testGetObjectContentType(getBlob);
         assertEquals(base16().lowerCase().encode(md5), base16().lowerCase().encode(getBlob.getInfo().getHash()));
         assertEquals(base16().lowerCase().decode(newEtag), getBlob.getInfo().getHash());
         assertEquals(getBlob.getInfo().getMetadata().entrySet().size(), 2);
         assertEquals(getBlob.getInfo().getMetadata().get("new-metadata-1"), "value-1");
         assertEquals(getBlob.getInfo().getMetadata().get("new-metadata-2"), "value-2");

         // Test PUT with invalid ETag (as if object's data was corrupted in
         // transit)
         String correctEtag = newEtag;
         String incorrectEtag = "0" + correctEtag.substring(1);
         object.getInfo().setHash(base16().lowerCase().decode(incorrectEtag));
         try {
            getApi().putObject(containerName, object);
         } catch (HttpResponseException e) {
            assertEquals(e.getResponse().getStatusCode(), 422);
         }

         // Test PUT chunked/streamed upload with data of "unknown" length
         ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
         SwiftObject blob = getApi().newSwiftObject();
         blob.getInfo().setName("chunked-object");
         blob.setPayload(bais);
         newEtag = getApi().putObject(containerName, blob);
         assertEquals(base16().lowerCase().encode(md5), base16().lowerCase().encode(getBlob.getInfo().getHash()));

         // Test GET with options
         // Non-matching ETag
View Full Code Here

TOP

Related Classes of org.jclouds.openstack.swift.domain.SwiftObject$Factory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.