Example usage for java.nio.file Path toAbsolutePath

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

Introduction

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

Prototype

Path toAbsolutePath();

Source Link

Document

Returns a Path object representing the absolute path of this path.

Usage

From source file:org.tallison.cc.CCGetter.java

private void fetch(CCIndexRecord r, Path rootDir, BufferedWriter writer) throws IOException {
    Path targFile = rootDir.resolve(r.getDigest().substring(0, 2) + "/" + r.getDigest());

    if (Files.isRegularFile(targFile)) {
        writeStatus(r, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer);
        logger.info("already retrieved:" + targFile.toAbsolutePath());
        return;/*from w w w. j av  a  2s . c  o m*/
    }

    String url = AWS_BASE + r.getFilename();
    URI uri = null;
    try {
        uri = new URI(url);
    } catch (URISyntaxException e) {
        logger.warn("Bad url: " + url);
        writeStatus(r, FETCH_STATUS.BAD_URL, writer);
        return;
    }
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpHost target = new HttpHost(uri.getHost());
    String urlPath = uri.getRawPath();
    if (uri.getRawQuery() != null) {
        urlPath += "?" + uri.getRawQuery();
    }
    HttpGet httpGet = null;
    try {
        httpGet = new HttpGet(urlPath);
    } catch (Exception e) {
        logger.warn("bad path " + uri.toString(), e);
        writeStatus(r, FETCH_STATUS.BAD_URL, writer);
        return;
    }
    if (proxyHost != null && proxyPort > -1) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
        httpGet.setConfig(requestConfig);
    }
    httpGet.addHeader("Range", r.getOffsetHeader());
    HttpCoreContext coreContext = new HttpCoreContext();
    CloseableHttpResponse httpResponse = null;
    URI lastURI = null;
    try {
        httpResponse = httpClient.execute(target, httpGet, coreContext);
        RedirectLocations redirectLocations = (RedirectLocations) coreContext
                .getAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);
        if (redirectLocations != null) {
            for (URI redirectURI : redirectLocations.getAll()) {
                lastURI = redirectURI;
            }
        } else {
            lastURI = httpGet.getURI();
        }
    } catch (IOException e) {
        logger.warn("IOException for " + uri.toString(), e);
        writeStatus(r, FETCH_STATUS.FETCHED_IO_EXCEPTION, writer);
        return;
    }
    lastURI = uri.resolve(lastURI);

    if (httpResponse.getStatusLine().getStatusCode() != 200
            && httpResponse.getStatusLine().getStatusCode() != 206) {
        logger.warn("Bad status for " + uri.toString() + " : " + httpResponse.getStatusLine().getStatusCode());
        writeStatus(r, FETCH_STATUS.FETCHED_NOT_200, writer);
        return;
    }
    Path tmp = null;
    Header[] headers = null;
    boolean isTruncated = false;
    try {
        //this among other parts is plagiarized from centic9's CommonCrawlDocumentDownload
        //probably saved me hours.  Thank you, Dominik!
        tmp = Files.createTempFile("cc-getter", "");
        try (InputStream is = new GZIPInputStream(httpResponse.getEntity().getContent())) {
            WARCRecord warcRecord = new WARCRecord(new FastBufferedInputStream(is), "", 0);
            ArchiveRecordHeader archiveRecordHeader = warcRecord.getHeader();
            if (archiveRecordHeader.getHeaderFields().containsKey(WARCConstants.HEADER_KEY_TRUNCATED)) {
                isTruncated = true;
            }
            headers = LaxHttpParser.parseHeaders(warcRecord, "UTF-8");

            Files.copy(warcRecord, tmp, StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (IOException e) {
        writeStatus(r, null, headers, 0L, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_READING_ENTITY,
                writer);
        deleteTmp(tmp);
        return;
    }

    String digest = null;
    long tmpLength = 0l;
    try (InputStream is = Files.newInputStream(tmp)) {
        digest = base32.encodeAsString(DigestUtils.sha1(is));
        tmpLength = Files.size(tmp);
    } catch (IOException e) {
        writeStatus(r, null, headers, tmpLength, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_SHA1, writer);
        logger.warn("IOException during digesting: " + tmp.toAbsolutePath());
        deleteTmp(tmp);
        return;
    }

    if (Files.exists(targFile)) {
        writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer);
        deleteTmp(tmp);
        return;
    }
    try {
        Files.createDirectories(targFile.getParent());
        Files.copy(tmp, targFile);
    } catch (IOException e) {
        writeStatus(r, digest, headers, tmpLength, isTruncated,
                FETCH_STATUS.FETCHED_EXCEPTION_COPYING_TO_REPOSITORY, writer);
        deleteTmp(tmp);

    }
    writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ADDED_TO_REPOSITORY, writer);
    deleteTmp(tmp);
}

From source file:codes.thischwa.c5c.impl.LocalConnector.java

@Override
public void createFolder(String backendDirectory, String sanitizedFolderName) throws C5CException {
    Path parentFolder = buildRealPathAndCheck(backendDirectory);
    Path newFolder = parentFolder.resolve(sanitizedFolderName);
    try {/*from ww w. j  a  va2 s.  co  m*/
        Files.createDirectories(newFolder);
    } catch (FileSystemAlreadyExistsException e) {
        logger.warn("Destination file already exists: {}", newFolder.toAbsolutePath());
        throw new FilemanagerException(FilemanagerAction.CREATEFOLDER, Key.DirectoryAlreadyExists,
                sanitizedFolderName);
    } catch (SecurityException | IOException e) {
        throw new FilemanagerException(FilemanagerAction.RENAME,
                FilemanagerException.Key.UnableToCreateDirectory, sanitizedFolderName);
    }
}

From source file:com.liferay.blade.cli.command.InstallExtensionCommandTest.java

@Test
public void testInstallCustomGithubExtension() throws Exception {
    Assume.assumeFalse(_isWindows());/* ww  w  .  jav  a2  s  .  c  o  m*/

    String[] args = { "extension", "install", "https://github.com/gamerson/blade-sample-command" };

    BladeTestResults bladeTestResults = TestUtil.runBlade(_rootDir, _extensionsDir, args);

    String output = bladeTestResults.getOutput();

    Assert.assertTrue("Expected output to contain \"successful\"\n" + output, output.contains(" successful"));

    Path rootPath = _rootDir.toPath();

    Path extensionJarPath = rootPath
            .resolve(Paths.get(".blade", "extensions", "blade-sample-command-master.jar"));

    boolean pathExists = Files.exists(extensionJarPath);

    Assert.assertTrue(extensionJarPath.toAbsolutePath() + " does not exist", pathExists);
}

From source file:org.sakuli.utils.SakuliPropertyPlaceholderConfigurerTest.java

@Test
public void testModifyPropertyFile() throws Exception {
    Path targetProps = Paths.get(this.getClass().getResource("properties-test/target.properties").toURI());

    Properties basicProps = new Properties();
    basicProps.put("test.prop.1", "test-value-1");
    basicProps.put("test.prop.2", "test-value-2");
    testling.modifyPropertiesConfiguration(targetProps.toAbsolutePath().toString(),
            Arrays.asList("test.prop.1", "test.prop.2"), basicProps);
    PropertiesConfiguration targetProfConf = new PropertiesConfiguration(targetProps.toFile());
    assertEquals(targetProfConf.getString("test.prop.1"), "test-value-1");
    assertEquals(targetProfConf.getString("test.prop.2"), "test-value-2");

    testling.restoreProperties();//  ww w  .j  av  a  2  s.  co m
    targetProfConf = new PropertiesConfiguration(targetProps.toFile());
    assertEquals(targetProfConf.getString("test.prop.1"), "xyz");
    assertEquals(targetProfConf.getString("test.prop.2"), "zyx");
}

From source file:com.vns.pdf.impl.PdfDocument.java

private PdfDocument(String pdfFileName) throws IOException {
    this.pdfFileName = pdfFileName;
    setWorkingDir();//ww  w  . j a v a2s. c  o  m
    Path filePath = Paths.get(pdfFileName);
    PosixFileAttributes attrs = Files.getFileAttributeView(filePath, PosixFileAttributeView.class)
            .readAttributes();
    String textAreaFileName = filePath.getFileName().toString() + "_" + filePath.toAbsolutePath().hashCode()
            + "_" + attrs.size() + "_" + attrs.lastModifiedTime().toString().replace(":", "_") + ".xml";
    textAreaFilePath = Paths.get(workingDir.toAbsolutePath().toString(), textAreaFileName);
    pdfTextStripper = new CustomPDFTextStripper();
    document = PDDocument.load(new File(pdfFileName));
    pdfRenderer = new PDFRenderer(document);

    if (Files.notExists(textAreaFilePath, LinkOption.NOFOLLOW_LINKS)) {
        pdfTextStripper.setSortByPosition(false);
        pdfTextStripper.setStartPage(0);
        pdfTextStripper.setEndPage(document.getNumberOfPages());

        this.doc = new Doc(new ArrayList<>(), new ArrayList<>());
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            PDPage pdPage = document.getPage(i);
            PDRectangle box = pdPage.getMediaBox();
            this.doc.getPages().add(new Page(new ArrayList<>(), new ArrayList<>(), (int) box.getWidth(),
                    (int) box.getHeight()));
        }

        Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
        try {
            pdfTextStripper.writeText(document, dummy);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
        parseBookmarksAnnotation();
        createTextAreaFile();
        //document.save(pdfFileName + ".pdf");
    } else {
        loadTextAreaFile();
    }
}

From source file:org.ballerinalang.composer.service.workspace.local.LocalFSWorkspace.java

private JsonObject getJsonObjForFile(Path root, boolean checkChildren) {
    JsonObject rootObj = new JsonObject();
    if (null != root.getFileName()) {
        Path fileName = root.getFileName();
        if (null != fileName) {
            rootObj.addProperty("text", fileName.toString());
        }/*from  w ww.  ja v  a  2 s. co m*/
    } else {
        rootObj.addProperty("text", root.toString());
    }
    rootObj.addProperty("id", root.toAbsolutePath().toString());
    if (Files.isDirectory(root) && checkChildren) {
        rootObj.addProperty("type", "folder");
        try {
            if (Files.list(root).count() > 0) {
                rootObj.addProperty("children", Boolean.TRUE);
            } else {
                rootObj.addProperty("children", Boolean.FALSE);
            }
        } catch (IOException e) {
            logger.debug("Error while fetching children of " + root.toString(), e);
            rootObj.addProperty("error", e.toString());
        }
    } else if (Files.isRegularFile(root) && checkChildren) {
        rootObj.addProperty("type", "file");
        rootObj.addProperty("children", Boolean.FALSE);
    }
    return rootObj;
}

From source file:com.qwazr.library.archiver.ArchiverTool.java

public void decompress_dir(final Path sourceDir, String sourceExtension, final Path destDir,
        final String destExtension) throws IOException, CompressorException {
    if (!Files.exists(sourceDir))
        throw new FileNotFoundException("The source directory does not exist: " + sourceDir.toAbsolutePath());
    if (!Files.exists(destDir))
        throw new FileNotFoundException(
                "The destination directory does not exist: " + destDir.toAbsolutePath());

    final Path[] sourceFiles;
    try (final Stream<Path> stream = Files.list(sourceDir)) {
        sourceFiles = stream.filter(p -> Files.isRegularFile(p)).toArray(Path[]::new);
    }//from ww  w  .j a v a  2s. co m
    if (sourceFiles == null)
        return;
    for (Path sourceFile : sourceFiles) {
        final String fileName = sourceFile.getFileName().toString();
        final String ext = FilenameUtils.getExtension(fileName);
        if (!sourceExtension.equals(ext))
            continue;
        String newName = FilenameUtils.getBaseName(fileName);
        if (destExtension != null)
            newName += '.' + destExtension;
        final Path destFile = destDir.resolve(newName);
        if (Files.exists(destFile))
            continue;
        decompress(sourceFile, destFile);
    }
}

From source file:com.nridge.connector.fs.con_fs.core.FileCrawler.java

/**
 * Invoked for a directory before entries in the directory are visited.
 * Unless overridden, this method returns {@link java.nio.file.FileVisitResult#CONTINUE}
 *
 * @param aDirectory Directory instance.
 * @param aFileAttributes File attribute instance.
 *//* w  ww. j  av  a  2s .c  om*/
@Override
public FileVisitResult preVisitDirectory(Path aDirectory, BasicFileAttributes aFileAttributes)
        throws IOException {
    Logger appLogger = mAppMgr.getLogger(this, "preVisitDirectory");

    if (mAppMgr.isAlive()) {
        String pathName = aDirectory.toAbsolutePath().toString();
        if (mCrawlFollow.isMatchedNormalized(pathName)) {
            appLogger.debug(String.format("Following Path: %s", pathName));
            return FileVisitResult.CONTINUE;
        } else {
            appLogger.debug(String.format("Skipping Path: %s", pathName));
            return FileVisitResult.SKIP_SUBTREE;
        }
    } else
        return FileVisitResult.TERMINATE;
}

From source file:org.tinymediamanager.core.movie.tasks.MovieUpdateDatasourceTask2.java

/**
 * simple NIO File.listFiles() replacement<br>
 * returns all files & folders in specified dir, filtering against our skip folders (NOT recursive)
 * //from w  w  w  .  ja va 2 s . c  o m
 * @param directory
 *          the folder to list the items for
 * @return list of files&folders
 */
public static List<Path> listFilesAndDirs(Path directory) {
    List<Path> fileNames = new ArrayList<>();
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
        for (Path path : directoryStream) {
            String fn = path.getFileName().toString().toUpperCase(Locale.ROOT);
            if (!skipFolders.contains(fn) && !fn.matches(skipRegex) && !MovieModuleManager.MOVIE_SETTINGS
                    .getMovieSkipFolders().contains(path.toFile().getAbsolutePath())) {
                fileNames.add(path.toAbsolutePath());
            } else {
                LOGGER.debug("Skipping: " + path);
            }
        }
    } catch (IOException ex) {
    }
    return fileNames;
}

From source file:org.apache.lucene.benchmark.byTask.feeds.LineDocSourceTest.java

private void doIndexAndSearchTestWithRepeats(Path file, Class<? extends LineParser> lineParserClass,
        int numAdds, String storedField) throws Exception {

    IndexReader reader = null;/*w w w. j  av  a2s . com*/
    IndexSearcher searcher = null;
    PerfRunData runData = null;
    try {
        Properties props = new Properties();

        // LineDocSource specific settings.
        props.setProperty("docs.file", file.toAbsolutePath().toString());
        if (lineParserClass != null) {
            props.setProperty("line.parser", lineParserClass.getName());
        }

        // Indexing configuration.
        props.setProperty("analyzer", WhitespaceAnalyzer.class.getName());
        props.setProperty("content.source", LineDocSource.class.getName());
        props.setProperty("directory", "RAMDirectory");
        props.setProperty("doc.stored", "true");
        props.setProperty("doc.index.props", "true");

        // Create PerfRunData
        Config config = new Config(props);
        runData = new PerfRunData(config);

        TaskSequence tasks = new TaskSequence(runData, "testBzip2", null, false);
        tasks.addTask(new CreateIndexTask(runData));
        for (int i = 0; i < numAdds; i++) {
            tasks.addTask(new AddDocTask(runData));
        }
        tasks.addTask(new CloseIndexTask(runData));
        try {
            tasks.doLogic();
        } finally {
            tasks.close();
        }

        reader = DirectoryReader.open(runData.getDirectory());
        searcher = newSearcher(reader);
        TopDocs td = searcher.search(new TermQuery(new Term("body", "body")), 10);
        assertEquals(numAdds, td.totalHits);
        assertNotNull(td.scoreDocs[0]);

        if (storedField == null) {
            storedField = DocMaker.BODY_FIELD; // added to all docs and satisfies field-name == value
        }
        assertEquals("Wrong field value", storedField, searcher.doc(0).get(storedField));
    } finally {
        IOUtils.close(reader, runData);
    }

}