Example usage for java.nio.file FileSystem getSeparator

List of usage examples for java.nio.file FileSystem getSeparator

Introduction

In this page you can find the example usage for java.nio.file FileSystem getSeparator.

Prototype

public abstract String getSeparator();

Source Link

Document

Returns the name separator, represented as a string.

Usage

From source file:business.services.FileService.java

public HttpEntity<InputStreamResource> downloadAccessLog(String filename,
        boolean writeContentDispositionHeader) {
    try {/* w w w.ja va  2s .  com*/
        FileSystem fileSystem = FileSystems.getDefault();
        Path path = fileSystem.getPath(accessLogsPath).normalize();
        filename = filename.replace(fileSystem.getSeparator(), "_");
        filename = URLEncoder.encode(filename, "utf-8");

        Path f = fileSystem.getPath(accessLogsPath, filename).normalize();
        // filter path names that point to places outside the logs path.
        // E.g., to prevent that in cases where clients use '../' in the filename
        // arbitrary locations are reachable.
        if (!Files.isSameFile(path, f.getParent())) {
            // Path f is not in the upload path. Maybe 'name' contains '..'?
            log.error("Invalid filename: " + filename);
            throw new FileDownloadError("Invalid file name");
        }
        if (!Files.isReadable(f)) {
            log.error("File does not exist: " + filename);
            throw new FileDownloadError("File does not exist");
        }

        InputStream input = new FileInputStream(f.toFile());
        InputStreamResource resource = new InputStreamResource(input);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_PLAIN);
        if (writeContentDispositionHeader) {
            headers.set("Content-Disposition", "attachment; filename=" + filename.replace(" ", "_"));
        }
        HttpEntity<InputStreamResource> response = new HttpEntity<InputStreamResource>(resource, headers);
        return response;
    } catch (IOException e) {
        log.error(e);
        throw new FileDownloadError();
    }
}

From source file:com.liferay.sync.engine.SyncSystemTest.java

protected String getString(JsonNode jsonNode, String key, String defaultValue) {

    JsonNode childJsonNode = jsonNode.get(key);

    if (childJsonNode == null) {
        return defaultValue;
    }//from ww w. jav  a2 s . c  om

    FileSystem fileSystem = FileSystems.getDefault();

    String value = childJsonNode.textValue();

    return value.replace("/", fileSystem.getSeparator());
}

From source file:com.liferay.sync.engine.SyncSystemTest.java

protected void addFile(Path testFilePath, JsonNode stepJsonNode) throws Exception {

    SyncSite syncSite = getSyncSite(stepJsonNode);

    String dependency = getString(stepJsonNode, "dependency");

    Path dependencyFilePath = getDependencyFilePath(testFilePath, dependency);

    FileSystem fileSystem = FileSystems.getDefault();

    dependency = getString(stepJsonNode, "target",
            dependency.replace("common" + fileSystem.getSeparator(), ""));

    Path targetFilePath = Paths.get(FileUtil.getFilePathName(syncSite.getFilePathName(), dependency));

    Files.copy(dependencyFilePath, targetFilePath);
}

From source file:com.liferay.sync.engine.SyncSystemTest.java

protected void addFolder(Path testFilePath, JsonNode stepJsonNode) throws Exception {

    SyncSite syncSite = getSyncSite(stepJsonNode);

    String dependency = getString(stepJsonNode, "dependency");

    final Path dependencyFilePath = getDependencyFilePath(testFilePath, dependency);

    FileSystem fileSystem = FileSystems.getDefault();

    final Path targetFilePath = Paths.get(FileUtil.getFilePathName(syncSite.getFilePathName(),
            dependency.replace("common" + fileSystem.getSeparator(), "")));

    Files.walkFileTree(dependencyFilePath, new SimpleFileVisitor<Path>() {

        @Override//  w  w w  .  ja v  a 2 s .  c  om
        public FileVisitResult preVisitDirectory(Path filePath, BasicFileAttributes basicFileAttributes)
                throws IOException {

            Path relativeFilePath = dependencyFilePath.relativize(filePath);

            Files.createDirectories(targetFilePath.resolve(relativeFilePath));

            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path filePath, BasicFileAttributes basicFileAttributes)
                throws IOException {

            Path relativeFilePath = dependencyFilePath.relativize(filePath);

            Files.copy(filePath, targetFilePath.resolve(relativeFilePath));

            return FileVisitResult.CONTINUE;
        }

    });
}

From source file:com.liferay.sync.engine.service.persistence.SyncFilePersistence.java

public void renameByParentFilePathName(final String sourceParentFilePathName,
        final String targetParentFilePathName) throws SQLException {

    Callable<Object> callable = new Callable<Object>() {

        @Override/*w  ww .  jav  a 2  s.  c  om*/
        public Object call() throws Exception {
            FileSystem fileSystem = FileSystems.getDefault();

            List<SyncFile> syncFiles = findByParentFilePathName(sourceParentFilePathName);

            for (SyncFile syncFile : syncFiles) {
                String filePathName = syncFile.getFilePathName();

                filePathName = StringUtils.replaceOnce(filePathName,
                        sourceParentFilePathName + fileSystem.getSeparator(),
                        targetParentFilePathName + fileSystem.getSeparator());

                syncFile.setFilePathName(filePathName);

                update(syncFile);
            }

            return null;
        }

    };

    callBatchTasks(callable);
}