Package com.google.common.io

Examples of com.google.common.io.ByteSource


      try {
         f = File.createTempFile("jclouds", "tmp");
         long length = (new Random().nextInt(32) + 1) * 1024 * 1024;
         TestUtils.randomByteSource().slice(0, length).copyTo(Files.asByteSink(f));

         ByteSource byteSource = asByteSource(f);
         payload = newByteSourcePayload(byteSource);
         byte[] digest = byteSource.hash(md5()).asBytes();
         String strDigest = base64().encode(digest);

         payload.getContentMetadata().setContentMD5(digest);
         payload.getContentMetadata().setContentLength(f.length());
         Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
View Full Code Here


      assertPayloadEquals(request, "whoops", "application/unknown", false);
   }

   public void testPutInputStreamPayloadWithMD5() throws NoSuchAlgorithmException, IOException, SecurityException,
         NoSuchMethodException {
      ByteSource byteSource = ByteSource.wrap("whoops".getBytes(UTF_8));
      Payload payload = Payloads.newByteSourcePayload(byteSource);
      payload.getContentMetadata().setContentLength(byteSource.size());
      payload.getContentMetadata().setContentMD5(byteSource.hash(Hashing.md5()).asBytes());

      Invokable<?, ?> method = method(TestTransformers.class, "put", Payload.class);
      GeneratedHttpRequest request = processor.apply(Invocation.create(method,
            ImmutableList.<Object> of(payload)));
      assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1");
View Full Code Here

      MockWebServer server = mockWebServer(new MockResponse().addHeader("x-Content-Disposition",
            "attachment; filename=photo.jpg"));
      IntegrationTestClient client = client(server.getUrl("/").toString());
      Payload payload = null;
      try {
         ByteSource body = ByteSource.wrap("foo".getBytes());
         payload = newByteSourcePayload(body);
         payload.getContentMetadata().setContentDisposition("attachment; filename=photo.jpg");
         payload.getContentMetadata().setContentLength(body.size());
         Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
         RecordedRequest request = server.takeRequest();
         assertEquals(request.getHeader("Content-Disposition"), "attachment; filename=photo.jpg");
         assertEquals(headers.get("x-Content-Disposition"), ImmutableList.of("attachment; filename=photo.jpg"));
      } finally {
View Full Code Here

   public void testPostContentEncoding() throws Exception {
      MockWebServer server = mockWebServer(new MockResponse().addHeader("x-Content-Encoding", "gzip"));
      IntegrationTestClient client = client(server.getUrl("/").toString());
      Payload payload = null;
      try {
         ByteSource body = ByteSource.wrap("foo".getBytes());
         payload = newByteSourcePayload(body);
         payload.getContentMetadata().setContentEncoding("gzip");
         payload.getContentMetadata().setContentLength(body.size());
         Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
         RecordedRequest request = server.takeRequest();
         assertEquals(request.getHeader("Content-Encoding"), "gzip");
         assertEquals(headers.get("x-Content-Encoding"), ImmutableList.of("gzip"));
      } finally {
View Full Code Here

   public void testPostContentLanguage() throws Exception {
      MockWebServer server = mockWebServer(new MockResponse().addHeader("x-Content-Language", "mi, en"));
      IntegrationTestClient client = client(server.getUrl("/").toString());
      Payload payload = null;
      try {
         ByteSource body = ByteSource.wrap("foo".getBytes());
         payload = newByteSourcePayload(body);
         payload.getContentMetadata().setContentLanguage("mi, en");
         payload.getContentMetadata().setContentLength(body.size());
         Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
         RecordedRequest request = server.takeRequest();
         assertEquals(request.getHeader("Content-Language"), "mi, en");
         assertEquals(headers.get("x-Content-Language"), ImmutableList.of("mi, en"));
      } finally {
View Full Code Here

      }
   }

   @Test(groups = { "integration", "live" })
   public void testFileGetParallel() throws Exception {
      final ByteSource supplier = createTestInput(32 * 1024);
      final String expectedContentDisposition = "attachment; filename=constit.txt";
      final String container = getContainerName();
      try {
         final String name = "constitution.txt";

         uploadByteSource(container, name, expectedContentDisposition, supplier);
         Map<Integer, ListenableFuture<?>> responses = Maps.newHashMap();
         for (int i = 0; i < 10; i++) {
            responses.put(i, this.exec.submit(new Callable<Void>() {
               @Override public Void call() throws Exception {
                  try {
                     Blob blob = view.getBlobStore().getBlob(container, name);
                     validateMetadata(blob.getMetadata(), container, name);
                     assertEquals(hashAndClose(blob.getPayload().openStream(), md5()), supplier.hash(md5()));
                     checkContentDisposition(blob, expectedContentDisposition);
                  } catch (IOException e) {
                     Throwables.propagate(e);
                  }
                  return null;
View Full Code Here

      assertEquals(request.getFilters().size(), 0);
   }

   public void testSignPutBlobWithGenerate() throws ArrayIndexOutOfBoundsException, SecurityException,
            IllegalArgumentException, NoSuchMethodException, IOException {
      ByteSource byteSource = ByteSource.wrap("foo".getBytes(Charsets.UTF_8));
      Blob blob = blobFactory.get().name(blobName)
         .payload(byteSource)
         .contentLength(byteSource.size())
         .contentMD5(byteSource.hash(Hashing.md5()).asBytes())
         .contentType("text/plain").build();
      byte[] md5 = { -84, -67, 24, -37, 76, -62, -8, 92, -19, -17, 101, 79, -52, -60, -92, -40 };
     
      assertEquals(blob.getPayload().getContentMetadata().getContentMD5(), md5);
View Full Code Here

         if (containers.isEmpty()) {
            continue;
         }
         String containerName = Iterables.getLast(containers).getName();

         final ByteSource input = ByteSource.wrap("str".getBytes());
         final HttpClient client = ctx.utils().http();

         // test signed put
         String blobName = "test-" + UUID.randomUUID();
         Blob blob2 = region.blobBuilder(blobName).forSigning()
               .contentLength(input.size())
               .contentMD5(input.hash(Hashing.md5()).asBytes())
               .contentType(MediaType.OCTET_STREAM.toString()).build();
         BlobRequestSigner signer = ctx.getSigner(regionId);
         HttpResponse response;
         try {
            HttpRequest putRequest;
            putRequest = signer.signPutBlob(containerName, blob2, 600);
            MutableContentMetadata metadata = blob2.getMetadata()
                  .getContentMetadata();
            HttpRequest.Builder<?> putRequestBuilder = putRequest.toBuilder()
                  .addHeader(HttpHeaders.CONTENT_TYPE,
                        metadata.getContentType());
            putRequestBuilder.addHeader(HttpHeaders.CONTENT_LENGTH,
                  String.valueOf(input.size()));
            putRequestBuilder.payload(input);
            putRequest = putRequestBuilder.build();
            Payload payload = Payloads.newPayload(input.read());
            putRequest.setPayload(payload);
            assertNotNull(putRequest, "regionId=" + regionId + ", container="
                  + containerName + ", blob=" + blobName);
            response = client.invoke(putRequest);
            if (response.getStatusCode() != 200
                  && response.getStatusCode() != 201) {
               fail("Signed PUT expected to return 200 or 201 but returned "
                     + response.getStatusCode());
            }
         } catch (Exception e) {
            fail("Failed signed put test: " + e);
         }

         // test signed get
         try {
            HttpRequest getRequest = signer.signGetBlob(containerName,
                  blobName);
            assertNotNull(getRequest, "regionId=" + regionId + ", container="
                  + containerName + ", blob=" + blobName);
            response = client.invoke(getRequest);
            if (response.getStatusCode() != 200) {
               fail("Signed GET expected to return 200 but returned "
                     + response.getStatusCode());
            }
            Payload payload = response.getPayload();
            assertEquals(ByteStreams2.toByteArrayAndClose(payload.openStream()), input.read(),
                  "Data with signed GET not identical to what was put");
         } catch (Exception e) {
            fail("Failed signed GET test: " + e);
         }
      }
View Full Code Here

         ImmutableList.of("vanilla"), ImmutableMap.of("Name", "foo"), "myUser", key, null, "happy bear");

   @Test
   public void testNodesParse() throws Exception {

      ByteSource byteSource = Resources.asByteSource(Resources.getResource("test1.yaml"));
      NodesFromYamlStream parser = new NodesFromYamlStream();

      assertEquals(parser.apply(byteSource).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
   }
View Full Code Here

   }

   @Test
   public void testNodesParseLocation() throws Exception {

      ByteSource byteSource = Resources.asByteSource(Resources.getResource("test_location.yaml"));
      NodesFromYamlStream parser = new NodesFromYamlStream();

      assertEquals(parser.apply(byteSource).asMap(), ImmutableMap.of(TEST2.getId(), TEST2, TEST3.getId(), TEST3));
   }
View Full Code Here

TOP

Related Classes of com.google.common.io.ByteSource

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.