/** * Quick check of path for invalid characters * * @param path * @return path * @throws IllegalArgumentException If path fails {@link #VALID} check */ public static String valid(String path) { return path(STRICT_PATH, path); }
@Override public String path() { return Paths.convert(file.getPath()); }
/** * Path construction. * * @param strictPath whether problematic characters are an error * @param path Items defining a Path * @return path Path used to identify a Resource */ static String path(boolean strictPath, String... path) { if (path == null || (path.length == 1 && path[0] == null)) { return null; } ArrayList<String> names = new ArrayList<String>(); for (String item : path) { names.addAll(names(item)); } return toPath(strictPath, names); }
@Test public void pathTest() { assertEquals(2, names("a/b").size()); assertEquals(1, names("a/").size()); assertEquals(1, names("a").size()); assertEquals(0, names("").size()); assertEquals(BASE, path("")); assertEquals("directory/file.txt", path("directory", "file.txt")); assertEquals("directory/folder/file3.txt", path("directory/folder", "file3.txt")); assertNull(path((String) null)); // edge case assertEquals("foo", path("foo/")); assertEquals("foo", path(".", "foo")); fail(". invalid relative path"); } catch (IllegalArgumentException expected) { assertEquals("foo", path("foo/bar", "..")); fail(".. invalid relative path"); } catch (IllegalArgumentException expected) { assertEquals(name, Paths.path(true, name)); assertEquals(name, Paths.path(false, name)); assertEquals(name, Paths.path(true, name)); fail("invalid: " + name); } catch (IllegalArgumentException expected) { assertEquals(name, Paths.path(false, name));
Files.asResource( new File( Paths.toFile( dd.get(Paths.BASE).dir(), Paths.path("workspaces", "gs", "foo", "t1")), "header.ftl"))); File srcFakeFtl = Files.asResource( new File( Paths.toFile( dd.get(Paths.BASE).dir(), Paths.path("workspaces", "gs", "foo", "t1")), "fake.ftl"))); Files.asResource( new File( Paths.toFile( td.get(Paths.BASE).dir(), Paths.path("workspaces", "gs", "foo", "t1")), "header.ftl")); Resource trgFakeFtl = Files.asResource( new File( Paths.toFile( td.get(Paths.BASE).dir(), Paths.path("workspaces", "gs", "foo", "t1")), "fake.ftl"));
@Override public File getFile(String path) { return Paths.toFile(baseDirectory, path); } });
/** * Convert a Resource path to file reference for provided base directory. * * <p>This method requires the base directory of the ResourceStore. Note ResourceStore * implementations may not create the file until needed. In the case of an absolute path, base * should be null. * * @param base Base directory, often GeoServer Data Directory * @param path Resource path reference * @return File reference */ public static File toFile(File base, String path) { for (String item : Paths.names(path)) { base = new File(base, item); } return base; } }
@Override public Resource parent() { return ResourceStore.EMPTY.get(Paths.parent(path)); }
@Override public String name() { return Paths.name(path); }
@Test public void pathTest() { assertEquals(2, names("a/b").size()); assertEquals(1, names("a/").size()); assertEquals(1, names("a").size()); assertEquals(0, names("").size()); assertEquals(BASE, path("")); assertEquals("directory/file.txt", path("directory", "file.txt")); assertEquals("directory/folder/file3.txt", path("directory/folder", "file3.txt")); assertNull(path((String) null)); // edge case assertEquals("foo", path("foo/")); assertEquals("foo", path(".", "foo")); fail(". invalid relative path"); } catch (IllegalArgumentException expected) { assertEquals("foo", path("foo/bar", "..")); fail(".. invalid relative path"); } catch (IllegalArgumentException expected) { assertEquals(name, Paths.path(true, name)); assertEquals(name, Paths.path(false, name)); assertEquals(name, Paths.path(true, name)); fail("invalid: " + name); } catch (IllegalArgumentException expected) { assertEquals(name, Paths.path(false, name));
public FileSystemResource(String path) { this.path = path; this.file = Paths.toFile(baseDirectory, path); }
List<String> items = Paths.names(path); int index = 0; if (index < items.size()) {
@Override public boolean move(String path, String target) { path = Paths.valid(path); target = Paths.valid(target); File file = Paths.toFile(baseDirectory, path); File dest = Paths.toFile(baseDirectory, target); if (!file.exists() && !dest.exists()) { return true; // moving an undefined resource } try { dest.getParentFile().mkdirs(); // Make sure there's somewhere to move to. java.nio.file.Files.move( java.nio.file.Paths.get(file.getAbsolutePath()), java.nio.file.Paths.get(dest.getAbsolutePath()), StandardCopyOption.ATOMIC_MOVE); return true; } catch (IOException e) { throw new IllegalStateException("Unable to move " + path + " to " + target, e); } }
@Override public String name() { return Paths.name(path); }
/** * Path construction. * * @param path Items defining a Path * @return path Path used to identify a Resource */ public static String path(String... path) { return path(STRICT_PATH, path); }