List of usage examples for java.net URL openStream
public final InputStream openStream() throws java.io.IOException
From source file:com.xerox.amazonws.ec2.EC2Utils.java
/** * This method makes a best effort to fetch all instance metadata. * * @return map of metadata//w w w . j av a 2 s .co m */ public static Map<String, String> getInstanceMetadata() { HashMap<String, String> result = new HashMap<String, String>(); int retries = 0; while (true) { try { URL url = new URL("http://169.254.169.254/latest/meta-data/"); BufferedReader rdr = new BufferedReader(new InputStreamReader(url.openStream())); String line = rdr.readLine(); while (line != null) { try { String val = getInstanceMetadata(line); result.put(line, val); } catch (IOException ex) { logger.error("Problem fetching piece of instance metadata!", ex); } line = rdr.readLine(); } return result; } catch (IOException ex) { if (retries == 5) { logger.debug("Problem getting instance data, retries exhausted..."); return result; } else { logger.debug("Problem getting instance data, retrying..."); try { Thread.sleep((int) Math.pow(2.0, retries) * 1000); } catch (InterruptedException e) { } } } } }
From source file:com.accumulobook.designs.rdf.FreebaseExample.java
public static List<Pair<String, String>> getProperties(final String subject) throws Exception { ArrayList<Pair<String, String>> properties = new ArrayList<>(); URL url = new URL("https://www.googleapis.com/freebase/v1/topic/en/" + subject); StringWriter writer = new StringWriter(); IOUtils.copy(url.openStream(), writer); String string = writer.toString(); JSONObject obj = new JSONObject(string); JSONObject propArray = obj.getJSONObject("property"); Iterator iter = propArray.keys(); while (iter.hasNext()) { String propertyName = (String) iter.next(); JSONObject property = propArray.getJSONObject(propertyName); JSONArray values = property.getJSONArray("values"); for (int i = 0; i < values.length(); i++) { properties.add(new Pair(propertyName, values.getJSONObject(i).getString("text"))); }/*from w w w .j ava2s . c om*/ } return properties; }
From source file:net.myrrix.common.io.IOUtils.java
/** * @param url URL whose contents are to be read and copied * @param file file to write contents to * @throws IOException if the URL can't be read or the file can't be written *//*from w w w . j a v a2s . c om*/ public static void copyURLToFile(URL url, File file) throws IOException { InputStream in = url.openStream(); try { copyStreamToFile(in, file); } finally { in.close(); } }
From source file:net.nifheim.beelzebu.coins.common.utils.dependencies.DependencyManager.java
private static File downloadDependency(File libDir, Dependency dependency) throws Exception { String fileName = dependency.name().toLowerCase() + "-" + dependency.getVersion() + ".jar"; File file = new File(libDir, fileName); if (file.exists()) { return file; }/*www. j a v a 2s. c o m*/ URL url = new URL(dependency.getUrl()); core.getMethods().log("Dependency '" + fileName + "' could not be found. Attempting to download."); try (InputStream in = url.openStream()) { Files.copy(in, file.toPath()); } if (!file.exists()) { throw new IllegalStateException("File not present. - " + file.toString()); } else { core.getMethods().log("Dependency '" + fileName + "' successfully downloaded."); return file; } }
From source file:com.easarrive.aws.plugins.common.util.SNSUtil.java
public static StringBuilder getHttpRequestContent(URL url) { if (url == null) { return null; }/*from w w w .ja va 2 s .c om*/ StringBuilder builder = null; try { builder = getHttpRequestContent(url.openStream()); } catch (IOException e2) { e2.printStackTrace(); } return builder; }
From source file:info.usbo.skypetwitter.Run.java
private static void vk() throws ParseException { String url = "https://api.vk.com/method/wall.get?v=5.28&domain=Depersonilized&filter=owner&extended=1"; String line = ""; try {/*from ww w . j av a 2s . com*/ URL url2 = new URL(url); BufferedReader reader = new BufferedReader(new InputStreamReader(url2.openStream(), "UTF-8")); line = reader.readLine(); reader.close(); } catch (MalformedURLException e) { // ... } catch (IOException e) { // ... } JSONObject json = (JSONObject) new JSONParser().parse(line); json = (JSONObject) new JSONParser().parse(json.get("response").toString()); JSONArray jsona = (JSONArray) new JSONParser().parse(json.get("items").toString()); vk.clear(); for (int i = 0; i < jsona.size(); i++) { vk.add(new VK(jsona.get(i).toString())); } }
From source file:net.sf.jasperreports.engine.util.JRXmlUtils.java
/** * Parses an URL stream as a XML document. * /* ww w . ja va 2 s. c o m*/ * @param url the URL * @return the document * @throws JRException */ public static Document parse(URL url, boolean isNamespaceAware) throws JRException { InputStream is = null; try { is = url.openStream(); return createDocumentBuilder(isNamespaceAware).parse(is); } catch (SAXException e) { throw new JRException(EXCEPTION_MESSAGE_KEY_DOCUMENT_PARSING_FAILURE, null, e); } catch (IOException e) { throw new JRException(EXCEPTION_MESSAGE_KEY_DOCUMENT_PARSING_FAILURE, null, e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { log.warn("Error closing stream of URL " + url, e); } } } }
From source file:net.myrrix.common.io.IOUtils.java
/** * @param url URL whose contents are to be read * @return the contents of the URL, interpreted as a UTF-8 encoded string * @throws IOException if the URL can't be read or the file can't be written *//*from ww w . j av a 2 s .co m*/ public static String readSmallTextFromURL(URL url) throws IOException { Reader in = new InputStreamReader(url.openStream(), Charsets.UTF_8); try { return CharStreams.toString(in); } finally { in.close(); } }
From source file:de.mfo.jsurf.grid.RotationGrid.java
public static void loadFromFile(URL url) throws IOException, Exception { Properties props = new Properties(); props.load(url.openStream()); loadFromProperties(props);//from w w w. j ava2 s . co m }
From source file:io.apiman.gateway.engine.jdbc.JdbcMetricsTest.java
/** * Initialize the DB with the apiman gateway DDL. * @param connection/*from w w w. j a v a 2s. c o m*/ */ private static void initDB(Connection connection) throws Exception { ClassLoader cl = JdbcMetricsTest.class.getClassLoader(); URL resource = cl.getResource("ddls/apiman-gateway_h2.ddl"); try (InputStream is = resource.openStream()) { System.out.println("======================================="); System.out.println("Initializing database."); DdlParser ddlParser = new DdlParser(); List<String> statements = ddlParser.parse(is); for (String sql : statements) { System.out.println(sql); PreparedStatement statement = connection.prepareStatement(sql); statement.execute(); } System.out.println("======================================="); } }