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.netflix.nicobar.core.module.ScriptModuleUtils.java

/**
 * Convert a ScriptModule to its compiled equivalent ScriptArchive.
 * <p>//from   ww w.  j av a2 s  .  c  o  m
 * A jar script archive is created containing compiled bytecode
 * from a script module, as well as resources and other metadata from
 * the source script archive.
 * <p>
 * This involves serializing the class bytes of all the loaded classes in
 * the script module, as well as copying over all entries in the original
 * script archive, minus any that have excluded extensions. The module spec
 * of the source script archive is transferred as is to the target bytecode
 * archive.
 *
 * @param module the input script module containing loaded classes
 * @param jarPath the path to a destination JarScriptArchive.
 * @param excludeExtensions a set of extensions with which
 *        source script archive entries can be excluded.
 *
 * @throws Exception
 */
public static void toCompiledScriptArchive(ScriptModule module, Path jarPath, Set<String> excludeExtensions)
        throws Exception {
    ScriptArchive sourceArchive = module.getSourceArchive();
    JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jarPath.toFile()));
    try {
        // First copy all resources (excluding those with excluded extensions)
        // from the source script archive, into the target script archive
        for (String archiveEntry : sourceArchive.getArchiveEntryNames()) {
            URL entryUrl = sourceArchive.getEntry(archiveEntry);
            boolean skip = false;
            for (String extension : excludeExtensions) {
                if (entryUrl.toString().endsWith(extension)) {
                    skip = true;
                    break;
                }
            }

            if (skip)
                continue;

            InputStream entryStream = entryUrl.openStream();
            byte[] entryBytes = IOUtils.toByteArray(entryStream);
            entryStream.close();

            jarStream.putNextEntry(new ZipEntry(archiveEntry));
            jarStream.write(entryBytes);
            jarStream.closeEntry();
        }

        // Now copy all compiled / loaded classes from the script module.
        Set<Class<?>> loadedClasses = module.getModuleClassLoader().getLoadedClasses();
        Iterator<Class<?>> iterator = loadedClasses.iterator();
        while (iterator.hasNext()) {
            Class<?> clazz = iterator.next();
            String classPath = clazz.getName().replace(".", "/") + ".class";
            URL resourceURL = module.getModuleClassLoader().getResource(classPath);
            if (resourceURL == null) {
                throw new Exception("Unable to find class resource for: " + clazz.getName());
            }

            InputStream resourceStream = resourceURL.openStream();
            jarStream.putNextEntry(new ZipEntry(classPath));
            byte[] classBytes = IOUtils.toByteArray(resourceStream);
            resourceStream.close();
            jarStream.write(classBytes);
            jarStream.closeEntry();
        }

        // Copy the source moduleSpec, but tweak it to specify the bytecode compiler in the
        // compiler plugin IDs list.
        ScriptModuleSpec moduleSpec = sourceArchive.getModuleSpec();
        ScriptModuleSpec.Builder newModuleSpecBuilder = new ScriptModuleSpec.Builder(moduleSpec.getModuleId());
        newModuleSpecBuilder.addCompilerPluginIds(moduleSpec.getCompilerPluginIds());
        newModuleSpecBuilder.addCompilerPluginId(BytecodeLoadingPlugin.PLUGIN_ID);
        newModuleSpecBuilder.addMetadata(moduleSpec.getMetadata());
        newModuleSpecBuilder.addModuleDependencies(moduleSpec.getModuleDependencies());

        // Serialize the modulespec with GSON and its default spec file name
        ScriptModuleSpecSerializer specSerializer = new GsonScriptModuleSpecSerializer();
        String json = specSerializer.serialize(newModuleSpecBuilder.build());
        jarStream.putNextEntry(new ZipEntry(specSerializer.getModuleSpecFileName()));
        jarStream.write(json.getBytes(Charsets.UTF_8));
        jarStream.closeEntry();
    } finally {
        if (jarStream != null) {
            jarStream.close();
        }
    }
}

From source file:WarUtil.java

public static boolean isResourceExist(URL resource) {
    if (resource.getProtocol().equals(FILE_PROTOCOL)) {
        try {//from  ww  w.  j a  v  a 2  s.c  o m
            File file = new File(resource.toURI());
            return file.exists();
        } catch (URISyntaxException e) {
            return false;
        }
    }
    InputStream is = null;
    try {
        is = resource.openStream();
        return (is != null);
    } catch (Exception ioe) {
        return false;
    }
}

From source file:org.shareok.data.kernel.api.services.ServiceUtil.java

public static boolean downloadRemoteFile(String link, String targetFile) {
    ByteArrayOutputStream out = null;
    InputStream in = null;/*from www .j ava 2  s. c  om*/
    FileOutputStream fos = null;
    try {
        URL linkUrl = new URL(link);
        in = new BufferedInputStream(linkUrl.openStream());
        out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int n = 0;
        while (-1 != (n = in.read(buffer))) {
            out.write(buffer, 0, n);
        }
        byte[] response = out.toByteArray();
        fos = new FileOutputStream(targetFile);
        fos.write(response);
        return true;
    } catch (MalformedURLException mex) {
        logger.error("Cannot set up the url for file download.", mex);
    } catch (IOException ioex) {
        logger.error("Cannot open the stream for the link URL.", ioex);
    } finally {
        try {
            if (null != out) {
                out.close();
            }
            if (null != in) {
                in.close();
            }
            if (null != fos) {
                fos.close();
            }
        } catch (IOException ioex) {
            logger.error("Cannot close the outputstream when downloading file online.", ioex);
        }
    }
    return false;
}

From source file:io.fabric8.kubernetes.pipeline.devops.ApplyStepExecution.java

protected static void addPropertiesFileToMap(URL url, Map<String, String> answer) throws AbortException {
    if (url != null) {
        try (InputStream in = url.openStream()) {
            Properties properties = new Properties();
            properties.load(in);//from  www  .j  a v a 2  s  .c o  m
            Map<String, String> map = toMap(properties);
            answer.putAll(map);
        } catch (IOException e) {
            throw new AbortException("Failed to load properties URL: " + url + ". " + e);
        }
    }
}

From source file:edu.ucsb.nceas.metacattest.ReplicationServerListTest.java

/**
 * Create a suite of tests to be run together
 *//*from   w  w w. java 2s  . c o  m*/
public static Test suite() {
    //Get DBConnection pool, this is only for junit test.
    //Because DBConnection is singleton class. So there is only one DBConnection
    //pool in the program
    try {
        DBConnectionPool pool = DBConnectionPool.getInstance();
    } //try
    catch (Exception e) {
        log.debug("Error in ReplicationServerList() to get" + " DBConnection pool" + e.getMessage());
    } //catch

    TestSuite suite = new TestSuite();
    suite.addTest(new ReplicationServerListTest("initialize"));

    try {

        //Add two server into xml_replication table
        URL dev = new URL(metacatReplicationURL + "?action=servercontrol&server=dev"
                + "&subaction=add&replicate=1&datareplicate=1&hub=1");
        URL epsilon = new URL(metacatReplicationURL + "?action=servercontrol"
                + "&server=epsilon&subaction=add&replicate=0&datareplicate=1&hub=0");
        InputStream input = dev.openStream();
        input.close();
        input = epsilon.openStream();
        input.close();

        //create a new server list
        ReplicationServerList list = new ReplicationServerList();

        //Doing test test cases
        suite.addTest(new ReplicationServerListTest("testSize", list));

        suite.addTest(new ReplicationServerListTest("testServerAt0", list));
        suite.addTest(new ReplicationServerListTest("testServerAt1", list));

        suite.addTest(new ReplicationServerListTest("testNonEmptyServerList", list));

        suite.addTest(new ReplicationServerListTest("testServerIsNotInList", list));
        suite.addTest(new ReplicationServerListTest("testServerIsInList", list));

        suite.addTest(new ReplicationServerListTest("testGetLastCheckedDate", list));

        suite.addTest(new ReplicationServerListTest("testGetReplicationValueFalse", list));

        suite.addTest(new ReplicationServerListTest("testGetReplicationValueTrue", list));
        suite.addTest(new ReplicationServerListTest("testGetDataReplicationValueFalse", list));

        suite.addTest(new ReplicationServerListTest("testGetHubValueTrue", list));
        suite.addTest(new ReplicationServerListTest("testGetHubValueFalse", list));

        //Delete this two server
        URL deleteDev = new URL(
                metacatReplicationURL + "?action=servercontrol" + "&server=dev&subaction=delete");
        URL deleteEpsilon = new URL(
                metacatReplicationURL + "?action=servercontrol" + "&server=epsilon&subaction=delete");
        input = deleteDev.openStream();
        input.close();
        input = deleteEpsilon.openStream();
        input.close();
    } //try
    catch (Exception e) {
        log.debug("Error in ReplicationServerListTest.suite: " + e.getMessage());
    } //catch

    return suite;
}

From source file:org.esigate.DriverFactory.java

/**
 * Loads all instances according to default configuration file.
 *///from   w  w w .jav  a2  s  . com
public static void configure() {
    InputStream inputStream = null;

    try {
        URL configUrl = getConfigUrl();

        if (configUrl == null) {
            throw new ConfigurationException(
                    "esigate.properties configuration file " + "was not found in the classpath");
        }

        inputStream = configUrl.openStream();

        Properties merged = new Properties();
        if (inputStream != null) {
            Properties props = new Properties();
            props.load(inputStream);
            merged.putAll(props);
        }

        configure(merged);
    } catch (IOException e) {
        throw new ConfigurationException("Error loading configuration", e);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }

        } catch (IOException e) {
            throw new ConfigurationException("failed to close stream", e);
        }
    }
}

From source file:com.mycompany.songbitmaven.RecommendationController.java

public static SongDataSet trackLookup(String text) {
    Api api = Api.DEFAULT_API;//from  www  .j  a v a  2s  .  c o  m
    final TrackSearchRequest request = api.searchTracks(text).market("US").build();

    try {
        final Page<com.wrapper.spotify.models.Track> trackSearchResult = request.get();
        String jsonURL = trackSearchResult.getNext();

        URL myurl = null;
        try {
            myurl = new URL(jsonURL);
        } catch (Exception e) {
            System.out.println("Improper URL " + jsonURL);
            return null;
        }

        // read from the URL
        Scanner scan = null;
        try {
            scan = new Scanner(myurl.openStream());
        } catch (Exception e) {
            System.out.println("Could not connect to " + jsonURL);
            return null;
        }

        String str = new String();
        while (scan.hasNext()) {
            str += scan.nextLine() + "\n";
        }
        scan.close();

        Gson gson = new Gson();

        System.out.println(jsonURL);
        SongDataSet dataset = gson.fromJson(str, SongDataSet.class);

        return dataset;
    } catch (Exception e) {
        System.out.println("Something went wrong!" + e.getMessage());
        return null;
    }
}

From source file:net.rptools.lib.FileUtil.java

/**
 * Loads the given {@link URL}, and unzips the URL's contents into the given <code>destDir</code>.
 * /*ww  w  . j  a v a2  s  . co m*/
 * @param url
 * @param destDir
 * @throws IOException
 */
public static void unzip(URL url, File destDir) throws IOException {
    if (url == null)
        throw new IOException("URL cannot be null");

    InputStream is = url.openStream();
    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(new BufferedInputStream(is));
        unzip(zis, destDir);
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:com.netxforge.oss2.config.DiscoveryConfigFactory.java

/**
 * <pre>/*from  w  w  w. j a  v  a  2s .  co m*/
 * The file URL is read and a 'specific IP' is added for each entry
 *  in this file. Each line in the URL file can be one of -
 *  &lt;IP&gt;&lt;space&gt;#&lt;comments&gt;
 *  or
 *  &lt;IP&gt;
 *  or
 *  #&lt;comments&gt;
 *
 *  Lines starting with a '#' are ignored and so are characters after
 *  a '&lt;space&gt;#' in a line.
 * </pre>
 *
 * @param specifics
 *            the list to add to
 * @param url
 *            the URL file
 * @param timeout
 *            the timeout for all entries in this URL
 * @param retries
 *            the retries for all entries in this URL
 * @return a boolean.
 */
public static boolean addToSpecificsFromURL(final List<IPPollAddress> specifics, final String url,
        final long timeout, final int retries) {
    // open the file indicated by the URL
    InputStream is = null;
    try {
        final URL fileURL = new URL(url);
        is = fileURL.openStream();
        // check to see if the file exists
        if (is == null) {
            // log something
            LogUtils.warnf(DiscoveryConfigFactory.class, "URL does not exist: %s", url);
            return true;
        } else {
            return addToSpecificsFromURL(specifics, fileURL.openStream(), timeout, retries);
        }
    } catch (final IOException e) {
        LogUtils.errorf(DiscoveryConfigFactory.class, "Error reading URL: %s", url);
        return false;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:io.ecarf.core.utils.Utils.java

/**
 * Copy a url to a local file/*  w  w  w.ja v a  2  s .  com*/
 * @param url
 * @param file
 * @throws IOException 
 */
public static void downloadFile(String url, String file) throws IOException {
    log.info("Downloading file from: " + url);
    URL webfile = new URL(url);
    try (ReadableByteChannel rbc = Channels.newChannel(webfile.openStream());
            FileOutputStream fos = new FileOutputStream(file)) {
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    }
}