@Override public void copyDirectory(String from, String to) { InputStream response = executor.getFileOrDirectoryFromContainerAsTar(cubeId, from); Path toPath = Paths.get(to); File toPathFile = toPath.toFile(); if (toPathFile.exists() && toPathFile.isFile()) { throw new IllegalArgumentException(String.format( "%s parameter should be a directory in copy operation but you set an already existing file not a directory. Check %s in your local directory because currently is a file.", "to", toPath.normalize().toString())); } try { Files.createDirectories(toPath); IOUtil.untar(response, toPathFile); } catch (IOException e) { throw new IllegalStateException(e); } }
public static Map<String, Object> deepMerge(Map<String, Object> original, Map<String, Object> newMap) { for (String key : newMap.keySet()) { if (newMap.get(key) instanceof Map && original.get(key) instanceof Map) { Map<String, Object> originalChild = (Map<String, Object>) original.get(key); Map<String, Object> newChild = (Map) newMap.get(key); original.put(key, deepMerge(originalChild, newChild)); } else { original.put(key, newMap.get(key)); } } return original; }
public SimianArmyScriptChaos(String chaosScript) { this.chaosScript = chaosScript; this.chaosScriptContent = IOUtil.asArrayString(SimianArmyScriptChaos.class.getResourceAsStream("/" + SCRIPTS_PACKAGE + "/" + this.chaosScript)); }
private String resolveCommand(String command, int port) { Map<String, String> values = new HashMap<String, String>(); values.put("port", Integer.toString(port)); String templateContent = IOUtil.asStringPreservingNewLines(PollingAwaitStrategy.class.getResourceAsStream(command + ".sh")); return IOUtil.replacePlaceholders(templateContent, values); } }
/** * Unzips the given input stream of a ZIP to the given directory */ public static void unzip(InputStream in, File toDir) throws IOException { ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in)); try { ZipEntry entry = zis.getNextEntry(); while (entry != null) { if (!entry.isDirectory()) { String entryName = entry.getName(); File toFile = new File(toDir, entryName); toFile.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(toFile); try { try { copy(zis, os); } finally { zis.closeEntry(); } } finally { close(os, true); } } entry = zis.getNextEntry(); } } finally { close(zis, true); } }
String content = IOUtil.asString(urlConnection.getInputStream());
@Test public void shouldTarFile() throws IOException { final File file = temporaryFolder.newFile("content.txt"); final FileOutputStream output = new FileOutputStream(file); IOUtils.copy(new ByteArrayInputStream("hello".getBytes()), output); output.flush(); output.close(); File outputFolder = temporaryFolder.newFolder(); IOUtil.tar(file, new File(outputFolder, "x.tar")); IOUtil.untar(new FileInputStream(new File(outputFolder, "x.tar")), outputFolder); final String content = IOUtil.asString(new FileInputStream(new File(outputFolder, "content.txt"))); assertThat(content, is("hello")); }
/** * Method that takes an inputstream, read it preserving the end lines, and subtitute using commons-lang-3 calls * the variables, first searching as system properties vars and then in environment var list. * In case of missing the property is replaced by white space. * @param stream * @return */ public static String replaceParameters(final InputStream stream) { String content = IOUtil.asStringPreservingNewLines(stream); return resolvePlaceholders(content); }
String content = IOUtil.asString(urlConnection.getInputStream());
private String resolveCommand(String command, int port) { Map<String, String> values = new HashMap<String, String>(); values.put("port", Integer.toString(port)); String templateContent = IOUtil.asStringPreservingNewLines(PollingAwaitStrategy.class.getResourceAsStream(command + ".sh")); return IOUtil.replacePlaceholders(templateContent, values); } }
/** * Unzips the given input stream of a ZIP to the given directory */ public static void unzip(InputStream in, File toDir) throws IOException { ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in)); try { ZipEntry entry = zis.getNextEntry(); while (entry != null) { if (!entry.isDirectory()) { String entryName = entry.getName(); File toFile = new File(toDir, entryName); toFile.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(toFile); try { try { copy(zis, os); } finally { zis.closeEntry(); } } finally { close(os, true); } } entry = zis.getNextEntry(); } } finally { close(zis, true); } }
/** * Method that takes an inputstream, read it preserving the end lines, and subtitute using commons-lang-3 calls * the variables, first searching as system properties vars and then in environment var list. * In case of missing the property is replaced by white space. * @param stream * @return */ public static String replaceParameters(final InputStream stream) { String content = IOUtil.asStringPreservingNewLines(stream); return resolvePlaceholders(content); }
private String getGitUrl(File basedir) { if (basedir.exists() && basedir.isDirectory()) { File gitConfig = new File(basedir, ".git/config"); if (gitConfig.isFile() && gitConfig.exists()) { try (InputStream is = new FileInputStream(gitConfig)) { String text = IOUtil.asString(is); if (text != null) { return getGitUrl(text); } } catch (IOException e) { logger.warn("Failed to read:[ " + gitConfig + "] file to annotate the namespace. Due to: " + e); } } } File parentFile = basedir.getParentFile(); if (parentFile != null) { return getGitUrl(parentFile); } return null; }
@Override public void copyDirectory(String from, String to) { InputStream response = executor.getFileOrDirectoryFromContainerAsTar(cubeId, from); Path toPath = Paths.get(to); File toPathFile = toPath.toFile(); if (toPathFile.exists() && toPathFile.isFile()) { throw new IllegalArgumentException(String.format( "%s parameter should be a directory in copy operation but you set an already existing file not a directory. Check %s in your local directory because currently is a file.", "to", toPath.normalize().toString())); } try { Files.createDirectories(toPath); IOUtil.untar(response, toPathFile); } catch (IOException e) { throw new IllegalStateException(e); } }
public static Map<String, Object> deepMerge(Map<String, Object> original, Map<String, Object> newMap) { for (String key : newMap.keySet()) { if (newMap.get(key) instanceof Map && original.get(key) instanceof Map) { Map<String, Object> originalChild = (Map<String, Object>) original.get(key); Map<String, Object> newChild = (Map) newMap.get(key); original.put(key, deepMerge(originalChild, newChild)); } else { original.put(key, newMap.get(key)); } } return original; }
private static DockerCompositions convert(URI uri, DefinitionFormat definitionFormat) throws IOException { try { Path definitionFilePath = Paths.get(uri); return convert(definitionFilePath, definitionFormat); } catch (FileSystemNotFoundException e) { String content = ""; if (uri.isAbsolute()) { content = IOUtil.asStringPreservingNewLines(uri.toURL().openStream()); } else { String fileContent = uri.toString(); content = IOUtil.asStringPreservingNewLines(new FileInputStream(fileContent)); } return convert(content, definitionFormat); } catch (IllegalArgumentException e) { String content = ""; if (uri.isAbsolute()) { content = IOUtil.asStringPreservingNewLines(uri.toURL().openStream()); } else { String fileContent = uri.toString(); content = IOUtil.asStringPreservingNewLines(new FileInputStream(fileContent)); } return convert(content, definitionFormat); } }
@Test public void testMultipleDynamicOpenShiftResource() throws Exception { OpenShiftResourceFactory.createResources("key", adapter, MultipleOpenShiftDynamicImageStreamResourceExample.class, configuration.getProperties()); ArgumentCaptor<ByteArrayInputStream> captor = ArgumentCaptor.forClass(ByteArrayInputStream.class); verify(adapter, times(2)).createResource(eq("key"), captor.capture()); List<ByteArrayInputStream> is = captor.getAllValues(); assertEquals(2, is.size()); assertEquals("{\"metadata\":{\"name\":\"stream-1.0\",\"annotations\":{\"openshift.io\\/image.insecureRepository\":false}},\"apiVersion\":\"v1\",\"kind\":\"ImageStream\",\"spec\":{\"tags\":[{\"importPolicy\":{\"insecure\":false},\"name\":\"1.0\",\"annotations\":{\"version\":\"1.0\"},\"from\":{\"kind\":\"DockerImage\",\"name\":\"registry.host.com\\/imageFamily\\/one\"}}]}}", IOUtil.asString(is.get(0))); assertEquals("{\"metadata\":{\"name\":\"stream-2.0\",\"annotations\":{\"openshift.io\\/image.insecureRepository\":true}},\"apiVersion\":\"v1\",\"kind\":\"ImageStream\",\"spec\":{\"tags\":[{\"importPolicy\":{\"insecure\":true},\"name\":\"2.0\",\"annotations\":{\"version\":\"2.0\"},\"from\":{\"kind\":\"DockerImage\",\"name\":\"registry.host.com\\/imageFamily\\/two\"}}]}}", IOUtil.asString(is.get(1))); } }
private void executeCopyAction(DockerClientExecutor dockerClientExecutor, String containerId, Copy configurationParameters) throws IOException { String to = null; String from = null; if (configurationParameters.getTo() != null && configurationParameters.getFrom() != null) { to = configurationParameters.getTo(); from = configurationParameters.getFrom(); } else { throw new IllegalArgumentException( String.format("to and from property is mandatory when copying files from container %s.", containerId)); } InputStream response = dockerClientExecutor.getFileOrDirectoryFromContainerAsTar(containerId, from); Path toPath = Paths.get(to); File toPathFile = toPath.toFile(); if (toPathFile.exists() && toPathFile.isFile()) { throw new IllegalArgumentException(String.format( "%s parameter should be a directory in copy operation but you set an already existing file not a directory. Check %s in your local directory because currently is a file.", "to", toPath.normalize().toString())); } Files.createDirectories(toPath); IOUtil.untar(response, toPathFile); } }
@Test public void shouldMergeToEmptyMap() { Map<String, Object> original = new HashMap<>(); Map<String, Object> element = new HashMap<>(); element.put("a", "b"); Map<String, Object> element2 = new HashMap<>(); element2.put("c", "d"); element.put("e", element2); IOUtil.deepMerge(original, element); assertThat(original.containsKey("a"), is(true)); assertThat(original.containsKey("e"), is(true)); assertThat((String) original.get("a"), is("b")); Map<String, Object> innerElement = (Map<String, Object>) original.get("e"); assertThat(innerElement.containsKey("c"), is(true)); assertThat((String) innerElement.get("c"), is("d")); }
private static DockerCompositions convert(URI uri, DefinitionFormat definitionFormat) throws IOException { try { Path definitionFilePath = Paths.get(uri); return convert(definitionFilePath, definitionFormat); } catch (FileSystemNotFoundException e) { String content = ""; if (uri.isAbsolute()) { content = IOUtil.asStringPreservingNewLines(uri.toURL().openStream()); } else { String fileContent = uri.toString(); content = IOUtil.asStringPreservingNewLines(new FileInputStream(fileContent)); } return convert(content, definitionFormat); } catch (IllegalArgumentException e) { String content = ""; if (uri.isAbsolute()) { content = IOUtil.asStringPreservingNewLines(uri.toURL().openStream()); } else { String fileContent = uri.toString(); content = IOUtil.asStringPreservingNewLines(new FileInputStream(fileContent)); } return convert(content, definitionFormat); } }