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.kalypso.service.unittests.WebDavWrite.java

/**
 * This function tries to copy a file to a webdav.
 *///  w ww.  ja  va 2s  . c o m
@Test
public void testWebDavWrite() throws IOException {
    final DefaultFileSystemManager manager = new DefaultFileSystemManager();
    manager.addProvider("webdav", new WebdavFileProvider());
    manager.addProvider("file", new DefaultLocalFileProvider());
    manager.addProvider("tmp", new TemporaryFileProvider());

    manager.setDefaultProvider(new UrlFileProvider());
    manager.setFilesCache(new DefaultFilesCache());
    manager.setTemporaryFileStore(new DefaultFileReplicator());
    manager.init();

    final File file = new File(FileUtilities.TMP_DIR, "davWrite.txt");
    final FileObject testFile = manager.toFileObject(file);

    final FileObject davFile = manager
            .resolveFile("webdav://albert:gnimfe@ibpm.bjoernsen.de/dav/pub/Test/test.txt");
    Assert.assertNotNull(davFile);

    FileUtil.copyContent(testFile, davFile);

    Assert.assertTrue(davFile.exists());
}

From source file:org.kalypso.service.wps.client.NonBlockingWPSRequest.java

@SuppressWarnings("unchecked")
public ExecuteResponseType getExecuteResponse(final FileSystemManager manager)
        throws Exception, InterruptedException {
    final FileObject statusFile = VFSUtilities.checkProxyFor(m_statusLocation, manager);
    if (statusFile.exists()) {
        /* Some variables for handling the errors. */
        boolean success = false;
        int cnt = 0;

        /* Try to read the status at least 3 times, before exiting. */
        JAXBElement<ExecuteResponseType> executeState = null;
        while (success == false) {
            final FileContent content = statusFile.getContent();
            InputStream inputStream = null;
            try {
                inputStream = content.getInputStream();
                final String xml = IOUtils.toString(inputStream);
                if (xml != null && !"".equals(xml)) //$NON-NLS-1$
                {/*from   w ww. jav  a 2s .  c  o  m*/
                    final Object object = MarshallUtilities.unmarshall(xml);
                    executeState = (JAXBElement<ExecuteResponseType>) object;
                    success = true;
                }
            } catch (final Exception e) {
                /* An error has occured while copying the file. */
                KalypsoServiceWPSDebug.DEBUG
                        .printf("An error has occured with the message: " + e.getLocalizedMessage() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$

                /* If a certain amount (here 2) of retries was reached before, rethrow the error. */
                if (cnt >= 2) {
                    KalypsoServiceWPSDebug.DEBUG
                            .printf("The second retry has failed, rethrowing the error ...\n"); //$NON-NLS-1$
                    throw e;
                }

                /* Retry the copying of the file. */
                cnt++;
                KalypsoServiceWPSDebug.DEBUG.printf("Retry: " + String.valueOf(cnt) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
                success = false;

                /* Wait for some milliseconds. */
                Thread.sleep(1000);
            } finally {
                IOUtils.closeQuietly(inputStream);
                statusFile.close();
            }
        }
        return executeState.getValue();
    } else
        return null;
}

From source file:org.kalypso.service.wps.server.operations.ExecuteOperation.java

/**
 * @see org.kalypso.service.wps.operations.IOperation#executeOperation(org.kalypso.service.ogc.RequestBean)
 *///from   w ww.  j a v  a 2  s .  c  om
@Override
public StringBuffer executeOperation(final RequestBean request) throws OWSException {
    final StringBuffer response = new StringBuffer();

    /* Start the operation. */
    KalypsoServiceWPSDebug.DEBUG.printf("Operation \"Execute\" started.\n"); //$NON-NLS-1$

    /* Gets the identifier, but also unmarshalls the request, so it has to be done! */
    final String requestXml = request.getBody();
    Object executeRequest = null;
    try {
        executeRequest = MarshallUtilities.unmarshall(requestXml);
    } catch (final JAXBException e) {
        throw new OWSException(OWSException.ExceptionCode.NO_APPLICABLE_CODE, e, ""); //$NON-NLS-1$
    }

    /* Execute the simulation via a manager, so that more than one simulation can be run at the same time. */
    final WPSSimulationManager manager = WPSSimulationManager.getInstance();

    // TODO version 1.0
    final ExecuteMediator executeMediator = new ExecuteMediator(executeRequest);
    final WPSSimulationInfo info = manager.startSimulation(executeMediator);

    /* Prepare the execute response. */
    FileObject resultFile = null;
    InputStream inputStream = null;
    try {
        final FileObject resultDir = manager.getResultDir(info.getId());
        resultFile = resultDir.resolveFile("executeResponse.xml"); //$NON-NLS-1$
        int time = 0;
        final int timeout = 10000;
        final int delay = 500;
        while (!resultFile.exists() && time < timeout) {
            Thread.sleep(delay);
            time += delay;
        }
        final FileContent content = resultFile.getContent();
        inputStream = content.getInputStream();
        final String responseXml = IOUtils.toString(inputStream);
        response.append(responseXml);
    } catch (final Exception e) {
        throw new OWSException(OWSException.ExceptionCode.NO_APPLICABLE_CODE, e, ""); //$NON-NLS-1$
    } finally {
        /* Close the file object. */
        VFSUtilities.closeQuietly(resultFile);

        /* Close the input stream. */
        IOUtils.closeQuietly(inputStream);
    }

    return response;
}

From source file:org.kalypso.service.wps.utils.WPSUtilities.java

public static ExecuteResponseType readExecutionResponse(final FileSystemManager manager,
        final String statusLocation) throws CoreException {
    try {/*from w w w  .jav a  2 s  . com*/
        final FileObject statusFile = VFSUtilities.checkProxyFor(statusLocation, manager);
        if (!statusFile.exists())
            return null;

        /* Try to read the status at least 3 times, before exiting. */
        Exception lastError = new Exception();

        // TODO: timeout defined as approximately 3 seconds is in some how not always usable, set to 100.
        // Better to set it from predefined properties.

        // Hi Ilya, I think you missunderstood the number here.
        // It does not represent a timeout, but the number of times to try.
        // The Thread.sleep( 1000 ) in case of an error is only the time to wait,
        // before it is retried to read the execution response.
        // I changed the value back to 3. Holger
        for (int i = 0; i < 3; i++) {
            InputStream inputStream = null;
            try {
                final FileContent content = statusFile.getContent();
                inputStream = content.getInputStream();
                final String xml = IOUtils.toString(inputStream);
                if (xml == null || "".equals(xml)) //$NON-NLS-1$
                    throw new IOException(Messages.getString("org.kalypso.service.wps.utils.WPSUtilities.4") //$NON-NLS-1$
                            + statusFile.toString());

                final Object object = MarshallUtilities.unmarshall(xml);
                final JAXBElement<?> executeState = (JAXBElement<?>) object;
                return (ExecuteResponseType) executeState.getValue();
            } catch (final Exception e) {
                lastError = e;

                KalypsoServiceWPSDebug.DEBUG
                        .printf("An error has occured with the message: " + e.getLocalizedMessage() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
                KalypsoServiceWPSDebug.DEBUG.printf("Retry: " + String.valueOf(i) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$

                Thread.sleep(1000);
            } finally {
                IOUtils.closeQuietly(inputStream);
                statusFile.close();
            }
        }

        KalypsoServiceWPSDebug.DEBUG.printf("The second retry has failed, rethrowing the error ..."); //$NON-NLS-1$ //$NON-NLS-2$
        final IStatus status = StatusUtilities.createStatus(IStatus.ERROR,
                Messages.getString("org.kalypso.service.wps.utils.WPSUtilities.5") //$NON-NLS-1$
                        + lastError.getLocalizedMessage(),
                lastError);
        throw new CoreException(status);
    } catch (final Exception e) {
        e.printStackTrace();

        final IStatus status = StatusUtilities.createStatus(IStatus.ERROR,
                Messages.getString("org.kalypso.service.wps.utils.WPSUtilities.6") + e.getLocalizedMessage(), //$NON-NLS-1$
                e);
        throw new CoreException(status);
    }
}

From source file:org.mycore.backend.filesystem.MCRCStoreVFS.java

@Override
protected boolean exists(MCRFileReader file) {
    try {//from  w w  w  .  j ava2 s . c  o  m
        FileObject targetObject = fsManager.resolveFile(getBase(), file.getStorageID());
        return targetObject.exists();
    } catch (FileSystemException e) {
        LOGGER.error(e);
        return false;
    }
}

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

private void readAdditionalData() throws IOException {
    FileObject src = VFS.getManager().resolveFile(fo, dataFile);
    if (!src.exists()) {
        LOGGER.warn("Metadata file is missing, repairing metadata...");
        data = new Element("collection");
        new Document(data);
        repairMetadata();/* w w w. j a v  a 2 s.  c o  m*/
    }
    try {
        data = new MCRVFSContent(src).asXML().getRootElement();
    } catch (JDOMException | SAXException e) {
        throw new IOException(e);
    }
}

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

/**
 * Creates and stores a new, empty file collection with the given ID
 * /*from  w ww .j a v a 2 s. c  o  m*/
 * @param id
 *            the ID of the file collection
 * @return a newly created file collection
 * @throws IOException
 *             when a file collection with the given ID already exists
 */
public MCRFileCollection create(int id) throws IOException {
    FileObject fo = getSlot(id);
    if (fo.exists()) {
        String msg = "FileCollection with ID " + id + " already exists";
        throw new MCRException(msg);
    }
    return new MCRFileCollection(this, id);
}

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

/**
 * Returns the file collection stored under the given ID, or null when no
 * collection is stored for the given ID.
 * //from   w ww .  j  a v a 2  s. c om
 * @param id
 *            the file collection's ID
 * @return the file collection with the given ID, or null
 */
public MCRFileCollection retrieve(int id) throws IOException {
    FileObject fo = getSlot(id);
    if (!fo.exists()) {
        return null;
    } else {
        return new MCRFileCollection(this, id);
    }
}

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

/**
 * Stores a newly created document under the given ID.
 * /*from w w w . j  ava  2 s . c  o  m*/
 * @param xml
 *            the XML document to be stored
 * @param id
 *            the ID under which the document should be stored
 * @return the stored metadata object
 */
public MCRStoredMetadata create(MCRContent xml, int id) throws IOException, JDOMException {
    if (id <= 0) {
        throw new MCRException("ID of metadata object must be a positive integer");
    }
    FileObject fo = getSlot(id);
    if (fo.exists()) {
        String msg = "Metadata object with ID " + id + " already exists in store";
        throw new MCRException(msg);
    }
    fo.createFile();
    MCRStoredMetadata meta = buildMetadataObject(fo, id);
    meta.create(xml);
    return meta;
}

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

/**
 * Returns the metadata stored under the given ID, or null
 * //from   www  .j a  v a2  s .com
 * @param id
 *            the ID of the XML document
 * @return the metadata stored under that ID, or null when there is no such
 *         metadata object
 */
public MCRStoredMetadata retrieve(int id) throws IOException {
    FileObject fo = getSlot(id);
    if (!fo.exists()) {
        return null;
    } else {
        return buildMetadataObject(fo, id);
    }
}