Search in sources :

Example 31 with FileObject

use of org.apache.commons.vfs2.FileObject in project metron by apache.

the class ClasspathFunctionResolver method initialize.

@Override
public void initialize(Context context) {
    super.initialize(context);
    if (context != null) {
        Optional<Object> optional = context.getCapability(STELLAR_CONFIG, false);
        if (optional.isPresent()) {
            Map<String, Object> stellarConfig = (Map<String, Object>) optional.get();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Setting up classloader using the following config: {}", stellarConfig);
            }
            include(STELLAR_SEARCH_INCLUDES_KEY.get(stellarConfig, String.class).split(STELLAR_SEARCH_DELIMS));
            exclude(STELLAR_SEARCH_EXCLUDES_KEY.get(stellarConfig, String.class).split(STELLAR_SEARCH_DELIMS));
            Optional<ClassLoader> vfsLoader = Optional.empty();
            try {
                vfsLoader = VFSClassloaderUtil.configureClassloader(STELLAR_VFS_PATHS.get(stellarConfig, String.class));
                if (vfsLoader.isPresent()) {
                    LOG.debug("CLASSLOADER LOADED WITH: {}", STELLAR_VFS_PATHS.get(stellarConfig, String.class));
                    if (LOG.isDebugEnabled()) {
                        for (FileObject fo : ((VFSClassLoader) vfsLoader.get()).getFileObjects()) {
                            LOG.error("{} - {}", fo.getURL(), fo.exists());
                        }
                    }
                    classLoaders(vfsLoader.get());
                }
            } catch (FileSystemException e) {
                LOG.error("Unable to process filesystem: {}", e.getMessage(), e);
            }
        } else {
            LOG.info("No stellar config set; I'm reverting to the context classpath with no restrictions.");
            if (LOG.isDebugEnabled()) {
                try {
                    throw new IllegalStateException("No config set, stacktrace follows.");
                } catch (IllegalStateException ise) {
                    LOG.error(ise.getMessage(), ise);
                }
            }
        }
    } else {
        throw new IllegalStateException("CONTEXT IS NULL!");
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject) Map(java.util.Map)

Example 32 with FileObject

use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.

the class SchedulerStateRest method pullFile.

/**
 * Either Pulls a file from the given DataSpace to the local file system or
 * list the content of a directory if the path refers to a directory In the
 * case the path to a file is given, the content of this file will be
 * returns as an input stream In the case the path to a directory is given,
 * the input stream returned will be a text stream containing at each line
 * the content of the directory
 *
 * @param sessionId
 *            a valid session id
 * @param spaceName
 *            the name of the data space involved (GLOBAL or USER)
 * @param filePath
 *            the path to the file or directory whose content must be
 *            received
 */
@Override
public InputStream pullFile(@HeaderParam("sessionid") String sessionId, @PathParam("spaceName") String spaceName, @PathParam("filePath") String filePath) throws IOException, NotConnectedRestException, PermissionRestException {
    checkAccess(sessionId, "pullFile");
    Session session = dataspaceRestApi.checkSessionValidity(sessionId);
    filePath = normalizeFilePath(filePath, null);
    FileObject sourcefo = dataspaceRestApi.resolveFile(session, spaceName, filePath);
    if (!sourcefo.exists() || !sourcefo.isReadable()) {
        RuntimeException ex = new IllegalArgumentException("File " + filePath + " does not exist or is not readable in space " + spaceName);
        logger.error(ex);
        throw ex;
    }
    if (sourcefo.getType().equals(FileType.FOLDER)) {
        logger.info("[pullFile] reading directory content from " + sourcefo.getURL());
        // if it's a folder we return an InputStream listing its content
        StringBuilder sb = new StringBuilder();
        String nl = System.lineSeparator();
        for (FileObject fo : sourcefo.getChildren()) {
            sb.append(fo.getName().getBaseName() + nl);
        }
        return IOUtils.toInputStream(sb.toString());
    } else if (sourcefo.getType().equals(FileType.FILE)) {
        logger.info("[pullFile] reading file content from " + sourcefo.getURL());
        return sourcefo.getContent().getInputStream();
    } else {
        RuntimeException ex = new IllegalArgumentException("File " + filePath + " has an unsupported type " + sourcefo.getType());
        logger.error(ex);
        throw ex;
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) HttpSession(javax.servlet.http.HttpSession) Session(org.ow2.proactive_grid_cloud_portal.common.Session)

Example 33 with FileObject

use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.

the class RestDataspaceImpl method create.

@POST
@Path("/{dataspace}/{path-name:.*}")
public Response create(@HeaderParam("sessionid") String sessionId, @PathParam("dataspace") String dataspacePath, @PathParam("path-name") String pathname, @FormParam("mimetype") String mimeType) throws NotConnectedRestException, PermissionRestException {
    Session session = checkSessionValidity(sessionId);
    try {
        checkPathParams(dataspacePath, pathname);
        FileObject fileObject = resolveFile(session, dataspacePath, pathname);
        if (mimeType.equals(org.ow2.proactive_grid_cloud_portal.common.FileType.FOLDER.getMimeType())) {
            logger.debug(String.format("Creating folder %s in %s", pathname, dataspacePath));
            fileObject.createFolder();
        } else if (mimeType.equals(org.ow2.proactive_grid_cloud_portal.common.FileType.FILE.getMimeType())) {
            logger.debug(String.format("Creating file %s in %s", pathname, dataspacePath));
            fileObject.createFile();
        } else {
            return serverErrorRes("Cannot create specified file since mimetype is not specified");
        }
        return Response.ok().build();
    } catch (FileSystemException e) {
        logger.error(String.format("Cannot create %s in %s", pathname, dataspacePath), e);
        throw rethrow(e);
    } catch (Throwable e) {
        logger.error(String.format("Cannot create %s in %s", pathname, dataspacePath), e);
        throw rethrow(e);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject) Session(org.ow2.proactive_grid_cloud_portal.common.Session)

Example 34 with FileObject

use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.

the class RestDataspaceImpl method metadata.

/**
 * Retrieve metadata of file in the location specified in <i>dataspace</i>.
 * The format of the HEAD URI is:
 * <p>
 * {@code http://<rest-server-path>/data/<dataspace>/<path-name>}
 * <p>
 * Example:
 * {@code http://localhost:8080/rest/rest/data/user/my-files/my-text-file.txt}
 */
@HEAD
@Path("/{dataspace}/{path-name:.*}")
public Response metadata(@HeaderParam("sessionid") String sessionId, @PathParam("dataspace") String dataspacePath, @PathParam("path-name") String pathname) throws NotConnectedRestException, PermissionRestException {
    Session session = checkSessionValidity(sessionId);
    try {
        checkPathParams(dataspacePath, pathname);
        FileObject fo = resolveFile(session, dataspacePath, pathname);
        if (!fo.exists()) {
            return notFoundRes();
        }
        logger.debug(String.format("Retrieving metadata for %s in %s", pathname, dataspacePath));
        MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(FileSystem.metadata(fo));
        return Response.ok().replaceAll(headers).build();
    } catch (Throwable error) {
        logger.error(String.format("Cannot retrieve metadata for %s in %s.", pathname, dataspacePath), error);
        throw rethrow(error);
    }
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject) Session(org.ow2.proactive_grid_cloud_portal.common.Session)

Example 35 with FileObject

use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.

the class RestDataspaceImpl method deleteDir.

private Response deleteDir(FileObject fo, List<String> includes, List<String> excludes) throws FileSystemException {
    if ((includes == null || includes.isEmpty()) && (excludes == null || excludes.isEmpty())) {
        fo.delete(SELECT_ALL);
        fo.delete();
        return noContentRes();
    } else {
        List<FileObject> children = FileSystem.findFiles(fo, includes, excludes);
        for (FileObject child : children) {
            if (!child.delete()) {
                child.delete(SELECT_ALL);
                child.delete();
            }
        }
        return noContentRes();
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)39 FileSystemException (org.apache.commons.vfs2.FileSystemException)14 IOException (java.io.IOException)8 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)8 Session (org.ow2.proactive_grid_cloud_portal.common.Session)7 ArrayList (java.util.ArrayList)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 HttpSession (javax.servlet.http.HttpSession)3 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)3 FileType (org.apache.commons.vfs2.FileType)3 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 URISyntaxException (java.net.URISyntaxException)2 LinkedList (java.util.LinkedList)2 ExecutionException (java.util.concurrent.ExecutionException)2 FileSelector (org.apache.commons.vfs2.FileSelector)2