Example usage for java.nio.file LinkOption NOFOLLOW_LINKS

List of usage examples for java.nio.file LinkOption NOFOLLOW_LINKS

Introduction

In this page you can find the example usage for java.nio.file LinkOption NOFOLLOW_LINKS.

Prototype

LinkOption NOFOLLOW_LINKS

To view the source code for java.nio.file LinkOption NOFOLLOW_LINKS.

Click Source Link

Document

Do not follow symbolic links.

Usage

From source file:org.opendedup.sdfs.filestore.cloud.BatchAwsS3ChunkStore.java

@Override
public void uploadFile(File f, String to, String pp) throws IOException {
    this.s3clientLock.readLock().lock();
    try {/*from   w w w . j  a  v  a 2s .co m*/
        InputStream in = null;
        while (to.startsWith(File.separator))
            to = to.substring(1);

        String pth = pp + "/" + EncyptUtils.encString(to, Main.chunkStoreEncryptionEnabled);
        SDFSLogger.getLog().info("uploading " + f.getPath() + " to " + to + " pth " + pth);
        boolean isDir = false;
        boolean isSymlink = false;
        if (!OSValidator.isWindows()) {
            isDir = Files.readAttributes(f.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS)
                    .isDirectory();
            isSymlink = Files.readAttributes(f.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS)
                    .isSymbolicLink();
        } else {
            isDir = f.isDirectory();
        }
        if (isSymlink) {
            try {
                HashMap<String, String> metaData = new HashMap<String, String>();
                metaData.put("encrypt", Boolean.toString(Main.chunkStoreEncryptionEnabled));
                metaData.put("lastmodified", Long.toString(f.lastModified()));
                String slp = EncyptUtils.encString(Files.readSymbolicLink(f.toPath()).toFile().getPath(),
                        Main.chunkStoreEncryptionEnabled);
                metaData.put("symlink", slp);
                ObjectMetadata md = new ObjectMetadata();
                md.setContentType("binary/octet-stream");
                md.setContentLength(pth.getBytes().length);
                md.setUserMetadata(metaData);
                PutObjectRequest req = new PutObjectRequest(this.name, pth,
                        new ByteArrayInputStream(pth.getBytes()), md);
                s3Service.putObject(req);
                if (this.isClustered())
                    this.checkoutFile(pth);
            } catch (Exception e1) {
                throw new IOException(e1);
            }
        } else if (isDir) {
            HashMap<String, String> metaData = FileUtils.getFileMetaData(f, Main.chunkStoreEncryptionEnabled);
            metaData.put("encrypt", Boolean.toString(Main.chunkStoreEncryptionEnabled));
            metaData.put("lastmodified", Long.toString(f.lastModified()));
            metaData.put("directory", "true");
            ObjectMetadata md = new ObjectMetadata();
            md.setContentType("binary/octet-stream");
            md.setContentLength(pth.getBytes().length);
            md.setUserMetadata(metaData);
            try {
                PutObjectRequest req = new PutObjectRequest(this.name, pth,
                        new ByteArrayInputStream(pth.getBytes()), md);
                s3Service.putObject(req);
                if (this.isClustered())
                    this.checkoutFile(pth);
            } catch (Exception e1) {
                SDFSLogger.getLog().error("error uploading", e1);
                throw new IOException(e1);
            }
        } else {
            String rnd = RandomGUID.getGuid();
            File p = new File(this.staged_sync_location, rnd);
            File z = new File(this.staged_sync_location, rnd + ".z");
            File e = new File(this.staged_sync_location, rnd + ".e");
            while (z.exists()) {
                rnd = RandomGUID.getGuid();
                p = new File(this.staged_sync_location, rnd);
                z = new File(this.staged_sync_location, rnd + ".z");
                e = new File(this.staged_sync_location, rnd + ".e");
            }
            try {
                BufferedInputStream is = new BufferedInputStream(new FileInputStream(f));
                BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(p));
                IOUtils.copy(is, os);
                os.flush();
                os.close();
                is.close();
                if (Main.compress) {
                    CompressionUtils.compressFile(p, z);
                    p.delete();
                    p = z;
                }
                byte[] ivb = null;
                if (Main.chunkStoreEncryptionEnabled) {
                    try {
                        ivb = PassPhrase.getByteIV();
                        EncryptUtils.encryptFile(p, e, new IvParameterSpec(ivb));

                    } catch (Exception e1) {
                        throw new IOException(e1);
                    }
                    p.delete();
                    p = e;
                }
                String objName = pth;
                ObjectMetadata md = new ObjectMetadata();
                Map<String, String> umd = FileUtils.getFileMetaData(f, Main.chunkStoreEncryptionEnabled);
                md.setUserMetadata(umd);
                md.addUserMetadata("lz4compress", Boolean.toString(Main.compress));
                md.addUserMetadata("encrypt", Boolean.toString(Main.chunkStoreEncryptionEnabled));
                if (ivb != null)
                    md.addUserMetadata("ivspec", BaseEncoding.base64().encode(ivb));
                md.addUserMetadata("lastmodified", Long.toString(f.lastModified()));
                if (simpleS3) {
                    md.setContentType("binary/octet-stream");
                    in = new BufferedInputStream(new FileInputStream(p), 32768);
                    try {
                        if (md5sum) {
                            byte[] md5Hash = ServiceUtils.computeMD5Hash(in);
                            in.close();
                            String mds = BaseEncoding.base64().encode(md5Hash);
                            md.setContentMD5(mds);
                            md.addUserMetadata("md5sum", mds);
                        }

                    } catch (NoSuchAlgorithmException e2) {
                        SDFSLogger.getLog().error("while hashing", e2);
                        throw new IOException(e2);
                    }

                    in = new FileInputStream(p);
                    md.setContentLength(p.length());
                    try {
                        PutObjectRequest req = new PutObjectRequest(this.name, objName, in, md);
                        s3Service.putObject(req);
                        if (this.isClustered())
                            this.checkoutFile(pth);
                        SDFSLogger.getLog().debug(
                                "uploaded=" + f.getPath() + " lm=" + md.getUserMetadata().get("lastmodified"));
                    } catch (AmazonS3Exception e1) {
                        if (e1.getStatusCode() == 409) {
                            try {
                                s3Service.deleteObject(this.name, objName);
                                this.uploadFile(f, to, pp);
                                return;
                            } catch (Exception e2) {
                                throw new IOException(e2);
                            }
                        } else {

                            throw new IOException(e1);
                        }
                    } catch (Exception e1) {
                        // SDFSLogger.getLog().error("error uploading", e1);
                        throw new IOException(e1);
                    }
                } else {
                    try {
                        md.setContentType("binary/octet-stream");
                        in = new BufferedInputStream(new FileInputStream(p), 32768);
                        byte[] md5Hash = ServiceUtils.computeMD5Hash(in);
                        in.close();
                        String mds = BaseEncoding.base64().encode(md5Hash);
                        md.setContentMD5(mds);
                        md.addUserMetadata("md5sum", mds);
                        in = new BufferedInputStream(new FileInputStream(p), 32768);

                        md.setContentLength(p.length());
                        PutObjectRequest req = new PutObjectRequest(this.name, objName, in, md);
                        multiPartUpload(req);
                        if (this.isClustered())
                            this.checkoutFile(pth);
                    } catch (AmazonS3Exception e1) {
                        if (e1.getStatusCode() == 409) {
                            try {
                                s3Service.deleteObject(this.name, objName);
                                this.uploadFile(f, to, pp);
                                return;
                            } catch (Exception e2) {
                                throw new IOException(e2);
                            }
                        } else {

                            throw new IOException(e1);
                        }
                    } catch (Exception e1) {
                        // SDFSLogger.getLog().error("error uploading", e1);
                        throw new IOException(e1);
                    }
                }
            } finally {
                try {
                    if (in != null)
                        in.close();
                } finally {
                    p.delete();
                    z.delete();
                    e.delete();
                }
            }
        }
    } finally {
        this.s3clientLock.readLock().unlock();
    }

}

From source file:org.ow2.authzforce.pap.dao.flatfile.FlatFileBasedDomainsDAO.java

@Override
public Set<String> getDomainIDs(final String externalId) throws IOException {
    synchronized (domainsRootDir) {
        if (externalId != null) {
            // externalId not null
            final String domainId = domainIDsByExternalId.get(externalId);
            if (domainId == null) {
                return Collections.<String>emptySet();
            }/*from ww w . j  a  va 2 s  .c om*/

            // domainId not null, check if domain is still there in the
            // repository
            final Path domainDirPath = this.domainsRootDir.resolve(domainId);
            if (Files.exists(domainDirPath, LinkOption.NOFOLLOW_LINKS)) {
                return Collections.<String>singleton(domainId);
            }

            // domain directory no longer exists, remove from map and so on
            removeDomainFromCache(domainId);
            return Collections.<String>emptySet();
        }

        // externalId == null
        /*
         * All changes to domainMap are synchronized by 'domainsRootDir'. So
         * we can iterate and change if necessary for synchronizing the
         * domains root directory with the domainMap (Using a domainMap is
         * necessary for quick access to domains' PDPs.)
         */
        final Set<String> oldDomainIDs = new HashSet<>(domainMap.keySet());
        final Set<String> newDomainIDs = new HashSet<>();
        try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(domainsRootDir)) {
            for (final Path domainDirPath : dirStream) {
                LOGGER.debug("Checking domain in file {}", domainDirPath);
                if (!Files.isDirectory(domainDirPath)) {
                    LOGGER.warn("Ignoring invalid domain file {} (not a directory)", domainDirPath);
                    continue;
                }

                // domain folder name is the domain ID
                final Path lastPathSegment = domainDirPath.getFileName();
                if (lastPathSegment == null) {
                    throw new RuntimeException(
                            "Invalid Domain folder path '" + domainDirPath + "': no filename");
                }

                final String domainId = lastPathSegment.toString();
                newDomainIDs.add(domainId);
                if (oldDomainIDs.remove(domainId)) {
                    // not new domain, but directory may have changed ->
                    // sync
                    final DOMAIN_DAO_CLIENT domain = domainMap.get(domainId);
                    if (domain != null) {
                        domain.getDAO().sync();
                    }
                } else {
                    // new domain directory
                    addDomainToCacheAfterDirectoryCreated(domainId, domainDirPath, null);
                }
            }
        } catch (final IOException e) {
            throw new IOException("Failed to scan files in the domains root directory '" + domainsRootDir
                    + "' looking for domain directories", e);
        }

        if (!oldDomainIDs.isEmpty()) {
            // old domains remaining in cache that don't match directories
            // -> removed
            // -> remove from cache
            for (final String domainId : oldDomainIDs) {
                removeDomainFromCache(domainId);
            }
        }

        return newDomainIDs;
    }
}

From source file:fr.amap.lidar.amapvox.gui.MainFrameController.java

private void getBoundingBoxOfPoints(final boolean quick) {

    if (textFieldInputFileALS.getText().equals("") && !removeWarnings) {

        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Information");
        alert.setHeaderText("An ALS file has to be open");
        alert.setContentText("An ALS file has to be open.\nTo proceed select ALS tab and choose a *.las file.");

        alert.showAndWait();//from  ww  w  .j  av a2s  . com

    } else {
        File file = new File(textFieldInputFileALS.getText());

        if (!Files.exists(file.toPath(), LinkOption.NOFOLLOW_LINKS)) {

            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Error");
            alert.setHeaderText("File not found");
            alert.setContentText("The file " + file.getAbsolutePath() + " cannot be found.");

            alert.showAndWait();

        } else if (FileManager.getExtension(file).equals(".las")
                || FileManager.getExtension(file).equals(".laz")) {

            Matrix4d identityMatrix = new Matrix4d();
            identityMatrix.setIdentity();

            ProgressDialog d;

            Service<Void> service = new Service<Void>() {

                @Override
                protected Task<Void> createTask() {

                    return new Task<Void>() {
                        @Override
                        protected Void call() throws InterruptedException {

                            final BoundingBox3d boundingBox = fr.amap.lidar.amapvox.util.Util
                                    .getBoundingBoxOfPoints(new File(textFieldInputFileALS.getText()),
                                            resultMatrix, quick, getListOfClassificationPointToDiscard());

                            Point3d minPoint = boundingBox.min;
                            Point3d maxPoint = boundingBox.max;

                            Platform.runLater(new Runnable() {

                                @Override
                                public void run() {
                                    voxelSpacePanelVoxelizationController.getTextFieldEnterXMin()
                                            .setText(String.valueOf(minPoint.x));
                                    voxelSpacePanelVoxelizationController.getTextFieldEnterYMin()
                                            .setText(String.valueOf(minPoint.y));
                                    voxelSpacePanelVoxelizationController.getTextFieldEnterZMin()
                                            .setText(String.valueOf(minPoint.z));

                                    voxelSpacePanelVoxelizationController.getTextFieldEnterXMax()
                                            .setText(String.valueOf(maxPoint.x));
                                    voxelSpacePanelVoxelizationController.getTextFieldEnterYMax()
                                            .setText(String.valueOf(maxPoint.y));
                                    voxelSpacePanelVoxelizationController.getTextFieldEnterZMax()
                                            .setText(String.valueOf(maxPoint.z));
                                }
                            });

                            return null;
                        }
                    };

                };
            };

            d = new ProgressDialog(service);
            d.initOwner(stage);
            d.setHeaderText("Please wait...");
            d.setResizable(true);

            d.show();

            service.start();

        }
    }
}