List of usage examples for java.net URL openStream
public final InputStream openStream() throws java.io.IOException
From source file:com.recomdata.datasetexplorer.proxy.XmlHttpProxy.java
/** * * CLI to the XmlHttpProxy// w w w . ja va2 s .com */ public static void main(String[] args) throws IOException, MalformedURLException { getLogger().info("XmlHttpProxy 1.1"); XmlHttpProxy xhp = new XmlHttpProxy(); if (args.length == 0) { System.out.println(USAGE); } InputStream xslInputStream = null; String serviceKey = null; String urlString = null; String xslURLString = null; String format = "xml"; String callback = null; String urlParams = null; String configURLString = "xhp.json"; String resourceBase = "file:src/conf/META-INF/resources/xsl/"; // read in the arguments int index = 0; while (index < args.length) { if (args[index].toLowerCase().equals("-url") && index + 1 < args.length) { urlString = args[++index]; } else if (args[index].toLowerCase().equals("-key") && index + 1 < args.length) { serviceKey = args[++index]; } else if (args[index].toLowerCase().equals("-id") && index + 1 < args.length) { serviceKey = args[++index]; } else if (args[index].toLowerCase().equals("-callback") && index + 1 < args.length) { callback = args[++index]; } else if (args[index].toLowerCase().equals("-xslurl") && index + 1 < args.length) { xslURLString = args[++index]; } else if (args[index].toLowerCase().equals("-urlparams") && index + 1 < args.length) { urlParams = args[++index]; } else if (args[index].toLowerCase().equals("-config") && index + 1 < args.length) { configURLString = args[++index]; } else if (args[index].toLowerCase().equals("-resources") && index + 1 < args.length) { resourceBase = args[++index]; } index++; } if (serviceKey != null) { try { InputStream is = (new URL(configURLString)).openStream(); JSONObject services = loadServices(is); JSONObject service = services.getJSONObject(serviceKey); // default to the service default if no url parameters are specified if (urlParams == null && service.has("defaultURLParams")) { urlParams = service.getString("defaultURLParams"); } String serviceURL = service.getString("url"); // build the URL properly if (urlParams != null && serviceURL.indexOf("?") == -1) { serviceURL += "?"; } else if (urlParams != null) { serviceURL += "&"; } urlString = serviceURL + service.getString("apikey") + "&" + urlParams; if (service.has("xslStyleSheet")) { xslURLString = service.getString("xslStyleSheet"); // check if the url is correct of if to load from the classpath } } catch (Exception ex) { getLogger().severe("XmlHttpProxy Error loading service: " + ex); System.exit(1); } } else if (urlString == null) { System.out.println(USAGE); System.exit(1); } // The parameters are feed to the XSL Stylsheet during transformation. // These parameters can provided data or conditional information. Map paramsMap = new HashMap(); if (format != null) { paramsMap.put("format", format); } if (callback != null) { paramsMap.put("callback", callback); } if (xslURLString != null) { URL xslURL = new URL(xslURLString); if (xslURL != null) { xslInputStream = xslURL.openStream(); } else { getLogger().severe("Error: Unable to locate XSL at URL " + xslURLString); } } xhp.doGet(urlString, System.out, xslInputStream, paramsMap); }
From source file:za.co.taung.httpdotserver.main.HttpDotServer.java
public static void main(String[] args) throws Exception { LOG.info("Initialise server"); // The parameter is the Port to listen on. Default is 8080. int port = 8080; if (args.length >= 1) { port = Integer.parseInt(args[0]); }//w ww . j av a 2s . c o m // Set up the HTTP protocol processor. HttpProcessor httpProcessor = HttpProcessorBuilder.create().add(new ResponseDate()) .add(new ResponseServer("HttpDotServer/1.1")).add(new ResponseContent()) .add(new ResponseConnControl()).build(); // Set up request handler. This is the method that generates SVG. UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper(); reqistry.register("*", new Dot2SVGHandler()); // Set up the HTTP service. HttpService httpService = new HttpService(httpProcessor, reqistry); // Set up SSL if listening on 8443 for https. SSLServerSocketFactory serverSocketFactory = null; if (port == 8443) { // Get the location of the keystore secrets. ClassLoader cl = HttpDotServer.class.getClassLoader(); URL url = cl.getResource("my.keystore"); if (url == null) { LOG.error("Keystore not found"); System.exit(1); } // Load the secret into a keystore and manage the key material. KeyStore keystore = KeyStore.getInstance("jks"); keystore.load(url.openStream(), "secret".toCharArray()); KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmfactory.init(keystore, "secret".toCharArray()); KeyManager[] keymanagers = kmfactory.getKeyManagers(); // Prepare the socket factory for use by the RequestListenerThread. SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(keymanagers, null, null); serverSocketFactory = sslcontext.getServerSocketFactory(); } LOG.debug("Start the RequestListenerThread"); Thread thread = new RequestListenerThread(port, httpService, serverSocketFactory); thread.setDaemon(false); thread.start(); }
From source file:com.occamlab.te.parsers.ImageParser.java
public static void main(String[] args) throws Exception { if (args.length < 2) { System.err.println("Parameters: xml_url image_url"); return;/* w ww . j av a 2 s . c o m*/ } java.net.URL xml_url; try { xml_url = new java.net.URL(args[0]); } catch (Exception e) { jlogger.log(Level.INFO, "Error building xmlurl, will prefix file://", e); xml_url = new java.net.URL("file://" + args[0]); } java.net.URL image_url; try { image_url = new java.net.URL(args[1]); } catch (Exception e) { jlogger.log(Level.INFO, "Error building xmlurl, will prefix file://", e); image_url = new java.net.URL("file://" + args[1]); } DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(xml_url.openStream()); // Element instruction = (Element) // doc.getElementsByTagNameNS("http://www.occamlab.com/te/parsers", // "ImageParser").item(0); Element instruction = (Element) doc.getDocumentElement(); PrintWriter logger = new PrintWriter(System.out); InputStream image_is = image_url.openConnection().getInputStream(); Document result = parse(image_is, instruction, logger); logger.flush(); if (result != null) { TransformerFactory tf = TransformerFactory.newInstance(); try { tf.setAttribute("http://saxon.sf.net/feature/strip-whitespace", "all"); } catch (IllegalArgumentException e) { jlogger.log(Level.INFO, "setAttribute(\"http://saxon.sf.net/feature/strip-whitespace\", \"all\");", e); } Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(result), new StreamResult(System.out)); } System.exit(0); }
From source file:edu.toronto.cs.xcurator.cli.CLIRunner.java
public static void main(String[] args) { Options options = setupOptions();//w w w. ja v a 2 s. co m CommandLineParser parser = new BasicParser(); try { CommandLine line = parser.parse(options, args); if (line.hasOption('t')) { fileType = line.getOptionValue('t'); } else { fileType = XML; } if (line.hasOption('o')) { tdbDirectory = line.getOptionValue('o'); File d = new File(tdbDirectory); if (!d.exists() || !d.isDirectory()) { throw new Exception("TDB directory does not exist, please create."); } } if (line.hasOption('h')) { domain = line.getOptionValue('h'); try { URL url = new URL(domain); } catch (MalformedURLException ex) { throw new Exception("The domain name is ill-formed"); } } else { printHelpAndExit(options); } if (line.hasOption('m')) { serializeMapping = true; mappingFilename = line.getOptionValue('m'); } if (line.hasOption('d')) { dirLocation = line.getOptionValue('d'); inputStreams = new ArrayList<>(); final List<String> files = Util.getFiles(dirLocation); for (String inputfile : files) { File f = new File(inputfile); if (f.isFile() && f.exists()) { System.out.println("Adding document to mapping discoverer: " + inputfile); inputStreams.add(new FileInputStream(f)); } // If it is a URL download link for the document from SEC else if (inputfile.startsWith("http") && inputfile.contains("://")) { // Download System.out.println("Adding remote document to mapping discoverer: " + inputfile); try { URL url = new URL(inputfile); InputStream remoteDocumentStream = url.openStream(); inputStreams.add(remoteDocumentStream); } catch (MalformedURLException ex) { throw new Exception("The document URL is ill-formed: " + inputfile); } catch (IOException ex) { throw new Exception("Error in downloading remote document: " + inputfile); } } else { throw new Exception("Cannot open XBRL document: " + f.getName()); } } } if (line.hasOption('f')) { fileLocation = line.getOptionValue('f'); inputStreams = new ArrayList<>(); File f = new File(fileLocation); if (f.isFile() && f.exists()) { System.out.println("Adding document to mapping discoverer: " + fileLocation); inputStreams.add(new FileInputStream(f)); } // If it is a URL download link for the document from SEC else if (fileLocation.startsWith("http") && fileLocation.contains("://")) { // Download System.out.println("Adding remote document to mapping discoverer: " + fileLocation); try { URL url = new URL(fileLocation); InputStream remoteDocumentStream = url.openStream(); inputStreams.add(remoteDocumentStream); } catch (MalformedURLException ex) { throw new Exception("The document URL is ill-formed: " + fileLocation); } catch (IOException ex) { throw new Exception("Error in downloading remote document: " + fileLocation); } } else { throw new Exception("Cannot open XBRL document: " + f.getName()); } } setupDocumentBuilder(); RdfFactory rdfFactory = new RdfFactory(new RunConfig(domain)); List<Document> documents = new ArrayList<>(); for (InputStream inputStream : inputStreams) { Document dataDocument = null; if (fileType.equals(JSON)) { String json = IOUtils.toString(inputStream); final String xml = Util.json2xml(json); final InputStream xmlInputStream = IOUtils.toInputStream(xml); dataDocument = createDocument(xmlInputStream); } else { dataDocument = createDocument(inputStream); } documents.add(dataDocument); } if (serializeMapping) { System.out.println("Mapping file will be saved to: " + new File(mappingFilename).getAbsolutePath()); rdfFactory.createRdfs(documents, tdbDirectory, mappingFilename); } else { rdfFactory.createRdfs(documents, tdbDirectory); } } catch (Exception ex) { ex.printStackTrace(); System.err.println("Unexpected exception: " + ex.getMessage()); System.exit(1); } }
From source file:edu.stanford.muse.launcher.Main.java
public static void main(String args[]) throws Exception { // set javawebstart.version to a dummy value if not already set (might happen when running with java -jar from cmd line) // exit.jsp doesn't allow us to showdown unless this prop is set if (System.getProperty("javawebstart.version") == null) System.setProperty("javawebstart.version", "UNKNOWN"); final int TIMEOUT_SECS = 60; if (args.length > 0) { out.print(args.length + " argument(s): "); for (int i = 0; i < args.length; i++) out.print(args[i] + " "); out.println();/* ww w.ja v a2s .c om*/ } Options options = getOpt(); CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Muse batch mode", options); return; } boolean debug = false; if (cmd.hasOption("debug")) { URL url = ClassLoader.getSystemResource("log4j.properties.debug"); out.println("Loading logging configuration from url: " + url); PropertyConfigurator.configure(url); debug = true; } else if (cmd.hasOption("debug-address-book")) { URL url = ClassLoader.getSystemResource("log4j.properties.debug.ab"); out.println("Loading logging configuration from url: " + url); PropertyConfigurator.configure(url); debug = false; } else if (cmd.hasOption("debug-groups")) { URL url = ClassLoader.getSystemResource("log4j.properties.debug.groups"); out.println("Loading logging configuration from url: " + url); PropertyConfigurator.configure(url); debug = false; } if (cmd.hasOption("no-browser-open")) browserOpen = false; if (cmd.hasOption("port")) { String portStr = cmd.getOptionValue('p'); try { PORT = Integer.parseInt(portStr); String mesg = " Running on port: " + PORT; out.println(mesg); } catch (NumberFormatException nfe) { out.println("invalid port number " + portStr); } } if (cmd.hasOption("start-page")) startPage = cmd.getOptionValue("start-page"); if (cmd.hasOption("base-dir")) baseDir = cmd.getOptionValue("base-dir"); if (cmd.hasOption("search-mode")) searchMode = true; if (cmd.hasOption("amuse-mode")) amuseMode = true; System.setSecurityManager(null); // this is important WebAppContext webapp0 = null; // deployWarAt("root.war", "/"); // for redirecting String path = "/muse"; WebAppContext webapp1 = deployWarAt("muse.war", path); if (webapp1 == null) { System.err.println("Aborting... no webapp"); return; } // if in any debug mode, turn blurring off if (debug) webapp1.setAttribute("noblur", true); // we set this and its read by JSPHelper within the webapp System.setProperty("muse.container", "jetty"); // need to copy crossdomain.xml file for String tmp = System.getProperty("java.io.tmpdir"); final URL url = Main.class.getClassLoader().getResource("crossdomain.xml"); try { InputStream is = url.openStream(); String file = tmp + File.separatorChar + "crossdomain.xml"; copy_stream_to_file(is, file); } catch (Exception e) { System.err.println("Aborting..." + e); return; } server = new Server(PORT); ResourceHandler resource_handler = new ResourceHandler(); // resource_handler.setWelcomeFiles(new String[]{ "index.html" }); resource_handler.setResourceBase(tmp); // set the header buffer size in the connectors, default is a ridiculous 4K, which causes failures any time there is // is a large request, such as selecting a few hundred folders. (even for posts!) // usually there is only one SocketConnector, so we just put the setHeaderBufferSize in a loop. Connector conns[] = server.getConnectors(); for (Connector conn : conns) { int NEW_BUFSIZE = 1000000; // out.println ("Connector " + conn + " buffer size is " + conn.getHeaderBufferSize() + " setting to " + NEW_BUFSIZE); conn.setHeaderBufferSize(NEW_BUFSIZE); } BASE_URL = "http://localhost:" + PORT + path; String MUSE_CHECK_URL = BASE_URL + "/js/muse.js"; // for quick check of existing muse or successful start up. BASE_URL may take some time to run and may not always be available now that we set dirAllowed to false and public mode does not serve /muse. String debugFile = tmp + File.separatorChar + "debug.txt"; HandlerList hl = new HandlerList(); if (webapp0 != null) hl.setHandlers(new Handler[] { webapp1, webapp0, resource_handler }); else hl.setHandlers(new Handler[] { webapp1, resource_handler }); out.println("Starting up Muse on the local computer at " + BASE_URL + ", " + formatDateLong(new GregorianCalendar())); out.println("***For troubleshooting information, see this file: " + debugFile + "***\n"); out.println("Current directory = " + System.getProperty("user.dir") + ", home directory = " + System.getProperty("user.home")); out.println("Memory status at the beginning: " + getMemoryStats()); if (Runtime.getRuntime().maxMemory() / MB < 512) aggressiveWarn( "You are probably running Muse without enough memory. \nIf you launched Muse from the command line, you can increase memory with an option like java -Xmx1g", 2000); server.setHandler(hl); // handle frequent error of user trying to launch another server when its already on // server.start() usually takes a few seconds to return // after that it takes a few seconds for the webapp to deploy // ignore any exceptions along the way and assume not if we can't prove it is alive boolean urlAlive = false; try { urlAlive = isURLAlive(MUSE_CHECK_URL); } catch (Exception e) { out.println("Exception: e"); e.printStackTrace(out); } boolean disableStart = false; if (urlAlive) { out.println("Oh! Muse is already running at the URL: " + BASE_URL + ", will have to kill it!"); killRunningServer(BASE_URL); Thread.sleep(3000); try { urlAlive = isURLAlive(MUSE_CHECK_URL); } catch (Exception e) { out.println("Exception: e"); e.printStackTrace(out); } if (!urlAlive) out.println("Good. Kill succeeded, will restart"); else { String message = "Previously running Muse still alive despite attempt to kill it, disabling fresh restart!\n"; message += "If you just want to use the previous instance of Muse, please go to http://localhost:9099/muse\n"; message += "\nTo kill this instance, please go to your computer's task manager and kill running java or javaw processes.\nThen try launching Muse again.\n"; aggressiveWarn(message, 2000); return; } } // else // out.println ("Muse not already alive at URL: ..." + URL); if (!disableStart) { out.println("Starting Muse at URL: ..." + BASE_URL); try { server.start(); } catch (BindException be) { out.println("port busy, but webapp not alive: " + BASE_URL + "\n" + be); throw new RuntimeException("Error: Port in use (Please kill Muse if its already running!)\n" + be); } } // webapp1.start(); -- not needed PrintStream debugOut1 = System.err; try { File f = new File(debugFile); if (f.exists()) f.delete(); // particular problem on windows :-( debugOut1 = new PrintStream(new FileOutputStream(debugFile), false, "UTF-8"); } catch (IOException ioe) { System.err.println("Warning: failed to delete debug file " + debugFile + " : " + ioe); } final PrintStream debugOut = debugOut1; Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { try { server.stop(); server.destroy(); debugOut.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } })); // InfoFrame frame = new InfoFrame(); // frame.doShow(); boolean success = waitTillPageAlive(MUSE_CHECK_URL, TIMEOUT_SECS); // frame.updateText ("Opening a browser window"); if (success) { // best effort to start shutdown thread // out.println ("Starting Muse shutdown listener at port " + JettyShutdownThread.SHUTDOWN_PORT); try { int shutdownPort = PORT + 1; // shut down port is arbitrarily set to port + 1. it is ASSUMED to be free. new JettyShutdownThread(server, shutdownPort).start(); out.println("Listening for Muse shutdown message on port " + shutdownPort); } catch (Exception e) { out.println( "Unable to start shutdown listener, you will have to stop the server manually using Cmd-Q on Mac OS or kill javaw processes on Windows"); } try { setupSystemTrayIcon(); } catch (Exception e) { out.println("Unable to setup system tray icon: " + e); e.printStackTrace(out); } // open browser window if (browserOpen) { preferredBrowser = null; // launch a browser here try { String link; if (System.getProperty("muse.mode.public") != null) link = "http://localhost:" + PORT + "/muse/archives/"; else link = "http://localhost:" + PORT + "/muse/index.jsp"; if (searchMode) { String u = "http://localhost:" + PORT + "/muse/search"; out.println("Launching URL in browser: " + u); link += "?mode=search"; } else if (amuseMode) { String u = "http://localhost:" + PORT + "/muse/amuse.jsp"; out.println("Launching URL in browser: " + u); link = u; } else if (startPage != null) { // startPage has to be absolute link = "http://localhost:" + PORT + "/muse/" + startPage; } if (baseDir != null) link = link + "?cacheDir=" + baseDir; // typically this is used when starting from command line. note: still using name, cacheDir out.println("Launching URL in browser: " + link); launchBrowser(link); } catch (Exception e) { out.println( "Warning: Unable to launch browser due to exception (use the -n option to prevent Muse from trying to launch a browser):"); e.printStackTrace(out); } } if (!cmd.hasOption("no-shutdown")) { // arrange to kill Muse after a period of time, we don't want the server to run forever // i clearly have too much time on my hands right now... long secs = KILL_AFTER_MILLIS / 1000; long hh = secs / 3600; long mm = (secs % 3600) / 60; long ss = secs % (60); out.print("Muse will shut down automatically after "); if (hh != 0) out.print(hh + " hours "); if (mm != 0 || (hh != 0 && ss != 0)) out.print(mm + " minutes"); if (ss != 0) out.print(ss + " seconds"); out.println(); Timer timer = new Timer(); TimerTask tt = new ShutdownTimerTask(); timer.schedule(tt, KILL_AFTER_MILLIS); } } else { out.println("\n\n\nSORRY!!! UNABLE TO DEPLOY WEBAPP, EXITING\n\n\n"); // frame.updateText("Sorry, looks like we are having trouble starting the jetty server\n"); } savedSystemOut = out; savedSystemErr = System.err; System.setOut(debugOut); System.setErr(debugOut); }
From source file:Main.java
public static String getURLContent(String urlStr) throws Exception { BufferedReader br = null;/*from w ww . ja va2 s .co m*/ URL url = new URL(urlStr); InputStream ins = url.openStream(); br = new BufferedReader(new InputStreamReader(ins)); StringBuilder sb = new StringBuilder(); String msg = null; while ((msg = br.readLine()) != null) { sb.append(msg); sb.append("\n"); // Append a new line } br.close(); return sb.toString(); }
From source file:Main.java
public static void saveImage(String imageUrl, String destinationFile) throws Exception { URL url = new URL(imageUrl); InputStream is = url.openStream(); OutputStream os = new FileOutputStream(destinationFile); byte[] b = new byte[2048]; int length;/* ww w .j av a 2s. c o m*/ while ((length = is.read(b)) != -1) { os.write(b, 0, length); } is.close(); os.close(); }
From source file:Main.java
public static byte[] downloadImageFromURL(String strUrl) throws Exception { InputStream in;/* w ww .j a va 2s .com*/ ByteArrayOutputStream out = new ByteArrayOutputStream(); URL url = new URL(strUrl); in = new BufferedInputStream(url.openStream()); byte[] buf = new byte[2048]; int n = 0; while (-1 != (n = in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray(); FileOutputStream fos = new FileOutputStream("/Users/image.jpg"); fos.write(response); fos.close(); return response; }
From source file:Main.java
public static Document getDoc(URL url) throws Exception { return getDoc(url.openStream()); }
From source file:Main.java
public static InputStream inputStream(URL file) { try {//from ww w . j ava 2s.c om return file.openStream(); } catch (final IOException e) { throw new RuntimeException(e); } }