Example usage for org.apache.commons.vfs FileType FOLDER

List of usage examples for org.apache.commons.vfs FileType FOLDER

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileType FOLDER.

Prototype

FileType FOLDER

To view the source code for org.apache.commons.vfs FileType FOLDER.

Click Source Link

Document

A folder.

Usage

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>//from   w  w  w. ja  v  a2 s  .c om
 *  ? ?? ?  .
 * </p>
 * @param source
 *        <code>String</code>
 * @param target
 *        <code>String</code>
 * @throws Exception
 */
public static void cp(String source, String target) throws Exception {

    try {
        final FileObject src = manager.resolveFile(basefile, source);
        FileObject dest = manager.resolveFile(basefile, target);

        if (dest.exists() && dest.getType() == FileType.FOLDER) {
            dest = dest.resolveFile(src.getName().getBaseName());
        }

        dest.copyFrom(src, Selectors.SELECT_ALL);
    } catch (FileSystemException fse) {
        log.error(fse.toString());
        ;
        throw new FileSystemException(fse);
    }
}

From source file:com.adito.networkplaces.store.cifs.CIFSMount.java

public FileObject createVFSFileObject(String path,
        PasswordCredentials credentials/*, DAVTransaction transaction*/)
        throws IOException, DAVAuthenticationRequiredException {

    super.getStore().getName();

    URI uri = getRootVFSURI(SystemProperties.get("jcifs.encoding", "cp860"));

    try {/*ww w  . jav  a2s .  c o  m*/
        uri.setScheme("smb");
        if (credentials != null) {
            uri.setUserinfo(DAVUtilities.encodeURIUserInfo(credentials.getUsername()
                    + (credentials.getPassword() != null ? ":" + new String(credentials.getPassword()) : "")));
        }
        uri.setPath(uri.getPath() + (uri.getPath().endsWith("/") ? "" : "/")
                + DAVUtilities.encodePath(path, SystemProperties.get("jcifs.encoding", "cp860")));
        FileObject root = getStore().getRepository().getFileSystemManager().resolveFile(uri.toString());
        if (root.getType().equals(FileType.FOLDER)) {
            // Extra check so that the correct exception is thrown.
            root.getChildren();
        }
        return root;
    } catch (FileSystemException fse) {
        if (fse.getCause().getClass().getName().equals("jcifs.smb.SmbAuthException")) {
            throw new DAVAuthenticationRequiredException(getMountString());
        }
        if (fse.getCause() != null && fse.getCause() instanceof SmbException
                && ((SmbException) fse.getCause()).getRootCause() != null
                && "Connection timeout".equals(((SmbException) fse.getCause()).getRootCause().getMessage())) {
            throw new UnknownHostException(uri.getHost());
        }
        if (log.isDebugEnabled())
            log.debug("File system exception! ", fse);
        throw fse;
    }
}

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>/*from w  ww  .  j a  va2 s .  co  m*/
 *  ? ?? ?  ??.
 * </p>
 * @param source
 *        <code>String</code>
 * @param target
 *        <code>String</code>
 * @throws Exception
 */
public static void mv(String source, String target) throws Exception {

    try {
        final FileObject src = manager.resolveFile(basefile, source);
        FileObject dest = manager.resolveFile(basefile, target);

        if (dest.exists() && dest.getType() == FileType.FOLDER) {
            dest = dest.resolveFile(src.getName().getBaseName());
        }

        src.moveTo(dest);
    } catch (FileSystemException fse) {
        log.error(fse.toString());
        ;
        throw new FileSystemException(fse);
    }
}

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Copies a file or a complete subdirectory tree using Apache Commons VFS.
 * //from   w ww.  ja v  a  2s .  c  o m
 * @param manager
 *        The file system manager
 * @param file
 *        The source file or directory
 * @param folder
 *        The target folder
 * @throws IOException
 *         In case of an I/O error
 */
public static void copyRecursive(FileSystemManager manager, FileObject file, FileObject folder)
        throws IOException {
    folder.createFolder();
    FileType type = file.getType();
    FileObject target = manager.resolveFile(folder + "/" + file.getName().getBaseName());
    if (type == FileType.FILE)
        org.apache.commons.vfs.FileUtil.copyContent(file, target);
    else if (type == FileType.FOLDER) {
        for (FileObject child : file.getChildren())
            copyRecursive(manager, child, target);
    }
}

From source file:com.adito.networkplaces.model.FileSystemItem.java

public int compareTo(Object arg0) {
    FileSystemItem fsi = (FileSystemItem) arg0;
    int lessThan = -1;
    int moreThan = 1;
    if (!sortFoldersFirst) {
        lessThan = 1;/*  w w w.jav  a 2s  .c  o  m*/
        moreThan = -1;
    }
    if (fsi.getFileType().equals(FileType.FOLDER.getName())
            && this.getFileType().equals(FileType.FILE.getName())) {
        return moreThan;
    } else if (fsi.getFileType().equals(FileType.FILE.getName())
            && this.getFileType().equals(FileType.FOLDER.getName())) {
        return lessThan;
    } else {
        if (!sortCaseSensitive)
            return fileName.compareToIgnoreCase(fsi.getFileName());
        else
            return fileName.compareTo(fsi.getFileName());
    }
}

From source file:com.thinkberg.vfs.s3.jets3t.Jets3tFileObject.java

protected void doRename(FileObject targetFileObject) throws Exception {
    String bucketId = bucket.getName();
    S3Object targetObject = ((Jets3tFileObject) targetFileObject).object;

    LOG.debug(String.format("move object '%s' to '%s'", getS3Key(), targetObject.getKey()));

    // if this is a folder, then rename all children of the current folder too
    if (FileType.FOLDER.equals(getType())) {
        String path = object.getKey();
        // make sure we add a '/' slash at the end to find children
        if (!"".equals(path)) {
            path = path + "/";
        }//from  ww  w.  j  av a  2s .  c om

        try {
            S3Object[] children = service.listObjects(bucket, path, null);
            String targetName = targetObject.getKey();
            for (S3Object child : children) {
                String targetChildName = child.getKey();
                targetChildName = targetName + targetChildName.substring(object.getKey().length());
                service.renameObject(bucketId, child.getKey(), new S3Object(bucket, targetChildName));
            }
        } catch (S3ServiceException e) {
            throw new FileSystemException(String.format("can't move children of '%s' to '%s'", object.getKey(),
                    targetObject.getKey()), e);
        }
    }

    try {
        service.renameObject(bucket.getName(), object.getKey(), ((Jets3tFileObject) targetFileObject).object);
    } catch (S3ServiceException e) {
        throw new FileSystemException("can't rename  object", e);
    }
}

From source file:com.thinkberg.moxo.vfs.s3.jets3t.Jets3tFileObject.java

protected FileType doGetType() throws Exception {
    if (null == object.getContentType()) {
        return FileType.IMAGINARY;
    }//from   ww w. j  a  v a2 s .  co  m

    String contentType = object.getContentType();
    if ("".equals(object.getKey()) || Mimetypes.MIMETYPE_JETS3T_DIRECTORY.equals(contentType)) {
        return FileType.FOLDER;
    }

    return FileType.FILE;
}

From source file:com.github.stephenc.javaisotools.iso9660.impl.CreateISOTest.java

@Test
public void canCreateAnIsoWithOneFile() throws Exception {
    final String contentString = "This is a test file";
    // Output file
    File outfile = new File(workDir, "one-file.iso");
    File contents = new File(workDir, "readme.txt");
    OutputStream os = new FileOutputStream(contents);
    IOUtil.copy(contentString, os);//  w w w  . j  av a 2 s. c o  m
    IOUtil.close(os);

    // Directory hierarchy, starting from the root
    ISO9660RootDirectory.MOVED_DIRECTORIES_STORE_NAME = "rr_moved";
    ISO9660RootDirectory root = new ISO9660RootDirectory();

    root.addFile(contents);

    StreamHandler streamHandler = new ISOImageFileHandler(outfile);
    CreateISO iso = new CreateISO(streamHandler, root);
    ISO9660Config iso9660Config = new ISO9660Config();
    iso9660Config.allowASCII(false);
    iso9660Config.setInterchangeLevel(1);
    iso9660Config.restrictDirDepthTo8(true);
    iso9660Config.setVolumeID("ISO Test");
    iso9660Config.forceDotDelimiter(true);
    RockRidgeConfig rrConfig = new RockRidgeConfig();
    rrConfig.setMkisofsCompatibility(false);
    rrConfig.hideMovedDirectoriesStore(true);
    rrConfig.forcePortableFilenameCharacterSet(true);

    JolietConfig jolietConfig = new JolietConfig();
    jolietConfig.setVolumeID("Joliet Test");
    jolietConfig.forceDotDelimiter(true);

    iso.process(iso9660Config, rrConfig, jolietConfig, null);

    assertThat(outfile.isFile(), is(true));
    assertThat(outfile.length(), not(is(0L)));

    FileSystemManager fsManager = VFS.getManager();
    // TODO figure out why we can't just do
    // FileObject isoFile = fsManager.resolveFile("iso:" + outfile.getPath() + "!/");
    // smells like a bug between loop-fs and commons-vfs
    FileObject isoFile = fsManager.resolveFile("iso:" + outfile.getPath() + "!/readme.txt").getParent();
    assertThat(isoFile.getType(), is(FileType.FOLDER));

    FileObject[] children = isoFile.getChildren();
    assertThat(children.length, is(1));
    assertThat(children[0].getName().getBaseName(), is("readme.txt"));
    assertThat(children[0].getType(), is(FileType.FILE));
    assertThat(IOUtil.toString(children[0].getContent().getInputStream()), is(contentString));
}

From source file:com.newatlanta.appengine.vfs.provider.GaeFileObject.java

private FileType getEntityFileType() {
    String typeName = (String) metadata.getProperty(FILETYPE);
    if (typeName != null) {
        if (typeName.equals(FileType.FILE.getName())) {
            return FileType.FILE;
        }/* w  w w.j av a  2 s . c  o  m*/
        if (typeName.equals(FileType.FOLDER.getName())) {
            return FileType.FOLDER;
        }
    }
    return FileType.IMAGINARY;
}

From source file:com.panet.imeta.core.fileinput.FileInputList.java

public static FileInputList createFileList(VariableSpace space, String[] fileName, String[] fileMask,
        String[] fileRequired, boolean[] includeSubdirs, FileTypeFilter[] fileTypeFilters) {
    FileInputList fileInputList = new FileInputList();

    // Replace possible environment variables...
    final String realfile[] = space.environmentSubstitute(fileName);
    final String realmask[] = space.environmentSubstitute(fileMask);

    for (int i = 0; i < realfile.length; i++) {
        final String onefile = realfile[i];
        final String onemask = realmask[i];

        final boolean onerequired = YES.equalsIgnoreCase(fileRequired[i]);
        final boolean subdirs = includeSubdirs[i];
        final FileTypeFilter filter = ((fileTypeFilters == null || fileTypeFilters[i] == null)
                ? FileTypeFilter.ONLY_FILES
                : fileTypeFilters[i]);/*w  w w  .  j a  v  a  2 s  .  c  o  m*/

        if (Const.isEmpty(onefile))
            continue;

        // 
        // If a wildcard is set we search for files
        //
        if (!Const.isEmpty(onemask)) {
            try {
                // Find all file names that match the wildcard in this directory
                //
                FileObject directoryFileObject = KettleVFS.getFileObject(onefile);
                if (directoryFileObject != null && directoryFileObject.getType() == FileType.FOLDER) // it's a directory
                {
                    FileObject[] fileObjects = directoryFileObject.findFiles(new AllFileSelector() {
                        public boolean traverseDescendents(FileSelectInfo info) {
                            return info.getDepth() == 0 || subdirs;
                        }

                        public boolean includeFile(FileSelectInfo info) {
                            // Never return the parent directory of a file list.
                            if (info.getDepth() == 0) {
                                return false;
                            }

                            FileObject fileObject = info.getFile();
                            try {
                                if (fileObject != null && filter.isFileTypeAllowed(fileObject.getType())) {
                                    String name = fileObject.getName().getBaseName();
                                    boolean matches = Pattern.matches(onemask, name);
                                    /*
                                    if (matches)
                                    {
                                        System.out.println("File match: URI: "+info.getFile()+", name="+name+", depth="+info.getDepth());
                                    }
                                    */
                                    return matches;
                                }
                                return false;
                            } catch (FileSystemException ex) {
                                // Upon error don't process the file.
                                return false;
                            }
                        }
                    });
                    if (fileObjects != null) {
                        for (int j = 0; j < fileObjects.length; j++) {
                            if (fileObjects[j].exists())
                                fileInputList.addFile(fileObjects[j]);
                        }
                    }
                    if (Const.isEmpty(fileObjects)) {
                        if (onerequired)
                            fileInputList.addNonAccessibleFile(directoryFileObject);
                    }

                    // Sort the list: quicksort, only for regular files
                    fileInputList.sortFiles();
                } else {
                    FileObject[] children = directoryFileObject.getChildren();
                    for (int j = 0; j < children.length; j++) {
                        // See if the wildcard (regexp) matches...
                        String name = children[j].getName().getBaseName();
                        if (Pattern.matches(onemask, name))
                            fileInputList.addFile(children[j]);
                    }
                    // We don't sort here, keep the order of the files in the archive.
                }
            } catch (Exception e) {
                LogWriter.getInstance().logError("FileInputList", Const.getStackTracker(e));
            }
        } else
        // A normal file...
        {
            try {
                FileObject fileObject = KettleVFS.getFileObject(onefile);
                if (fileObject.exists()) {
                    if (fileObject.isReadable()) {
                        fileInputList.addFile(fileObject);
                    } else {
                        if (onerequired)
                            fileInputList.addNonAccessibleFile(fileObject);
                    }
                } else {
                    if (onerequired)
                        fileInputList.addNonExistantFile(fileObject);
                }
            } catch (Exception e) {
                LogWriter.getInstance().logError("FileInputList", Const.getStackTracker(e));
            }
        }
    }

    return fileInputList;
}