Example usage for java.nio.file Path normalize

List of usage examples for java.nio.file Path normalize

Introduction

In this page you can find the example usage for java.nio.file Path normalize.

Prototype

Path normalize();

Source Link

Document

Returns a path that is this path with redundant name elements eliminated.

Usage

From source file:io.github.kitarek.elasthttpd.plugins.consumers.file.mapper.UriToFileMapper.java

private String normalizeUrlRequestPath(String uriRequestPath) {
    String localRequestPath;//from  www . j av  a2s  . com
    try {
        localRequestPath = URLDecoder.decode(uriRequestPath, Charset.defaultCharset().displayName());
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("Cannot decode URI. Using original URI path for file", e);
        localRequestPath = uriRequestPath;
    }
    final Path p = Paths.get(localRequestPath);
    return p.normalize().toAbsolutePath().toString();
}

From source file:org.sonar.api.batch.fs.internal.DefaultInputDir.java

/**
 * For testing purpose. Will be automaticall set when dir is added to {@link DefaultFileSystem}
 *//*from  w w w  .j av a 2s.c  o  m*/
public DefaultInputDir setModuleBaseDir(Path moduleBaseDir) {
    this.moduleBaseDir = moduleBaseDir.normalize();
    return this;
}

From source file:misc.FileHandler.java

/**
 * Creates any non-existing parent directories for the given file name path.
 * /* w  ww. j a v a  2  s  . c om*/
 * @param fileName
 *            path to a file name. May include arbitrary many parent
 *            directories. May not be <code>null</code>.
 * @return <code>true</code>, if all parent directories were created
 *         successfully or if there were not any parent directories to
 *         create. Otherwise, <code>false</code> is returned.
 */
public static boolean makeParentDirs(Path fileName) {
    if (fileName == null) {
        throw new NullPointerException("fileName may not be null!");
    }

    boolean result = true;
    Path normalizedPath = fileName.normalize();
    Path parent = normalizedPath.getParent();

    if (parent != null) {
        result = parent.toFile().mkdirs();
    }

    return result;
}

From source file:org.apache.storm.daemon.logviewer.utils.LogFileDownloader.java

/**
 * Checks authorization for the log file and download.
 *
 * @param fileName file to download/*from  ww w  . j a  v a2 s.  c om*/
 * @param user username
 * @param isDaemon true if the file is a daemon log, false if the file is an worker log
 * @return a Response which lets browsers download that file.
 */
public Response downloadFile(String fileName, String user, boolean isDaemon) throws IOException {
    Path rootDir = isDaemon ? daemonLogRoot : logRoot;
    Path rawFile = rootDir.resolve(fileName);
    Path file = rawFile.toAbsolutePath().normalize();
    if (!file.startsWith(rootDir) || !rawFile.normalize().toString().equals(rawFile.toString())) {
        //Ensure filename doesn't contain ../ parts 
        return LogviewerResponseBuilder.buildResponsePageNotFound();
    }
    if (isDaemon && Paths.get(fileName).getNameCount() != 1) {
        //Prevent daemon log reads from pathing into worker logs
        return LogviewerResponseBuilder.buildResponsePageNotFound();
    }

    if (file.toFile().exists()) {
        if (isDaemon || resourceAuthorizer.isUserAllowedToAccessFile(user, fileName)) {
            fileDownloadSizeDistMb.update(Math.round((double) file.toFile().length() / FileUtils.ONE_MB));
            return LogviewerResponseBuilder.buildDownloadFile(file.toFile(), numFileDownloadExceptions);
        } else {
            return LogviewerResponseBuilder.buildResponseUnauthorizedUser(user);
        }
    } else {
        return LogviewerResponseBuilder.buildResponsePageNotFound();
    }
}

From source file:misc.FileHandler.java

/**
 * Returns whether the given folder is shared. A folder is shared, if a
 * group access bundle exists in the folder.
 * //from   w w  w .  j a  v  a  2 s  .  c  o m
 * @param prefix
 *            the path to the client's root directory. May not be
 *            <code>null</code>.
 * @param folder
 *            a folder path relative to prefix which may contain a group
 *            access bundle. May not be <code>null</code>.
 * @return <code>true</code>, if and only if the given folder is shared.
 *         <code>false</code>, otherwise.
 */
public static boolean isShared(Path prefix, Path folder) {
    if (prefix == null) {
        throw new NullPointerException("prefix may not be null!");
    }
    if (folder == null) {
        throw new NullPointerException("folder may not be null!");
    }

    return !"".equals(folder.normalize().toString().trim()) && (Files
            .exists(Paths.get(prefix.toString(), folder.toString(), AccessBundle.ACCESS_BUNDLE_FILENAME)));
}

From source file:edu.usc.goffish.gofs.namenode.DataNode.java

private DataNode(Path dataNodePath) throws IOException {
    _dataNodePath = dataNodePath.normalize().toAbsolutePath();
    _dataNodeSliceDirPath = _dataNodePath.resolve(DATANODE_SLICE_DIR);

    _config = new PropertiesConfiguration();
    _config.setDelimiterParsingDisabled(true);
    try {//w w  w  .j av a 2s . c  om
        _config.load(Files.newInputStream(_dataNodePath.resolve(DATANODE_CONFIG)));
    } catch (ConfigurationException e) {
        throw new IOException(e);
    }

    // ensure this is valid data node
    boolean installed = _config.getBoolean(DATANODE_INSTALLED_KEY);
    if (!installed) {
        throw new InvalidPropertiesFormatException(
                "data node config must contain key " + DATANODE_INSTALLED_KEY);
    }

    // retrieve localhost uri
    {
        String localhostString = _config.getString(DATANODE_LOCALHOSTURI_KEY);
        if (localhostString == null) {
            throw new InvalidPropertiesFormatException(
                    "data node config must contain key " + DATANODE_LOCALHOSTURI_KEY);
        }

        try {
            _localhostURI = new URI(localhostString);
        } catch (URISyntaxException e) {
            throw new InvalidPropertiesFormatException("data node config key " + DATANODE_LOCALHOSTURI_KEY
                    + " has invalid format - " + e.getMessage());
        }
    }

    // retrieve name node
    try {
        _nameNode = NameNodeProvider.loadNameNodeFromConfig(_config, DATANODE_NAMENODE_TYPE_KEY,
                DATANODE_NAMENODE_LOCATION_KEY);
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException("Unable to load name node", e);
    }

    // retrieve slice manager
    _sliceSerializer = _nameNode.getSerializer();
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiver.java

private Path getRelativeSymLinkTarget(File source, File baseDir) throws IOException {
    Path sourcePath = source.toPath();
    Path linkTarget = Files.readSymbolicLink(sourcePath);
    // link target may be relative, so we resolve it first
    Path resolvedLinkTarget = sourcePath.getParent().resolve(linkTarget);
    Path relative = baseDir.toPath().relativize(resolvedLinkTarget);
    Path normalizedSymLinkPath = relative.normalize();
    log.debug("Computed symlink target path " + slashify(normalizedSymLinkPath) + " for symlink " + source
            + " relative to " + baseDir);
    return normalizedSymLinkPath;
}

From source file:net.sf.jasperreports.repo.FileRepositoryService.java

protected Path rootNormalizedPath() {
    Path rootPath = rootNormalizedPath;
    if (rootPath == null) {
        Path path = Paths.get(root);
        rootPath = rootNormalizedPath = path.normalize();
    }//w  ww  .j av  a  2s .  co m
    return rootPath;
}

From source file:edu.usc.goffish.gofs.util.partitioning.metis.MetisPartitioner.java

public MetisPartitioner(Path metisBinaryPath, String[] extraMetisOptions) {
    if (metisBinaryPath == null) {
        throw new IllegalArgumentException();
    }/*  w  w  w.j a v a 2  s  .co  m*/

    _metisBinaryPath = metisBinaryPath.normalize();
    _extraMetisOptions = extraMetisOptions;
}

From source file:neembuu.uploader.zip.generator.NUZipFileGenerator.java

private Class load(Path f, Path uploadersDirectory) throws Exception {
    String p = uploadersDirectory.normalize().relativize(f.normalize()).toString();
    p = p.replace(File.separatorChar, '.');
    p = p.substring(0, p.lastIndexOf('.'));//to remove .class
    return classLoader.loadClass(p);
}