List of usage examples for java.net MalformedURLException toString
public String toString()
From source file:net.zifnab.google.google.java
public static String[] search(String terms) { String key = "ABQIAAAA6KnsrpTIyjEMJ1EqHjKG_xRBaorPdDj7IJd2xMtxtE9DwSoKhRQgazFCenlI-wnGV1574jW06163iw"; String ip = "173.255.208.207"; System.out.println("Google-ing: " + terms); terms = terms.replace(' ', '+'); try {/* www. j a va2 s .c om*/ //Opens URL URL googlesearch = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&" + "q=" + terms + "&key=" + key + "&userip=" + ip); URLConnection connection = googlesearch.openConnection(); connection.addRequestProperty("Referer", "zifnab06.net"); //Read in JSON crap from URL. String line; StringBuilder results = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = reader.readLine()) != null) { results.append(line + "\n"); } //JSON Crap. Google needs to use XML.... JSONObject json = new JSONObject(results.toString()); JSONArray ja = json.getJSONObject("responseData").getJSONArray("results"); JSONObject j = ja.getJSONObject(0); //Returns Title: URL String[] ret = { j.toString(), StringEscapeUtils.unescapeHtml4(j.getString("titleNoFormatting")) + ": " + URLDecoder.decode(j.getString("url"), "UTF-8") }; return ret; } catch (MalformedURLException e) { //Shouldn't EVER hit this. System.out.println(e); String[] ret = { "error", e.toString() }; return (ret); } catch (IOException e) { //Shouldn't EVER hit this. System.out.println(e); String[] ret = { "error", e.toString() }; return (ret); } catch (Exception e) { System.out.println(e); String[] ret = { "error", "No results found!" }; return ret; } }
From source file:com.cloudera.sqoop.util.JdbcUrl.java
/** * @return the hostname from the connect string, or null if we can't. *//*w w w .j a v a 2s . c o m*/ public static String getHostName(String connectString) { try { String sanitizedString = null; int schemeEndOffset = connectString.indexOf("://"); if (-1 == schemeEndOffset) { // Couldn't find one? ok, then there's no problem, it should work as a // URL. sanitizedString = connectString; } else { sanitizedString = "http" + connectString.substring(schemeEndOffset); } URL connectUrl = new URL(sanitizedString); return connectUrl.getHost(); } catch (MalformedURLException mue) { LOG.error("Malformed connect string URL: " + connectString + "; reason is " + mue.toString()); return null; } }
From source file:com.cloudera.sqoop.util.JdbcUrl.java
/** * @return the port from the connect string, or -1 if we can't. *//*from www . ja v a2 s.c o m*/ public static int getPort(String connectString) { try { String sanitizedString = null; int schemeEndOffset = connectString.indexOf("://"); if (-1 == schemeEndOffset) { // Couldn't find one? ok, then there's no problem, it should work as a // URL. sanitizedString = connectString; } else { sanitizedString = "http" + connectString.substring(schemeEndOffset); } URL connectUrl = new URL(sanitizedString); return connectUrl.getPort(); } catch (MalformedURLException mue) { LOG.error("Malformed connect string URL: " + connectString + "; reason is " + mue.toString()); return -1; } }
From source file:com.ibm.wala.cast.ipa.callgraph.CAstCallGraphUtil.java
public static SourceFileModule[] handleFileNames(String[] fileNameArgs) { SourceFileModule[] fileNames = new SourceFileModule[fileNameArgs.length]; for (int i = 0; i < fileNameArgs.length; i++) { if (new File(fileNameArgs[i]).exists()) { try { fileNames[i] = CAstCallGraphUtil.makeSourceModule(new File(fileNameArgs[i]).toURI().toURL(), fileNameArgs[i]); } catch (MalformedURLException e) { Assertions.UNREACHABLE(e.toString()); }/*from w ww.j a v a 2 s. c om*/ } else { URL url = CAstCallGraphUtil.class.getClassLoader().getResource(fileNameArgs[i]); fileNames[i] = CAstCallGraphUtil.makeSourceModule(url, fileNameArgs[i]); } } return fileNames; }
From source file:com.cloudera.sqoop.util.JdbcUrl.java
/** * @return the database name from the connect string, which is typically the * 'path' component, or null if we can't. *//*from w w w. ja v a 2 s . c o m*/ public static String getDatabaseName(String connectString) { try { String sanitizedString = null; int schemeEndOffset = connectString.indexOf("://"); if (-1 == schemeEndOffset) { // couldn't find one? try our best here. sanitizedString = "http://" + connectString; LOG.warn("Could not find database access scheme in connect string " + connectString); } else { sanitizedString = "http" + connectString.substring(schemeEndOffset); } URL connectUrl = new URL(sanitizedString); String databaseName = connectUrl.getPath(); if (null == databaseName) { return null; } // This is taken from a 'path' part of a URL, which may have leading '/' // characters; trim them off. while (databaseName.startsWith("/")) { databaseName = databaseName.substring(1); } return databaseName; } catch (MalformedURLException mue) { LOG.error("Malformed connect string URL: " + connectString + "; reason is " + mue.toString()); return null; } }
From source file:io.github.retz.scheduler.Launcher.java
private static void maybeRequeueRunningJobs(String master, String frameworkId, List<Job> running) { LOG.info("{} jobs found in DB 'STARTING' or 'STARTED' state. Requeuing...", running.size()); int offset = 0; int limit = 128; Map<String, Job> runningMap = running.stream().collect(Collectors.toMap(job -> job.taskId(), job -> job)); List<Job> recoveredJobs = new LinkedList<>(); while (true) { try {//from w w w. java 2 s . c o m List<Map<String, Object>> tasks = MesosHTTPFetcher.fetchTasks(master, frameworkId, offset, limit); if (tasks.isEmpty()) { break; } for (Map<String, Object> task : tasks) { String state = (String) task.get("state"); // Get TaskId String taskId = (String) task.get("id"); if (runningMap.containsKey(taskId)) { Job job = runningMap.remove(taskId); recoveredJobs.add(JobQueue.updateJobStatus(job, state)); } else { LOG.warn("Unknown job!"); } } offset = offset + tasks.size(); } catch (MalformedURLException e) { LOG.error(e.toString()); throw new RuntimeException(e.toString()); } } Database.getInstance().updateJobs(recoveredJobs); LOG.info("{} jobs rescheduled, {} jobs didn't need change.", recoveredJobs.size(), runningMap.size()); }
From source file:io.github.retz.web.JobRequestRouter.java
private static String fetchHTTP(String addr, int retry) throws MalformedURLException, IOException { LOG.debug("Fetching {}", addr); HttpURLConnection conn = null; try {//www.j a v a 2s . c o m conn = (HttpURLConnection) new URL(addr).openConnection(); conn.setRequestMethod("GET"); conn.setDoOutput(true); LOG.debug(conn.getResponseMessage()); } catch (MalformedURLException e) { LOG.error(e.toString()); throw e; } catch (IOException e) { LOG.error(e.toString()); throw e; } try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8))) { StringBuilder builder = new StringBuilder(); String line; do { line = reader.readLine(); builder.append(line); } while (line != null); LOG.debug("Fetched {} bytes from {}", builder.toString().length(), addr); return builder.toString(); } catch (FileNotFoundException e) { throw e; } catch (IOException e) { // Somehow this happens even HTTP was correct LOG.debug("Cannot fetch file {}: {}", addr, e.toString()); // Just retry until your stack get stuck; thanks to SO:33340848 // and to that crappy HttpURLConnection if (retry < 0) { LOG.error("Retry failed. Last error was: {}", e.toString()); throw e; } return fetchHTTP(addr, retry - 1); } finally { conn.disconnect(); } }
From source file:com.photon.phresco.nativeapp.eshop.json.JSONHelper.java
/** * Get the JSON object from the specified URL * * @param url/*w w w. j a va 2s .c o m*/ * @return JSONObject * @throws IOException * @throws ioException * @throws ClientProtocolException */ public static JSONObject getJSONObjectFromURL(String url) throws IOException { InputStream is = null; String result = null; JSONObject jObject = null; try { is = HttpRequest.get(url); } catch (MalformedURLException ex) { PhrescoLogger.info(TAG + "getJSONObjectFromURL - MalformedURLException: " + ex.toString()); PhrescoLogger.warning(ex); } catch (UnsupportedEncodingException ex) { PhrescoLogger.info(TAG + "getJSONObjectFromURL - UnsupportedEncodingException: " + ex.toString()); PhrescoLogger.warning(ex); } catch (IllegalStateException ex) { PhrescoLogger.info(TAG + "getJSONObjectFromURL - IllegalStateException: " + ex.toString()); PhrescoLogger.warning(ex); } // Convert the input stream in to string if (is != null) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), Integer.parseInt("8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); reader.close(); result = sb.toString(); } catch (Exception ex) { PhrescoLogger.info(TAG + ex.toString()); PhrescoLogger.warning(ex); } // Parse the string to a JSON object try { jObject = new JSONObject(result); } catch (Exception ex) { PhrescoLogger.info(TAG + "getJSONObjectFromURL() - JSONException: " + ex.toString()); PhrescoLogger.warning(ex); } } return jObject; }
From source file:com.ts.db.connector.ConnectorDriverManager.java
static Driver getDriver(String url, String driverClassName, String classpath) throws SQLException { assert !StringUtils.isBlank(url); final boolean hasClasspath = !StringUtils.isBlank(classpath); if (!hasClasspath) { for (Driver driver : new ArrayList<Driver>(drivers)) { if (driver.acceptsURL(url)) { return driver; }//from www. j a v a2s . c o m } } List<File> jars = new ArrayList<File>(); ClassLoader cl; if (hasClasspath) { List<URL> urls = new ArrayList<URL>(); for (String path : classpath.split(pathSeparator)) { final File file = new File(path); if (isJarFile(file)) { jars.add(file); } try { urls.add(file.toURI().toURL()); } catch (MalformedURLException ex) { log.warn(ex.toString()); } } cl = new URLClassLoader(urls.toArray(new URL[urls.size()])); } else { jars.addAll(getJarFiles(".")); jars.addAll(driverFiles); List<URL> urls = new ArrayList<URL>(); for (File file : jars) { try { urls.add(file.toURI().toURL()); } catch (MalformedURLException ex) { log.warn(ex.toString()); } } cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), ClassLoader.getSystemClassLoader()); } driverFiles.addAll(jars); final boolean hasDriverClassName = !StringUtils.isBlank(driverClassName); if (hasDriverClassName) { try { Driver driver = DynamicLoader.newInstance(driverClassName, cl); assert driver != null; return driver; } catch (DynamicLoadingException ex) { Throwable cause = (ex.getCause() != ex) ? ex.getCause() : ex; SQLException exception = new SQLException(cause.toString()); exception.initCause(cause); throw exception; } } final String jdbcDrivers = System.getProperty("jdbc.drivers"); if (!StringUtils.isBlank(jdbcDrivers)) { for (String jdbcDriver : jdbcDrivers.split(":")) { try { Driver driver = DynamicLoader.newInstance(jdbcDriver, cl); if (driver != null) { if (!hasClasspath) { drivers.add(driver); } return driver; } } catch (DynamicLoadingException ex) { log.warn(ex.toString()); } } } for (File jar : jars) { try { Driver driver = getDriver(jar, url, cl); if (driver != null) { if (!hasClasspath) { drivers.add(driver); } return driver; } } catch (IOException ex) { log.warn(ex.toString()); } } for (String path : System.getProperty("java.class.path", "").split(pathSeparator)) { if (isJarFile(path)) { Driver driver; try { driver = getDriver(new File(path), url, cl); if (driver != null) { drivers.add(driver); return driver; } } catch (IOException ex) { log.warn(ex.toString()); } } } throw new SQLException("driver not found"); }
From source file:eionet.gdem.conversion.datadict.DataDictUtil.java
/** * Returns the DD container schema URL. It holds the elements definitions * * @param schema_url// ww w . j a v a 2s . com * @return * @throws GDEMException */ public static String getContainerSchemaUrl(String schema_url) throws GDEMException { try { URL SchemaURL = new URL(schema_url); String containerSchemaUrl = schema_url.replace(DataDictUtil.SCHEMA_SERVLET, CONTAINER_SCHEMA_SERVLET); URL InstanceURL = new URL(containerSchemaUrl); return containerSchemaUrl; } catch (MalformedURLException e) { throw new GDEMException("Error getting Container Schema URL: " + e.toString() + " - " + schema_url); } catch (Exception e) { throw new GDEMException("Error getting Container Schema URL: " + e.toString() + " - " + schema_url); } }