Example usage for java.net URL toURI

List of usage examples for java.net URL toURI

Introduction

In this page you can find the example usage for java.net URL toURI.

Prototype

public URI toURI() throws URISyntaxException 

Source Link

Document

Returns a java.net.URI equivalent to this URL.

Usage

From source file:com.mhs.hboxmaintenanceserver.utils.Utils.java

/**
 * List directory contents for a resource folder. Not recursive. This is
 * basically a brute-force implementation. Works for regular files and also
 * JARs.//w  w w.  ja  v a  2 s.com
 *
 * @author Greg Briggs
 * @param path Should end with "/", but not start with one.
 * @return Just the name of each member item, not the full paths.
 * @throws URISyntaxException
 * @throws IOException
 */
public static String[] getResourceListing(String path) throws URISyntaxException, IOException {
    URL dirURL = Utils.class.getResource(path);
    if (dirURL != null && dirURL.getProtocol().equals("file")) {
        /* A file path: easy enough */
        return new File(dirURL.toURI()).list();
    }

    if (dirURL == null) {
        /* 
         * In case of a jar file, we can't actually find a directory.
         * Have to assume the same jar as clazz.
         */
        String me = Utils.class.getName().replace(".", "/") + ".class";
        dirURL = Utils.class.getClassLoader().getResource(me);
    }
    if (dirURL.getProtocol().equals("file")) {
        /* A file path: easy enough */
        String parent = (new File(dirURL.toURI()).getParent());
        return new File(parent + "/../../../../../../resources/" + path).list();
    }

    if (dirURL.getProtocol().equals("jar")) {
        /* A JAR path */
        String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
        JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
        Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
        Set<String> result = new HashSet<>(); //avoid duplicates in case it is a subdirectory
        while (entries.hasMoreElements()) {
            String name = entries.nextElement().getName();
            if (name.startsWith(path)) { //filter according to the path
                String entry = name.substring(path.length());
                int checkSubdir = entry.indexOf("/");
                if (checkSubdir >= 0) {
                    // if it is a subdirectory, we just return the directory name
                    entry = entry.substring(0, checkSubdir);
                }
                result.add(entry);
            }
        }
        return result.toArray(new String[result.size()]);
    }

    throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
}

From source file:io.fabric8.che.starter.util.ProjectHelper.java

public String getProjectNameFromGitRepository(final String repositoryUrl)
        throws URISyntaxException, MalformedURLException {
    String httpsRepositoryUrl = changeProtocolToHttpsIfNeeded(repositoryUrl);
    URL url = new URL(httpsRepositoryUrl);
    URI uri = url.toURI();
    String path = uri.getPath();// w  w w  .ja  v a2s. c  om
    return getProjectNameFromPath(path);
}

From source file:io.kahu.hawaii.sso.util.FileJsonWebKeySetRepository.java

public FileJsonWebKeySetRepository(URL url) throws URISyntaxException, IOException, ParseException {
    this(new File(url.toURI()));
}

From source file:au.org.ala.delta.confor.ConforTestCase.java

private File urlToFile(String urlString) throws Exception {
    URL url = ToDistTest.class.getResource(urlString);
    File file = new File(url.toURI());
    return file;//from  w  w  w . j a va2 s .co  m
}

From source file:com.github.rabid_fish.proxy.mock.MockMapHelper.java

protected String readFileFromResourcePath(String filename) throws URISyntaxException, IOException {

    URL resource = getClass().getResource(filename);
    File file = new File(resource.toURI());
    String body = FileUtils.readFileToString(file);
    return body;//from   www .  j  a v a2 s. com
}

From source file:au.org.ala.delta.translation.TranslatorTest.java

protected File classloaderPathToFile(String path) throws URISyntaxException {
    URL resource = getClass().getResource(path);
    return new File(resource.toURI());
}

From source file:com.zextras.zimbradrive.statustest.ConnectionTestUtils.java

public String assertHttpGetRequestResponse(URL url) throws URISyntaxException, IOException {
    HttpGet httpGet = new HttpGet(url.toURI());

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse httpResponse;/*from   w w w  . j a v a2 s .  c o  m*/
    httpResponse = client.execute(httpGet);

    BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
    return basicResponseHandler.handleResponse(httpResponse);
}

From source file:io.spring.gradle.testkit.junit.rules.TestKit.java

public GradleRunner withProjectResource(String projectResourceName) throws IOException, URISyntaxException {
    ClassLoader classLoader = getClass().getClassLoader();
    Enumeration<URL> resources = classLoader.getResources(projectResourceName);
    if (!resources.hasMoreElements()) {
        throw new IOException("Cannot find resource " + projectResourceName + " with " + classLoader);
    }//w  w w  . j a  va  2s  . c  om
    URL resourceUrl = resources.nextElement();
    File projectDir = Paths.get(resourceUrl.toURI()).toFile();
    return withProjectDir(projectDir);
}

From source file:org.exoplatform.container.spring.FileSystemXmlApplicationContextProvider.java

/**
 * {@inheritDoc}/*w ww . j  a v  a2s. c o m*/
 */
public ApplicationContext getApplicationContext(ApplicationContext parent) {
    try {
        String[] paths = new String[params.getValues().size()];
        int i = 0;
        for (String value : params.getValues()) {
            URL url = cm.getResource(value);
            paths[i++] = url.toURI().toString();
        }
        return new FileSystemXmlApplicationContext(paths, true, parent);
    } catch (Exception e) {
        throw new RuntimeException("Could not create the ApplicationContext", e);
    }
}

From source file:org.owasp.webgoat.i18n.PluginMessages.java

@Override
@SneakyThrows//from  www  .ja  v  a  2 s . c  om
protected PropertiesHolder refreshProperties(String filename, PropertiesHolder propHolder) {
    Properties properties = new Properties();
    long lastModified = System.currentTimeMillis();

    Enumeration<URL> resources = Thread.currentThread().getContextClassLoader()
            .getResources(filename + PROPERTIES_SUFFIX);
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        String sourcePath = resource.toURI().toString().replace(PROPERTIES_SUFFIX, "");
        PropertiesHolder holder = super.refreshProperties(sourcePath, propHolder);
        properties.putAll(holder.getProperties());
    }
    return new PropertiesHolder(properties, lastModified);
}