List of usage examples for org.apache.commons.vfs2 FileType FOLDER
FileType FOLDER
To view the source code for org.apache.commons.vfs2 FileType FOLDER.
Click Source Link
From source file:org.obiba.opal.web.FilesResourceTest.java
@Test public void testCreateFolder_FolderCreatedSuccessfully() throws FileSystemException, URISyntaxException { expect(fileObjectMock.getType()).andReturn(FileType.FOLDER).atLeastOnce(); expect(fileObjectMock.exists()).andReturn(true).atLeastOnce(); FileObject childFolderMock = createMock(FileObject.class); FileName fileNameMock = createMock(FileName.class); expect(fileNameMock.getBaseName()).andReturn("folder").atLeastOnce(); expect(fileNameMock.getPath()).andReturn("folder1/folder").atLeastOnce(); expect(childFolderMock.getName()).andReturn(fileNameMock).atLeastOnce(); expect(childFolderMock.exists()).andReturn(false).atLeastOnce(); FileContent mockContent = createMock(FileContent.class); expect(childFolderMock.getContent()).andReturn(mockContent).atLeastOnce(); expect(mockContent.getLastModifiedTime()).andReturn((long) 1).atLeastOnce(); childFolderMock.createFolder();// w w w . j av a2 s . co m FileObject parentFolderMock = createMock(FileObject.class); expect(childFolderMock.getParent()).andReturn(parentFolderMock).atLeastOnce(); expect(childFolderMock.isReadable()).andReturn(true).atLeastOnce(); expect(childFolderMock.isWriteable()).andReturn(true).atLeastOnce(); expect(parentFolderMock.isWriteable()).andReturn(true).atLeastOnce(); expect(fileObjectMock.resolveFile("folder")).andReturn(childFolderMock).atLeastOnce(); expect(uriInfoMock.getBaseUriBuilder()).andReturn(UriBuilder.fromPath("/")); replay(fileObjectMock, uriInfoMock, parentFolderMock, childFolderMock, fileNameMock, mockContent); Response response = getFileResource().createFolder("folder1", "folder", uriInfoMock); assertThat(response.getStatus()).isEqualTo(Status.CREATED.getStatusCode()); verify(fileObjectMock, uriInfoMock, parentFolderMock, childFolderMock, fileNameMock, mockContent); }
From source file:org.obiba.opal.web.FileSystemResource.java
@GET public Opal.FileDto getFileSystem() throws FileSystemException { FileObject root = opalRuntime.getFileSystem().getRoot(); // Create a root FileDto representing the root of the FileSystem. Opal.FileDto.Builder fileBuilder = Opal.FileDto.newBuilder(); fileBuilder.setName("root").setType(Opal.FileDto.FileType.FOLDER).setPath("/"); fileBuilder.setReadable(root.isReadable()).setWritable(root.isWriteable()); // Create FileDtos for each file & folder in the FileSystem and add them to the root FileDto recursively. addFiles(fileBuilder, root);//from w w w . j a va2 s.com return fileBuilder.build(); }
From source file:org.obiba.opal.web.FileSystemResource.java
private void addFiles(Opal.FileDto.Builder parentFolderBuilder, FileObject parentFolder) throws FileSystemException { Opal.FileDto.Builder fileBuilder;// w ww . jav a 2 s . co m // Get the children for the current folder (list of files & folders). List<FileObject> children = Arrays.asList(parentFolder.getChildren()); Collections.sort(children, new Comparator<FileObject>() { @Override public int compare(FileObject arg0, FileObject arg1) { return arg0.getName().compareTo(arg1.getName()); } }); // Loop through all children. for (FileObject child : children) { // Build a FileDto representing the child. fileBuilder = Opal.FileDto.newBuilder(); fileBuilder.setName(child.getName().getBaseName()).setPath(child.getName().getPath()); fileBuilder.setType( child.getType() == FileType.FILE ? Opal.FileDto.FileType.FILE : Opal.FileDto.FileType.FOLDER); fileBuilder.setReadable(child.isReadable()).setWritable(child.isWriteable()); // If the current child is a folder, add its children recursively. if (child.getType() == FileType.FOLDER && child.isReadable()) { addFiles(fileBuilder, child); } // Add the current child to the parent FileDto (folder). parentFolderBuilder.addChildren(fileBuilder.build()); } }
From source file:org.ow2.proactive_grid_cloud_portal.dataspace.RestDataspaceImpl.java
/** * Delete file(s) from the specified location in the <i>dataspace</i>. The * format of the DELETE URI is:/*www. j a v a 2 s. c o m*/ * <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} * <ul> * <li>dataspace: can have two possible values, 'user' or 'global', * depending on the target <i>DATASPACE</i></li> * <li>path-name: location of the file(s) to be deleted.</li> * </ul> * <b>Notes:</b> * <ul> * <li>Only empty directories can be deleted.</li> * <li>File names or regular expressions can be used as 'includes' and * 'excludes' query parameters, in order to select which files to be deleted * inside the specified directory (path-name).</li> * </ul> * */ @DELETE @Path("/{dataspace}/{path-name:.*}") public Response delete(@HeaderParam("sessionid") String sessionId, @PathParam("dataspace") String dataspace, @PathParam("path-name") String pathname, @QueryParam("includes") List<String> includes, @QueryParam("excludes") List<String> excludes) throws NotConnectedRestException, PermissionRestException { checkPathParams(dataspace, pathname); Session session = checkSessionValidity(sessionId); try { FileObject fo = resolveFile(session, dataspace, pathname); if (!fo.exists()) { return Response.status(Response.Status.NO_CONTENT).build(); } if (fo.getType() == FileType.FOLDER) { logger.debug(String.format("Deleting directory %s in %s", pathname, dataspace)); return deleteDir(fo, includes, excludes); } else { logger.debug(String.format("Deleting file %s in %s", pathname, dataspace)); fo.close(); return fo.delete() ? noContentRes() : serverErrorRes("Cannot delete the file: %s", pathname); } } catch (Throwable error) { logger.error(String.format("Cannot delete %s in %s.", pathname, dataspace), error); throw rethrow(error); } }
From source file:org.ow2.proactive_grid_cloud_portal.dataspace.RestDataspaceImpl.java
@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 { checkPathParams(dataspacePath, pathname); Session session = checkSessionValidity(sessionId); try {/*www .j ava 2s . c om*/ 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); } }
From source file:org.ow2.proactive_grid_cloud_portal.scheduler.SchedulerStateRest.java
/** * 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/* ww w. j a v a 2s . c o m*/ * * @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; } }
From source file:org.ow2.proactive_grid_cloud_portal.scheduler.SchedulerStateRest.java
/** * Deletes a file or recursively delete a directory from the given DataSpace * /*from w w w. ja va 2 s . c o m*/ * @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 which must be deleted **/ @Override public boolean deleteFile(@HeaderParam("sessionid") String sessionId, @PathParam("spaceName") String spaceName, @PathParam("filePath") String filePath) throws IOException, NotConnectedRestException, PermissionRestException { checkAccess(sessionId, "deleteFile"); Session session = dataspaceRestApi.checkSessionValidity(sessionId); filePath = normalizeFilePath(filePath, null); FileObject sourcefo = dataspaceRestApi.resolveFile(session, spaceName, filePath); if (!sourcefo.exists() || !sourcefo.isWriteable()) { RuntimeException ex = new IllegalArgumentException( "File or Folder " + filePath + " does not exist or is not writable in space " + spaceName); logger.error(ex); throw ex; } if (sourcefo.getType().equals(FileType.FILE)) { logger.info("[deleteFile] deleting file " + sourcefo.getURL()); sourcefo.delete(); } else if (sourcefo.getType().equals(FileType.FOLDER)) { logger.info("[deleteFile] deleting folder (and all its descendants) " + sourcefo.getURL()); sourcefo.delete(Selectors.SELECT_ALL); } else { RuntimeException ex = new IllegalArgumentException( "File " + filePath + " has an unsupported type " + sourcefo.getType()); logger.error(ex); throw ex; } return true; }
From source file:org.pentaho.big.data.impl.vfs.hdfs.HDFSFileObject.java
@Override protected FileType doGetType() throws Exception { HadoopFileStatus status = null;/*from ww w . j a v a 2 s .com*/ try { status = hdfs.getFileStatus(hdfs.getPath(getName().getPath())); } catch (Exception ex) { // Ignore } if (status == null) { return FileType.IMAGINARY; } else if (status.isDir()) { return FileType.FOLDER; } else { return FileType.FILE; } }
From source file:org.pentaho.big.data.impl.vfs.hdfs.HDFSFileObjectTest.java
@Test public void testDoGetTypeFolder() throws Exception { HadoopFileStatus hadoopFileStatus = mock(HadoopFileStatus.class); when(hadoopFileSystem.getFileStatus(hadoopFileSystemPath)).thenReturn(hadoopFileStatus); when(hadoopFileStatus.isDir()).thenReturn(true); assertEquals(FileType.FOLDER, hdfsFileObject.doGetType()); }
From source file:org.pentaho.di.core.hadoop.HadoopConfigurationBootstrap.java
public synchronized List<HadoopConfigurationInfo> getHadoopConfigurationInfos() throws KettleException, ConfigurationException, IOException { List<HadoopConfigurationInfo> result = new ArrayList<>(); FileObject hadoopConfigurationsDir = resolveHadoopConfigurationsDirectory(); // If the folder doesn't exist, return an empty list if (hadoopConfigurationsDir.exists()) { String activeId = getActiveConfigurationId(); String willBeActiveId = getWillBeActiveConfigurationId(); for (FileObject childFolder : hadoopConfigurationsDir.getChildren()) { if (childFolder.getType() == FileType.FOLDER) { String id = childFolder.getName().getBaseName(); FileObject configPropertiesFile = childFolder.getChild(CONFIG_PROPERTIES); if (configPropertiesFile.exists()) { Properties properties = new Properties(); properties.load(configPropertiesFile.getContent().getInputStream()); result.add(new HadoopConfigurationInfo(id, properties.getProperty("name", id), id.equals(activeId), willBeActiveId.equals(id))); }// w w w . ja va 2 s . c om } } } return result; }