Example usage for java.net URI toURL

List of usage examples for java.net URI toURL

Introduction

In this page you can find the example usage for java.net URI toURL.

Prototype

public URL toURL() throws MalformedURLException 

Source Link

Document

Constructs a URL from this URI.

Usage

From source file:biz.gabrys.lesscss.extended.compiler.source.HttpSourceFactory.java

public HttpSource createRelativeSource(final LessSource source, final String importRelativePath) {
    try {/*w  w  w .  j a v a  2s  .  c  o m*/
        final String sourcePath = source.getPath();
        final String parentPath = sourcePath.substring(0, sourcePath.lastIndexOf('/'));
        final URI importUri = new URI(parentPath + '/' + importRelativePath).normalize();
        return new HttpSource(importUri.toURL());
    } catch (final URISyntaxException e) {
        throw new SourceFactoryException("Cannot normalize URL", e);
    } catch (final MalformedURLException e) {
        throw new SourceFactoryException("Cannot create relative URL", e);
    }
}

From source file:org.apache.tika.dl.imagerec.DL4JInceptionV3Net.java

public static synchronized File cachedDownload(File cacheDir, URI uri) throws IOException {

    if ("file".equals(uri.getScheme()) || uri.getScheme() == null) {
        return new File(uri);
    }/* ww  w  .  j  av a2 s  .c o  m*/
    if (!cacheDir.exists()) {
        cacheDir.mkdirs();
    }
    String[] parts = uri.toASCIIString().split("/");
    File cacheFile = new File(cacheDir, parts[parts.length - 1]);
    File successFlag = new File(cacheFile.getAbsolutePath() + ".success");

    if (cacheFile.exists() && successFlag.exists()) {
        LOG.info("Cache exist at {}. Not downloading it", cacheFile.getAbsolutePath());
    } else {
        if (successFlag.exists()) {
            successFlag.delete();
        }
        LOG.info("Cache doesn't exist. Going to make a copy");
        LOG.info("This might take a while! GET {}", uri);
        FileUtils.copyURLToFile(uri.toURL(), cacheFile, 5000, 5000);
        //restore the success flag again
        FileUtils.write(successFlag, "CopiedAt:" + System.currentTimeMillis(), Charset.defaultCharset());
    }
    return cacheFile;
}

From source file:grails.plugin.cloudfoundry.GrailsHttpRequestFactory.java

@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
    URL url = uri.toURL();
    URLConnection urlConnection = proxy != null ? url.openConnection(proxy) : url.openConnection();
    Assert.isInstanceOf(HttpURLConnection.class, urlConnection);
    prepareConnection((HttpURLConnection) urlConnection, httpMethod.name());
    return new GrailsHttpRequest(wrap((HttpURLConnection) urlConnection));
}

From source file:jails.http.client.SimpleClientHttpRequestFactory.java

public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
    HttpURLConnection connection = openConnection(uri.toURL(), proxy);
    prepareConnection(connection, httpMethod.name());
    return new SimpleClientHttpRequest(connection);
}

From source file:org.mycore.common.content.MCRVFSContent.java

public MCRVFSContent(URI uri) throws IOException {
    this(uri.toURL());
}

From source file:org.jrecruiter.service.GoogleMapsStaticTest.java

private void sendGetRequest() {

    // Send a GET request to the servlet
    try {//w  ww  .  java 2  s.  c  om

        final URI url = GoogleMapsUtils.buildGoogleMapsStaticUrl(BigDecimal.TEN, BigDecimal.TEN, 10);
        final URLConnection conn = url.toURL().openConnection();

        final BufferedImage img = ImageIO.read(conn.getInputStream());

        Assert.assertNotNull(img);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.betaconceptframework.astroboa.engine.jcr.io.ContentSourceExtractor.java

public InputStream extractStream(URI contentSource) throws Exception {
    dispose();/*from w w  w .  java2 s .  co  m*/

    String filename = contentSource.toURL().getFile();

    if (StringUtils.isBlank(filename)) {
        throw new Exception("No file name from URL " + contentSource.toString());
    }

    if (filename.endsWith(".zip")) {
        tmpZip = File.createTempFile("rep", ".zip");

        FileUtils.copyURLToFile(contentSource.toURL(), tmpZip);

        zipFile = new ZipFile(tmpZip);

        Enumeration entries = zipFile.getEntries();

        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement();

            if (entry.getName() != null && entry.getName().endsWith(".xml")) {
                return zipFile.getInputStream(entry);
            }
        }

        return null;
    } else if (filename.endsWith(".xml")) {
        return contentSource.toURL().openStream();
    } else {
        throw new Exception("Unsupported file extension " + filename);
    }
}

From source file:cc.arduino.net.CustomProxySelector.java

private URL toUrl(URI uri) {
    try {//from w ww . ja  v a  2  s  .  co m
        return uri.toURL();
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.asterics.mw.services.ResourceRegistry.java

/**
 * Tests whether the given URI exists by 
 * 1) testing if it is a File and invoking the File.exists method
 * 2) trying to open an InputStream/*  w w w.  j  a  v  a2 s.c  om*/
 * @param uri
 * @return
 */
public static boolean resourceExists(URI uri) {
    try {
        if (ResourceRegistry.toFile(uri).exists()) {
            return true;
        }
    } catch (URISyntaxException e) {
        InputStream in = null;
        try {
            in = uri.toURL().openStream();
            return true;
        } catch (IOException e1) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    return false;
}

From source file:org.apache.whirr.service.puppet.integration.PuppetServiceTest.java

@Test(timeout = TestConstants.ITEST_TIMEOUT)
public void testHttpAvailable() throws Exception {

    // check that the http server started
    for (Instance instance : cluster.getInstances()) {
        // first, check the socket
        IPSocket socket = new IPSocket(instance.getPublicAddress().getHostAddress(), 80);
        assert socketTester.apply(socket) : instance;

        // then, try a GET
        URI httpUrl = URI.create("http://" + instance.getPublicAddress().getHostAddress());
        Strings2.toStringAndClose(httpUrl.toURL().openStream());
    }/*w w w.  j a v  a2  s . c o m*/

}