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:org.exoplatform.services.rss.parser.RSSParser.java

public synchronized <T extends IRSSChannel, E extends IRSSItem> RSSDocument<T, E> createDocument(URI uri,
        String charset, Class<T> channelClazz, Class<E> itemClazz) throws Exception {
    try {/* w  ww  .  j a v  a  2 s.co  m*/
        return createDocument(uri.toURL(), charset, channelClazz, itemClazz);
    } catch (Exception e) {
        try {
            File file = new File(uri);
            return createDocument(file, charset, channelClazz, itemClazz);
        } catch (Exception exp) {
            return null;
        }
    }
}

From source file:com.exzogeni.dk.http.HttpTask.java

private V onPerformNetworkRequest(URI uri) throws Exception {
    Logger.debug("%s", uri);
    final HttpURLConnection cn = (HttpURLConnection) uri.toURL().openConnection();
    try {//from w  w w .  j ava2 s .co m
        onPrepareConnectionInternal(cn);
        onPerformRequest(cn);
        return onSuccessInternal(cn);
    } finally {
        cn.disconnect();
    }
}

From source file:org.codice.ddf.registry.report.action.provider.RegistryReportActionProvider.java

private Action getAction(String metacardId, String sourceId) {
    if (StringUtils.isNotBlank(sourceId)) {
        sourceId = SOURCE_ID_QUERY_PARAM + sourceId;
    }/*from ww  w  .  j a v a 2  s  . c o  m*/

    URL url;
    try {

        URI uri = new URI(SystemBaseUrl.EXTERNAL.constructUrl(
                String.format("%s/%s%s%s%s", REGISTRY_PATH, metacardId, REPORT_PATH, FORMAT, sourceId), true));
        url = uri.toURL();

    } catch (MalformedURLException e) {
        LOGGER.debug("Malformed URL exception", e);
        return null;
    } catch (URISyntaxException e) {
        LOGGER.debug("URI Syntax exception", e);
        return null;
    }

    return new ActionImpl(getId(), title, description, url);
}

From source file:com.joyent.manta.client.MantaClientSigningIT.java

@Test
public final void testCanCreateSignedURIWithEncodedCharacters() throws IOException {
    final String path = testPathPrefix + " quack ";

    mantaClient.put(path, TEST_DATA, UTF_8);
    Assert.assertEquals(mantaClient.getAsString(path), TEST_DATA);

    final URI uri = mantaClient.getAsSignedURI(path, "GET", Instant.now().plus(Duration.ofHours(1)));
    final HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();

    try (final InputStream is = conn.getInputStream()) {
        conn.setReadTimeout(3000);//from   w  ww. j a v  a2  s .  c  o m
        conn.connect();

        Assert.assertEquals(conn.getResponseCode(), HttpStatus.SC_OK);
        Assert.assertEquals(IOUtils.toString(is, UTF_8), TEST_DATA);
    } finally {
        conn.disconnect();
    }
}

From source file:org.apache.axis2.jaxws.util.BaseWSDLLocator.java

/**
 * Returns an InputSource pointed at an imported wsdl document whose
 * parent document was located at parentLocation and whose
 * relative location to the parent document is specified by
 * relativeLocation./*from  ww  w . j  a  v a  2  s .co m*/
 *
 * @param parentLocation a URI specifying the location of the
 * document doing the importing.
 * @param relativeLocation a URI specifying the location of the
 * document to import, relative to the parent document's location.
 */
public InputSource getImportInputSource(String parentLocation, String relativeLocation) {
    if (log.isDebugEnabled()) {
        log.debug("getImportInputSource, parentLocation= " + parentLocation + " relativeLocation= "
                + relativeLocation);
    }
    InputStream is = null;
    URL absoluteURL = null;

    String redirectedURI = getRedirectedURI(relativeLocation, parentLocation);
    if (redirectedURI != null)
        relativeLocation = redirectedURI;

    try {
        if (isAbsoluteImport(relativeLocation)) {
            try {
                absoluteURL = new URL(relativeLocation);
                is = absoluteURL.openStream();
                lastestImportURI = absoluteURL.toExternalForm();
            } catch (Throwable t) {
                if (relativeLocation.startsWith("file://")) {
                    try {
                        relativeLocation = "file:/" + relativeLocation.substring("file://".length());
                        absoluteURL = new URL(relativeLocation);
                        is = absoluteURL.openStream();
                        lastestImportURI = absoluteURL.toExternalForm();
                    } catch (Throwable t2) {
                    }
                }
            }
            if (is == null) {
                try {
                    URI fileURI = new URI(relativeLocation);
                    absoluteURL = fileURI.toURL();
                    is = absoluteURL.openStream();
                    lastestImportURI = absoluteURL.toExternalForm();
                } catch (Throwable t) {
                    //No FFDC code needed  
                }
            }
            if (is == null) {
                try {
                    File file = new File(relativeLocation);
                    absoluteURL = file.toURL();
                    is = absoluteURL.openStream();
                    lastestImportURI = absoluteURL.toExternalForm();
                } catch (Throwable t) {
                    //No FFDC code needed           
                }
            }

        } else {
            String importPath = normalizePath(parentLocation, relativeLocation);
            is = getInputStream(importPath);
            lastestImportURI = importPath;
        }
    } catch (IOException ex) {
        throw ExceptionFactory.makeWebServiceException(
                Messages.getMessage("WSDLRelativeErr1", relativeLocation, parentLocation, ex.toString()));
    }
    if (is == null) {
        throw ExceptionFactory.makeWebServiceException(
                Messages.getMessage("WSDLRelativeErr2", relativeLocation, parentLocation));
    }
    if (log.isDebugEnabled()) {
        log.debug("Loaded file: " + relativeLocation);
    }
    return new InputSource(is);
}

From source file:org.apache.hadoop.hdfs.server.namenode.ClusterJspHelper.java

private static String queryMbean(URI httpAddress, Configuration conf) throws IOException {
    /**/* www.j ava2  s  .c o m*/
     * Although the other namenode might support HTTPS, it is fundamentally
     * broken to get the JMX via an HTTPS connection inside the namenode,
     * because in HTTPS set up the principal of the client and the one of
     * the namenode differs. Therefore, there is no guarantees that the
     * HTTPS connection can be set up.
     *
     * As a result, we just hard code the connection as an HTTP connection.
     */
    URL url = new URL(httpAddress.toURL(), JMX_QRY);
    return readOutput(url);
}

From source file:de.dfki.km.perspecting.obie.corpus.BBCMusicCorpus.java

public Reader getGroundTruth(final URI uri) throws Exception {
    if (labelFileMediaType == MediaType.DIRECTORY) {
        return new StringReader(FileUtils.readFileToString(new File(uri)));
    } else if (labelFileMediaType == MediaType.ZIP) {
        ZipFile zipFile = new ZipFile(labelFolder);
        String[] entryName = uri.toURL().getFile().split("/");
        ZipEntry entry = zipFile.getEntry(
                URLDecoder.decode(entryName[entryName.length - 1], "utf-8").replace("txt", "dumps") + ".rdf");

        if (entry != null) {
            log.info("found labels for: " + uri.toString());
        } else {//from w w  w. j a  v  a2s  .  c  o  m
            throw new Exception("did not found labels for: " + uri.toString());
        }
        return new InputStreamReader(zipFile.getInputStream(entry));
    } else {
        throw new Exception("Unsupported media format for labels: " + labelFileMediaType + ". "
                + "Please use zip or plain directories instead.");
    }
}

From source file:org.bsc.confluence.xmlrpc.ConfluenceExportDecorator.java

/**
 * /*from  w w  w  .  ja  v a2s.co m*/
 * @param space
 * @param pageTitle
 * @param format
 * @param outputFile
 * @throws Exception 
 */
public final void exportPage(String space, String pageTitle, ExportFormat format,
        final java.io.File outputFile) {
    assert space != null;
    assert pageTitle != null;
    assert format != null;
    assert outputFile != null;

    if (space == null) {
        throw new IllegalArgumentException("space is null!");
    }
    if (pageTitle == null) {
        throw new IllegalArgumentException("pageTitle is null!");
    }
    if (format == null) {
        throw new IllegalArgumentException("format is null!");
    }
    if (outputFile == null) {
        throw new IllegalArgumentException("outputFile is null!");
    }
    if (outputFile.exists() && !outputFile.isFile()) {
        throw new IllegalArgumentException("outputFile is not a file!");
    }

    confluence.getPage(space, pageTitle).thenAccept(p -> {

        Model.Page page = p.orElseThrow(() -> new RuntimeException(format("page [%s] not found!", pageTitle)));

        final HttpClient client = new HttpClient();
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

        login(client, String.format("%s?pageId=%s", format.url, page.getId()), (location) -> {
            final java.net.URI uri = java.net.URI.create(url).resolve(location).normalize();

            System.out.printf("==> EXPORT URL [%s]\n", uri.toString());

            exportpdf(client, uri.toURL(), outputFile);
        });

    }).exceptionally(ex -> {
        ex.printStackTrace();
        return null;
    });

}

From source file:de.dfki.km.perspecting.obie.corpus.BBCNatureCorpus.java

public Reader getGroundTruth(final URI uri) throws Exception {
    if (labelFileMediaType == MediaType.DIRECTORY) {
        return new StringReader(FileUtils.readFileToString(new File(uri)));
    } else if (labelFileMediaType == MediaType.ZIP) {
        ZipFile zipFile = new ZipFile(labelFolder);
        String[] entryName = uri.toURL().getFile().split("/");
        ZipEntry entry = zipFile/*w w  w . j  a v a  2s  . co  m*/
                .getEntry(URLDecoder.decode(entryName[entryName.length - 1], "utf-8").replace("text", "rdf"));

        if (entry != null) {
            log.info("found labels for: " + uri.toString());
        } else {
            throw new Exception("did not found labels for: " + uri.toString());
        }
        return new InputStreamReader(zipFile.getInputStream(entry));
    } else {
        throw new Exception("Unsupported media format for labels: " + labelFileMediaType + ". "
                + "Please use zip or plain directories instead.");
    }
}

From source file:me.hqm.plugindev.wget.WGCommand.java

/**
 * Get a URL from an input string./*from   w w w. ja v  a 2s.  c o  m*/
 *
 * @param input URL String
 * @return Valid URL
 */
private URL getUrl(String input) {
    // We only accept jar or zip files
    if (input.endsWith(".jar") || input.endsWith(".zip")) {
        // Try to create the object, and test the connection
        try {
            URI uri = new URI(input);
            URL url = uri.toURL();
            URLConnection conn = url.openConnection();
            conn.connect();
            // Return the url
            return url;
        } catch (IOException | URISyntaxException ignored) {
        }
    }
    // Failed, return null
    return null;
}