Example usage for org.apache.commons.vfs2 FileObject getType

List of usage examples for org.apache.commons.vfs2 FileObject getType

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getType.

Prototype

FileType getType() throws FileSystemException;

Source Link

Document

Returns this file's type.

Usage

From source file:de.innovationgate.wgpublisher.design.fs.DesignFileDocument.java

public InputStream getFileData(String strFile) throws WGAPIException {
    try {/*w w  w. jav a2s  . c  o  m*/
        if (getType() != WGDocument.TYPE_FILECONTAINER) {
            return null;
        }
        FileObject file = getFileContainerFile(strFile);
        if (file == null) {
            return null;
        }
        if (file.getType().equals(FileType.FILE)) {
            return file.getContent().getInputStream();
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new WGBackendException("Exception reading container file data", e);
    }
}

From source file:de.innovationgate.wgpublisher.services.WGACoreServicesImpl.java

public void initializeOverlay(RemoteSession session, PluginInfo pluginInfo, String designFolder)
        throws WGAServiceException {

    StandardFileSystemManager fsManager = new StandardFileSystemManager();
    try {//from w w w  .j  ava2s . c  om
        fsManager.init();
        if (!isAdminServiceEnabled()) {
            throw new WGAServiceException("Administrative services are disabled");
        }

        if (!isAdminSession(session)) {
            throw new WGAServiceException("You need an administrative login to access this service.");
        }

        WGAPlugin basePlugin = _core.getPluginSet().getPluginsByInstallationKey()
                .get(pluginInfo.getInstallationKey());
        if (basePlugin == null) {
            throw new WGAServiceException(
                    "No plugin is installed with installation key " + pluginInfo.getInstallationKey());
        }
        WGDatabase pluginDB = _core.getContentdbs().get(basePlugin.buildDatabaseKey());
        if (pluginDB == null) {
            throw new WGAServiceException(
                    "Plugin database not connected for plugin " + basePlugin.getIdentification());
        }

        FileSystemDesignSource fsDesignSource = (FileSystemDesignSource) _core.getDesignManager()
                .getDesignSources().get(WGAConfiguration.UID_DESIGNSOURCE_FILESYSTEM);
        WGADesign overlayDesign = fsDesignSource.getDesign(designFolder);
        if (overlayDesign == null) {
            throw new WGAServiceException("File Directory Design '" + designFolder + "' does not exist");
        }

        String designFolderLocation = fsDesignSource.getDesignLocation(overlayDesign);

        FileObject designFolderObj = fsManager.resolveFile(designFolderLocation);
        if (!designFolderObj.exists() || !designFolderObj.getType().equals(FileType.FOLDER)) {
            throw new WGAServiceException(
                    "Design folder '" + designFolder + "' cannot be found or is no folder");
        }

        // Determine design encoding of folder to import to
        DesignDefinition designDef = null;
        FileObject designDefFile = designFolderObj.resolveFile("design.xml");
        if (designDefFile.exists()) {
            designDef = DesignDefinition.load(designDefFile);
        }

        CSConfig csConfig = null;
        FileObject csConfigFile = designFolderObj.resolveFile("files/system/csconfig.xml");
        if (csConfigFile.exists()) {
            csConfig = CSConfig.load(csConfigFile);
        }

        String designEncoding = FileSystemDesignManager.determineDesignEncoding(designDef, csConfig);

        // Trigger overlay init/uprade process
        OverlayStatus status = FileSystemDesignProvider.determineOverlayStatus(
                (FileSystemDesignProvider) pluginDB.getDesignProvider(), basePlugin.getPluginID(),
                designFolderObj, designEncoding, _core.getLog(), _core.getDesignFileValidator());
        FileSystemDesignProvider.upgradeOverlay((FileSystemDesignProvider) pluginDB.getDesignProvider(),
                basePlugin.getPluginID(), status, designFolderObj, designEncoding, _core.getLog(),
                _core.getDesignFileValidator());

        // Disconnect consumer apps
        WGADesignManager designManager = _core.getDesignManager();
        for (WGDatabase consumerDb : _core.getContentdbs().values()) {

            WGDesignProvider provider = consumerDb.getDesignProvider();
            if (provider instanceof OverlayDesignProvider) {
                OverlayDesignProvider overlayProvider = (OverlayDesignProvider) provider;
                if (overlayProvider.getOverlay() instanceof FileSystemDesignProvider) {
                    FileSystemDesignProvider fsProvider = (FileSystemDesignProvider) overlayProvider
                            .getOverlay();
                    if (fsProvider.getBaseFolder().equals(designFolderObj)) {
                        _core.removeContentDB(consumerDb.getDbReference());
                    }
                }
            }
        }

        // Run update content dbs to reconnect
        _core.updateContentDBs();

    } catch (Exception e) {
        if (e instanceof WGAServiceException) {
            throw (WGAServiceException) e;
        } else {
            throw new WGAServiceException("Overlay initialisation failed.", e);
        }
    } finally {
        fsManager.close();
    }

}

From source file:com.sludev.commons.vfs.simpleshell.SimpleShell.java

/**
 * Does a 'cat' command.//from w w w .j ava2  s.  c om
 * 
 * @param cmd
 * @throws SimpleShellException
 */
public void cat(String[] cmd) throws SimpleShellException {
    if (cmd.length < 2) {
        throw new SimpleShellException("USAGE: cat <path>");
    }

    // Locate the file
    FileObject file;
    try {
        file = mgr.resolveFile(cwd, cmd[1]);
    } catch (FileSystemException ex) {
        String errMsg = String.format("Error resolving file '%s'", cmd[1]);
        //log.error( errMsg, ex);
        throw new SimpleShellException(errMsg, ex);
    }

    try {
        if (file.exists() == false) {
            // Can't cat a file that doesn't exist
            System.out.println(String.format("File '%s' does not exist", cmd[1]));
            return;
        }
    } catch (FileSystemException ex) {
        String errMsg = String.format("Error exists() on file '%s'", cmd[1]);
        //log.error( errMsg, ex);
        throw new SimpleShellException(errMsg, ex);
    }

    try {
        if (file.getType() != FileType.FILE) {
            // Only cat files
            System.out.println(String.format("File '%s' is not an actual file.", cmd[1]));
            return;
        }
    } catch (FileSystemException ex) {
        String errMsg = String.format("Error getType() on file '%s'", cmd[1]);
        //log.error( errMsg, ex);
        throw new SimpleShellException(errMsg, ex);
    }

    try {
        // Dump the contents to System.out
        FileUtil.writeContent(file, System.out);
    } catch (IOException ex) {
        String errMsg = String.format("Error WriteContent() on file '%s'", cmd[1]);
        //log.error( errMsg, ex);
        throw new SimpleShellException(errMsg, ex);
    }

    System.out.println();
}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Refresh ftp cache./*from  ww w  .  j  a v  a 2  s .  com*/
 * 
 * @param fileObject the file object
 * @throws FileSystemException
 */
public void refreshFtpCache(FileObject fileObject) throws FileSystemException {

    if (fileObject == null) {
        return;
    }

    if (!(fileObject.getFileSystem() instanceof FtpFileSystem)) {
        return;
    }
    if (fileObject.exists()) {
        return;
    }
    FileObject fileObjectParent = fileObject.getParent();
    if (fileObjectParent == null) {
        return;
    }

    fileObjectParent.getType(); // force to attach : needed for force refresh
    fileObjectParent.refresh();

}

From source file:de.innovationgate.wgpublisher.services.WGACoreServicesImpl.java

private List<FSDesignResourceState> createStates(FileObject base, FileObject file)
        throws NoSuchAlgorithmException, IOException {
    List<FSDesignResourceState> states = new ArrayList<FSDesignResourceState>();
    FSDesignResourceState state = new FSDesignResourceState();
    String path = computeRelativePath(base, file);
    state.setPath(path);/*w  w  w. j  a va2s .  co  m*/
    states.add(state);
    if (file.getType() == FileType.FOLDER) {
        state.setType(FSDesignResourceState.TYPE_FOLDER);
        long lastModified = 0;
        for (FileObject child : file.getChildren()) {
            List<FSDesignResourceState> childStates = createStates(base, child);
            for (FSDesignResourceState childState : childStates) {
                states.add(childState);
                lastModified = Math.max(lastModified, childState.getLastmodified());
            }
        }
        state.setLastmodified(lastModified);
    } else {
        state.setType(FSDesignResourceState.TYPE_FILE);
        state.setLastmodified(file.getContent().getLastModifiedTime());
        state.setMd5sum(WGUtils.createMD5HEX(file.getContent().getInputStream()));
    }
    return states;
}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java

private List<ModuleFile> getFileContainerFiles(FileObject filesFolder, String prefix)
        throws FileSystemException {
    FileObject[] files = filesFolder.getChildren();
    List<ModuleFile> designFiles = new ArrayList<ModuleFile>();
    for (int i = 0; i < files.length; i++) {
        FileObject file = files[i];
        if (file.getType().equals(FileType.FOLDER) && isValidDesignFile(file)) {
            designFiles.add(new ModuleFile(file, prefix, "", WGDocument.TYPE_FILECONTAINER));
            String newPrefix = (prefix.trim().equals("") ? file.getName().getBaseName().toLowerCase()
                    : prefix + DIRECTORY_DIVIDER + file.getName().getBaseName().toLowerCase());
            designFiles.addAll(getFileContainerFiles(file, newPrefix));
        }/* www.  j ava 2s . c o  m*/
    }
    return designFiles;
}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Copy file./*from  ww  w . j  a  v  a2  s. c o m*/
 * 
 * @param from the from
 * @param to the to
 * 
 * @throws MotuException the motu exception
 */
public void copyFile(FileObject from, FileObject to) throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("copyFile(FileObject, FileObject) - entering");
    }

    try {
        if ((to.exists()) && (to.getType() == FileType.FOLDER)) {
            throw new MotuException(String.format(
                    "File copy from '%s' to '%s' is rejected: the destination already exists and is a folder. You were about to loose all of the content of '%s' ",
                    from.getName().toString(), to.getName().toString(), to.getName().toString()));
        }
        this.copyFrom(from, to, Selectors.SELECT_ALL);
    } catch (MotuException e) {
        LOG.error("copyFile(FileObject, FileObject)", e);

        throw e;
    } catch (Exception e) {
        LOG.error("copyFile(FileObject, FileObject)", e);

        // throw new MotuException(String.format("Unable to copy file '%s' to '%s'",
        // foSrc.getURL().toString(), foDest.getURL().toString()), e);
        throw new MotuException(String.format("Unable to copy file '%s' to '%s'", from.getName().toString(),
                to.getName().toString()), e);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("copyFile(FileObject, FileObject) - exiting");
    }
}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Delete the file repsented by the file parameter.
 * /*from   w w w  . j a  va2 s . c  om*/
 * @param file the file
 * 
 * @return true, if successful
 * @throws MotuException
 */
public boolean deleteFile(FileObject file) throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("deleteFile(FileObject) - entering");
    }

    boolean deleted = false;
    try {

        if (file.exists()) {
            if (file.getType() != FileType.FILE) {
                throw new MotuException(String.format("Delete file '%s' is rejected: it is a folder. ",
                        file.getName().toString()));
            }

            deleted = file.delete();
        }
    } catch (FileSystemException e) {
        LOG.error("deleteFile(FileObject)", e);

        // throw new MotuException(String.format("Unable to copy file '%s' to '%s'",
        // foSrc.getURL().toString(), foDest.getURL().toString()), e);
        throw new MotuException(String.format("Unable to delete '%s'", file.getName().toString()), e);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("deleteFile(FileObject) - exiting");
    }
    return deleted;
}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java

protected ModuleFile getFileContainerFile(String name) throws FileSystemException, WGDesignSyncException {

    FileObject folder = getFilesFolder();
    List<String> paths = WGUtils.deserializeCollection(name, ":");
    boolean found = false;

    for (String path : paths) {
        FileObject fileContainer = findMatchingChild(folder, path, Collections.singleton(""), true);
        if (fileContainer != null && fileContainer.getType().equals(FileType.FOLDER)
                && isValidDesignFile(fileContainer)) {
            found = true;/* w  w w.ja  v a  2s  . com*/
            folder = fileContainer;
        } else {
            found = false;
            break;
        }
    }

    if (found == true) {
        return new ModuleFile(folder, WGUtils.serializeCollection(paths.subList(0, paths.size() - 1), ":"), "",
                WGDocument.TYPE_FILECONTAINER);
    } else {
        return null;
    }

}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java

protected void fetchFileSystem(WGACore core) throws WGDesignSyncException, IOException {

    // Dont run if already fetched
    if (_fsResources != null) {
        return;//  ww  w. j  a va2  s.  c  o  m
    }

    FileObject baseFolder = null;

    try {
        baseFolder = _fsManager.resolveFile(_designPath);
        if (!baseFolder.exists() || !baseFolder.getType().equals(FileType.FOLDER)) {
            throw new WGDesignSyncException(
                    "Directory location " + _designPath + " either does not exist or is not directory");
        }
    } catch (FileSystemException e) {
        throw new WGDesignSyncException("Unparseable VFS URI: " + _designPath, e);
    }

    _fsResources = new FileSystemResources(baseFolder);
}