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:org.panbox.desktop.common.vfs.backend.VirtualRootMultiuserVolume.java

public synchronized SecretKey getShareKeyForFile(String fileName, int version)
        throws SecretKeyNotFoundException {
    String normalizedFileName = FilenameUtils.normalize(fileName);
    for (VirtualMultiuserRootFile f : rootFilesPerUser.values()) {
        for (VFSShare share : f.getShares()) {
            if (share.contains(normalizedFileName.substring(normalizedFileName.indexOf(File.separator, 1)))) {
                return share.getShareKey(version);
            }/*ww  w  . ja v  a  2 s  .  co  m*/
        }
    }
    throw new SecretKeyNotFoundException(
            "Could not obtain a share key for the specified file '" + normalizedFileName + "'.");
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootMultiuserVolume.java

public synchronized ShareKey getLatestShareKeyForFile(String fileName) throws SecretKeyNotFoundException {
    String normalizedFileName = FilenameUtils.normalize(fileName);
    for (VirtualMultiuserRootFile f : rootFilesPerUser.values()) {
        for (VFSShare share : f.getShares()) {
            if (share.contains(normalizedFileName.substring(normalizedFileName.indexOf(File.separator, 1)))) {
                return share.getLatestShareKey();
            }/*  w  w w .j  av a 2  s.com*/
        }
    }
    throw new SecretKeyNotFoundException(
            "Could not obtain a share key for the specified file '" + normalizedFileName + "'.");
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootMultiuserVolume.java

/**
 * Gets a VirtualFile instance of the file that has been specified in the
 * fileName parameter. This will iterate over all shares and check which
 * share contains this file. If the share notifies, that it contains the
 * file, a getFile-call will be placed on the share in order to forward to
 * request to the share itself.//from  w w  w.  java 2 s  .  c  om
 * 
 * @param fileName
 *            Relative path of the file.
 * @return VirtualFile instance of the specified file.
 * @throws IOException
 */
public synchronized VirtualFile getFile(final String fileName) throws IOException {
    String normalizedFileName = FilenameUtils.normalize(fileName);
    if (normalizedFileName.equals(File.separator)) {
        return rootFile;
    }
    if (rootFilesPerUser.get(normalizedFileName.substring(1)) != null) {
        // this is a user request!
        return rootFilesPerUser.get(normalizedFileName.substring(1));
    } else {
        // ask the shares!
        for (VirtualMultiuserRootFile f : rootFilesPerUser.values()) {
            for (VFSShare share : f.getShares()) {
                if (share.contains(normalizedFileName)) {
                    return share.getFile(normalizedFileName);
                }
            }
        }
    }
    throw new FileNotFoundException(
            "Could not get the VirtualFile for the specified file '" + normalizedFileName + "'.");
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootVolume.java

@Override
public SecretKey getObfuscationKeyForFile(String fileName) throws SecretKeyNotFoundException {
    String normalizedFileName = FilenameUtils.normalize(fileName);
    for (VFSShare share : shares) {
        if (share.contains(normalizedFileName)) {
            return share.getObfuscationKey();
        }/*w  w  w  . j  a  va2  s  .  c  o m*/
    }
    throw new SecretKeyNotFoundException(
            "Could not obtain a share key for the specified file '" + normalizedFileName + "'.");
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootVolume.java

@Override
public Obfuscator getObfuscator(String fileName) throws SecretKeyNotFoundException {
    String normalizedFileName = FilenameUtils.normalize(fileName);
    for (VFSShare share : shares) {
        if (share.contains(normalizedFileName)) {
            return share.getObfuscator();
        }/* w  ww. j  av  a2s  . co m*/
    }
    return null;
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootVolume.java

@Override
public SecretKey getShareKeyForFile(String fileName, int version) throws SecretKeyNotFoundException {
    String normalizedFileName = FilenameUtils.normalize(fileName);
    for (VFSShare share : shares) {
        if (share.contains(normalizedFileName)) {
            return share.getShareKey(version);
        }/*w w  w . jav  a  2 s  . c o m*/
    }
    throw new SecretKeyNotFoundException(
            "Could not obtain a share key for the specified file '" + normalizedFileName + "'.");
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootVolume.java

@Override
public ShareKey getLatestShareKeyForFile(String fileName) throws SecretKeyNotFoundException {
    String normalizedFileName = FilenameUtils.normalize(fileName);
    for (VFSShare share : shares) {
        if (share.contains(normalizedFileName)) {
            return share.getLatestShareKey();
        }// ww  w .j  a v  a 2  s  .co m
    }
    throw new SecretKeyNotFoundException(
            "Could not obtain a share key for the specified file '" + normalizedFileName + "'.");
}

From source file:org.panbox.desktop.common.vfs.backend.VirtualRootVolume.java

@Override
public VirtualFile getFile(final String fileName) throws IOException {
    String normalizedFileName = FilenameUtils.normalize(fileName);
    if (normalizedFileName.equals(File.separator)) {
        return rootFile;
    }//from  w w  w . j a va2s  .  c o  m
    for (VFSShare share : shares) {
        if (share.contains(normalizedFileName)) {
            return share.getFile(normalizedFileName);
        }
    }
    throw new FileNotFoundException(
            "Could not get the VirtualFile for the specified file '" + normalizedFileName + "'.");
}

From source file:org.panbox.desktop.common.vfs.PanboxFSLinux.java

public synchronized void symlink(final String target, final String link) throws IOException {

    try {/*from www. j  av  a 2s.c o m*/
        // TODO: Here, we parse 3 times for the Share that manages the File
        VirtualFile vlink = getVirtualFileForFileName(link, true);
        String fullpath = FilenameUtils.getFullPath(link);
        String sharePath = FilenameUtils.normalize(fullpath.substring(0, fullpath.indexOf('/', 1) + 1));

        String shareloc = FilenameUtils.concat(fullpath, target);

        if (FilenameUtils.equals(sharePath, shareloc) || FilenameUtils.directoryContains(sharePath, shareloc)) {

            SecretKey sk = backingStorage.getObfuscationKeyForFile(shareloc);
            Obfuscator obfuscator = backingStorage.getObfuscator(shareloc);

            // create obfuscated symlink target
            String[] targetparts = target.split("/");
            StringBuffer res = new StringBuffer();

            if (target.startsWith(File.separator)) {
                res.append(File.separator);
            }

            for (int i = 0; i < targetparts.length; i++) {
                String cur = targetparts[i];
                if (cur.equals(".") || cur.equals("..")) {
                    res.append(cur);
                } else {
                    // append obfuscated part of path
                    res.append(obfuscator.obfuscate(cur, sk, true));
                }
                // append intermediary separators
                if (i < targetparts.length - 1) {
                    res.append(File.separator);
                }
            }

            if (target.endsWith(File.separator)) {
                res.append(File.separator);
            }

            String obfuscatedTarget = res.toString();
            Path ptarget = Paths.get(obfuscatedTarget);

            Path plink = Paths.get(vlink.getFileName());
            Files.createSymbolicLink(plink, ptarget);
            logger.debug("symlink, Target : " + obfuscatedTarget + ", Link: " + vlink.getFileName());
        } else {
            throw new IOException("Symlinks outside of shares are not supported.");
        }
    } catch (ObfuscationException e) {
        // logger.error("Could not obfuscate symlink target!", e);
        throw new IOException("Could not obfuscate symlink target!", e);
    }

}

From source file:org.panbox.desktop.common.vfs.PanboxFSLinux.java

public synchronized void readlink(final String path, final ByteBuffer buffer, final long size)
        throws IOException {

    try {/*from   ww w .  ja v  a  2s .c om*/
        // TODO: Here, we parse 3 times for the Share that manages the File
        String fullpath = FilenameUtils.getFullPath(path);
        String sharePath = FilenameUtils.normalize(fullpath.substring(0, fullpath.indexOf('/', 1) + 1));

        VirtualFile vpath = getVirtualFileForFileName(path);
        String target = Files.readSymbolicLink(vpath.getFile().toPath()).toString();

        String shareloc = FilenameUtils.concat(fullpath, target.toString());

        if (FilenameUtils.directoryContains(sharePath, shareloc) || FilenameUtils.equals(sharePath, shareloc)) {

            SecretKey sk = backingStorage.getObfuscationKeyForFile(shareloc);
            Obfuscator obfuscator = backingStorage.getObfuscator(shareloc);

            // create deobfuscated symlink target
            String[] targetparts = target.split("/");
            StringBuffer res = new StringBuffer();

            if (target.startsWith(File.separator)) {
                res.append(File.separator);
            }

            for (int i = 0; i < targetparts.length; i++) {
                String cur = targetparts[i];
                if (cur.equals(".") || cur.equals("..")) {
                    res.append(cur);
                } else {
                    // append obfuscated part of path
                    res.append(obfuscator.deObfuscate(cur, sk));
                }
                // append intermediary separators
                if (i < targetparts.length - 1) {
                    res.append(File.separator);
                }
            }

            if (target.endsWith(File.separator)) {
                res.append(File.separator);
            }

            byte[] ret = res.toString().getBytes();
            int realsize = Math.min(ret.length, (int) size);
            buffer.put(ret, 0, realsize);
            logger.debug("readline, Link : " + path + ", Target: " + res.toString());
        } else {
            throw new IOException("Symlinks outside of shares are not supported.");
        }
    } catch (ObfuscationException e) {
        throw new IOException("Error deobfuscating symlink contents!", e);
    }
}