List of usage examples for java.net URLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:com.sencko.basketball.stats.advanced.FIBAJsonParser.java
private static Game readGameFromNet(String location) throws IOException, MalformedURLException { InputStream jsonInputStream;//from w ww .j a v a 2s . c o m String prefix = "http://webcast-a.live.sportingpulseinternational.com/matches/"; String suffix = "//data.json"; String html_suffix = "//index.html"; URL url = new URL(prefix + location + suffix); logger.log(Level.FINEST, "Downloading file {0} from internet", url.toString()); URLConnection connection = url.openConnection(); connection.connect(); jsonInputStream = connection.getInputStream(); try { Game game = readGameFromStream(jsonInputStream); try { URL html_url = new URL(prefix + location + html_suffix); String html = IOUtils.toString(html_url, "UTF-8"); Matcher match = teamAndTime.matcher(html); if (match.matches()) { game.getTm().get(1).setTeam(match.group(1)); game.getTm().get(2).setTeam(match.group(2)); Calendar calendar = Calendar.getInstance(); //TimeZone.getTimeZone("EET") calendar.set(Integer.parseInt(match.group(7)), Integer.parseInt(match.group(6)) - 1, Integer.parseInt(match.group(5)), Integer.parseInt(match.group(3)), Integer.parseInt(match.group(4))); game.setDate(calendar.getTime()); } } catch (Exception ex) { ex.printStackTrace(); } return game; } finally { jsonInputStream.close(); } }
From source file:org.opencastproject.rest.RestServiceTestEnv.java
/** * Return a localhost base URL with a random port between 8081 and 9000. * The method features a port usage detection to ensure it returns a free port. *///w w w.j a va 2s.c om public static URL localhostRandomPort() { for (int tries = 100; tries > 0; tries--) { final URL url = UrlSupport.url("http", "localhost", 8081 + new Random(System.currentTimeMillis()).nextInt(919)); InputStream in = null; try { final URLConnection con = url.openConnection(); con.setConnectTimeout(1000); con.setReadTimeout(1000); con.getInputStream(); } catch (IOException e) { IoSupport.closeQuietly(in); return url; } } throw new RuntimeException("Cannot find free port. Giving up."); }
From source file:Resources.java
/** * Gets a URL as an input stream//from www.ja v a 2 s .c o m * * @param urlString - the URL to get * @return An input stream with the data from the URL * @throws IOException If the resource cannot be found or read */ public static InputStream getUrlAsStream(String urlString) throws IOException { URL url = new URL(urlString); URLConnection conn = url.openConnection(); return conn.getInputStream(); }
From source file:net.amigocraft.mpt.command.InstallCommand.java
@SuppressWarnings("unchecked") public static void downloadPackage(String id) throws MPTException { JSONObject packages = (JSONObject) Main.packageStore.get("packages"); if (packages != null) { JSONObject pack = (JSONObject) packages.get(id); if (pack != null) { if (pack.containsKey("name") && pack.containsKey("version") && pack.containsKey("url")) { if (pack.containsKey("sha1") || !Config.ENFORCE_CHECKSUM) { String name = pack.get("name").toString(); String version = pack.get("version").toString(); String fullName = name + " v" + version; String url = pack.get("url").toString(); String sha1 = pack.containsKey("sha1") ? pack.get("sha1").toString() : ""; if (pack.containsKey("installed")) { //TODO: compare versions throw new MPTException(ID_COLOR + name + ERROR_COLOR + " is already installed"); }/* w ww .ja v a 2 s. co m*/ try { URLConnection conn = new URL(url).openConnection(); conn.connect(); ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream()); File file = new File(Main.plugin.getDataFolder(), "cache" + File.separator + id + ".zip"); file.setReadable(true, false); file.setWritable(true, false); file.getParentFile().mkdirs(); file.createNewFile(); FileOutputStream os = new FileOutputStream(file); os.getChannel().transferFrom(rbc, 0, MiscUtil.getFileSize(new URL(url))); os.close(); if (!sha1.isEmpty() && !sha1(file.getAbsolutePath()).equals(sha1)) { file.delete(); throw new MPTException(ERROR_COLOR + "Failed to install package " + ID_COLOR + fullName + ERROR_COLOR + ": checksum mismatch!"); } } catch (IOException ex) { throw new MPTException( ERROR_COLOR + "Failed to download package " + ID_COLOR + fullName); } } else throw new MPTException(ERROR_COLOR + "Package " + ID_COLOR + id + ERROR_COLOR + " is missing SHA-1 checksum! Aborting..."); } else throw new MPTException(ERROR_COLOR + "Package " + ID_COLOR + id + ERROR_COLOR + " is missing required elements!"); } else throw new MPTException(ERROR_COLOR + "Cannot find package with id " + ID_COLOR + id); } else { throw new MPTException(ERROR_COLOR + "Package store is malformed!"); } }
From source file:org.ednovo.gooru.application.converter.GooruImageUtil.java
public static String downloadWebResourceToFile(String srcUrl, String outputFolderPath, String fileNamePrefix, String fileExtension) {// ww w. j av a 2 s . c om if (srcUrl == null || outputFolderPath == null || fileNamePrefix == null) { return null; } try { File outputFolder = new File(outputFolderPath); URL url = new URL(srcUrl); URLConnection urlCon = url.openConnection(); InputStream inputStream = urlCon.getInputStream(); if (!outputFolder.exists()) { outputFolder.mkdirs(); } if (fileExtension == null) { fileExtension = getWebFileExtenstion(urlCon.getContentType()); } String destFilePath = outputFolderPath + fileNamePrefix + "." + fileExtension; File outputFile = new File(destFilePath); OutputStream out = new FileOutputStream(outputFile); byte buf[] = new byte[1024]; int len; while ((len = inputStream.read(buf)) > 0) out.write(buf, 0, len); out.close(); inputStream.close(); return destFilePath; } catch (Exception e) { logger.warn("DownloadImage failed:exception:", e); return null; } }
From source file:com.hazelcast.qasonar.utils.Utils.java
private static InputStream getBaseAuthInputStreamFromURL(String query, String basicAuthString) throws IOException { URL url = new URL(query); URLConnection uc = url.openConnection(); uc.setRequestProperty("Authorization", basicAuthString); return uc.getInputStream(); }
From source file:com.net2plan.utils.HTMLUtils.java
/** * <p>Returns the HTML text from a given URL.</p> * * <p>This method is intended for HTML files enclosed into the same JAR file as that enclosing the calling class. Therefore, the URL must point the location of the HTML file within that JAR file.</p> * * <p>For example, assuming that a file named "example.html" is in the path "/aux-files/html" within the JAR file, then the calling would be as follows:</p> * * {@code String html = HTMLUtils.getHTMLFromURL(getClass().getResource("/aux-files/html/examples.html").toURI().toURL());} * * <p><b>Important</b>: Image paths are converted to absolute paths.</p> * * @param url A valid URL//from www. j a v a2 s . c o m * @return A HTML String */ public static String getHTMLFromURL(URL url) { try { StringBuilder content = new StringBuilder(); URLConnection urlConnection = url.openConnection(); try (BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), StandardCharsets.UTF_8))) { String line; while ((line = bufferedReader.readLine()) != null) content.append(line).append(StringUtils.getLineSeparator()); } return prepareImagePath(content.toString(), url); } catch (Throwable e) { throw new RuntimeException(e); } }
From source file:fr.cls.atoll.motu.library.misc.cas.TestCASRest.java
public static void testDownloadCASifiedResource() throws MotuException, IOException, MotuCasBadRequestException { if (LOG.isDebugEnabled()) { LOG.debug("testDownloadCASifiedResource() - entering"); }//from w w w .j a v a 2s. c o m // http://atoll-dev.cls.fr:43080/thredds/dodsC/mercator_modified //String serviceURL = "http://atoll-dev.cls.fr:30080/atoll-motuservlet/OpendapAuth?action=productdownload&data=http://atoll-dev.cls.fr:43080/thredds/dodsC/nrt_glo_hr_infrared_sst&x_lo=2&x_hi=3&y_lo=1&y_hi=4&t_lo=2009-12-01&t_hi=2009-12-01&variable=Grid_0001&mode=console"; String serviceURL = "http://atoll-dev.cls.fr:30080/mis-gateway-servlet/Motu?z_lo=0&y_lo=30&t_hi=2011-01-26&mode=console&product=dataset-psy2v3-pgs-med-myocean-bestestimate&x_lo=-6.0&y_hi=31&t_lo=2011-01-26&z_hi=0&action=productdownload&service=http%3A%2F%2Fpurl.org%2Fcls%2Fatoll%2Fontology%2Findividual%2Fatoll%23motu-opendap-mercator-myocean&x_hi=1.0&variable=u&variable=v"; String username = "xxx"; String password = "xxx"; String casRestUrlSuffix = Organizer.getMotuConfigInstance().getCasRestUrlSuffix(); String casRestUrl = RestUtil.getCasRestletUrl(serviceURL, casRestUrlSuffix); String serviceTicket = RestUtil.loginToCAS(casRestUrl, username, password, serviceURL); if (LOG.isDebugEnabled()) { LOG.debug("testDownloadCASifiedResource() - serviceTicket:" + serviceTicket); } // final Map CONST_ATTRIBUTES = new HashMap(); // CONST_ATTRIBUTES.put(username, serviceTicket); // // final AttributePrincipal CONST_PRINCIPAL = new AttributePrincipalImpl(username); // final Assertion assertion = new AssertionImpl(CONST_PRINCIPAL, // CONST_ATTRIBUTES); // // // AssertionHolder.setAssertion(assertion); // //////////////String path = AssertionUtils.addCASTicket(serviceTicket, serviceURL); InputStream is; fr.cls.atoll.motu.library.misc.tds.server.Catalog catalogXml; try { // JAXBContext jc = JAXBContext.newInstance(TDS_SCHEMA_PACK_NAME); // Unmarshaller unmarshaller = jc.createUnmarshaller(); URL url = new URL(AssertionUtils.addCASTicket(serviceTicket, serviceURL)); URLConnection conn = url.openConnection(); is = conn.getInputStream(); InputStreamReader eisr = new InputStreamReader(is); BufferedReader in = new BufferedReader(eisr); String nextLine = ""; // StringBuffer stringBuffer = new StringBuffer(); while ((nextLine = in.readLine()) != null) { // stringBuffer.append(nextLine); System.out.println(nextLine); } in.close(); } catch (Exception e) { throw new MotuException("Error in loadConfigTds", e); } try { is.close(); } catch (IOException io) { io.getMessage(); } if (LOG.isDebugEnabled()) { LOG.debug("testLoginToCAS() - exiting"); } }
From source file:fr.cls.atoll.motu.library.misc.cas.TestCASRest.java
public static void testgetCASifiedResource() throws MotuException, IOException, MotuCasBadRequestException { if (LOG.isDebugEnabled()) { LOG.debug("testLoginToCAS() - entering"); }/*from w w w .j ava 2 s . c o m*/ String serviceURL = "http://atoll-dev.cls.fr:43080/thredds/catalog.xml"; String username = "xxx"; String password = "xxx"; String casRestUrlSuffix = Organizer.getMotuConfigInstance().getCasRestUrlSuffix(); String casRestUrl = RestUtil.getCasRestletUrl(serviceURL, casRestUrlSuffix); String serviceTicket = RestUtil.loginToCAS(casRestUrl, username, password, serviceURL); if (LOG.isDebugEnabled()) { LOG.debug("testLoginToCAS() - serviceTicket:" + serviceTicket); } // final Map CONST_ATTRIBUTES = new HashMap(); // CONST_ATTRIBUTES.put(username, serviceTicket); // // final AttributePrincipal CONST_PRINCIPAL = new AttributePrincipalImpl(username); // final Assertion assertion = new AssertionImpl(CONST_PRINCIPAL, // CONST_ATTRIBUTES); // // // AssertionHolder.setAssertion(assertion); // //////////////String path = AssertionUtils.addCASTicket(serviceTicket, serviceURL); InputStream in; fr.cls.atoll.motu.library.misc.tds.server.Catalog catalogXml; try { // JAXBContext jc = JAXBContext.newInstance(TDS_SCHEMA_PACK_NAME); // Unmarshaller unmarshaller = jc.createUnmarshaller(); URL url = new URL(AssertionUtils.addCASTicket(serviceTicket, serviceURL)); URLConnection conn = url.openConnection(); in = conn.getInputStream(); if (Organizer.getUnmarshallerTdsConfig() == null) { Organizer.initJAXBTdsConfig(); } synchronized (Organizer.getUnmarshallerTdsConfig()) { catalogXml = (fr.cls.atoll.motu.library.misc.tds.server.Catalog) Organizer .getUnmarshallerTdsConfig().unmarshal(in); } } catch (Exception e) { throw new MotuException("Error in loadConfigTds", e); } if (catalogXml == null) { throw new MotuException(String.format( "Unable to load Tds configuration (in loadConfigOpendap, cataloXml is null) - url : %s", serviceURL)); } try { in.close(); } catch (IOException io) { io.getMessage(); } if (LOG.isDebugEnabled()) { LOG.debug("testLoginToCAS() - catalogXml:" + catalogXml); } if (LOG.isDebugEnabled()) { LOG.debug("testLoginToCAS() - exiting"); } }
From source file:Main.java
public static String getURLContent(String urlStr) throws Exception { URL url = new URL(urlStr); URLConnection connection = url.openConnection(); connection.setDoOutput(true);//ww w . j av a 2 s .co m connection.connect(); OutputStream ous = connection.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ous)); bw.write("index.htm"); bw.flush(); bw.close(); printRequestHeaders(connection); InputStream ins = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(ins)); StringBuffer sb = new StringBuffer(); String msg = null; while ((msg = br.readLine()) != null) { sb.append(msg); sb.append("\n"); // Append a new line } br.close(); return sb.toString(); }