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

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

Introduction

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

Prototype

FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:org.mycore.datamodel.ifs2.MCRDirectory.java

/**
 * Returns the MCRFile or MCRDirectory that is represented by the given
 * FileObject, which is a direct child of the directory FileObject this
 * MCRDirectory is stored in.//from  w w w .  j ava2  s.c om
 * 
 * @return an MCRFile or MCRDirectory child
 */
@Override
protected MCRStoredNode buildChildNode(FileObject fo) throws IOException {
    if (fo == null) {
        return null;
    }

    Element childData = getChildData(fo.getName().getBaseName());
    if (fo.getType().equals(FileType.FILE)) {
        return new MCRFile(this, fo, childData);
    } else {
        return new MCRDirectory(this, fo, childData);
    }
}

From source file:org.mycore.datamodel.ifs2.MCRNode.java

/**
 * Returns the children of this node. Directories and container files like
 * zip or tar may have child nodes./*from   www . j  a v  a2s  .  c o m*/
 * 
 * @return a List of child nodes, which may be empty, in undefined order
 */
public List<MCRNode> getChildren() throws IOException {
    List<MCRNode> children = new ArrayList<MCRNode>();
    FileObject father = getFather();
    if (father != null) {
        FileObject[] childFos = father.getChildren();
        for (FileObject childFo : childFos) {
            String name = childFo.getName().getBaseName();
            MCRNode child = getChild(name);
            if (child != null) {
                children.add(child);
            }
        }
    }
    return children;
}

From source file:org.mycore.datamodel.ifs2.MCRStore.java

/**
 * Lists all IDs currently used in the store, in ascending or descending
 * order/*w  w w  .j  a v  a2  s .  c  om*/
 * 
 * @see #ASCENDING
 * @see #DESCENDING
 * 
 * @param order
 *            the order in which IDs should be returned.
 * @return all IDs currently used in the store
 */
public Iterator<Integer> listIDs(final boolean order) {
    return new Iterator<Integer>() {
        /**
         * List of files or directories in store not yet handled
         */
        List<FileObject> files = new ArrayList<FileObject>();

        /**
         * The next ID to return, when 0, all IDs have been returned
         */
        int nextID;

        /**
         * The last ID that was returned
         */
        int lastID;

        /**
         * The order in which the IDs should be returned, ascending or
         * descending
         */
        boolean order;

        @Override
        public boolean hasNext() {
            return nextID > 0;
        }

        @Override
        public Integer next() {
            if (nextID < 1) {
                throw new NoSuchElementException();
            }

            lastID = nextID;
            nextID = findNextID();
            return lastID;
        }

        @Override
        public void remove() {
            if (lastID == 0) {
                throw new IllegalStateException();
            }
            try {
                MCRStore.this.delete(lastID);
            } catch (final Exception ex) {
                throw new MCRException("Could not delete " + MCRStore.this.getID() + " " + lastID, ex);
            }
            lastID = 0;
        }

        /**
         * Initializes the enumeration and searches for the first ID to
         * return
         * 
         * @param order
         *            the return order, ascending or descending
         */
        Iterator<Integer> init(final boolean order) {
            this.order = order;
            try {
                addChildren(baseDirectory);
            } catch (final FileSystemException e) {
                e.printStackTrace();
            }
            nextID = findNextID();
            return this;
        }

        /**
         * Adds children of the given directory to the list of files to
         * handle next. Depending on the return sort order, ascending or
         * descending file name order is used.
         * 
         * @param dir
         *            the directory thats children should be added
         * @throws FileSystemException 
         */
        private void addChildren(final FileObject dir) throws FileSystemException {
            if (dir.getType() == FileType.FOLDER) {
                final FileObject[] children = dir.getChildren();
                Arrays.sort(children, new MCRFileObjectComparator());

                for (int i = 0; i < children.length; i++) {
                    files.add(order ? i : 0, children[i]);
                }
            }
        }

        /**
         * Finds the next ID used in the store.
         * 
         * @return the next ID, or 0 if there is no other ID any more
         */
        private int findNextID() {
            if (files.isEmpty()) {
                return 0;
            }

            final FileObject first = files.remove(0);
            // checks basename length against prefix (projectId_typeId), file suffix (.xml) and configured id length
            // if they match it should be a parseable id
            if (first.getName().getBaseName().length() == idLength + prefix.length() + suffix.length()) {
                return MCRStore.this.slot2id(first.getName().getBaseName());
            }

            try {
                addChildren(first);
            } catch (final FileSystemException e) {
                e.printStackTrace();
            }
            return findNextID();
        }
    }.init(order);
}

From source file:org.obiba.opal.core.service.security.DefaultDecryptService.java

@Override
public void decryptData(String projectName, String datasourceName, FileObject file)
        throws NoSuchIdentifiersMappingException, NoSuchProjectException, IllegalArgumentException,
        IOException {// w  w  w .j  a  va2s .  c  o  m
    // Validate the datasource name.
    Datasource destinationDatasource = MagmaEngine.get().getDatasource(datasourceName);

    // Create an FsDatasource for the specified file.
    Datasource sourceDatasource = new FsDatasource(file.getName().getBaseName(),
            opalRuntime.getFileSystem().getLocalFile(file),
            projectName != null ? getProjectEncryptionStrategy(projectName) : getSystemEncryptionStrategy());
    try {
        MagmaEngine.get().addDatasource(sourceDatasource);
        copyValueTables(sourceDatasource, destinationDatasource);
    } finally {
        MagmaEngine.get().removeDatasource(sourceDatasource);
    }
}

From source file:org.obiba.opal.fs.OpalFileSystemTest.java

@SuppressWarnings("ReuseOfLocalVariable")
@Test/*from   www. jav a 2 s .  com*/
public void testResolveFileFromObfuscatedPath_PathIsResolved() throws FileSystemException {
    FileObject file = fsLocal.resolveFileFromObfuscatedPath(fsLocalRoot, "227379988e6f2c3e9eb87b1f7d7bd055");
    assertThat(file.getName().getPath()).isEqualTo("/temp.pem");

    file = fsLocal.resolveFileFromObfuscatedPath(fsLocalRoot, "269dd3644748e20182274c7a9de2ee6");
    assertThat(file.getName().getPath()).isEqualTo("/test2/test21/temp2.pem");

    file = fsLocal.resolveFileFromObfuscatedPath(fsLocalRoot, "30aa4ab41dbfeecb9e92d223bcaccb4");
    assertThat(file.getName().getPath()).isEqualTo("/test2/test21/temp.pem");

    file = fsLocal.resolveFileFromObfuscatedPath(fsLocalRoot, "508e41fae4e3de7a3045c2c9108f7fec");
    assertThat(file.getName().getPath()).isEqualTo("/test2/temp.pem");

}

From source file:org.obiba.opal.fs.security.SecuredFileObject.java

private boolean isPermitted(FileObject file, String method) {
    String permission = "rest:/files" + file.getName().getPath() + ":" + method;
    log.trace("checking permission {}", permission);
    return authorizer.isPermitted(permission);
}

From source file:org.obiba.opal.shell.commands.CopyCommand.java

/**
 * Resolves the output file based on the command parameter. Creates the necessary parent folders (when required).
 *
 * @return A FileObject representing the output file.
 * @throws FileSystemException//  w  ww.j  av a2s.c  o m
 */
private FileObject resolveOutputFileAndCreateParentFolders() throws FileSystemException {
    FileObject outputFile;
    outputFile = getFile(options.getOut());

    // Create the parent directory, if it doesn't already exist.
    FileObject directory = outputFile.getParent();
    if (directory != null) {
        directory.createFolder();
    }

    if (Strings.isNullOrEmpty(outputFile.getName().getExtension())) {
        outputFile.createFolder();
    }

    if ("xls".equals(outputFile.getName().getExtension())) {
        getShell().printf(
                "WARNING: Writing to an Excel 97 spreadsheet. These are limited to 256 columns and 65536 rows "
                        + "which may not be sufficient for writing large tables.\nUse an 'xlsx' extension to use Excel 2007 format "
                        + "which supports 16K columns.\n");
    }
    return outputFile;
}

From source file:org.obiba.opal.shell.commands.DecryptCommand.java

private String getOutputFileName(FileObject inputFile) {
    String inputFilename = inputFile.getName().getBaseName();
    String inputFilenameExt = "";
    String inputFilenamePrefix = "";
    int inputFilenameExtIndex = inputFilename.lastIndexOf(".");
    if (inputFilenameExtIndex > 0) {
        inputFilenamePrefix = inputFilename.substring(0, inputFilenameExtIndex);
        inputFilenameExt = inputFilename.substring(inputFilenameExtIndex, inputFilename.length());
    }//from   w  ww.  j  a v  a 2s .com

    return inputFilenamePrefix + "-plaintext" + inputFilenameExt;
}

From source file:org.obiba.opal.shell.commands.EncryptCommand.java

private void encryptFile(FileObject inputFile, FileObject outputDir) throws IOException {
    FileObject outputFile = getFile(outputDir, inputFile.getName().getBaseName());

    GeneratedSecretKeyDatasourceEncryptionStrategy s = new GeneratedSecretKeyDatasourceEncryptionStrategy();
    s.setKeyProvider(new KeyProvider() {

        @Override/*from ww w.  j  av  a2s  .  com*/
        public PublicKey getPublicKey(Datasource datasource) throws NoSuchKeyException {
            try {
                return projectsKeyStoreService.getKeyStore(projectService.getProject(options.getUnit()))
                        .getKeyStore().getCertificate(options.getAlias()).getPublicKey();
            } catch (KeyStoreException e) {
                throw new MagmaCryptRuntimeException(e);
            }
        }

        @Override
        public KeyPair getKeyPair(PublicKey publicKey) throws NoSuchKeyException, KeyProviderSecurityException {
            return null;
        }

        @Override
        public KeyPair getKeyPair(String alias) throws NoSuchKeyException, KeyProviderSecurityException {
            return null;
        }
    });

    FsDatasource input = new FsDatasource("input", getLocalFile(inputFile));
    FsDatasource output = new FsDatasource(DECRYPT_DATASOURCE_NAME, getLocalFile(outputFile), s);

    try {
        Initialisables.initialise(input, output);
        DatasourceCopier.Builder.newCopier().build().copy(input, output);
    } finally {
        Disposables.silentlyDispose(input);
        Disposables.silentlyDispose(output);
    }
}

From source file:org.obiba.opal.shell.commands.ImportCommand.java

private void archive(FileObject file) throws IOException {
    if (!options.isArchive()) {
        return;/*from   w ww .j  av  a  2 s .  c o  m*/
    }

    String archivePath = options.getArchive();
    try {
        FileObject archiveDir = getFile(archivePath);
        if (archiveDir == null) {
            throw new IOException("Cannot archive file " + file.getName().getPath()
                    + ". Archive directory is null: " + archivePath);
        }
        archiveDir.createFolder();
        FileObject archiveFile = archiveDir.resolveFile(file.getName().getBaseName());
        file.moveTo(archiveFile);
    } catch (FileSystemException ex) {
        throw new IOException("Failed to archive file " + file.getName().getPath() + " to " + archivePath);
    }
}