Example usage for java.nio.file Path toUri

List of usage examples for java.nio.file Path toUri

Introduction

In this page you can find the example usage for java.nio.file Path toUri.

Prototype

URI toUri();

Source Link

Document

Returns a URI to represent this path.

Usage

From source file:com.marklogic.entityservices.examples.CSVLoader.java

public void go() throws InterruptedException {

    logger.info("job started.");

    File dir = new File(projectDir + "/data/third-party/csv");

    WriteHostBatcher batcher = moveMgr.newWriteHostBatcher().withBatchSize(100).withThreadCount(10)
            .onBatchSuccess((client, batch) -> logger.info(getSummaryReport(batch)))
            .onBatchFailure((client, batch, throwable) -> {
                logger.warn("FAILURE on batch:" + batch.toString() + "\n", throwable);
                throwable.printStackTrace();
            });/*w  ww. j a  v a 2  s .  c o  m*/

    ticket = moveMgr.startJob(batcher);

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir.toPath(), "*.csv")) {
        for (Path entry : stream) {
            logger.debug("Adding " + entry.getFileName().toString());

            MappingIterator<ObjectNode> it = csvMapper.readerFor(ObjectNode.class).with(bootstrapSchema)
                    .readValues(entry.toFile());
            long i = 0;
            while (it.hasNext()) {
                ObjectNode jsonNode = it.next();
                String jsonString = mapper.writeValueAsString(jsonNode);

                String uri = entry.toUri().toString() + "-" + Long.toString(i++) + ".json";
                DocumentMetadataHandle metadata = new DocumentMetadataHandle() //
                        .withCollections("raw", "csv") //
                        .withPermission("race-reader", Capability.READ) //
                        .withPermission("race-writer", Capability.INSERT, Capability.UPDATE);
                batcher.add(uri, metadata, new StringHandle(jsonString));
                if (i % 1000 == 0)
                    logger.debug("Inserting JSON document " + uri);
            }
            it.close();
        }
    }

    catch (IOException e)

    {
        e.printStackTrace();
    }

    batcher.flush();
}

From source file:edu.emory.mathcs.nlp.util.IOUtilsTest.java

@Test
public void fileNonStdFileSystem() throws Exception {
    FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
    Path testFile = fs.getPath("/foo.txt");
    try (Writer writer = Files.newBufferedWriter(testFile)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }/*from   w  w  w. j ava 2  s .c o m*/

    try (InputStream is = IOUtils.createArtifactInputStream(testFile)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    String uri = testFile.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    Path testFilexz = fs.getPath("/foo.txt.xz");
    try (Writer writer = new OutputStreamWriter(
            new XZOutputStream(Files.newOutputStream(testFilexz), new LZMA2Options()), UTF_8)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }

    try (InputStream is = IOUtils.createArtifactInputStream(testFilexz)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    uri = testFilexz.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    Path testFilegz = fs.getPath("/foo.txt.gz");
    try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(Files.newOutputStream(testFilegz)),
            UTF_8)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }

    try (InputStream is = IOUtils.createArtifactInputStream(testFilegz)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    uri = testFilegz.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    // test plain old file system
    try (InputStream is = IOUtils.createArtifactInputStream("src/test/resources/a/test/some.txt")) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    // test classpath
    try (InputStream is = IOUtils.createArtifactInputStream("a/test/some.txt")) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }
}

From source file:org.apache.coheigea.bigdata.knox.ranger.KnoxRangerTest.java

private void makeHBaseInvocation(int statusCode, String user, String password) throws IOException {
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }//  w  w w  .ja va  2s  .c o  m
    Path path = FileSystems.getDefault().getPath(basedir, "/src/test/resources/webhbase-table-list.xml");

    hbaseServer.expect().method("GET").pathInfo("/").header("Accept", ContentType.XML.toString()).respond()
            .status(HttpStatus.SC_OK).content(IOUtils.toByteArray(path.toUri()))
            .contentType(ContentType.XML.toString());

    given().log().all().auth().preemptive().basic(user, password).header("X-XSRF-Header", "jksdhfkhdsf")
            .header("Accept", ContentType.XML.toString()).when()
            .get("http://localhost:" + gateway.getAddresses()[0].getPort() + "/gateway/cluster/hbase").then()
            .statusCode(statusCode).log().body();
}

From source file:it.tidalwave.bluemarine2.persistence.impl.DefaultPersistence.java

/*******************************************************************************************************************
 *
 * Imports the repository from the given file.
 *
 * @param   path                    where to import the data from
 * @throws  RDFHandlerException// ww w  . ja v a  2  s  .  c om
 * @throws  IOException
 * @throws  RepositoryException
 *
 ******************************************************************************************************************/
private void importFromFile(final @Nonnull Path path)
        throws IOException, RepositoryException, RDFParseException {
    try (final RepositoryConnection connection = repository.getConnection();
            final Reader reader = Files.newBufferedReader(path, UTF_8)) {
        log.info("Importing repository from {} ...", path);
        connection.add(reader, path.toUri().toString(), RDFFormat.N3);
        connection.commit();
    }
}

From source file:org.matonto.etl.rest.impl.DelimitedRestImpl.java

/**
 * Finds the uploaded delimited file with the specified name.
 *
 * @param fileName the name of the uploaded delimited file
 * @return the uploaded file if it was found
 *///www.  jav a  2s .co m
private Optional<File> getUploadedFile(String fileName) {
    Path filePath = Paths.get(TEMP_DIR + "/" + fileName);
    if (Files.exists(filePath)) {
        return Optional.of(new File(filePath.toUri()));
    } else {
        return Optional.empty();
    }
}

From source file:com.github.jrialland.ajpclient.AbstractTomcatTest.java

@SuppressWarnings("serial")
protected void addStaticResource(final String mapping, final Path file) {

    if (!Files.isRegularFile(file)) {
        final FileNotFoundException fnf = new FileNotFoundException(file.toString());
        fnf.fillInStackTrace();/*from w w w  .  j  a  v a 2 s . co m*/
        throw new IllegalArgumentException(fnf);
    }

    String md5;
    try {
        final InputStream is = file.toUri().toURL().openStream();
        md5 = computeMd5(is);
        is.close();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    final String fMd5 = md5;

    addServlet(mapping, new HttpServlet() {
        @Override
        protected void service(final HttpServletRequest req, final HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setContentLength((int) Files.size(file));
            resp.setHeader("Content-MD5", fMd5);
            final String mime = Files.probeContentType(file);
            if (mime != null) {
                resp.setContentType(mime);
            }
            final OutputStream out = resp.getOutputStream();
            Files.copy(file, out);
            out.flush();
        }
    });
}

From source file:org.apache.coheigea.bigdata.knox.ranger.KnoxRangerTest.java

private void makeWebHDFSInvocation(int statusCode, String user, String password) throws IOException {

    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }/*from  w w w .  jav a2s  .c  om*/
    Path path = FileSystems.getDefault().getPath(basedir, "/src/test/resources/webhdfs-liststatus-test.json");

    hdfsServer.expect().method("GET").pathInfo("/v1/hdfstest").queryParam("op", "LISTSTATUS").respond()
            .status(HttpStatus.SC_OK).content(IOUtils.toByteArray(path.toUri()))
            .contentType("application/json");

    ValidatableResponse response = given().log().all().auth().preemptive().basic(user, password)
            .header("X-XSRF-Header", "jksdhfkhdsf").queryParam("op", "LISTSTATUS").when()
            .get("http://localhost:" + gateway.getAddresses()[0].getPort() + "/gateway/cluster/webhdfs"
                    + "/v1/hdfstest")
            .then().statusCode(statusCode).log().body();

    if (statusCode == HttpStatus.SC_OK) {
        response.body("FileStatuses.FileStatus[0].pathSuffix", is("dir"));
    }
}

From source file:org.apache.coheigea.bigdata.knox.ranger.KnoxRangerTest.java

private void makeSolrInvocation(int statusCode, String user, String password) throws IOException {
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }/*w w w  . ja va 2  s. c  o  m*/
    Path path = FileSystems.getDefault().getPath(basedir, "/src/test/resources/query_response.xml");

    solrServer.expect().method("GET").pathInfo("/solr/gettingstarted/select")
            .queryParam("q", "author_s:William+Shakespeare").respond().status(HttpStatus.SC_OK)
            .content(IOUtils.toByteArray(path.toUri())).contentType("application/json");

    given().auth().preemptive().basic(user, password).header("X-XSRF-Header", "jksdhfkhdsf")
            .header("Accept", "application/json").when()
            .get("http://localhost:" + gateway.getAddresses()[0].getPort() + "/gateway/cluster/solr"
                    + "/gettingstarted/select?q=author_s:William+Shakespeare")
            .then().log().all().statusCode(statusCode);

}

From source file:org.spoutcraft.api.addon.AddonLoader.java

public Addon load(Path path) throws InvalidAddonException, InvalidDescriptionException {
    final AddonDescription description = create(path);
    Addon addon = null;/*from   w  ww  .  ja v a 2  s  .  c  om*/
    AddonClassLoader loader;

    if (description.isValidMode(game.getSide())) {
        final Path dataPath = Paths.get(path.getParent().toString(), description.getIdentifier());
        try {
            loader = new AddonClassLoader(this.getClass().getClassLoader(), this);
            loader.addURL(path.toUri().toURL());
            Class<?> addonMain = Class.forName(description.getMain(), true, loader);
            Class<? extends Addon> addonClass = addonMain.asSubclass(Addon.class);
            Constructor<? extends Addon> constructor = addonClass.getConstructor();
            addon = constructor.newInstance();
            addon.initialize(game, this, description, loader, dataPath, path);
            addonMD5s.put(description.getIdentifier(), DigestUtils.md5Hex(new FileInputStream(path.toFile())));
        } catch (Exception e) {
            throw new InvalidAddonException(e);
        }
        loader.setAddon(addon);
        loaders.put(description.getIdentifier(), loader);
    }
    return addon;
}

From source file:org.perfcake.util.Utils.java

/**
 * Converts location to URL with check for the location existence. If location specifies a protocol, it is immediately converted. Without a protocol specified, the following paths
 * are checked for the existence://  w ww. j a  v  a2s  . com
 * 1. file://location
 * 2. file://$defaultLocationProperty/location or file://defaultLocation/location (when the property is not set)
 * 3. file://$defaultLocationProperty/location.suffix or file://defaultLocation/location.suffix (when the property is not set) with all the provided suffixes
 * If the file was not found, the result is simply file://location
 *
 * @param location
 *       The location of the resource.
 * @param defaultLocationProperty
 *       The property to read the default location prefix.
 * @param defaultLocation
 *       The default value for defaultLocationProperty if this property is undefined.
 * @param defaultSuffix
 *       The array of default default suffixes to try when searching for the resource.
 * @return The URL representing the location.
 * @throws MalformedURLException
 *       When the location cannot be converted to an URL.
 */
public static URL locationToUrlWithCheck(final String location, final String defaultLocationProperty,
        final String defaultLocation, final String... defaultSuffix) throws MalformedURLException {
    String uri;

    // if we are looking for a file and there is no path specified, remove the prefix for later automatic directory insertion
    if (location.startsWith("file://")) {
        uri = location.substring(7);
    } else {
        uri = location;
    }

    // if there is no protocol specified, try some file locations
    if (!uri.contains("://")) {
        boolean found = false;
        Path p = Paths.get(uri);

        if (!Files.exists(p) || Files.isDirectory(p)) {
            p = Paths.get(Utils.getProperty(defaultLocationProperty, defaultLocation), uri);

            if (!Files.exists(p) || Files.isDirectory(p)) {
                if (defaultSuffix != null && defaultSuffix.length > 0) {
                    //                  boolean found = false;

                    for (final String suffix : defaultSuffix) {
                        p = Paths.get(Utils.getProperty(defaultLocationProperty, defaultLocation),
                                uri + suffix);
                        if (Files.exists(p) && !Files.isDirectory(p)) {
                            found = true;
                            break;
                        }
                    }
                }
            } else {
                found = true;
            }
        } else {
            found = true;
        }

        if (found) {
            uri = p.toUri().toString();
        } else {
            uri = "file://" + uri;
        }
    }

    return new URL(uri);
}