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:sf.net.experimaestro.server.ContentServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    URL url = ContentServlet.class.getResource(format("/web%s", request.getRequestURI()));

    if (url != null) {
        FileSystemManager fsManager = VFS.getManager();
        FileObject file = fsManager.resolveFile(url.toExternalForm());
        if (file.getType() == FileType.FOLDER) {
            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            response.setHeader("Location", format("%sindex.html", request.getRequestURI()));
            return;
        }// w ww  . j av  a  2s . c  o m

        String filename = url.getFile();
        if (filename.endsWith(".html"))
            response.setContentType("text/html");
        else if (filename.endsWith(".png"))
            response.setContentType("image/png");
        else if (filename.endsWith(".css"))
            response.setContentType("text/css");
        response.setStatus(HttpServletResponse.SC_OK);

        final ServletOutputStream out = response.getOutputStream();
        InputStream in = url.openStream();
        byte[] buffer = new byte[8192];
        int read;
        while ((read = in.read(buffer)) > 0) {
            out.write(buffer, 0, read);
        }
        out.flush();
        in.close();
        return;
    }

    // Not found
    error404(request, response);

}

From source file:tain.kr.test.vfs.v01.Shell.java

/**
 * Does a 'cp' command./*  ww w  . jav a  2 s. c o m*/
 */
private void cp(final String[] cmd) throws Exception {

    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);

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

    dest.copyFrom(src, Selectors.SELECT_ALL);
}

From source file:tain.kr.test.vfs.v01.Shell.java

/**
 * Does an 'ls' command./*w  w w  . j a v a2  s. c  o  m*/
 */
private void ls(final String[] cmd) throws FileSystemException {

    int pos = 1;
    final boolean recursive;

    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
        file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName());
        listChildren(file, recursive, "");
    } else {
        // Stat the file
        System.out.println(file.getName());
        final FileContent content = file.getContent();
        System.out.println("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        System.out.println("Last modified: " + lastMod);
    }
}

From source file:tain.kr.test.vfs.v01.Shell.java

/**
 * Lists the children of a folder./* ww w .  j a v a2  s.c o m*/
 */
private void listChildren(final FileObject dir, final boolean recursive, final String prefix)
        throws FileSystemException {

    final FileObject[] children = dir.getChildren();
    for (final FileObject child : children) {
        System.out.print(prefix);
        System.out.print(child.getName().getBaseName());

        if (child.getType() == FileType.FOLDER) {

            System.out.println("/");
            if (recursive) {
                listChildren(child, recursive, prefix + "    ");
            }
        } else {
            System.out.println();
        }
    }
}

From source file:tain.kr.test.vfs.v01.ShowProperties.java

private static void test01(String[] args) throws Exception {

    if (flag)/* w  w w .j av  a2 s. c om*/
        new ShowProperties();

    if (flag) {

        if (args.length == 0) {
            System.err.println("Please pass the name of a file as parameter.");
            System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
            return;
        }

        for (final String arg : args) {
            try {
                final FileSystemManager mgr = VFS.getManager();
                System.out.println();
                System.out.println("Parsing   : " + arg);
                final FileObject file = mgr.resolveFile(arg);
                System.out.println("URL       : " + file.getURL());
                System.out.println("getName() : " + file.getName());
                System.out.println("BaseName  : " + file.getName().getBaseName());
                System.out.println("Extension : " + file.getName().getExtension());
                System.out.println("Path      : " + file.getName().getPath());
                System.out.println("Scheme    : " + file.getName().getScheme());
                System.out.println("URI       : " + file.getName().getURI());
                System.out.println("Root URI  : " + file.getName().getRootURI());
                System.out.println("Parent    : " + file.getName().getParent());
                System.out.println("Type      : " + file.getType());
                System.out.println("Exists    : " + file.exists());
                System.out.println("Readable  : " + file.isReadable());
                System.out.println("Writeable : " + file.isWriteable());
                System.out.println("Root path : " + file.getFileSystem().getRoot().getName().getPath());

                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE)) {
                        System.out.println("Size: " + file.getContent().getSize() + " bytes");

                    } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {

                        final FileObject[] children = file.getChildren();
                        System.out.println("Directory with " + children.length + " files");

                        for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                            System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
                            if (iterChildren > SHOW_MAX) {
                                break;
                            }
                        }
                    }

                    System.out.println("Last modified: " + DateFormat.getInstance()
                            .format(new Date(file.getContent().getLastModifiedTime())));
                } else {
                    System.out.println("The file does not exist");
                }

                file.close();
            } catch (final FileSystemException ex) {
                ex.printStackTrace();
            }
        }
    }
}