Example usage for org.apache.commons.vfs FileObject getParent

List of usage examples for org.apache.commons.vfs FileObject getParent

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject getParent.

Prototype

public FileObject getParent() throws FileSystemException;

Source Link

Document

Returns the folder that contains this file.

Usage

From source file:org.efaps.webdav4vfs.handler.PutHandler.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {//from  w  w  w  .  j  a  v  a 2s  .  c o m
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }
    // it is forbidden to write data on a folder
    if (object.exists() && FileType.FOLDER.equals(object.getType())) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    FileObject parent = object.getParent();
    if (!parent.exists()) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    if (!FileType.FOLDER.equals(parent.getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    InputStream is = request.getInputStream();
    OutputStream os = object.getContent().getOutputStream();
    long bytesCopied = IOUtils.copyLarge(is, os);
    String contentLengthHeader = request.getHeader("Content-length");
    LOG.debug(String.format("sent %d/%s bytes", bytesCopied,
            contentLengthHeader == null ? "unknown" : contentLengthHeader));
    os.flush();
    object.close();

    response.setStatus(HttpServletResponse.SC_CREATED);
}

From source file:org.efaps.webdav4vfs.lock.LockManager.java

/**
 * Discover locks for a given file object. This will find locks for the
 * object itself and parent path locks with a depth that reaches the file
 * object.// w w w.  ja  va2  s  .c  o m
 *
 * @param object the file object to find locks for
 * @return the locks that are found for this file object
 * @throws FileSystemException if the file object or its parents cannot be
 *                             accessed
 */
public List<Lock> discoverLock(final FileObject object) throws FileSystemException {
    FileObject parent = object;
    while (parent != null) {
        List<Lock> parentLocks = lockMap.get(parent);
        if ((parentLocks != null) && !parentLocks.isEmpty()) {
            return parentLocks;
        }
        parent = parent.getParent();
    }
    return null;
}

From source file:org.efaps.webdav4vfs.lock.LockManager.java

/**
 * Check whether a lock conflicts with already existing locks up and down the path.
 * First we go up the path to check for parent locks that may include the file object
 * and the go down the directory tree (if depth requires it) to check locks that
 * will conflict./*w  ww. ja va  2s. c  o m*/
 *
 * @param requestedLock the lock requested
 * @throws LockConflictException if a conflicting lock was found
 * @throws FileSystemException   if the file object or path cannot be accessed
 */
private void checkConflicts(final Lock requestedLock) throws LockConflictException, FileSystemException {
    // find locks in the parent path
    FileObject parent = requestedLock.getObject();
    while (parent != null) {
        List<Lock> parentLocks = lockMap.get(parent);
        if (parentLocks != null && !parentLocks.isEmpty()) {
            for (Lock parentLock : parentLocks) {
                if (Lock.EXCLUSIVE.equals(requestedLock.getScope())
                        || Lock.EXCLUSIVE.equals(parentLock.getScope())) {
                    throw new LockConflictException(parentLocks);
                }
            }
        }
        parent = parent.getParent();
    }

    // look for locks down the path (if depth requests it)
    if (requestedLock.getDepth() != 0 && requestedLock.getObject().getChildren().length > 0) {
        requestedLock.getObject().findFiles(new DepthFileSelector(1, requestedLock.getDepth()) {
            @Override()
            public boolean includeFile(FileSelectInfo fileSelectInfo) throws Exception {
                List<Lock> childLocks = lockMap.get(fileSelectInfo.getFile());
                for (Lock childLock : childLocks) {
                    if (Lock.EXCLUSIVE.equals(requestedLock.getScope())
                            || Lock.EXCLUSIVE.equals(childLock.getScope())) {
                        throw new LockConflictException(childLocks);
                    }
                }
                return false;
            }
        }, false, new ArrayList<Object>());
    }
}

From source file:org.jboss.dashboard.ui.components.FileNavigationHandler.java

public CommandResponse actionDeleteFile(CommandRequest request) throws FileSystemException {
    if (isDeleteFileAllowed() || getUserStatus().isRootUser()) {
        FileObject currentFile = getCurrentFile();
        if (currentFile != null && currentFile.isWriteable()) {
            FileObject parentDir = currentFile.getParent();
            if (parentDir != null && parentDir.isWriteable()) {
                if (currentFile.delete()) {
                    currentFilePath = null;
                }/*ww w. j  ava  2 s  .c  om*/
            }
        }
    }
    return getCommonResponse(request);
}

From source file:org.jboss.dashboard.ui.components.FileNavigationHandler.java

public CommandResponse actionDeleteFolder(CommandRequest request) throws FileSystemException {
    if (isDeleteFolderAllowed() || getUserStatus().isRootUser()) {
        if (!getCurrentPath().equals(getBaseDirPath())) {
            FileObject currentDir = getCurrentDir();
            if (currentDir != null && currentDir.isWriteable()
                    && (currentDir.getChildren() == null || currentDir.getChildren().length == 0)) {
                FileObject parentDir = currentDir.getParent();
                if (parentDir != null && parentDir.isWriteable()) {
                    if (currentDir.delete()) {
                        currentPath = currentPath.substring(0, currentPath.lastIndexOf("/"));
                    }/*from  ww w  . ja  v a2  s  . co m*/
                }
            }
        }
    }
    return getCommonResponse(request);
}

From source file:org.jboss.dashboard.ui.formatters.FileNavigationActionsFormatter.java

protected void renderDeleteFile() throws FileSystemException {
    if (fileNavigationHandler.isDeleteFileAllowed() || fileNavigationHandler.getUserStatus().isRootUser()) {
        FileObject currentFile = fileNavigationHandler.getCurrentFile();
        if (currentFile != null && currentFile.isWriteable()) {
            FileObject parentDir = currentFile.getParent();
            if (parentDir != null && parentDir.isWriteable()) {
                //setAttribute("folderName", parentDir.getName().getBaseName());
                setAttribute("fileName", currentFile.getName().getBaseName());
                renderFragment("deleteFile");
            }/*from   ww  w.  j  av a  2s.c  o m*/
        }
    }
}

From source file:org.jboss.dashboard.ui.formatters.FileNavigationActionsFormatter.java

protected void renderDeleteFolder() throws FileSystemException {
    if (fileNavigationHandler.isDeleteFolderAllowed() || fileNavigationHandler.getUserStatus().isRootUser()) {
        if (!fileNavigationHandler.getCurrentPath().equals(fileNavigationHandler.getStartingPath())) {
            FileObject currentDir = fileNavigationHandler.getCurrentDir();
            if (currentDir != null && currentDir.isWriteable()
                    && (currentDir.getChildren() == null || currentDir.getChildren().length == 0)) {
                FileObject parentDir = currentDir.getParent();
                if (parentDir != null && parentDir.isWriteable()) {
                    setAttribute("folderName", currentDir.getName().getBaseName());
                    renderFragment("deleteFolder");
                }/*from   www .j a  va2  s .c om*/
            }
        }
    }
}

From source file:org.josso.tooling.gshell.install.commands.InstallJavaAgentCommand.java

private void processDir(FileObject dir, boolean recursive) throws Exception { //recursively traverse directories
    FileObject[] children = dir.getChildren();
    for (FileObject subfile : children) {
        if (subfile.getType() == FileType.FOLDER) {
            if (recursive)
                processDir(subfile, recursive);
        } else {// ww w  . j a  v a 2  s  . com
            getInstaller().installComponent(createArtifact(subfile.getParent().getURL().toString(),
                    JOSSOScope.AGENT, subfile.getName().getBaseName()), true);
        }
    }
}

From source file:org.josso.tooling.gshell.install.installer.TomcatInstaller.java

@Override
public boolean backupAgentConfigurations(boolean remove) {
    try {//from w w w.j a va2  s  .c  o m
        super.backupAgentConfigurations(remove);
        // backup jaas.conf
        FileObject jaasConfigFile = targetConfDir.resolveFile("jaas.conf");
        if (jaasConfigFile.exists()) {
            // backup file in the same folder it is installed
            backupFile(jaasConfigFile, jaasConfigFile.getParent());
            if (remove) {
                jaasConfigFile.delete();
            }
        }
        // backup setenv.sh and setenv.bat
        FileObject[] libs = targetBinDir.getChildren();
        for (int i = 0; i < libs.length; i++) {
            FileObject cfgFile = libs[i];

            if (!cfgFile.getType().getName().equals(FileType.FILE.getName())) {
                // ignore folders
                continue;
            }
            if (cfgFile.getName().getBaseName().startsWith("setenv")
                    && (cfgFile.getName().getBaseName().endsWith(".sh")
                            || cfgFile.getName().getBaseName().endsWith(".bat"))) {
                // backup files in the same folder they're installed in
                backupFile(cfgFile, cfgFile.getParent());
                if (remove) {
                    cfgFile.delete();
                }
            }
        }
    } catch (Exception e) {
        getPrinter().printErrStatus("BackupAgentConfigurations", e.getMessage());
        return false;
    }
    return true;
}

From source file:org.josso.tooling.gshell.install.installer.VFSInstaller.java

public boolean backupGatewayConfigurations(boolean remove) {
    try {//w  ww . j av  a  2 s. c  o m
        FileObject[] libs = targetJOSSOConfDir.getChildren();
        for (int i = 0; i < libs.length; i++) {
            FileObject cfgFile = libs[i];

            if (!cfgFile.getType().getName().equals(FileType.FILE.getName())) {
                // ignore folders
                continue;
            }
            if (cfgFile.getName().getBaseName().startsWith("josso-")
                    && !cfgFile.getName().getBaseName().startsWith("josso-agent")
                    && (cfgFile.getName().getBaseName().endsWith(".xml")
                            || (cfgFile.getName().getBaseName().endsWith(".properties")))) {
                //backup files in the same folder they're installed in
                backupFile(cfgFile, cfgFile.getParent());
                if (remove) {
                    cfgFile.delete();
                }
            }
        }
    } catch (Exception e) {
        getPrinter().printErrStatus("BackupGatewayConfigurations", e.getMessage());
        return false;
    }
    return true;
}