Example usage for org.apache.commons.io FilenameUtils normalize

List of usage examples for org.apache.commons.io FilenameUtils normalize

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils normalize.

Prototype

public static String normalize(String filename) 

Source Link

Document

Normalizes a path, removing double and single dot path steps.

Usage

From source file:pt.webdetails.cpf.utils.PluginUtils.java

/**
 * Calls out for resources in the plugin, on the specified path
 *
 * @param elementPath Relative to the plugin directory
 * @param recursive Do we want to enable recursivity?
 * @param pattern regular expression to filter the files
 * @return Files found/*from w  w w .j  a  v a 2 s . co m*/
 */
@Override
public Collection<File> getPluginResources(String elementPath, Boolean recursive, String pattern) {

    IOFileFilter fileFilter = TrueFileFilter.TRUE;

    if (pattern != null && !pattern.equals("")) {
        fileFilter = new RegexFileFilter(pattern);
    }

    IOFileFilter dirFilter = recursive.equals(Boolean.TRUE) ? TrueFileFilter.TRUE : null;
    // TODO: why doesn't this use repository access?
    // Get directory name. We need to make sure we're not allowing this to fetch other resources
    String basePath = null;
    String elementFullPath = null;

    try {
        basePath = URLDecoder.decode(FilenameUtils.normalize(getPluginDirectory().getAbsolutePath()),
                CharsetHelper.getEncoding());
        elementFullPath = URLDecoder.decode(FilenameUtils.normalize(basePath + File.separator + elementPath),
                CharsetHelper.getEncoding());
    } catch (UnsupportedEncodingException e) {
        //CharseHelper.getEncoding() returns a valid encoding
    }

    if (!elementFullPath.startsWith(basePath)) {
        logger.warn("PluginUtils.getPluginResources is trying to access a parent path - denied : "
                + elementFullPath);
        return null;
    }

    File dir = new File(elementFullPath);
    if (!dir.exists() || !dir.isDirectory()) {
        return null;
    }

    return FileUtils.listFiles(dir, fileFilter, dirFilter);

}

From source file:pt.webdetails.cpf.utils.PluginUtils.java

/**
 * From a full path, returns the relative path
 *
 * @param fullPath//from  w ww.  jav a2s .  c  o  m
 * @param includePluginDir
 * @return The relative path
 */
@Override
public String getPluginRelativeDirectory(String fullPath, boolean includePluginDir)
        throws FileNotFoundException {
    // Get directory name. We need to make sure we're not allowing this to fetch other resources
    File pluginDir = getPluginDirectory();
    if (includePluginDir) {
        pluginDir = pluginDir.getParentFile();
    }
    String basePath = null;
    String elementFullPath = null;
    try {
        basePath = URLDecoder.decode(FilenameUtils.normalize(pluginDir.getAbsolutePath()),
                CharsetHelper.getEncoding());
        elementFullPath = URLDecoder.decode(FilenameUtils.getFullPath(FilenameUtils.normalize(fullPath)),
                CharsetHelper.getEncoding());
    } catch (UnsupportedEncodingException e) {
        //CharseHelper.getEncoding() returns a valid encoding
    }

    if (elementFullPath.indexOf(basePath) < 0) {
        throw new FileNotFoundException("Can't extract relative path from file " + fullPath);
    }

    return elementFullPath.substring(basePath.length());

}

From source file:pt.webdetails.cpk.testUtils.PluginUtilsForTesting.java

/**
 * Calls out for resources in the plugin, on the specified path
 *
 * @param elementPath Relative to the plugin directory
 * @param recursive   Do we want to enable recursivity?
 * @param pattern     regular expression to filter the files
 * @return Files found//from w w w  .  ja  v  a  2s. c  o m
 */
@Override
public Collection<File> getPluginResources(String elementPath, Boolean recursive, String pattern) {

    IOFileFilter fileFilter = TrueFileFilter.TRUE;

    if (pattern != null && !pattern.equals("")) {
        fileFilter = new RegexFileFilter(pattern);

    }

    IOFileFilter dirFilter = recursive.equals(Boolean.TRUE) ? TrueFileFilter.TRUE : null;

    // Get directory name. We need to make sure we're not allowing this to fetch other resources
    String basePath = FilenameUtils.normalize(getPluginDirectory().getAbsolutePath());
    String elementFullPath = FilenameUtils.normalize(basePath + File.separator + elementPath);

    if (!elementFullPath.startsWith(basePath)) {
        logger.warn("PluginUtils.getPluginResources is trying to access a parent path - denied : "
                + elementFullPath);
        return null;
    }

    File dir = new File(elementFullPath);
    if (!dir.exists() || !dir.isDirectory()) {
        return null;
    }

    return FileUtils.listFiles(dir, fileFilter, dirFilter);

}

From source file:pt.webdetails.cpk.testUtils.PluginUtilsForTesting.java

/**
 * From a full path, returns the relative path
 *
 * @param fullPath// www.  j a  v  a  2 s  .c om
 * @param includePluginDir
 * @return The relative path
 */
@Override
public String getPluginRelativeDirectory(String fullPath, boolean includePluginDir)
        throws FileNotFoundException {

    // Get directory name. We need to make sure we're not allowing this to fetch other resources
    File pluginDir = getPluginDirectory();
    if (includePluginDir) {
        pluginDir = pluginDir.getParentFile();
    }

    String basePath = FilenameUtils.normalize(pluginDir.getAbsolutePath());
    String elementFullPath = FilenameUtils.getFullPath(FilenameUtils.normalize(fullPath));

    if (elementFullPath.indexOf(basePath) < 0) {
        throw new FileNotFoundException("Can't extract relative path from file " + fullPath);
    }

    return elementFullPath.substring(basePath.length());

}

From source file:ro.ieugen.fileserver.http.HttpStaticFileServerHandler.java

protected String sanitizeUri(String uri) {
    // Decode the path.
    try {//from  w w w. j a  v a 2 s  .c o  m
        uri = URLDecoder.decode(uri, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        try {
            uri = URLDecoder.decode(uri, "ISO-8859-1");
        } catch (UnsupportedEncodingException e1) {
            throw new Error();
        }
    }
    String normalizedRelativePath = FilenameUtils.normalize(uri);
    // Convert to absolute path.
    return new File(new File(rootUri), normalizedRelativePath).getAbsolutePath();
}

From source file:se.nbis.sftpsquid.SftpSquid.java

/**
 * Remove superflous parts of the path, such as double /
 *
 * @param String path to normalize// w  w w  .j  a v  a  2 s . c o  m
 * @return String
 */
private String normalizePath(String path) {
    String normalized = FilenameUtils.normalize(path);
    return FilenameUtils.separatorsToUnix(normalized); // In case we run on windows
}

From source file:webhooks.core.services.impl.DocumentServiceImp.java

/**
 *   return all documents/folder from published paths
 * @param max//  w  w  w.  j  a v a2s . c o  m
 * @param offset
 * @return
 */
@Override
public DocumentList getRootFiles(int max, int offset) {
    List<Document> finalList = new ArrayList<Document>();

    DocumentList ret = null;
    int oldMax = max;

    for (String path : ConfigBean.getInstance().getPaths()) {
        try {
            String normalize = FilenameUtils.normalize(path);
            if (normalize.startsWith("~/")) {
                normalize = System.getProperty("user.home") + normalize.substring(1);
            } else if (normalize.startsWith("~")) {
                normalize = System.getProperty("user.home") + File.separator + normalize.substring(1);
            }

            ret = listFilesForFolder(new File(normalize), max, offset);
            if (ret.getDocuments().size() > 0) {
                finalList.addAll(finalList.size(), ret.getDocuments());
                offset = 0;
                max = max - ret.getDocuments().size();
            }

            if (finalList.size() == oldMax)
                break;

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return new DocumentList(finalList);
}

From source file:webhooks.core.services.impl.DocumentServiceImp.java

@Override
public DocumentList findFiles(String query, int max, int offset) {
    List<Document> finalList = new ArrayList<Document>();

    List<Document> ret = null;
    int oldMax = max;

    for (String path : ConfigBean.getInstance().getPaths()) {
        try {/*from w w  w  .ja va  2  s .  c  o  m*/
            String normalize = FilenameUtils.normalize(path);
            if (normalize.startsWith("~/")) {
                normalize = System.getProperty("user.home") + normalize.substring(1);
            } else if (normalize.startsWith("~")) {
                normalize = System.getProperty("user.home") + File.separator + normalize.substring(1);
            }

            ret = traverse(normalize, query, max, offset);
            if (ret.size() > 0) {
                finalList.addAll(finalList.size(), ret);
                offset = 0;
                max = max - ret.size();
            }

            if (finalList.size() == oldMax)
                break;

        } catch (Exception e) {

        }
    }

    return new DocumentList(finalList);
}