public String sign(String toSign) { try { byte[] encrypted = ByteStreams2.toByteArrayAndClose(new RSAEncryptingPayload(crypto, Payloads.newStringPayload(toSign), supplyKey.get()).openStream()); return base64().encode(encrypted); } catch (IOException e) { throw new HttpException("error signing request", e); } }
@VisibleForTesting String hashBody(Payload payload) { if (payload == null) return emptyStringHash; payload = useTheFilePartIfForm(payload); checkArgument(payload != null, "payload was null"); checkArgument(payload.isRepeatable(), "payload must be repeatable: " + payload); try { return base64().encode(ByteStreams2.hashAndClose(payload.getInput(), sha1()).asBytes()); } catch (Exception e) { Throwables.propagateIfPossible(e); throw new HttpException("error creating sigature for payload: " + payload, e); } }
public String sign(String toSign) { try { byte[] encrypted = ByteStreams2.toByteArrayAndClose(new RSAEncryptingPayload(crypto, Payloads.newStringPayload(toSign), supplyKey.get()).openStream()); return base64().encode(encrypted); } catch (IOException e) { throw new HttpException("error signing request", e); } }
@VisibleForTesting String hashBody(Payload payload) { if (payload == null) return emptyStringHash; payload = useTheFilePartIfForm(payload); checkArgument(payload != null, "payload was null"); checkArgument(payload.isRepeatable(), "payload must be repeatable: " + payload); try { return base64().encode(ByteStreams2.hashAndClose(payload.getInput(), sha1()).asBytes()); } catch (Exception e) { Throwables.propagateIfPossible(e); throw new HttpException("error creating sigature for payload: " + payload, e); } }
@Override public byte[] decrypt(InputStream input) throws IOException { @SuppressWarnings("resource") Payload payload = new RSADecryptingPayload(crypto, Payloads.newPayload(input), privateKey.get()); return ByteStreams2.toByteArrayAndClose(payload.openStream()); }
private void assertValidMd5(final InputStream input, String md5) throws IOException { assertEquals(base64().encode(ByteStreams2.hashAndClose(input, md5()).asBytes()), md5); }
@Override public byte[] encrypt(InputStream input) throws IOException { @SuppressWarnings("resource") Payload payload = new RSAEncryptingPayload(crypto, Payloads.newPayload(input), privateKey.get()); return ByteStreams2.toByteArrayAndClose(payload.openStream()); }
@Test(groups = "live") public void testRemoteInputStream() throws Exception { try { URL url = new URL(checkNotNull(sysHttpStreamUrl, "sysHttpStreamUrl")); URLConnection connection = url.openConnection(); HttpWire wire = setUp(); InputStream in = wire.input(connection.getInputStream()); byte[] compare = ByteStreams2.hashAndClose(in, md5()).asBytes(); Thread.sleep(100); assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5)); assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484); } catch (UnknownHostException e) { // probably in offline mode } }
@Override public byte[] encrypt(InputStream input) throws IOException { @SuppressWarnings("resource") Payload payload = new RSAEncryptingPayload(crypto, Payloads.newPayload(input), privateKey.get()); return ByteStreams2.toByteArrayAndClose(payload.openStream()); }
@Test(groups = "live") public void testRemoteInputStreamSynch() throws Exception { try { URL url = new URL(checkNotNull(sysHttpStreamUrl, "sysHttpStreamUrl")); URLConnection connection = url.openConnection(); HttpWire wire = setUpSynch(); InputStream in = wire.input(connection.getInputStream()); byte[] compare = ByteStreams2.hashAndClose(in, md5()).asBytes(); Thread.sleep(100); assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5)); assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484); } catch (UnknownHostException e) { // probably in offline mode } }
@Override public byte[] decrypt(InputStream input) throws IOException { @SuppressWarnings("resource") Payload payload = new RSADecryptingPayload(crypto, Payloads.newPayload(input), privateKey.get()); return ByteStreams2.toByteArrayAndClose(payload.openStream()); }
private static void handlePost(HttpServletRequest request, HttpServletResponse response) throws IOException { InputStream body = request.getInputStream(); try { if (request.getHeader("Content-MD5") != null) { String expectedMd5 = request.getHeader("Content-MD5"); String realMd5FromRequest; realMd5FromRequest = base64().encode(ByteStreams2.hashAndClose(body, md5()).asBytes()); boolean matched = expectedMd5.equals(realMd5FromRequest); if (matched) { response.setStatus(SC_OK); response.addHeader("x-Content-MD5", realMd5FromRequest); } else { response.sendError(500, "didn't match"); } } else { String responseString = (request.getContentLength() < 10240) ? toStringAndClose(body) + "POST" : "POST"; body = null; for (String header : new String[] { CONTENT_DISPOSITION, CONTENT_LANGUAGE, CONTENT_ENCODING }) if (request.getHeader(header) != null) { response.addHeader("x-" + header, request.getHeader(header)); } response.setStatus(SC_OK); response.getWriter().println(responseString); } Request.class.cast(request).setHandled(true); } catch (IOException e) { closeQuietly(body); response.sendError(500, getStackTraceAsString(e)); } }
@Test(groups = "live", dependsOnMethods = "testGetObject") public void testCopyObject() throws IOException { GoogleCloudStorageObject gcsObject = api().copyObject(BUCKET_NAME2, COPIED_OBJECT_NAME, BUCKET_NAME, UPLOAD_OBJECT_NAME); assertNotNull(gcsObject); assertEquals(gcsObject.bucket(), BUCKET_NAME2); assertEquals(gcsObject.name(), COPIED_OBJECT_NAME); assertEquals(gcsObject.contentType(), "text/plain"); // Test for data PayloadEnclosing impl = api().download(BUCKET_NAME2, COPIED_OBJECT_NAME); assertNotNull(impl); assertEquals(ByteStreams2.toByteArrayAndClose(impl.getPayload().openStream()), ByteStreams2.toByteArrayAndClose(testPayload.getPayload().openStream())); }
@Test(dependsOnMethods = "testListCookbookVersionsWithChefService") public void testDownloadCookbooks() throws Exception { Iterable<? extends CookbookVersion> cookbooks = chefService.listCookbookVersions(); for (CookbookVersion cookbook : cookbooks) { for (Resource resource : ImmutableList.<Resource> builder().addAll(cookbook.getDefinitions()) .addAll(cookbook.getFiles()).addAll(cookbook.getLibraries()).addAll(cookbook.getSuppliers()) .addAll(cookbook.getRecipes()).addAll(cookbook.getResources()).addAll(cookbook.getRootFiles()) .addAll(cookbook.getTemplates()).addAll(cookbook.getAttributes()).build()) { InputStream stream = api.getResourceContents(resource); assertNotNull(stream, "Resource contents are null for resource: " + resource.getName()); byte[] md5 = ByteStreams2.hashAndClose(stream, md5()).asBytes(); assertEquals(md5, resource.getChecksum()); } } }
@Test(groups = "live", dependsOnMethods = "testRewrite") public void testDownload() throws IOException { PayloadEnclosing impl = api().download(BUCKET_NAME, UPLOAD_OBJECT_NAME); ContentMetadata meta = impl.getPayload().getContentMetadata(); assertNotNull(impl); assertNotNull(impl.getPayload()); assertNotNull(meta); assertEquals(meta.getContentType(), "text/plain"); testPayload.getPayload().getContentMetadata().setContentDisposition("attachment"); assertEquals(ByteStreams2.toByteArrayAndClose(impl.getPayload().openStream()), ByteStreams2.toByteArrayAndClose(testPayload.getPayload().openStream())); }
@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())); } catch (IOException e) { Throwables.propagate(e); } return null; } }));
@Test(groups = "live", dependsOnMethods = "testGetObject") public void testCopyObjectWithUpdatedMetadata() throws IOException { String METADATA_KEY = "key1"; String METADATA_VALUE = "value1"; ObjectTemplate template = new ObjectTemplate().contentLanguage("fr").contentType("text/plain") .contentDisposition("attachment").customMetadata(METADATA_KEY, METADATA_VALUE); GoogleCloudStorageObject gcsObject = api().copyObject(BUCKET_NAME2, COPIED_OBJECT_NAME2, BUCKET_NAME, UPLOAD_OBJECT_NAME, template); assertNotNull(gcsObject); assertEquals(gcsObject.bucket(), BUCKET_NAME2); assertEquals(gcsObject.name(), COPIED_OBJECT_NAME2); assertNotNull(gcsObject.acl()); assertEquals(gcsObject.contentType(), "text/plain"); assertEquals(gcsObject.metadata().get(METADATA_KEY), METADATA_VALUE); assertEquals(gcsObject.contentLanguage(), "fr"); // Test for data PayloadEnclosing impl = api().download(BUCKET_NAME2, COPIED_OBJECT_NAME2); assertNotNull(impl); assertEquals(ByteStreams2.toByteArrayAndClose(impl.getPayload().openStream()), ByteStreams2.toByteArrayAndClose(testPayload.getPayload().openStream())); }
@Override @BeforeClass(groups = "live") public void setup() { super.setup(); for (String regionId : regions) { boolean created = getApi().getContainerApi(regionId).create(containerName); if (!created) { deleteAllObjectsInContainer(regionId, containerName); } } GenericArchive files = ShrinkWrap.create(GenericArchive.class, "files.tar.gz"); StringAsset content = new StringAsset("foo"); for (int i = 0; i < OBJECT_COUNT; i++) { paths.add(containerName + "/file" + i); files.add(content, "/file" + i); } try { tarGz = ByteStreams2.toByteArrayAndClose(files.as(TarGzExporter.class).exportAsInputStream()); } catch (IOException e) { throw Throwables.propagate(e); } }
public void testGetBlob() throws IOException { String blobKey = TestUtils.createRandomBlobKey(); GetOptions options = null; Blob resultBlob; blobStore.createContainerInLocation(null, CONTAINER_NAME); resultBlob = blobStore.getBlob(CONTAINER_NAME, blobKey, options); assertNull(resultBlob, "Blob exists"); // create blob TestUtils.createBlobsInContainer(CONTAINER_NAME, blobKey); resultBlob = blobStore.getBlob(CONTAINER_NAME, blobKey, options); assertNotNull(resultBlob, "Blob exists"); // checks file content ByteSource expectedFile = Files.asByteSource(new File(TARGET_CONTAINER_NAME, blobKey)); assertEquals(expectedFile.read(), ByteStreams2.toByteArrayAndClose(resultBlob.getPayload().openStream()), "Blob payload differs from file content"); // metadata are verified in the test for blobMetadata, so no need to // perform a complete test here assertNotNull(resultBlob.getMetadata(), "Metadata null"); MutableBlobMetadata metadata = resultBlob.getMetadata(); assertEquals(blobKey, metadata.getName(), "Wrong blob metadata"); }
} catch (ClassCastException cce) { try { byteSource = ByteSource.wrap(ByteStreams2.toByteArrayAndClose(blob.getPayload().openStream())); } catch (IOException e) { throw new RuntimeException(e);