private String getPath(String subPath, String id) { return subPath+"/"+Strings.makeValidFilename(id); }
private String toSimpleName(String name) { String simpleName = name.substring(name.lastIndexOf(".")+1); if (Strings.isBlank(simpleName)) simpleName = name.trim(); return Strings.makeValidFilename(simpleName); }
/** as {@link #simpleClassName(Object)} but making the result clean for use on filesystems and as java identifiers */ public static String cleanSimpleClassName(Object x) { return Strings.makeValidFilename(simpleClassName(x)); }
/** as {@link #simpleClassName(Object)} but making the result clean for use on filesystems and as java identifiers */ public static String cleanSimpleClassName(Class<?> x) { return Strings.makeValidFilename(simpleClassName(x)); }
@Test(expectedExceptions = { NullPointerException.class }) public void testMakeValidFilenameNull() { Strings.makeValidFilename(null); } @Test(expectedExceptions = { IllegalArgumentException.class })
public void testMakeValidFilename() { assertEquals("abcdef", Strings.makeValidFilename("abcdef")); assertEquals("abc_def", Strings.makeValidFilename("abc$$$def")); assertEquals("abc_def", Strings.makeValidFilename("$$$abc$$$def$$$")); assertEquals("a_b_c", Strings.makeValidFilename("a b c")); assertEquals("a.b.c", Strings.makeValidFilename("a.b.c")); } @Test(expectedExceptions = { NullPointerException.class })
@Test(expectedExceptions = { IllegalArgumentException.class }) public void testMakeValidFilenameEmpty() { Strings.makeValidFilename(""); } @Test(expectedExceptions = { IllegalArgumentException.class })
@Test(expectedExceptions = { IllegalArgumentException.class }) public void testMakeValidFilenameBlank() { Strings.makeValidFilename(" \t "); }
/** creates a private temp file which will be deleted on exit; * either prefix or ext may be null; * if ext is non-empty and not > 4 chars and not starting with a ., then a dot will be inserted; * if either name part is too long it will be shortened to prevent filesystem errors */ public static File newTempFile(String prefix, String ext) { String sanitizedPrefix = (Strings.isNonEmpty(prefix) ? Strings.makeValidFilename(prefix) + "-" : ""); if (sanitizedPrefix.length()>101) sanitizedPrefix = sanitizedPrefix.substring(0, 100)+"--"; String extWithPrecedingSeparator = (Strings.isNonEmpty(ext) ? ext.startsWith(".") || ext.length()>4 ? ext : "."+ext : ""); if (extWithPrecedingSeparator.length()>13) sanitizedPrefix = sanitizedPrefix.substring(0, 12)+"--"; try { File tempFile = File.createTempFile(sanitizedPrefix, extWithPrecedingSeparator, new File(tmp())); tempFile.deleteOnExit(); return tempFile; } catch (IOException e) { throw Exceptions.propagate(e); } }
/** creates a temp dir which will be deleted on exit */ public static File newTempDir(String prefix) { String sanitizedPrefix = (prefix==null ? "" : Strings.makeValidFilename(prefix) + "-"); String tmpParent = tmp(); //With lots of stale temp dirs it is possible to have //name collisions so we need to retry until a unique //name is found for (int i = 0; i < TEMP_DIR_ATTEMPTS; i++) { String baseName = sanitizedPrefix + Identifiers.makeRandomId(4); File tempDir = new File(tmpParent, baseName); if (!tempDir.exists()) { if (tempDir.mkdir()) { Os.deleteOnExitRecursively(tempDir); return tempDir; } else { log.warn("Attempt to create temp dir failed " + tempDir + ". Either an IO error (disk full, no rights) or someone else created the folder after the !exists() check."); } } else { log.debug("Attempt to create temp dir failed, already exists " + tempDir + ". With ID of length 4 it is not unusual (15% chance) to have duplicate names at the 2000 samples mark."); } } throw new IllegalStateException("cannot create temporary folders in parent " + tmpParent + " after " + TEMP_DIR_ATTEMPTS + " attempts."); }
public ParsingResult<Csar> uploadSingleYaml(InputStream resourceFromUrl, String callerReferenceName) { try { String nameCleaned = Strings.makeValidFilename(callerReferenceName); File tmpBase = new File(tmpRoot, nameCleaned + "_" + Identifiers.makeRandomId(6)); File tmpExpanded = new File(tmpBase, nameCleaned + "_" + Identifiers.makeRandomId(6)); File tmpTarget = new File(tmpExpanded.toString() + ".csar.zip"); boolean created = tmpExpanded.mkdirs(); if (!created) { throw new Exception("Failed to create '" + tmpExpanded + "' when uploading yaml from " + nameCleaned); } FileUtils.copyInputStreamToFile(resourceFromUrl, new File(tmpExpanded, nameCleaned + ".yaml")); ArchiveBuilder.archive(tmpTarget.toString()).addDirContentsAt(tmpExpanded, "").create(); try { return uploadArchive(tmpTarget, callerReferenceName); } finally { Os.deleteRecursively(tmpBase); } } catch (Exception e) { throw Exceptions.propagate(e); } }
protected String getNodeName() { // (node name is needed so we can node delete it) // TODO would be better if CHEF_NODE_NAME were a freemarker template, could access entity.id, or hostname, etc, // in addition to supporting hard-coded node names (which is all we support so far). String nodeName = entity().getConfig(ChefConfig.CHEF_NODE_NAME); if (Strings.isNonBlank(nodeName)) return Strings.makeValidFilename(nodeName); // node name is taken from ID of this entity, if not specified return entity().getId(); }
protected String getNodeName() { // (node name is needed so we can node delete it) // TODO would be better if CHEF_NODE_NAME were a freemarker template, could access entity.id, or hostname, etc, // in addition to supporting hard-coded node names (which is all we support so far). String nodeName = entity().getConfig(ChefConfig.CHEF_NODE_NAME); if (Strings.isNonBlank(nodeName)) return Strings.makeValidFilename(nodeName); // node name is taken from ID of this entity, if not specified return entity().getId(); }
String safeXmlId = Strings.makeValidFilename(xmlId); if (!Objects.equal(id, safeXmlId)) LOG.warn("ID mismatch on "+type.toCamelCase()+", "+id+" from path, "+safeXmlId+" from xml");
public ParsingResult<Csar> uploadArchive(File zipFile, String callerReferenceName) { try { String nameCleaned = Strings.makeValidFilename(callerReferenceName); ParsingResult<Csar> result = archiveUploadService.upload(Paths.get(zipFile.toString())); if (ArchiveUploadService.hasError(result, null)) { LOG.debug("A4C parse notes for " + nameCleaned + ":\n " + Strings.join(result.getContext().getParsingErrors(), "\n ")); } if (ArchiveUploadService.hasError(result, ParsingErrorLevel.ERROR)) { // archive will not be installed in this case, so we should throw throw new UserFacingException("Could not parse " + callerReferenceName + " as TOSCA:\n " + Strings.join(result.getContext().getParsingErrors(), "\n ")); } return result; } catch (Exception e) { throw Exceptions.propagate("Error uploading archive " + callerReferenceName, e); } }
public ToolAbstractExecScript(Map<String,?> props) { this.props = props; this.separator = getOptionalVal(props, PROP_SEPARATOR); this.out = getOptionalVal(props, PROP_OUT_STREAM); this.err = getOptionalVal(props, PROP_ERR_STREAM); this.scriptDir = getOptionalVal(props, PROP_SCRIPT_DIR); this.runAsRoot = Boolean.TRUE.equals(getOptionalVal(props, PROP_RUN_AS_ROOT)); this.authSudo = Boolean.TRUE.equals(getOptionalVal(props, PROP_AUTH_SUDO)); this.noExtraOutput = Boolean.TRUE.equals(getOptionalVal(props, PROP_NO_EXTRA_OUTPUT)); this.noDeleteAfterExec = Boolean.TRUE.equals(getOptionalVal(props, PROP_NO_DELETE_SCRIPT)); this.password = getOptionalVal(props, PROP_PASSWORD); this.execTimeout = getOptionalVal(props, PROP_EXEC_TIMEOUT); String summary = getOptionalVal(props, PROP_SUMMARY); if (summary!=null) { summary = Strings.makeValidFilename(summary); if (summary.length()>30) summary = summary.substring(0,30); } this.scriptNameWithoutExtension = "brooklyn-"+ Time.makeDateStampString()+"-"+Identifiers.makeRandomId(4)+ (Strings.isBlank(summary) ? "" : "-"+summary); this.scriptPath = Os.mergePathsUnix(scriptDir, scriptNameWithoutExtension+".sh"); }
Urls.mergePaths(chefDirectory, "tmp-"+Strings.makeValidFilename(cookbookName)+"-"+Identifiers.makeRandomId(4));
Urls.mergePaths(chefDirectory, "tmp-"+Strings.makeValidFilename(cookbookName)+"-"+Identifiers.makeRandomId(4));