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

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

Introduction

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

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:com.thinkberg.moxo.vfs.s3.S3FileProviderTest.java

public void testMissingFile() throws FileSystemException {
    FileObject object = VFS.getManager().resolveFile(ROOT + "/nonexisting.txt");
    assertFalse(object.exists());
}

From source file:com.pongasoft.kiwidoc.builder.DirectoryContentHandler.java

/**
 * Loads the content of the given resource.
 *
 * @param resource the resource to load/*from w w  w . ja  v a 2  s .com*/
 * @return the content as an object since it depends on which content is being read (ex: manifest,
 *         library, packages, class) (never <code>null</code>)
 * @throws NoSuchContentException if the content does not exist
 * @throws StoreException         if there is a problem reading the content.
 */
public M loadContent(R resource) throws NoSuchContentException, StoreException {
    if (resource == null)
        throw new NoSuchContentException(resource);

    Collection<String> childResources = new ArrayList<String>();

    FileObject root = getResourceFile(resource);

    try {
        if (!root.exists())
            throw new NoSuchContentException(resource);

        if (root.getType() == FileType.FOLDER) {
            FileObject[] children = root.getChildren();
            for (FileObject child : children) {
                if (child.getType() == FileType.FOLDER) {
                    childResources.add(child.getName().getBaseName());
                }
            }
        }
    } catch (FileSystemException e) {
        throw new StoreException(e);
    }

    return _modelFactory.buildModel(resource, childResources);
}

From source file:com.newatlanta.appengine.junit.vfs.provider.GaeProviderTestCase.java

/**
  * Returns the base folder for tests. Copies test files from the local file
  * system to GaeVFS. Note that SVN (.svn) folders are not copied; if the are,
  * then the size of the LRUFilesCache created within GaeFileSystemManager.prepare()
  * must be increased to avoid testcase failures.
  *//*from w  w  w.  j  ava 2s  .  co  m*/
@Override
public FileObject getBaseTestFolder(FileSystemManager manager) throws Exception {
    FileObject gaeTestBaseDir = manager.getBaseFile().resolveFile("test-data");
    if (!gaeTestBaseDir.exists()) {
        FileObject localTestBaseDir = manager
                .resolveFile("file://" + GaeFileNameParser.getRootPath(manager.getBaseFile().getName())
                        + gaeTestBaseDir.getName().getPath());
        gaeTestBaseDir.copyFrom(localTestBaseDir, new TestFileSelector());
        // confirm that the correct number of files were copied
        FileObject[] testFiles = localTestBaseDir.findFiles(new TestFileSelector());
        FileObject[] gaeFiles = gaeTestBaseDir.findFiles(Selectors.SELECT_FILES);
        assertEquals(testFiles.length, gaeFiles.length);
    }
    return gaeTestBaseDir;
}

From source file:com.bedatadriven.renjin.appengine.AppEngineLocalFilesSystemProviderTest.java

@Test
public void test() throws FileSystemException {

    File basePath = new File(getClass().getResource("/jarfiletest.jar").getFile()).getParentFile();

    FileSystemManager dfsm = AppEngineContextFactory
            .createFileSystemManager(new AppEngineLocalFilesSystemProvider(basePath));

    FileObject jarFile = dfsm.resolveFile("/jarfiletest.jar");
    assertThat(jarFile.getName().getURI(), equalTo("file:///jarfiletest.jar"));
    assertThat(jarFile.exists(), equalTo(true));

    FileObject jarRoot = dfsm.resolveFile("jar:file:///jarfiletest.jar!/r/library");
    assertThat(jarRoot.exists(), equalTo(true));
    assertThat(jarRoot.getType(), equalTo(FileType.FOLDER));
    assertThat(jarRoot.getChildren().length, equalTo(1));
}

From source file:com.nary.util.LogFileObject.java

private LogFileObject(FileObject logPath) throws Exception {
    filename = logPath;/*w  ww  . j  a va  2s  . c o  m*/

    if (!logPath.exists())
        logPath.createFile();

    outFileRandomAccess = logPath.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);
    logFileSize = outFileRandomAccess.length();
    outFileRandomAccess.seek(logFileSize);
}

From source file:com.panet.imeta.job.entry.validator.FileDoesNotExistValidator.java

public boolean validate(CheckResultSourceInterface source, String propertyName,
        List<CheckResultInterface> remarks, ValidatorContext context) {

    String filename = ValidatorUtils.getValueAsString(source, propertyName);
    VariableSpace variableSpace = getVariableSpace(source, propertyName, remarks, context);
    boolean failIfExists = getFailIfExists(source, propertyName, remarks, context);

    if (null == variableSpace) {
        return false;
    }//from   w  w w.  j a  v  a2 s  .c  o m

    String realFileName = variableSpace.environmentSubstitute(filename);
    FileObject fileObject = null;
    try {
        fileObject = KettleVFS.getFileObject(realFileName);

        if (fileObject.exists() && failIfExists) {
            JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks,
                    JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME));
            return false;
        }
        try {
            fileObject.close(); // Just being paranoid
        } catch (IOException ignored) {
        }
    } catch (Exception e) {
        JobEntryValidatorUtils.addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e);
        return false;
    }
    return true;
}

From source file:com.thinkberg.webdav.PropFindHandler.java

/**
 * Handle a PROPFIND request./*from  ww w  . ja  va  2s .  co m*/
 *
 * @param request  the servlet request
 * @param response the servlet response
 * @throws IOException if there is an error that cannot be handled normally
 */
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    SAXReader saxReader = new SAXReader();
    try {
        Document propDoc = saxReader.read(request.getInputStream());
        logXml(propDoc);

        Element propFindEl = propDoc.getRootElement();
        for (Object propElObject : propFindEl.elements()) {
            Element propEl = (Element) propElObject;
            if (VALID_PROPFIND_TAGS.contains(propEl.getName())) {
                FileObject object = VFSBackend.resolveFile(request.getPathInfo());
                if (object.exists()) {
                    // respond as XML encoded multi status
                    response.setContentType("text/xml");
                    response.setCharacterEncoding("UTF-8");
                    response.setStatus(SC_MULTI_STATUS);

                    Document multiStatusResponse = getMultiStatusResponse(object, propEl, getBaseUrl(request),
                            getDepth(request));
                    logXml(multiStatusResponse);

                    // write the actual response
                    XMLWriter writer = new XMLWriter(response.getWriter(), OutputFormat.createCompactFormat());
                    writer.write(multiStatusResponse);
                    writer.flush();
                    writer.close();

                } else {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                }
                break;
            }
        }
    } catch (DocumentException e) {
        LOG.error("invalid request: " + e.getMessage());
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
}

From source file:com.nary.util.LogFileObject.java

private void rotateLogFile() {
    try {/*ww  w  .j a  v  a  2  s  .c o  m*/
        outFileRandomAccess.close();

        // rename the old file to a new one
        int x = 1;
        String sfname = filename.getName().getBaseName();
        FileObject newFile = filename.getParent().resolveFile(sfname + "." + x);

        while (newFile.exists()) {
            newFile = filename.getParent().resolveFile(sfname + "." + (x++));
        }

        filename.moveTo(newFile);

        //Delete the old one
        filename.delete();
        filename.createFile();

        outFileRandomAccess = filename.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);
        ;
        logFileSize = 0;

    } catch (IOException ignoreException) {
        // - the rotation failed; so lets just reset the current file
    }
}

From source file:com.thinkberg.moxo.dav.PropFindHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    SAXReader saxReader = new SAXReader();
    try {//from   www . j  a  v a  2  s  .c o  m
        Document propDoc = saxReader.read(request.getInputStream());
        // log(propDoc);
        Element propFindEl = propDoc.getRootElement();
        Element propEl = (Element) propFindEl.elementIterator().next();
        String propElName = propEl.getName();

        List<String> requestedProperties = new ArrayList<String>();
        boolean ignoreValues = false;
        if (TAG_PROP.equals(propElName)) {
            for (Object id : propEl.elements()) {
                requestedProperties.add(((Element) id).getName());
            }
        } else if (TAG_ALLPROP.equals(propElName)) {
            requestedProperties = DavResource.ALL_PROPERTIES;
        } else if (TAG_PROPNAMES.equals(propElName)) {
            requestedProperties = DavResource.ALL_PROPERTIES;
            ignoreValues = true;
        }

        FileObject object = getResourceManager().getFileObject(request.getPathInfo());
        if (object.exists()) {
            // respond as XML encoded multi status
            response.setContentType("text/xml");
            response.setCharacterEncoding("UTF-8");
            response.setStatus(SC_MULTI_STATUS);

            Document multiStatusResponse = getMultiStatusRespons(object, requestedProperties,
                    getBaseUrl(request), getDepth(request), ignoreValues);
            //log(multiStatusResponse);

            // write the actual response
            XMLWriter writer = new XMLWriter(response.getWriter(), OutputFormat.createCompactFormat());
            writer.write(multiStatusResponse);
            writer.flush();
            writer.close();

        } else {
            log("!! " + object.getName().getPath() + " NOT FOUND");
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } catch (DocumentException e) {
        log("!! inavlid request: " + e.getMessage());
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
}

From source file:com.adito.vfs.VFSOutputStream.java

/**
 * <p>Rename the temporary {@link File} to the original one.</p>
 *//*from w  w  w  . j a  v a 2 s.c  om*/
protected void rename(FileObject temporary, FileObject original) throws IOException {
    if ((original.exists()) && (!original.delete())) {
        throw new IOException("Unable to delete original file");
    }
    temporary.moveTo(original);
}