/** * true if the URL points to content which must be resolved on the server-side (i.e. classpath) * and which is safe to do so (currently just images, though in future perhaps also javascript and * html plugins) * * <p>note we do not let caller access classpath through this mechanism, just those which are * supplied by the platform administrator e.g. as an icon url */ public boolean isUrlServerSideAndSafe(String url) { if (Strings.isEmpty(url)) return false; String ext = Files.getFileExtension(url); if (Strings.isEmpty(ext)) return false; MediaType mime = WebResourceUtils.getImageMediaTypeFromExtension(ext); if (mime == null) return false; return !Urls.isUrlWithProtocol(url) || url.startsWith("classpath:"); }
@Test(groups = "Integration") public void testCopyToFailBadSubdirCreate() throws IOException { String fn = Urls.mergePaths(tempDir.getPath(), "non-existent-subdir-to-create/file"); SshPutTaskWrapper t = submit(SshTasks.newSshPutTaskFactory(host, fn).contents("hello world").createDirectory()); t.block(); // directory should be created, and file readable now Assert.assertEquals(FileUtils.readFileToString(new File(fn)), "hello world"); Assert.assertEquals(t.getExitCode(), (Integer) 0); }
@Test(groups = "Integration") public void testCopyTo() throws IOException { String fn = Urls.mergePaths(tempDir.getPath(), "f1"); SshPutTaskWrapper t = submit(SshTasks.newSshPutTaskFactory(host, fn).contents("hello world")); t.block(); Assert.assertEquals(FileUtils.readFileToString(new File(fn)), "hello world"); // and make sure this doesn't throw Assert.assertTrue(t.isDone()); Assert.assertTrue(t.isSuccessful()); Assert.assertEquals(t.get(), null); Assert.assertEquals(t.getExitCode(), (Integer) 0); }
@Test(groups = "Integration") public void testSshFetch() throws IOException { String fn = Urls.mergePaths(tempDir.getPath(), "f2"); FileUtils.write(new File(fn), "hello fetched world"); SshFetchTaskFactory tf = SshTasks.newSshFetchTaskFactory(host, fn); SshFetchTaskWrapper t = tf.newTask(); mgmt.getExecutionManager().submit(t); t.block(); Assert.assertTrue(t.isDone()); Assert.assertEquals(t.get(), "hello fetched world"); Assert.assertEquals(t.getBytes(), "hello fetched world".getBytes()); }
@Test(groups = "Integration") public void testCopyToFailBadSubdirAllow() throws IOException { String fn = Urls.mergePaths(tempDir.getPath(), "non-existent-subdir/file"); SshPutTaskWrapper t = submit(SshTasks.newSshPutTaskFactory(host, fn).contents("hello world").allowFailure()); // this doesn't fail t.block(); Assert.assertTrue(t.isDone()); // and this doesn't fail either Assert.assertEquals(t.get(), null); // but it's not successful Assert.assertNotNull(t.getException()); Assert.assertFalse(t.isSuccessful()); // exit code probably null, but won't be zero Assert.assertNotEquals(t.getExitCode(), (Integer) 0); }
@Test(groups = "Integration") public void testCopyToFailBadSubdir() throws IOException { String fn = Urls.mergePaths(tempDir.getPath(), "non-existent-subdir/file"); SshPutTaskWrapper t = submit(SshTasks.newSshPutTaskFactory(host, fn).contents("hello world")); // this doesn't fail t.block(); Assert.assertTrue(t.isDone()); setExpectingFailure(); try { // but this does t.get(); } catch (Exception e) { log.info("The error if file cannot be written is: " + e); clearExpectedFailure(); } checkExpectedFailure(); // and the results indicate failure Assert.assertFalse(t.isSuccessful()); Assert.assertNotNull(t.getException()); Assert.assertNotEquals(t.getExitCode(), (Integer) 0); }