/** takes {@link #getResourceFromUrl(String)} and reads fully, into a string */ public String getResourceAsString(String url) { try { return Streams.readFullyStringAndClose(getResourceFromUrl(url)); } catch (Exception e) { log.debug("ResourceUtils got error reading "+url+(context==null?"":" "+context)+" (rethrowing): "+e); throw Throwables.propagate(e); } }
public static String getContent(String url) { try { return Streams.readFullyStringAndClose(SslTrustUtils.trustAll(new URL(url).openConnection()).getInputStream()); } catch (Exception e) { throw Throwables.propagate(e); } }
public static String getContent(String url) { try { return Streams.readFullyStringAndClose(SslTrustUtils.trustAll(new URL(url).openConnection()).getInputStream()); } catch (Exception e) { throw Throwables.propagate(e); } }
@Override protected void doCall(BlobStore blobStore, String indent) throws Exception { Blob content = blobStore.getBlob(container, blob); stdout.println(indent+"Blob "+container+" : " +blob +" {"); stdout.println(indent+"\tHeaders {"); for (Map.Entry<String, String> entry : content.getAllHeaders().entries()) { stdout.println(indent+"\t\t"+entry.getKey() + " = " + entry.getValue()); } stdout.println(indent+"\t}"); stdout.println(indent+"\tmetadata : "+content.getMetadata()); stdout.println(indent+"\tpayload : "+ Streams.readFullyStringAndClose(content.getPayload().openStream())); stdout.println(indent+"}"); } }
@Test public void testInvalidCredentialsNotPassed() throws Exception { String url = baseUrl + "/no_credentials?no:auth@needed"; InputStream stream = utils.getResourceFromUrl(url); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test(groups="Integration") public void testGetResourceViaSftp() throws Exception { InputStream stream = utils.getResourceFromUrl("sftp://localhost:"+tempFile.getAbsolutePath()); assertEquals(Streams.readFullyStringAndClose(stream), tempFileContents); }
@Test public void testRedirect() throws Exception { InputStream stream = utils.getResourceFromUrl(baseUrl + "/redirect"); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test(expectedExceptions = RuntimeException.class) public void testCycleRedirect() throws Exception { InputStream stream = utils.getResourceFromUrl(baseUrl + "/cycle"); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testGetEmpty() throws Exception { InputStream stream = utils.getResourceFromUrl(baseUrl + "/empty"); assertEquals(Streams.readFullyStringAndClose(stream), ""); }
@Test public void testGetResourceViaFileWithPrefix() throws Exception { // The correct format for file URLs is file:///<absolute path>. // On UNIX file:///tmp. // On Windows both file:/C:/temp and file:///C:/temp are supported by Java, // while Windows itself supports the latter only. // Note that file://C:/temp is *wrong*, because C: is interpreted as the host InputStream stream = utils.getResourceFromUrl(tempFile.toURI().toURL().toString()); assertEquals(Streams.readFullyStringAndClose(stream), tempFileContents); }
@Test public void testGetResourceViaFileWithoutPrefix() throws Exception { InputStream stream = utils.getResourceFromUrl(tempFile.getAbsolutePath()); assertEquals(Streams.readFullyStringAndClose(stream), tempFileContents); }
@Test(groups="Integration") public void testGetResourceViaSftpWithUsername() throws Exception { String user = System.getProperty("user.name"); InputStream stream = utils.getResourceFromUrl("sftp://"+user+"@localhost:"+tempFile.getAbsolutePath()); assertEquals(Streams.readFullyStringAndClose(stream), tempFileContents); }
@Test public void testGet() throws Exception { InputStream stream = utils.getResourceFromUrl(baseUrl + "/simple"); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testGetProtected() throws Exception { String url = baseUrl.replace("http://", "http://test:test@") + "/auth"; InputStream stream = utils.getResourceFromUrl(url); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testGetProtectedEscape() throws Exception { String url = baseUrl.replace("http://", "http://test%40me%3A%2F:test@") + "/auth_escape"; InputStream stream = utils.getResourceFromUrl(url); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testGetProtectedEscape2() throws Exception { String url = baseUrl.replace("http://", "http://test%40me%3Atest@") + "/auth_escape2"; InputStream stream = utils.getResourceFromUrl(url); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
public static String jsonFixture(String path) { InputStream stream = RestApiTestUtils.class.getClassLoader().getResourceAsStream(path); if (stream==null) throw new IllegalStateException("Cannot find resource: "+path); return asJson(fromJson(Streams.readFullyStringAndClose(stream), Object.class)); }
@Test public void testReadAManifest() throws Exception { Enumeration<URL> manifests = this.getClass().getClassLoader().getResources("META-INF/MANIFEST.MF"); log.info("Bundles and exported packages:"); MutableSet<String> allPackages = MutableSet.of(); while (manifests.hasMoreElements()) { ManifestHelper mf = ManifestHelper.forManifestContents(Streams.readFullyStringAndClose(manifests.nextElement().openStream())); List<String> mfPackages = mf.getExportedPackages(); log.info(" " + mf.getSymbolicNameVersion() + ": " + mfPackages); allPackages.addAll(mfPackages); } log.info("Total export package count: " + allPackages.size()); Assert.assertTrue(allPackages.size() > 20, "did not find enough packages"); // probably much larger Assert.assertTrue(allPackages.contains(EmbeddedFelixFramework.class.getPackage().getName())); }
private String getTestYaml() { final ResourceUtils resourceUtils = ResourceUtils.create(); final InputStream yaml = resourceUtils.getResourceFromUrl("classpath://test-highstate.yaml"); return Streams.readFullyStringAndClose(yaml); } }
public void testCreateListDestroyContainer() throws IOException { context.getBlobStore().createContainerInLocation(null, testContainerName); context.getBlobStore().list(testContainerName); PageSet<? extends StorageMetadata> ps = context.getBlobStore().list(); assertHasItemNamed(ps, testContainerName); Blob b = context.getBlobStore().blobBuilder("my-blob-1").payload(Streams.newInputStreamWithContents("hello world")).build(); context.getBlobStore().putBlob(testContainerName, b); Blob b2 = context.getBlobStore().getBlob(testContainerName, "my-blob-1"); Assert.assertEquals(Streams.readFullyStringAndClose(b2.getPayload().openStream()), "hello world"); context.getBlobStore().deleteContainer(testContainerName); }