use of java.nio.file.Path in project crate by crate.
the class FileReadingIterator method getUrisWithGlob.
private List<UriWithGlob> getUrisWithGlob(Collection<String> fileUris) {
List<UriWithGlob> uris = new ArrayList<>(fileUris.size());
for (String fileUri : fileUris) {
URI uri = toURI(fileUri);
URI preGlobUri = null;
Predicate<URI> globPredicate = null;
Matcher hasGlobMatcher = HAS_GLOBS_PATTERN.matcher(uri.toString());
if (hasGlobMatcher.matches()) {
if (fileUri.startsWith("/") || fileUri.startsWith("file://")) {
/*
* Substitute a symlink with the real path.
* The wildcard needs to be maintained, though, because it is used to generate the matcher.
* Take the part before the wildcard (*) and try to resolved the real path.
* If the part before the wildcard contains a part of the filename (e.g. /tmp/foo_*.json) then use the
* parent directory of this filename to resolved the real path.
* Then replace this part with the real path and generate the URI.
*/
Path oldPath = Paths.get(toURI(hasGlobMatcher.group(1)));
if (!Files.isDirectory(oldPath)) {
oldPath = oldPath.getParent();
}
String oldPathAsString;
String newPathAsString;
try {
oldPathAsString = oldPath.toUri().toString();
newPathAsString = oldPath.toRealPath().toUri().toString();
} catch (IOException e) {
continue;
}
String resolvedFileUrl = uri.toString().replace(oldPathAsString, newPathAsString);
uri = toURI(resolvedFileUrl);
preGlobUri = toURI(newPathAsString);
} else {
preGlobUri = URI.create(hasGlobMatcher.group(1));
}
globPredicate = new GlobPredicate(uri);
}
uris.add(new UriWithGlob(uri, preGlobUri, globPredicate));
}
return uris;
}
use of java.nio.file.Path in project cryptomator by cryptomator.
the class MainApplication method handleCommandLineArg.
private void handleCommandLineArg(String arg, Stage primaryStage, MainController mainCtrl) {
// find correct location:
final Path path = FileSystems.getDefault().getPath(arg);
final Path vaultPath;
if (Files.isDirectory(path)) {
vaultPath = path;
} else if (Files.isRegularFile(path)) {
vaultPath = path.getParent();
} else {
LOG.warn("Invalid vault path %s", arg);
return;
}
// add vault to ctrl:
Platform.runLater(() -> {
mainCtrl.addVault(vaultPath, true);
primaryStage.setIconified(false);
primaryStage.show();
primaryStage.toFront();
primaryStage.requestFocus();
});
}
use of java.nio.file.Path in project crate by crate.
the class AdminUIStaticFileRequestFilter method serveSite.
private void serveSite(RestRequest request, RestChannel channel) throws IOException {
if (request.method() != RestRequest.Method.GET) {
channel.sendResponse(new BytesRestResponse(FORBIDDEN));
return;
}
String sitePath = StringUtils.stripFront(request.rawPath(), '/');
// this is a relative path under _site configured by the plugin.
if (sitePath.length() == 0) {
sitePath = "index.html";
}
final Path siteFile = environment.pluginsFile().resolve("crate-admin").resolve("_site");
final String separator = siteFile.getFileSystem().getSeparator();
// Convert file separators.
sitePath = sitePath.replace("/", separator);
Path file = siteFile.resolve(sitePath);
// return not found instead of forbidden to prevent malicious requests to find out if files exist or don't exist
if (!Files.exists(file) || FileSystemUtils.isHidden(file) || !file.toAbsolutePath().normalize().startsWith(siteFile.toAbsolutePath().normalize())) {
channel.sendResponse(new BytesRestResponse(NOT_FOUND));
return;
}
BasicFileAttributes attributes = readAttributes(file, BasicFileAttributes.class);
if (!attributes.isRegularFile()) {
// If it's not a regular file, we send a 403
channel.sendResponse(new BytesRestResponse(FORBIDDEN));
return;
}
try {
byte[] data = Files.readAllBytes(file);
channel.sendResponse(new BytesRestResponse(OK, guessMimeType(file.toAbsolutePath().toString()), data));
} catch (IOException e) {
channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR));
}
}
use of java.nio.file.Path in project che by eclipse.
the class XMLTreeTest method shouldBeAbleToCreateTreeFromPath.
@Test
public void shouldBeAbleToCreateTreeFromPath() throws Exception {
final byte[] bytes = XML_CONTENT.getBytes();
final Path path = targetDir().resolve("test-xml.xml");
write(path, bytes);
final XMLTree tree = XMLTree.from(path);
assertEquals(tree.getBytes(), bytes);
delete(path);
}
use of java.nio.file.Path in project che by eclipse.
the class PersistTestModuleBuilderTest method generatesPersistenceXml.
@Test
public void generatesPersistenceXml() throws Exception {
Path path = new PersistTestModuleBuilder().setDriver("org.h2.Driver").addEntityClass(MyEntity1.class).addEntityClass("org.eclipse.che.commons.test.db.PersistTestModuleBuilderTest$MyEntity2").setUrl("jdbc:h2:mem:test").setUser("username").setPassword("secret").setLogLevel("FINE").setPersistenceUnit("test-unit").setExceptionHandler(MyExceptionHandler.class).setProperty("custom-property", "value").savePersistenceXml();
URL url = Thread.currentThread().getContextClassLoader().getResource("org/eclipse/che/commons/test/db/test-persistence-1.xml");
assertNotNull(url);
assertEquals(new String(Files.readAllBytes(path), UTF_8), Resources.toString(url, UTF_8));
}
Aggregations