public static String getMd5Checksum(InputStream in) { MessageDigest md; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw Exceptions.propagate(e); } DigestInputStream dis = new DigestInputStream(in, md); readFullyAndClose(dis); byte[] digest = md.digest(); StringBuilder result = new StringBuilder(); for (byte b: digest) { result.append(Strings.padStart(Integer.toHexString((256+b)%256), 2, '0')); } return result.toString().toUpperCase(); }
@Override public void put(ByteSource bytes) { try { synchronized (map) { map.put(key, Streams.readFullyAndClose(bytes.openStream())); mapModTime.put(key, new Date()); } } catch (IOException e) { throw Exceptions.propagate(e); } } @Override
private void checkValidArchive(String url, long minSize) { byte[] bytes; try { bytes = Streams.readFullyAndClose(ResourceUtils.create(this).getResourceFromUrl(url)); log.info("read "+bytes.length+" bytes from "+url+" for "+JavaClassNames.callerNiceClassAndMethod(1)); } catch (Exception e) { log.warn("Unable to read URL "+url+" for " +JavaClassNames.callerNiceClassAndMethod(1)+ "; this test may require hosted (sonatype/mavencentral) repo to be populated"); Assert.fail("Unable to read URL "+url+"; this test may require hosted (sonatype/mavencentral) repo to be populated"); throw Exceptions.propagate(e); } // confirm this follow redirects! Assert.assertTrue(bytes.length > minSize, "download of "+url+" is suspect ("+Strings.makeSizeString(bytes.length)+")"); // (could also check it is a zip etc) }
@Override public void run() { try { byte[] bytes = Streams.readFullyAndClose(ResourceUtils.create(this).getResourceFromUrl(url)); // confirm this follow redirects! Assert.assertTrue(bytes.length > 100*1000, "download of "+url+" is suspect ("+Strings.makeSizeString(bytes.length)+")"); // (could also check it is a zip etc) } catch (Exception e) { throw Exceptions.propagate(e); } }}); }
private KeyPair readPem(String url, String passphrase) { return SecureKeys.readPem(Streams.readFullyAndClose(ResourceUtils.create(this).getResourceFromUrl(url)), passphrase); } }
@Test public void testMementoNotTooBig() throws Exception { origApp.addChild(EntitySpec.create(TestEntity.class)); // dynamic conf key origApp.config().set(TestEntity.CONF_NAME, "slim"); // declared sensor origApp.sensors().set(TestApplication.MY_ATTRIBUTE, "foo"); // dynamic sensor origApp.sensors().set(TestEntity.SEQUENCE, 98765); // dynamic effector origApp.getMutableEntityType().addEffector(SayHiBody.EFFECTOR); RebindTestUtils.stopPersistence(origApp); File mementoFile = new File(new File(mementoDir, "entities"), origApp.getId()); String memento = Streams.readFullyAndClose(new FileReader(mementoFile)); log.info("memento is:\n"+memento); // make sure it's not too long, and doesn't have declared items Assert.assertTrue(memento.length() < 4000, "length is: "+memento.length()); Assert.assertFalse(memento.contains("restart")); Assert.assertFalse(memento.contains(TestApplication.MY_ATTRIBUTE.getDescription())); Assert.assertFalse(memento.toLowerCase().contains("typetoken")); }
private void assertMgmtHasSampleMyCatalogApp(String symbolicName, String bundleUrl) { RegisteredType item = mgmt.getTypeRegistry().get(symbolicName); assertNotNull(item, "failed to load item with id=" + symbolicName + " from catalog. Entries were: " + Joiner.on(",").join(mgmt.getTypeRegistry().getAll())); assertEquals(item.getSymbolicName(), symbolicName); RegisteredTypes.tryValidate(item, RegisteredTypeLoadingContexts.spec(Entity.class)).get(); // stored as yaml, not java String planYaml = RegisteredTypes.getImplementationDataStringForSpec(item); assertNotNull(planYaml); Assert.assertTrue(planYaml.contains("io.camp.mock:AppServer")); // and let's check we have libraries Collection<OsgiBundleWithUrl> libs = item.getLibraries(); assertEquals(libs.size(), 1); OsgiBundleWithUrl bundle = Iterables.getOnlyElement(libs); assertEquals(bundle.getUrl(), bundleUrl); // now let's check other things on the item assertEquals(item.getDisplayName(), "My Catalog App"); assertEquals(item.getDescription(), "My description"); assertEquals(item.getIconUrl(), "classpath:/org/apache/brooklyn/test/osgi/entities/icon.gif"); // and confirm we can resolve ICON byte[] iconData = Streams.readFullyAndClose(ResourceUtils.create(CatalogUtils.newClassLoadingContext(mgmt, item)).getResourceFromUrl(item.getIconUrl())); assertEquals(iconData.length, 43); }
@Override public void put(ByteSource val) { if (writesFailSilently) return; for (ObjectStoreTransactionListener listener: listeners) { try { listener.recordDataOut("writing (binary) "+path, Streams.readFullyAndClose(val.openStream()).length); } catch (IOException e) { throw Exceptions.propagate(e); } } delegate.put(val); } @Override
protected void runCatalogTest(String catalogFile, Reader yamlApp, Predicate<? super Application> assertion) throws Exception { Reader catalogInput = Streams.reader(new ResourceUtils(this).getResourceFromUrl(catalogFile)); String catalogContent = Streams.readFullyAndClose(catalogInput); Iterable<? extends CatalogItem<?, ?>> items = launcher.getManagementContext().getCatalog().addItems(catalogContent); try { final Application app = launcher.launchAppYaml(yamlApp); assertNoFires(app); assertTrue(assertion.apply(app)); Application newApp = rebind(); assertNoFires(newApp); assertTrue(assertion.apply(app)); } finally { for (CatalogItem<?, ?> item : items) { launcher.getManagementContext().getCatalog().deleteCatalogItem(item.getSymbolicName(), item.getVersion()); } } }
@BeforeMethod(alwaysRun = true) @Override public void setUp() throws Exception { super.setUp(); // Load the bytes of the jar TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/" + jarName); InputStream resource = getClass().getResourceAsStream("/brooklyn/osgi/" + jarName); final byte[] jarBytes = Streams.readFullyAndClose(resource); // Start a mock web-server that will return the jar requestInterceptor = new TestHttpRecordingRequestInterceptor(); webServer = new TestHttpServer() .handler("/" + jarName, new TestHttpRequestHandler().code(200).response(jarBytes)) .handler("/" + malformedJarName, new TestHttpRequestHandler().code(200).response("simulating-malformed-jar")) .interceptor(requestInterceptor) .start(); jarUrl = new URL(Urls.mergePaths(webServer.getUrl(), jarName)); malformedJarUrl = new URL(Urls.mergePaths(webServer.getUrl(), malformedJarName)); }