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

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

Introduction

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

Prototype

boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:org.fuin.vfs2.filter.SizeFileFilter.java

/**
 * Checks to see if the size of the file is favorable.
 * <p>/*  ww  w . jav a2 s .  c o m*/
 * If size equals threshold and smaller files are required, file <b>IS
 * NOT</b> selected. If size equals threshold and larger files are required,
 * file <b>IS</b> selected.
 * <p>
 * Non-existing files return always false (will never be accepted).
 * 
 * @param fileInfo
 *            the File to check
 * 
 * @return true if the filename matches
 */
@Override
public boolean accept(final FileSelectInfo fileInfo) {
    try {
        final FileObject file = fileInfo.getFile();
        if (!file.exists()) {
            return false;
        }
        final FileContent content = file.getContent();
        try {
            final long length = content.getSize();
            final boolean smaller = length < size;
            return acceptLarger ? !smaller : smaller;
        } finally {
            content.close();
        }
    } catch (final FileSystemException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.jahia.modules.external.modules.ModulesDataSource.java

private void saveProperties(ExternalData data) throws RepositoryException {
    OutputStream outputStream = null;
    try {/*from  ww w .  j av a  2  s. c om*/
        ExtendedNodeType propertiesType = NodeTypeRegistry.getInstance()
                .getNodeType(Constants.JAHIAMIX_VIEWPROPERTIES);
        Map<String, ExtendedPropertyDefinition> propertyDefinitionMap = propertiesType
                .getDeclaredPropertyDefinitionsAsMap();
        Properties properties = new SortedProperties();
        for (Map.Entry<String, String[]> property : data.getProperties().entrySet()) {
            if (propertyDefinitionMap.containsKey(property.getKey())) {
                String[] v = property.getValue();
                if (v != null) {
                    String propertyValue = StringUtils.join(v, ",");
                    if (propertyDefinitionMap.get(property.getKey()).getRequiredType() != PropertyType.BOOLEAN
                            || !propertyValue.equals("false")) {
                        properties.put(property.getKey(), propertyValue);
                    }
                }
            }
        }
        FileObject file = getFile(StringUtils.substringBeforeLast(data.getPath(), ".") + PROPERTIES_EXTENSION);
        Properties original = new Properties();
        if (file.exists()) {
            original.load(file.getContent().getInputStream());
            for (String s : propertyDefinitionMap.keySet()) {
                original.remove(s);
            }
        }
        properties.putAll(original);
        if (!properties.isEmpty()) {
            outputStream = file.getContent().getOutputStream();
            properties.store(outputStream, data.getPath());
        } else {
            if (file.exists()) {
                file.delete();
            }
        }
        ResourceBundle.clearCache();
    } catch (FileSystemException e) {
        logger.error(e.getMessage(), e);
        throw new RepositoryException("Failed to write source code", e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        throw new RepositoryException("Failed to write source code", e);
    } catch (NoSuchNodeTypeException e) {
        logger.error("Unable to find type : " + data.getType() + " for node " + data.getPath(), e);
        throw e;
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:org.jahia.modules.external.modules.ModulesDataSource.java

private void saveCndResourceBundle(ExternalData data, String key) throws RepositoryException {
    String resourceBundleName = module.getResourceBundleName();
    if (resourceBundleName == null) {
        resourceBundleName = "resources." + module.getId();
    }/*from   w  ww  .  ja v a 2  s .c om*/
    String rbBasePath = "/src/main/resources/resources/"
            + StringUtils.substringAfterLast(resourceBundleName, ".");
    Map<String, Map<String, String[]>> i18nProperties = data.getI18nProperties();
    if (i18nProperties != null) {
        List<File> newFiles = new ArrayList<File>();
        for (Map.Entry<String, Map<String, String[]>> entry : i18nProperties.entrySet()) {
            String lang = entry.getKey();
            Map<String, String[]> properties = entry.getValue();

            String[] values = properties.get(Constants.JCR_TITLE);
            String title = ArrayUtils.isEmpty(values) ? null : values[0];

            values = properties.get(Constants.JCR_DESCRIPTION);
            String description = ArrayUtils.isEmpty(values) ? null : values[0];

            String rbPath = rbBasePath + "_" + lang + PROPERTIES_EXTENSION;
            InputStream is = null;
            InputStreamReader isr = null;
            OutputStream os = null;
            OutputStreamWriter osw = null;
            try {
                FileObject file = getFile(rbPath);
                FileContent content = file.getContent();
                Properties p = new SortedProperties();
                if (file.exists()) {
                    is = content.getInputStream();
                    isr = new InputStreamReader(is, Charsets.ISO_8859_1);
                    p.load(isr);
                    isr.close();
                    is.close();
                } else if (StringUtils.isBlank(title) && StringUtils.isBlank(description)) {
                    continue;
                } else {
                    newFiles.add(new File(file.getName().getPath()));
                }
                if (!StringUtils.isEmpty(title)) {
                    p.setProperty(key, title);
                }
                if (!StringUtils.isEmpty(description)) {
                    p.setProperty(key + "_description", description);
                }
                os = content.getOutputStream();
                osw = new OutputStreamWriter(os, Charsets.ISO_8859_1);
                p.store(osw, rbPath);
                ResourceBundle.clearCache();
            } catch (FileSystemException e) {
                logger.error("Failed to save resourceBundle", e);
                throw new RepositoryException("Failed to save resourceBundle", e);
            } catch (IOException e) {
                logger.error("Failed to save resourceBundle", e);
                throw new RepositoryException("Failed to save resourceBundle", e);
            } finally {
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(isr);
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(osw);
            }
        }
        SourceControlManagement sourceControl = module.getSourceControl();
        if (sourceControl != null) {
            try {
                sourceControl.add(newFiles);
            } catch (IOException e) {
                logger.error("Failed to add files to source control", e);
                throw new RepositoryException("Failed to add new files to source control: " + newFiles, e);
            }
        }
    }
}

From source file:org.jahia.modules.external.modules.ModulesDataSource.java

/**
 * Get the local NodeTypeRegistry for one specific file. Contains system definitions, dependencies and
 * definitions from the current file./*  w  w  w  . j ava  2 s .  c  o  m*/
 *
 * @param path
 * @return
 * @throws RepositoryException
 */
private synchronized NodeTypeRegistry loadRegistry(String path) throws RepositoryException {
    NodeTypeRegistry ntr = nodeTypeRegistryMap.get(path);
    if (ntr != null) {
        return ntr;
    } else {
        try {
            ntr = createBaseRegistry();
            FileObject file = getFile(path);
            if (file.exists()) {
                nodeTypeRegistryMap.put(path, ntr);
                namespaceDefinitions.put(ntr, new HashMap<String, String>());
                ntr.addDefinitionsFile(new UrlResource(file.getURL()), module.getId());
            }
        } catch (ParseException | IOException e) {
            throw new RepositoryException("Failed to load node type registry", e);
        }
        return ntr;
    }
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

@Override
public boolean itemExists(String path) {
    try {//from  w  w  w  . j a va  2  s .c o m
        FileObject file = getFile(
                path.endsWith(JCR_CONTENT_SUFFIX) ? StringUtils.substringBeforeLast(path, JCR_CONTENT_SUFFIX)
                        : path);
        return file.exists();
    } catch (FileSystemException e) {
        logger.warn("Unable to check file existence for path " + path, e);
    }
    return false;
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

public ExternalData getItemByPath(String path) throws PathNotFoundException {
    try {/*from  ww  w  .j av  a 2  s  . c o m*/
        if (path.endsWith(JCR_CONTENT_SUFFIX)) {
            FileObject fileObject = getFile(StringUtils.substringBeforeLast(path, JCR_CONTENT_SUFFIX));
            FileContent content = fileObject.getContent();
            if (!fileObject.exists()) {
                throw new PathNotFoundException(path);
            }
            return getFileContent(content);
        } else {
            FileObject fileObject = getFile(path);
            if (!fileObject.exists()) {
                throw new PathNotFoundException(path);
            }
            return getFile(fileObject);
        }

    } catch (FileSystemException e) {
        throw new PathNotFoundException("File system exception while trying to retrieve " + path, e);
    }
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

public List<String> getChildren(String path) throws RepositoryException {
    try {// www .j ava  2  s . co m
        if (!path.endsWith(JCR_CONTENT_SUFFIX)) {
            FileObject fileObject = getFile(path);
            if (fileObject.getType() == FileType.FILE) {
                return JCR_CONTENT_LIST;
            } else if (fileObject.getType() == FileType.FOLDER) {
                FileObject[] files = fileObject.getChildren();
                if (files.length > 0) {
                    List<String> children = new LinkedList<String>();
                    for (FileObject object : files) {
                        if (getSupportedNodeTypes().contains(getDataType(object))) {
                            children.add(object.getName().getBaseName());
                        }
                    }
                    return children;
                } else {
                    return Collections.emptyList();
                }
            } else {
                if (fileObject.exists()) {
                    logger.warn("Found non file or folder entry at path {}, maybe an alias. VFS file type: {}",
                            fileObject, fileObject.getType());
                } else {
                    throw new PathNotFoundException(path);
                }
            }
        }
    } catch (FileSystemException e) {
        logger.error("Cannot get node children", e);
    }

    return Collections.emptyList();
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

@Override
public List<ExternalData> getChildrenNodes(String path) throws RepositoryException {
    try {//from   w  ww .  ja  va2 s.c  om
        if (!path.endsWith(JCR_CONTENT_SUFFIX)) {
            FileObject fileObject = getFile(path);
            if (fileObject.getType() == FileType.FILE) {
                final FileContent content = fileObject.getContent();
                return Collections.singletonList(getFileContent(content));
            } else if (fileObject.getType() == FileType.FOLDER) {
                fileObject.refresh(); //in case of folder, refresh because it could be changed external               
                FileObject[] files = fileObject.getChildren();
                if (files.length > 0) {
                    List<ExternalData> children = new LinkedList<ExternalData>();
                    for (FileObject object : files) {
                        if (getSupportedNodeTypes().contains(getDataType(object))) {
                            children.add(getFile(object));
                            if (object.getType() == FileType.FILE) {
                                children.add(getFileContent(object.getContent()));
                            }
                        }
                    }
                    return children;
                } else {
                    return Collections.emptyList();
                }
            } else {
                if (fileObject.exists()) {
                    logger.warn("Found non file or folder entry at path {}, maybe an alias. VFS file type: {}",
                            fileObject, fileObject.getType());
                } else {
                    throw new PathNotFoundException(path);
                }
            }
        }
    } catch (FileSystemException e) {
        logger.error("Cannot get node children", e);
    }

    return Collections.emptyList();
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

@Override
public void move(String oldPath, String newPath) throws RepositoryException {
    if (oldPath.equals(newPath)) {
        return;//from www. ja v  a2  s  . c  o m
    }
    try {
        FileObject origin = getFile(oldPath);
        if (origin.isContentOpen()) {
            origin.close();
        }
        FileObject destination = getFile(newPath);
        if (destination.exists() && destination.isContentOpen()) {
            destination.close();
        }
        origin.moveTo(destination);
    } catch (FileSystemException e) {
        throw new RepositoryException(oldPath, e);
    }
}

From source file:org.jfunktor.common.vfs.VirtualFileSystem.java

/**
 * Checks whether the given file url is valid or not
 *
 * @param location//from  ww w  .  j a v a2  s .c o m
 * @return
 */
public static boolean exists(URL location) throws FileNotFoundException, VFSException {
    FileObject obj = null;
    try {
        obj = VFS.getManager().resolveFile(location.toString());
        return obj.exists();
    } catch (FileSystemException e) {
        throw new VFSException(e);
    }
}