Example usage for java.net URL openStream

List of usage examples for java.net URL openStream

Introduction

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

Prototype

public final InputStream openStream() throws java.io.IOException 

Source Link

Document

Opens a connection to this URL and returns an InputStream for reading from that connection.

Usage

From source file:com.cedarsoft.crypt.X509Support.java

/**
 * Reads the x509 certificate from the given url
 *
 * @param certificateUrl the certificate url
 * @return the certificate/*from  w w  w.j  av a2 s. c  om*/
 *
 * @throws IOException if any.
 * @throws GeneralSecurityException
 *                             if any.
 */
@Nonnull
public static X509Certificate readCertificate(@Nonnull URL certificateUrl)
        throws IOException, GeneralSecurityException {
    //Read the cert
    DataInputStream in = new DataInputStream(certificateUrl.openStream());
    try {
        CertificateFactory cf = CertificateFactory.getInstance(X_509_CERTIFICATE_TYPE);
        X509Certificate certificate = (X509Certificate) cf.generateCertificate(in);
        certificate.checkValidity();
        return certificate;
    } finally {
        in.close();
    }
}

From source file:com.lostinsoftware.xsdparser.XSDParser.java

public static XSDElement parseXSD(URL url, String mainElement) throws IOException, ClassNotFoundException,
        InstantiationException, IllegalAccessException, ClassCastException {
    return parseXSD(url.openStream(), buildHelperList(mainElement), true);
}

From source file:com.lostinsoftware.xsdparser.XSDParser.java

public static XSDElement parseXSD(URL url, List<String> elements) throws IOException, ClassNotFoundException,
        InstantiationException, IllegalAccessException, ClassCastException {
    return parseXSD(url.openStream(), elements);
}

From source file:com.frostwire.gui.updates.PortableUpdater.java

private static void createScript(String scriptName) {
    File fileJS = new File(CommonUtils.getUserSettingsDir(), scriptName);
    //        if (fileJS.exists()) {
    //            return;
    //        }//  w w  w.ja  v  a  2 s  .c o  m

    URL url = ResourceManager.getURLResource(scriptName);

    InputStream is = null;
    OutputStream out = null;

    try {
        if (url != null) {
            is = new BufferedInputStream(url.openStream());
            out = new FileOutputStream(fileJS);
            IOUtils.copy(is, out);

            fileJS.setExecutable(true);
        }
    } catch (IOException e) {
        LOG.error("Error creating script", e);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(out);
    }
}

From source file:com.conversantmedia.mapreduce.tool.RunJob.java

private static String readVersionFromManifest(String version) {
    // Try again to see if this class's MANIFEST was unjar'd by the hadoop command
    // - read it from the unjar'd file instead.
    InputStream in = null;//from  w ww . j a  v  a 2s  .  com
    try {
        URL manifestUrl = findManifestForDriver();
        if (manifestUrl != null) {
            in = manifestUrl.openStream();
            Manifest manifest = new Manifest(in);
            version = manifest.getMainAttributes().getValue("Implementation-Version");
        }
    } catch (Exception e) {
        // No point in exiting the app because we fail to read version from manifest.
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(in);
    }
    return version;
}

From source file:com.kantenkugel.discordbot.moduleutils.DocParser.java

private static void download() {
    LOG.info("Downloading source-file");
    try {/* w  ww.  java 2  s .c  o  m*/
        HttpResponse<String> response = Unirest.get(JENKINS_PREFIX + ARTIFACT_SUFFIX).asString();
        if (response.getStatus() < 300 && response.getStatus() > 199) {
            JSONArray artifacts = new JSONObject(response.getBody()).getJSONArray("artifacts");
            for (int i = 0; i < artifacts.length(); i++) {
                JSONObject artifact = artifacts.getJSONObject(i);
                if (artifact.getString("fileName").endsWith("sources.jar")) {
                    URL artifactUrl = new URL(
                            JENKINS_PREFIX + "artifact/" + artifact.getString("relativePath"));
                    InputStream is = artifactUrl.openStream();
                    Files.copy(is, LOCAL_SRC_PATH, StandardCopyOption.REPLACE_EXISTING);
                    is.close();
                    LOG.info("Done downloading source-file");
                }
            }
        }
    } catch (UnirestException | IOException e) {
        LOG.log(e);
    }
}

From source file:net.roboconf.agent.internal.misc.UserDataUtils.java

/**
 * Configures the agent from a IaaS registry.
 * @param logger a logger/*w  w  w  .j a  va2  s . c o m*/
 * @return the agent's data, or null if they could not be parsed
 */
public static AgentProperties findParametersForAmazonOrOpenStack(Logger logger) {

    // Copy the user data
    String userData = "";
    InputStream in = null;
    try {
        URL userDataUrl = new URL("http://169.254.169.254/latest/user-data");
        in = userDataUrl.openStream();
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        Utils.copyStreamSafely(in, os);
        userData = os.toString("UTF-8");

    } catch (IOException e) {
        logger.severe("The agent properties could not be read. " + e.getMessage());
        Utils.logException(logger, e);
    }

    AgentProperties result = null;
    in = null;
    try {
        // Parse the user data
        result = AgentProperties.readIaasProperties(userData, logger);

        // We need to ask our IP address because we may have several network interfaces.
        URL userDataUrl = new URL("http://169.254.169.254/latest/meta-data/public-ipv4");
        in = userDataUrl.openStream();
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        Utils.copyStreamSafely(in, os);
        String ip = os.toString("UTF-8");
        if (!AgentUtils.isValidIP(ip)) {
            // Failed retrieving public IP: try private one instead
            Utils.closeQuietly(in);
            userDataUrl = new URL("http://169.254.169.254/latest/meta-data/local-ipv4");
            in = userDataUrl.openStream();
            os = new ByteArrayOutputStream();

            Utils.copyStreamSafely(in, os);
            ip = os.toString("UTF-8");
        }

        if (!AgentUtils.isValidIP(ip))
            throw new IOException("No IP address could be retrieved (either public-ipv4 or local-ipv4)");

        result.setIpAddress(os.toString("UTF-8"));

    } catch (IOException e) {
        logger.severe("The network properties could not be read. " + e.getMessage());
        Utils.logException(logger, e);

    } finally {
        Utils.closeQuietly(in);
    }

    return result;
}

From source file:es.usc.citius.composit.transformer.wsc.wscxml.WSCTransformer.java

public static URL obtainXmlBaseUri(URL taxonomyOwlUrl)
        throws IOException, XPathExpressionException, ParserConfigurationException, SAXException {
    return obtainXmlBaseUri(taxonomyOwlUrl.openStream());
}

From source file:net.sourceforge.fenixedu.util.report.ReportsUtils.java

public static void loadReportsProperties(final Properties properties, final String fileName)
        throws IOException {
    final Enumeration<URL> resources = ReportsUtils.class.getClassLoader().getResources(fileName);
    while (resources.hasMoreElements()) {
        URL reportsURL = resources.nextElement();
        final InputStream inputStream = reportsURL.openStream();
        if (inputStream != null) {
            logger.debug("loaded resource from: ", reportsURL);
            properties.load(inputStream);
        }/* w  w  w. ja  va2  s.  c om*/
    }
}

From source file:com.aol.advertising.qiao.util.CommonUtils.java

public static Properties loadProperties(String propFile) throws IOException {
    Properties p = new Properties();
    URL url = ClassLoader.getSystemResource(propFile);
    if (url != null)
        p.load(url.openStream());
    return p;/* ww w  . ja  va  2  s  .com*/
}