List of usage examples for java.nio.file Path normalize
Path normalize();
From source file:org.niord.core.batch.BatchService.java
/** * Computes the absolute path to the given local path within the repository * * @param localPath the local path withing the batch job repository * @return the absolute path to the given local path within the repository *///from w w w. j a v a 2s . c om public Path computeBatchJobPath(Path localPath) { Path path = batchJobRoot.normalize().resolve(localPath); // Check that it is a valid path within the batch job repository root path = path.normalize(); if (!path.startsWith(batchJobRoot)) { throw new RuntimeException("Invalid path " + localPath); } return path; }
From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryHelper.java
public String getGitPath(String path) { Path gitPath = Paths.get(path); gitPath = gitPath.normalize(); try {/* w w w. ja v a 2s. co m*/ gitPath = Paths.get(FILE_SEPARATOR).relativize(gitPath); } catch (IllegalArgumentException e) { logger.debug("Path: " + path + " is already relative path."); } if (StringUtils.isEmpty(gitPath.toString())) { return "."; } String toRet = gitPath.toString(); toRet = FilenameUtils.separatorsToUnix(toRet); return toRet; }
From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryHelper.java
/** * Perform an initial commit after large changes to a site. Will not work against the global config repo. * @param site/* ww w . j a va 2s . c om*/ * @param message * @return true if successful, false otherwise */ public boolean performInitialCommit(String site, String message, String sandboxBranch) { boolean toReturn = true; Repository repo = getRepository(site, GitRepositories.SANDBOX, sandboxBranch); try (Git git = new Git(repo)) { Status status = git.status().call(); if (status.hasUncommittedChanges() || !status.isClean()) { DirCache dirCache = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS).call(); RevCommit commit = git.commit().setMessage(message).call(); // TODO: SJ: Do we need the commit id? // commitId = commit.getName(); } checkoutSandboxBranch(site, repo, sandboxBranch); // Create Published by cloning Sandbox // Build a path for the site/sandbox Path siteSandboxPath = buildRepoPath(GitRepositories.SANDBOX, site); // Built a path for the site/published Path sitePublishedPath = buildRepoPath(GitRepositories.PUBLISHED, site); try (Git publishedGit = Git.cloneRepository() .setURI(sitePublishedPath.relativize(siteSandboxPath).toString()) .setDirectory(sitePublishedPath.normalize().toAbsolutePath().toFile()).call()) { Repository publishedRepo = publishedGit.getRepository(); publishedRepo = optimizeRepository(publishedRepo); checkoutSandboxBranch(site, publishedRepo, sandboxBranch); publishedRepo.close(); publishedGit.close(); } catch (GitAPIException | IOException e) { logger.error("Error adding origin (sandbox) to published repository", e); } git.close(); } catch (GitAPIException err) { logger.error("error creating initial commit for site: " + site, err); toReturn = false; } return toReturn; }
From source file:com.qspin.qtaste.testsuite.impl.JythonTestScript.java
private void dumpScriptPythonStackDetails(TestResult result, Throwable error) { StringBuilder stackTrace = new StringBuilder(); // get stacktrace from PyException traceback, because easier and line number is not always correct in Java stack trace if (error instanceof PyException) { List<PyFrame> stacktrace = new ArrayList<>(); PyTraceback previousTraceback = null; PyTraceback traceback = ((PyException) error).traceback; while (traceback != null && traceback != previousTraceback) { PyFrame frame = traceback.tb_frame; String fileName;/*w w w . ja v a2 s .c om*/ String function; if (frame != null && frame.f_code != null && (fileName = frame.f_code.co_filename) != null && (function = frame.f_code.co_name) != null) { // skip execfile() call in the embedded jython, doStep() and doSteps() functions, // private __invokexxx() methods of the __TestAPIWrapper class, // private __checkPresent() method of a test API wrapper class, // user_line() method of the __ScriptDebugger class and a function of the debugger if ((!fileName.equals("embedded_jython") || (!function.equals("<module>") && !function.equals("doStep") && !function.equals("doSteps") && !function.startsWith("_TestAPIWrapper__invoke") && !function.endsWith("__checkPresent") && !function.equals("user_line"))) && !fileName.endsWith(File.separator + "bdb.py")) { stacktrace.add(frame); } } previousTraceback = traceback; traceback = (PyTraceback) traceback.tb_next; } // extract all necessary details from stacktrace from last frame to first one boolean stackLastDataExtracted = false; ListIterator<PyFrame> frameIterator = stacktrace.listIterator(stacktrace.size()); while (frameIterator.hasPrevious()) { PyFrame frame = frameIterator.previous(); String fileName = frame.f_code.co_filename; String function = frame.f_code.co_name; int lineNumber = frame.f_lineno; // convert absolute path to relative path Path filePath = Paths.get(fileName); if (filePath.isAbsolute()) { filePath = Paths.get("").toAbsolutePath().relativize(filePath); } // normalize path fileName = filePath.normalize().toString(); if (function.equals("<module>")) { stackTrace.append("at file ").append(fileName).append(" line ").append(lineNumber).append('\n'); } else if (!function.equals("importTestScript")) { stackTrace.append("function ").append(function); if (!fileName.equals("embedded_jython") && !fileName.equals("JythonTestScript.java")) { stackTrace.append(" at file ").append(fileName).append(" line ").append(lineNumber); } stackTrace.append('\n'); } if (!stackLastDataExtracted && !fileName.equals("embedded_jython") && !fileName.equals("JythonTestScript.java")) { stackLastDataExtracted = true; result.setFailedLineNumber(lineNumber); result.setFailedFunctionId(function); } result.addStackTraceElement(new StackTraceElement("", function, fileName, lineNumber)); } } result.setStackTrace(stackTrace.toString().trim()); }
From source file:org.wrml.runtime.service.file.FileSystemService.java
private Path getKeyLinkPath(final URI keyedSchemaUri, final Object keyValue) { final Path rootDirectoryPath = getRootDirectoryPath(); Path path = rootDirectoryPath.resolve(StringUtils.stripStart(keyedSchemaUri.getPath(), "/")); path = path.resolve(STATIC_PATH_SEGMENT_KEYS); String keyValueString = null; if (keyValue instanceof URI) { final URI uri = (URI) keyValue; final String host = uri.getHost(); if (host == null || host.trim().isEmpty()) { return null; }/* www. j av a 2s . co m*/ path = path.resolve(host); final int port = (uri.getPort() == -1) ? 80 : uri.getPort(); path = path.resolve(String.valueOf(port)); keyValueString = StringUtils.stripStart(uri.getPath(), "/"); } else { final Context context = getContext(); keyValueString = context.getSyntaxLoader().formatSyntaxValue(keyValue); } if (keyValueString == null || keyValueString.equals("null")) { return null; } if (keyValueString.trim().isEmpty() || keyValueString.endsWith("/")) { keyValueString = "index"; } if (!keyValueString.endsWith(getFileExtension())) { keyValueString += getFileExtension(); } path = path.resolve(keyValueString); return path.normalize(); }
From source file:org.wso2.carbon.appmgt.impl.utils.AppManagerUtil.java
/** * Resolve file path avoiding Potential Path Traversals * * @param baseDirPath base directory file path * @param fileName filename/*from ww w.j a v a2 s. c o m*/ * @return */ public static String resolvePath(String baseDirPath, String fileName) { final Path basePath = Paths.get(baseDirPath); final Path filePath = Paths.get(fileName); if (!basePath.isAbsolute()) { throw new IllegalArgumentException("Base directory path '" + baseDirPath + "' must be absolute"); } if (filePath.isAbsolute()) { throw new IllegalArgumentException( "Invalid file name '" + fileName + "' with an absolute file path is provided"); } // Join the two paths together, then normalize so that any ".." elements final Path resolvedPath = basePath.resolve(filePath).normalize(); // Make sure the resulting path is still within the required directory. if (!resolvedPath.startsWith(basePath.normalize())) { throw new IllegalArgumentException("File '" + fileName + "' is not within the required directory."); } return String.valueOf(resolvedPath); }