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:com.seleniumtests.util.helper.AppTestDocumentation.java

private static void parseTest(Path path) throws FileNotFoundException {
    javadoc.append(String.format("\nh2. Tests: %s\n", path.getFileName().toString()));

    FileInputStream in = new FileInputStream(path.toAbsolutePath().toString());

    // parse the file
    CompilationUnit cu = JavaParser.parse(in);

    // prints the resulting compilation unit to default system output
    cu.accept(new ClassVisitor(), "Tests");
    cu.accept(new TestMethodVisitor(), null);
}

From source file:org.exist.xquery.modules.httpclient.HTTPClientModule.java

private static void setConfigFromFile(final Path configFile, final HttpClient http) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("http.configfile='" + configFile.toAbsolutePath() + "'");
    }//from w w  w.  j a v  a  2s.com

    final Properties props = new Properties();
    try (final InputStream is = Files.newInputStream(configFile)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Loading proxy settings from " + configFile.toAbsolutePath());
        }

        props.load(is);

        // Hostname / port
        final String proxyHost = props.getProperty("proxy.host");
        final int proxyPort = Integer.parseInt(props.getProperty("proxy.port", "8080"));

        // Username / password
        final String proxyUser = props.getProperty("proxy.user");
        final String proxyPassword = props.getProperty("proxy.password");

        // NTLM specifics
        String proxyDomain = props.getProperty("proxy.ntlm.domain");
        if ("NONE".equalsIgnoreCase(proxyDomain)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Forcing removal NTLM");
            }
            proxyDomain = null;
        }

        // Set scope
        final AuthScope authScope = new AuthScope(proxyHost, proxyPort);

        // Setup right credentials
        final Credentials credentials;
        if (proxyDomain == null) {
            credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Using NTLM authentication for '" + proxyDomain + "'");
            }
            credentials = new NTCredentials(proxyUser, proxyPassword, proxyHost, proxyDomain);
        }

        // Set details
        final HttpState state = http.getState();
        http.getHostConfiguration().setProxy(proxyHost, proxyPort);
        state.setProxyCredentials(authScope, credentials);

        if (LOG.isDebugEnabled()) {
            LOG.info("Set proxy: " + proxyUser + "@" + proxyHost + ":" + proxyPort
                    + (proxyDomain == null ? "" : " (NTLM:'" + proxyDomain + "')"));
        }
    } catch (final IOException ex) {
        LOG.error("Failed to read proxy configuration from '" + configFile + "'");
    }
}

From source file:de.teamgrit.grit.preprocess.fetch.SvnFetcher.java

/**
 * Updates the directory path for remote svn repos.
 *
 * @param location/*from   www .  ja  va  2s  .  co  m*/
 *            the adress of the repo
 * @param oldPath
 *            the current path
 * @return an updated path
 */
private static Path updateDirectoryPath(String location, Path oldPath) {
    Path targetDirectory = Paths.get(oldPath.toAbsolutePath().toString());
    // check whether the svn repo is given via a url or is given via a path
    if (!location.startsWith("file://")) {

        // We need to get the name of the checked out folder / repo.
        int occurences = StringUtils.countMatches(location, "/");
        int index = StringUtils.ordinalIndexOf(location, "/", occurences);
        String temp = location.substring(index + 1);

        // stitch the last part of the hyperlink to the targetDirectory to
        // receive the structure
        targetDirectory = Paths.get(targetDirectory.toString(), temp);
    } else {
        targetDirectory = targetDirectory.resolve(Paths.get(location).getFileName());
    }
    return targetDirectory;
}

From source file:org.apache.rya.api.path.PathUtils.java

/**
 * Converts a {@link Path} to a {@link org.apache.hadoop.fs.Path}.
 * @param path The {@link Path}.//ww  w.ja v  a2 s  .  c  o m
 * @return the resulting {@link org.apache.hadoop.fs.Path}.
 */
public static org.apache.hadoop.fs.Path toHadoopPath(final Path path) {
    if (path != null) {
        final String stringPath = FilenameUtils.separatorsToUnix(path.toAbsolutePath().toString());
        final org.apache.hadoop.fs.Path hadoopPath = new org.apache.hadoop.fs.Path(stringPath);
        return hadoopPath;
    }
    return null;
}

From source file:org.exist.xquery.modules.httpclient.HTTPClientModule.java

private static HttpClient setupHttpClient() {
    final HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    final HttpClient client = new HttpClient(httpConnectionManager);

    //config from file if present
    final String configFile = System.getProperty("http.configfile");
    if (configFile != null) {
        final Path f = Paths.get(configFile);
        if (Files.exists(f)) {
            setConfigFromFile(f, client);
        } else {/*from  ww w  .  j  a v  a 2 s  .co m*/
            LOG.warn("http.configfile '" + f.toAbsolutePath() + "' does not exist!");
        }
    }

    // Legacy: set the proxy server (if any) from system properties
    final String proxyHost = System.getProperty("http.proxyHost");
    if (proxyHost != null) {
        //TODO: support for http.nonProxyHosts e.g. -Dhttp.nonProxyHosts="*.devonline.gov.uk|*.devon.gov.uk"
        final ProxyHost proxy = new ProxyHost(proxyHost,
                Integer.parseInt(System.getProperty("http.proxyPort")));
        client.getHostConfiguration().setProxyHost(proxy);
    }

    return client;
}

From source file:org.roda.common.certification.PDFSignatureUtils.java

public static int countSignaturesPDF(Path file) {
    int counter = -1;
    try {/*ww  w  .  ja v a  2 s . com*/
        PdfReader reader = new PdfReader(file.toAbsolutePath().toString());
        AcroFields af = reader.getAcroFields();
        ArrayList<String> names = af.getSignatureNames();
        counter = names.size();
    } catch (IOException e) {
        LOGGER.error("Error getting path of file {}", e.getMessage());
    }
    return counter;
}

From source file:org.mitre.mpf.mvc.util.NIOUtils.java

public static String getPathContentType(Path path) {
    String contentType = null;//from   w  ww .  jav  a 2  s .com
    if (path != null && Files.isRegularFile(path)) {
        try {
            contentType = Files.probeContentType(path);
        } catch (IOException e) {
            log.error("Error determining the content type of file '{}'", path.toAbsolutePath().toString());
        }
    }
    return contentType;
}

From source file:org.apache.rya.api.path.PathUtils.java

/**
 * Indicates whether file lives in a secure directory relative to the
 * program's user.// ww  w  .  ja  v  a 2 s.c o m
 * @param file {@link Path} to test.
 * @param user {@link UserPrincipal} to test. If {@code null}, defaults to
 * current user.
 * @param symlinkDepth Number of symbolic links allowed.
 * @return {@code true} if file's directory is secure.
 */
public static boolean isInSecureDir(Path file, UserPrincipal user, final int symlinkDepth) {
    if (!file.isAbsolute()) {
        file = file.toAbsolutePath();
    }
    if (symlinkDepth <= 0) {
        // Too many levels of symbolic links
        return false;
    }
    // Get UserPrincipal for specified user and superuser
    final Path fileRoot = file.getRoot();
    if (fileRoot == null) {
        return false;
    }
    final FileSystem fileSystem = Paths.get(fileRoot.toString()).getFileSystem();
    final UserPrincipalLookupService upls = fileSystem.getUserPrincipalLookupService();
    UserPrincipal root = null;
    try {
        if (SystemUtils.IS_OS_UNIX) {
            root = upls.lookupPrincipalByName("root");
        } else {
            root = upls.lookupPrincipalByName("Administrators");
        }
        if (user == null) {
            user = upls.lookupPrincipalByName(System.getProperty("user.name"));
        }
        if (root == null || user == null) {
            return false;
        }
    } catch (final IOException x) {
        return false;
    }
    // If any parent dirs (from root on down) are not secure, dir is not secure
    for (int i = 1; i <= file.getNameCount(); i++) {
        final Path partialPath = Paths.get(fileRoot.toString(), file.subpath(0, i).toString());
        try {
            if (Files.isSymbolicLink(partialPath)) {
                if (!isInSecureDir(Files.readSymbolicLink(partialPath), user, symlinkDepth - 1)) {
                    // Symbolic link, linked-to dir not secure
                    return false;
                }
            } else {
                final UserPrincipal owner = Files.getOwner(partialPath);
                if (!user.equals(owner) && !root.equals(owner)) {
                    // dir owned by someone else, not secure
                    return SystemUtils.IS_OS_UNIX ? false : Files.isWritable(partialPath);
                }
            }
        } catch (final IOException x) {
            return false;
        }
    }
    return true;
}

From source file:com.spotify.heroic.HeroicShell.java

static String formatDefaults(Path[] defaultConfigs) {
    final List<Path> alternatives = new ArrayList<>(defaultConfigs.length);

    for (final Path path : defaultConfigs) {
        alternatives.add(path.toAbsolutePath());
    }// w  w w  .  j a v a 2  s.c  o m

    return StringUtils.join(alternatives, ", ");
}

From source file:com.edduarte.argus.util.PluginLoader.java

private static Class loadPlugin(Path pluginFile) throws ClassNotFoundException, IOException {

    CustomClassLoader loader = new CustomClassLoader();

    String url = "file:" + pluginFile.toAbsolutePath().toString();
    URL myUrl = new URL(url);
    URLConnection connection = myUrl.openConnection();
    InputStream input = connection.getInputStream();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int data = input.read();

    while (data != -1) {
        buffer.write(data);/* w  w  w  . j  a v a2s .  c om*/
        data = input.read();
    }

    input.close();

    byte[] classData = buffer.toByteArray();
    Class loadedClass;

    try {
        loadedClass = loader.defineClass(classData);

    } catch (NoClassDefFoundError ex) {
        loadedClass = null;
    }

    loader.clearAssertionStatus();
    loader = null;
    System.gc();

    return loadedClass;
}