@Override public List<Resource> list() { List<Resource> children = new ArrayList<Resource>(); for (Resource child : delegate.list()) { children.add(new SerializableResourceWrapper(child)); } return children; }
/** * Returns filtered children of a directory * * @param dir parent directory * @param filter the filter that selects children * @param recursive searches recursively * @return filtered list */ public static List<Resource> list(Resource dir, Filter<Resource> filter, boolean recursive) { List<Resource> res = new ArrayList<Resource>(); for (Resource child : dir.list()) { if (filter.accept(child)) { res.add(child); } if (recursive && child.getType() == Type.DIRECTORY) { res.addAll(list(child, filter, true)); } } return res; }
SortedSet<String> listFiles(Resource dir) { SortedSet<String> result = new TreeSet<String>(); List<Resource> dirs = dir.list(); for (Resource d : dirs) { if (d.getType() == Type.DIRECTORY && d.get(CONFIG_FILENAME).getType() == Type.RESOURCE) { result.add(d.name()); } } return result; }
/** * Search for resources using pattern and last modified time. * * @param resource Resource indicated * @param lastModified time stamp to search from * @return list of modified resources */ public static List<Resource> search(Resource resource, long lastModified) { if (resource.getType() == Type.DIRECTORY) { ArrayList<Resource> results = new ArrayList<Resource>(); for (Resource child : resource.list()) { switch (child.getType()) { case RESOURCE: if (child.lastmodified() > lastModified) { results.add(child); } break; default: break; } } return results; } return Collections.emptyList(); }
root.list() .parallelStream() .filter(r -> filter.accept(r))
@Theory public void theoryLeavesHaveEmptyListOfChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(resource())); Collection<Resource> result = res.list(); assertThat(result, empty()); }
@Theory public void theoryUndefinedHaveEmptyListOfChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(undefined())); Collection<Resource> result = res.list(); assertThat(result, empty()); }
@Theory public void theoryDirectoriesHaveChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Collection<Resource> result = res.list(); assertThat(result, notNullValue()); }
@Theory public void theoryChildrenKnowTheirParents(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Collection<Resource> children = res.list(); assumeThat(children, not(empty())); // Make sure this resource has children for (Resource child : children) { Resource parent = child.parent(); assertThat(parent, equalTo(res)); } }
/** * Write the contents of a resource into another resource. Also supports directories * (recursively). * * @param data resource to read * @param destination resource to write to * @throws IOException If data could not be copied to destination */ public static void copy(Resource data, Resource destination) throws IOException { if (data.getType() == Type.DIRECTORY) { for (Resource child : data.list()) { copy(child, destination.get(child.name())); } } else { try (InputStream in = data.in()) { copy(in, destination); } } }
@Theory public void theoryParentsKnowTheirChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Resource parent = res.parent(); assumeThat(path, parent, notNullValue()); // Make sure this resource has a parent Collection<Resource> result = parent.list(); assertThat(path, result, hasItem(res)); // this assumed equals was written! }
@Theory public void theoryRecursiveDelete(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, is(directory())); assumeThat(res, is(directory())); Collection<Resource> result = res.list(); assumeThat(result.size(), greaterThan(0)); assertTrue(res.delete()); }
@Theory public void theoryAddingFileToDirectoryAddsResource(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); File dir = res.dir(); File file = new File(dir, "newFileCreatedDirectly"); assumeTrue(file.createNewFile()); Resource child = getResource(Paths.path(res.path(), "newFileCreatedDirectly")); Collection<Resource> children = res.list(); assertThat(child, is(defined())); assertThat(children, hasItem(child)); }
@Theory public void theoryDirectoriesHaveFileWithSameNamedChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); File dir = res.dir(); Collection<Resource> resChildren = res.list(); String[] fileChildrenNames = dir.list(); String[] resChildrenNames = new String[resChildren.size()]; int i = 0; for (Resource child : resChildren) { resChildrenNames[i] = child.name(); i++; } assertThat(fileChildrenNames, arrayContainingInAnyOrder(resChildrenNames)); }
@Test public void testReloadDefaultStyles() throws Exception { // clear up all "point" styles final Resource styles = getDataDirectory().getStyles(); styles.list() .stream() .filter(r -> r.getType() == Resource.Type.RESOURCE && r.name().contains("point")) .forEach(r -> r.delete()); // reload getGeoServer().reload(); // check the default point style has been re-created final StyleInfo point = getCatalog().getStyleByName("point"); assertNotNull(point); }
.list() .parallelStream() .filter(r -> Resources.DirectoryFilter.INSTANCE.accept(r))
Map<String, Map<String, Object>> dataStores = creader.dataStores(); for (Resource featureTypeDir : featureTypesDir.list()) { if (featureTypeDir.getType() != Type.DIRECTORY) { continue; if (destFeatureTypeDir != null) { for (Resource file : featureTypeDir.list()) { if (file.getType() == Type.RESOURCE && !featureTypeInfo.equals(file)) { IOUtils.copy(file.in(), destFeatureTypeDir.get(file.name()).out());
@Theory public void theoryDirectoriesHaveChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Collection<Resource> result = res.list(); assertThat(result, notNullValue()); }
@Theory public void theoryParentsKnowTheirChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Resource parent = res.parent(); assumeThat(path, parent, notNullValue()); // Make sure this resource has a parent Collection<Resource> result = parent.list(); assertThat(path, result, hasItem(res)); // this assumed equals was written! }