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:io.crate.frameworks.mesos.CrateExecutor.java

private boolean fetchAndExtractUri(URI uri) {
    boolean success;
    try {//from   ww w  .j ava  2  s . co m
        URL download = uri.toURL();
        String fn = new File(download.getFile()).getName();
        File tmpFile = new File(fn);
        if (!tmpFile.exists()) {
            if (tmpFile.createNewFile()) {
                LOGGER.debug("Fetch: {} -> {}", download, tmpFile);
                ReadableByteChannel rbc = Channels.newChannel(download.openStream());
                FileOutputStream stream = new FileOutputStream(tmpFile);
                stream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }
        } else {
            LOGGER.debug("tarball already downloaded");
        }
        success = extractFile(tmpFile);
    } catch (IOException e) {
        e.printStackTrace();
        success = false;
    }
    return success;
}

From source file:org.apache.openaz.xacml.pdp.std.StdPolicyFinder.java

private PolicyDef loadPolicyDefFromURI(URI uri) throws StdPolicyFinderException {
    this.logger.info("Loading policy from URI " + uri.toString());
    URL url = null;/*from w ww  . j a  v a2 s.  com*/
    try {
        url = uri.toURL();
        this.logger.debug("Loading policy from URL " + url.toString());
    } catch (MalformedURLException ex) {
        this.logger.debug("Unknown protocol for URI " + uri.toString());
        return null;
    }

    try (InputStream inputStream = url.openStream()) {
        return DOMPolicyDef.load(inputStream);
    } catch (Exception ex) {
        this.logger.error("Exception loading policy definition", ex);
        throw new StdPolicyFinderException(
                "Exception loading policy def from \"" + uri.toString() + "\": " + ex.getMessage(), ex);
    }
}

From source file:de.catma.ui.repository.wizard.FileTypePanel.java

private ProtocolHandler getProtocolHandlerForUri(URI inputFileURI, String fileID, String inputFileMimeType)
        throws MalformedURLException, IOException {
    if (inputFileURI.toURL().getProtocol().toLowerCase().equals("http")
            || inputFileURI.toURL().getProtocol().toLowerCase().equals("https")) {

        final String destinationFileUri = repository.getFileURL(fileID,
                ((CatmaApplication) UI.getCurrent()).getTempDirectory() + "/");

        return new HttpProtocolHandler(inputFileURI, destinationFileUri);
    } else {/*from  ww  w . j a va2 s  .  c om*/
        return new DefaultProtocolHandler(inputFileURI, inputFileMimeType);
    }
}

From source file:com.senseidb.test.TestHttpRestSenseiServiceImpl.java

public void testURIBuilding() throws JSONException, SenseiException, UnsupportedEncodingException,
        URISyntaxException, MalformedURLException {
    SenseiRequest aRequest = createNonRandomSenseiRequest();
    List<NameValuePair> queryParams = HttpRestSenseiServiceImpl.convertRequestToQueryParams(aRequest);
    HttpRestSenseiServiceImpl senseiService = createSenseiService();
    URI requestURI = senseiService.buildRequestURI(queryParams);

    assertTrue(requestURI.toURL().toString().length() > 0); // force resolving the URI to a string

    List<NameValuePair> parsedParams = URLEncodedUtils.parse(requestURI, "UTF-8");
    MockServletRequest mockServletRequest = MockServletRequest.create(parsedParams);
    DataConfiguration params = new DataConfiguration(new ServletRequestConfiguration(mockServletRequest));
    SenseiRequest bRequest = DefaultSenseiJSONServlet.convertSenseiRequest(params);
    assertEquals(aRequest, bRequest);/*from ww w.  ja  v a2  s .c  om*/
}

From source file:org.wrml.runtime.format.application.schema.json.JsonSchemaLoader.java

public JsonSchema load(final URI jsonSchemaUri) throws IOException {

    if (_JsonSchemas.containsKey(jsonSchemaUri)) {
        return _JsonSchemas.get(jsonSchemaUri);
    }/*from ww w.j ava 2  s. com*/

    final ObjectNode rootNode = new ObjectMapper().readValue(jsonSchemaUri.toURL(), ObjectNode.class);
    return load(rootNode, jsonSchemaUri);
}

From source file:org.apache.ode.bpel.compiler.DefaultResourceFinder.java

private InputStream openFileResource(URI uri) throws MalformedURLException, IOException {
    URI absolute = _absoluteDir.toURI();
    if (__log.isDebugEnabled()) {
        __log.debug(/*from  w w  w .ja  v  a  2s . c  om*/
                "openResource: uri=" + uri + " relativeDir=" + _relativeDir + " absoluteDir=" + _absoluteDir);
    }

    if (uri.isAbsolute() && uri.getScheme().equals("file")) {
        try {
            return uri.toURL().openStream();
        } catch (Exception except) {
            __log.debug("openResource: unable to open file URL " + uri + "; " + except.toString());
            return null;
        }
    }

    // Note that if we get an absolute URI, the relativize operation will simply
    // return the absolute URI.
    URI relative = _relativeDir.toURI().relativize(uri);
    if (relative.isAbsolute() && !(relative.getScheme().equals("urn"))) {
        __log.fatal("openResource: invalid scheme (should be urn:)  " + uri);
        return null;
    }

    File f = new File(absolute.getPath(), relative.getPath());
    if (f.exists()) {
        return new FileInputStream(f);
    } else {
        if (__log.isDebugEnabled()) {
            __log.debug("fileNotFound: " + f);
        }
        return null;
    }
}

From source file:org.dita.dost.ant.PluginInstallTask.java

private Set<Registry> readRegistry(final String name, final SemVerMatch version) {
    log(String.format("Reading registries for %s@%s", name, version), Project.MSG_INFO);
    Registry res = null;/* www  .j  a  v a  2s.  co  m*/
    for (final String registry : registries) {
        final URI registryUrl = URI.create(registry + name + ".json");
        log(String.format("Read registry %s", registry), Project.MSG_INFO);
        try (BufferedInputStream in = new BufferedInputStream(registryUrl.toURL().openStream())) {
            log("Parse registry", Project.MSG_INFO);
            final JsonFactory factory = mapper.getFactory();
            final JsonParser parser = factory.createParser(in);
            final JsonNode obj = mapper.readTree(parser);
            final Collection<Registry> regs;
            if (obj.isArray()) {
                regs = Arrays.asList(mapper.treeToValue(obj, Registry[].class));
            } else {
                regs = resolveAlias(mapper.treeToValue(obj, Alias.class));
            }
            final Optional<Registry> reg = findPlugin(regs, version);
            if (reg.isPresent()) {
                final Registry plugin = reg.get();
                log(String.format("Plugin found at %s@%s", registryUrl, plugin.vers), Project.MSG_INFO);
                res = plugin;
                break;
            }
        } catch (MalformedURLException e) {
            log(String.format("Invalid registry URL %s: %s", registryUrl, e.getMessage()), e, Project.MSG_ERR);
        } catch (FileNotFoundException e) {
            log(String.format("Registry configuration %s not found", registryUrl), e, Project.MSG_INFO);
        } catch (IOException e) {
            log(String.format("Failed to read registry configuration %s: %s", registryUrl, e.getMessage()), e,
                    Project.MSG_ERR);
        }
    }
    if (res == null) {
        throw new BuildException("Unable to find plugin " + pluginFile);
    }

    Set<Registry> results = new HashSet<>();
    results.add(res);
    res.deps.stream().filter(dep -> !installedPlugins.contains(dep.name))
            .flatMap(dep -> readRegistry(dep.name, dep.req).stream()).forEach(results::add);

    return results;
}

From source file:com.jaeksoft.searchlib.crawler.web.process.WebCrawlMaster.java

private void extractSiteMapList() throws SearchLibException {
    HttpDownloader httpDownloader = null;
    try {//from  w w  w.j a  v a 2s. c  o  m
        httpDownloader = getNewHttpDownloader(true);
        SiteMapList siteMapList = getConfig().getSiteMapList();
        if (siteMapList != null && siteMapList.getArray() != null) {
            setStatus(CrawlStatus.LOADING_SITEMAP);
            UrlManager urlManager = getConfig().getUrlManager();
            List<UrlItem> workInsertUrlList = new ArrayList<UrlItem>();
            for (SiteMapItem siteMap : siteMapList.getArray()) {
                Set<SiteMapUrl> siteMapUrlSet = siteMap.load(getNewHttpDownloader(true), null);
                for (SiteMapUrl siteMapUrl : siteMapUrlSet) {

                    URI uri = siteMapUrl.getLoc();
                    String sUri = uri.toString();
                    URL url;
                    try {
                        url = uri.toURL();
                    } catch (MalformedURLException e) {
                        continue;
                    }

                    if (exclusionMatcher != null)
                        if (exclusionMatcher.matchPattern(url, sUri))
                            continue;
                    if (inclusionMatcher != null)
                        if (!inclusionMatcher.matchPattern(url, sUri))
                            continue;

                    if (!urlManager.exists(sUri)) {
                        workInsertUrlList.add(
                                urlManager.getNewUrlItem(new LinkItem(sUri, LinkItem.Origin.sitemap, null)));
                    }
                }
            }
            if (workInsertUrlList.size() > 0)
                urlManager.updateUrlItems(workInsertUrlList);
        }
    } finally {
        if (httpDownloader != null)
            httpDownloader.release();
    }
}

From source file:eionet.gdem.utils.InputFile.java

/**
 * Stores the URL of remote file.//from  www. j  a  va2 s.com
 *
 * @param strUrl URL of input file
 * @throws MalformedURLException Invalid URL.
 */
private void setURL(String strUrl) throws MalformedURLException {
    try {
        URI uri = new URI(escapeSpaces(strUrl));
        parseUri(uri);

        this.url = uri.toURL();

    } catch (URISyntaxException ue) {
        throw new MalformedURLException(ue.toString());
    } catch (IllegalArgumentException ae) {
        throw new MalformedURLException(ae.toString());
    }
}

From source file:org.datavec.api.records.reader.impl.LineRecordReader.java

@Override
public List<Record> loadFromMetaData(List<RecordMetaData> recordMetaDatas) throws IOException {
    //First: create a sorted list of the RecordMetaData
    List<Triple<Integer, RecordMetaDataLine, List<Writable>>> list = new ArrayList<>();
    Set<URI> uris = new HashSet<>();
    Iterator<RecordMetaData> iter = recordMetaDatas.iterator();
    int count = 0;
    while (iter.hasNext()) {
        RecordMetaData rmd = iter.next();
        if (!(rmd instanceof RecordMetaDataLine)) {
            throw new IllegalArgumentException(
                    "Invalid metadata; expected RecordMetaDataLine instance; got: " + rmd);
        }/*w  w  w .j  av a  2  s  .co  m*/
        list.add(new Triple<>(count++, (RecordMetaDataLine) rmd, (List<Writable>) null));
        if (rmd.getURI() != null)
            uris.add(rmd.getURI());
    }
    List<URI> sortedURIs = null;
    if (uris.size() > 0) {
        sortedURIs = new ArrayList<>(uris);
        Collections.sort(sortedURIs);
    }

    //Sort by URI first (if possible - don't always have URIs though, for String split etc), then sort by line number:
    Collections.sort(list, new Comparator<Triple<Integer, RecordMetaDataLine, List<Writable>>>() {
        @Override
        public int compare(Triple<Integer, RecordMetaDataLine, List<Writable>> o1,
                Triple<Integer, RecordMetaDataLine, List<Writable>> o2) {
            if (o1.getSecond().getURI() != null) {
                if (!o1.getSecond().getURI().equals(o2.getSecond().getURI())) {
                    return o1.getSecond().getURI().compareTo(o2.getSecond().getURI());
                }
            }
            return Integer.compare(o1.getSecond().getLineNumber(), o2.getSecond().getLineNumber());
        }
    });

    if (uris.size() > 0 && sortedURIs != null) {
        //URIs case - possibly with multiple URIs
        Iterator<Triple<Integer, RecordMetaDataLine, List<Writable>>> metaIter = list.iterator(); //Currently sorted by URI, then line number

        URI currentURI = sortedURIs.get(0);
        Iterator<String> currentUriIter = IOUtils
                .lineIterator(new InputStreamReader(currentURI.toURL().openStream()));
        int currentURIIdx = 0; //Index of URI
        int currentLineIdx = 0; //Index of the line for the current URI
        String line = currentUriIter.next();
        while (metaIter.hasNext()) {
            Triple<Integer, RecordMetaDataLine, List<Writable>> t = metaIter.next();
            URI thisURI = t.getSecond().getURI();
            int nextLineIdx = t.getSecond().getLineNumber();

            //First: find the right URI for this record...
            while (!currentURI.equals(thisURI)) {
                //Iterate to the next URI
                currentURIIdx++;
                if (currentURIIdx >= sortedURIs.size()) {
                    //Should never happen
                    throw new IllegalStateException(
                            "Count not find URI " + thisURI + " in URIs list: " + sortedURIs);
                }
                currentURI = sortedURIs.get(currentURIIdx);
                currentLineIdx = 0;
                if (currentURI.equals(thisURI)) {
                    //Found the correct URI for this MetaData instance
                    closeIfRequired(currentUriIter);
                    currentUriIter = IOUtils
                            .lineIterator(new InputStreamReader(currentURI.toURL().openStream()));
                    line = currentUriIter.next();
                }
            }

            //Have the correct URI/iter open -> scan to the required line
            while (currentLineIdx < nextLineIdx && currentUriIter.hasNext()) {
                line = currentUriIter.next();
                currentLineIdx++;
            }
            if (currentLineIdx < nextLineIdx && !currentUriIter.hasNext()) {
                throw new IllegalStateException("Could not get line " + nextLineIdx + " from URI " + currentURI
                        + ": has only " + currentLineIdx + " lines");
            }
            t.setThird(Collections.<Writable>singletonList(new Text(line)));
        }
    } else {
        //Not URI based: String split, etc
        Iterator<String> iterator = getIterator(0);
        Iterator<Triple<Integer, RecordMetaDataLine, List<Writable>>> metaIter = list.iterator();
        int currentLineIdx = 0;
        String line = iterator.next();
        while (metaIter.hasNext()) {
            Triple<Integer, RecordMetaDataLine, List<Writable>> t = metaIter.next();
            int nextLineIdx = t.getSecond().getLineNumber();
            while (currentLineIdx < nextLineIdx && iterator.hasNext()) {
                line = iterator.next();
                currentLineIdx++;
            }
            t.setThird(Collections.<Writable>singletonList(new Text(line)));
        }
        closeIfRequired(iterator);
    }

    //Now, sort by the original (request) order:
    Collections.sort(list, new Comparator<Triple<Integer, RecordMetaDataLine, List<Writable>>>() {
        @Override
        public int compare(Triple<Integer, RecordMetaDataLine, List<Writable>> o1,
                Triple<Integer, RecordMetaDataLine, List<Writable>> o2) {
            return Integer.compare(o1.getFirst(), o2.getFirst());
        }
    });

    //And return...
    List<Record> out = new ArrayList<>();
    for (Triple<Integer, RecordMetaDataLine, List<Writable>> t : list) {
        out.add(new org.datavec.api.records.impl.Record(t.getThird(), t.getSecond()));
    }
    return out;
}