List of usage examples for java.net MalformedURLException toString
public String toString()
From source file:ConnectionProtccol.HttpClient.java
public String OpenHTTPConnection(String URLToConnect, String fileToWrite) { URL url;/* w ww .java2 s . c o m*/ HttpURLConnection con = null; try { loggerObj.log(Level.SEVERE, "Going to connect to the URL" + URLToConnect); url = new URL(URLToConnect); con = (HttpURLConnection) url.openConnection(); } catch (MalformedURLException ex) { loggerObj.log(Level.SEVERE, "Malformed URL Exception while trying to establish connection with MEDC portal" + ex.toString()); System.out.println("Malformed URL Exception while trying to establish connection with MEDC portal" + ex.toString()); return null; } catch (IOException ex) { loggerObj.log(Level.SEVERE, "Exception while trying to establish connection with MEDC portal" + ex.toString()); System.out.println("Exception while trying to establish connection with MEDC portal" + ex.toString()); return null; } String result = ConnectionHandler(con, fileToWrite); System.out.println("result" + result); //loggerObj.log(Level.INFO, "The Result obtained from connecting to the URL "+ URLToConnect +"is: " + result); return result; }
From source file:ConnectionProtccol.HttpsClient.java
public String OpenHTTPSConnection(String URLToConnect) { URL url;//from ww w.ja v a 2 s .co m HttpsURLConnection con = null; try { loggerObj.log(Level.SEVERE, "Going to connect to the URL" + URLToConnect); url = new URL(URLToConnect); con = (HttpsURLConnection) url.openConnection(); } catch (MalformedURLException ex) { loggerObj.log(Level.SEVERE, "Malformed URL Exception while trying to establish connection with MEDC portal" + ex.toString()); System.out.println("Malformed URL Exception while trying to establish connection with MEDC portal" + ex.toString()); return null; } catch (IOException ex) { loggerObj.log(Level.SEVERE, "Exception while trying to establish connection with MEDC portal" + ex.toString()); System.out.println("Exception while trying to establish connection with MEDC portal" + ex.toString()); return null; } String result = ConnectionHandler(con); //loggerObj.log(Level.INFO, "The Result obtained from connecting to the URL "+ URLToConnect +"is: " + result); return result; }
From source file:eionet.gdem.utils.Utils.java
/** * Reads the XML declaration from instance file. *//*from ww w .j a va 2s .c o m*/ public static String getEncodingFromStream(String str_url) { BufferedReader br = null; try { URL url = new URL(str_url); // ins = new DataInputStream(url.openStream()); br = new BufferedReader(new InputStreamReader(url.openStream())); String xml_decl = br.readLine(); if (xml_decl == null) { return null; } if (!xml_decl.startsWith("<?xml version=") && !xml_decl.endsWith("?>")) { return null; } int idx = xml_decl.indexOf("encoding="); if (idx == -1) { return null; } String start = xml_decl.substring(idx + 10); int end_idx = start.indexOf("\""); if (end_idx == -1) { return null; } String enc = start.substring(0, end_idx); return enc; } catch (MalformedURLException e) { LOGGER.debug("It is not url: " + str_url + "; " + e.toString()); return null; } catch (IOException e) { LOGGER.debug("could not read encoding from url: " + str_url + "; " + e.toString()); return null; } catch (Exception e) { return null; // couldn't read encoding } finally { try { if (br != null) { br.close(); } } catch (IOException e) { LOGGER.error("Exception: ", e); } } }
From source file:org.apache.nutch.plugin.PluginDescriptor.java
/** * Returns a cached classloader for a plugin. Until classloader creation all * needed libraries are collected. A classloader use as first the plugins own * libraries and add then all exported libraries of dependend plugins. * // www . j ava2 s . co m * @return PluginClassLoader the classloader for the plugin */ public PluginClassLoader getClassLoader() { if (fClassLoader != null) return fClassLoader; ArrayList<URL> arrayList = new ArrayList<URL>(); arrayList.addAll(fExportedLibs); arrayList.addAll(fNotExportedLibs); arrayList.addAll(getDependencyLibs()); File file = new File(getPluginPath()); try { for (File file2 : file.listFiles()) { if (file2.getAbsolutePath().endsWith("properties")) arrayList.add(file2.getParentFile().toURL()); } } catch (MalformedURLException e) { LOG.debug(getPluginId() + " " + e.toString()); } URL[] urls = arrayList.toArray(new URL[arrayList.size()]); fClassLoader = new PluginClassLoader(urls, PluginDescriptor.class.getClassLoader()); return fClassLoader; }
From source file:edu.indiana.lib.twinpeaks.search.SearchResultBase.java
/** * Form a full URL (protocol, server, arguments) from a base URL and * provided parameters//from www .j ava2s. c om * @param baseUrl The base (or template) URL * @param urlFragment The (possibly) relative URL to be combined with the base * @return A full URL (as a String) */ public String getFullUrl(String baseUrl, String urlFragment) { String thisUrl = baseUrl; if (thisUrl == null) { thisUrl = _baseUrl; } if (thisUrl != null) { try { URL base = new URL(thisUrl); return new URL(base, urlFragment).toString(); } catch (MalformedURLException exception) { throw new SearchException(exception.toString()); } } return urlFragment; }
From source file:org.openhab.binding.ebus.EBusBinding.java
@Override public void updated(Dictionary<String, ?> properties) throws ConfigurationException { logger.info("Update eBus Binding configuration ..."); try {/*w ww . j a v a2 s . com*/ // stop last thread if active if (connector != null && connector.isAlive()) { connector.interrupt(); } // check to ensure that it is available checkConfigurationProvider(); // clear current configuration configurationProvider.clear(); // load parser from default url parser = new EBusTelegramParser(configurationProvider); URL configurationUrl = null; String parsers = (String) properties.get("parsers"); if (StringUtils.isEmpty(parsers)) { // set to current stable configurations as default parsers = "common,wolf"; } for (String elem : parsers.split(",")) { configurationUrl = null; // check for keyword custom to load custom configuration if (elem.trim().equals("custom")) { String parserUrl = (String) properties.get("parserUrl"); if (parserUrl != null) { logger.debug("Load custom eBus Parser with url {}", parserUrl); configurationUrl = new URL(parserUrl); } } else { logger.debug("Load eBus Parser Configuration \"{}\" ...", elem.trim()); configurationUrl = this.getClass() .getResource("/META-INF/" + elem.trim() + "-configuration.json"); } if (configurationUrl != null) { configurationProvider.loadConfigurationFile(configurationUrl); } } // check minimal config if (properties.get("serialPort") != null && properties.get("hostname") != null) { throw new ConfigurationException("hostname", "Set property serialPort or hostname, not both!"); } if (StringUtils.isNotEmpty((String) properties.get("serialPort"))) { // use the serial connector connector = new EBusSerialConnector((String) properties.get("serialPort")); } else if (StringUtils.isNotEmpty((String) properties.get("hostname"))) { // use the tcp-ip connector connector = new EBusTCPConnector((String) properties.get("hostname"), Integer.parseInt((String) properties.get("port"))); } // Set eBus sender id or default 0xFF if (StringUtils.isNotEmpty((String) properties.get("senderId"))) { connector.setSenderId(EBusUtils.toByte((String) properties.get("senderId"))); } // add event listener connector.addEBusEventListener(this); // start thread connector.start(); // set the new connector commandProcessor.setConnector(connector); commandProcessor.setConfigurationProvider(configurationProvider); } catch (MalformedURLException e) { logger.error(e.toString(), e); } catch (IOException e) { throw new ConfigurationException("general", e.toString(), e); } }
From source file:org.apache.axis2.jaxws.description.impl.DescriptionUtils.java
/** * This is a helper method that will open a stream to an @HandlerChain configuration file. * * @param configFile - The path to the file * @param className - The class in which the annotation was declared. This is used in case the * file path is relative. * @param classLoader - ClassLoader used to load relative file paths. * @return//from w ww. j a va 2s . c om */ public static InputStream openHandlerConfigStream(String configFile, String className, ClassLoader classLoader) { InputStream configStream = null; URL configURL; if (log.isDebugEnabled()) { log.debug("Attempting to load @HandlerChain configuration file: " + configFile + " relative to class: " + className); } // Attempt 1: // Try absolute loading try { if (log.isDebugEnabled()) { log.debug("Attempt 1: Try absolute load of (" + configFile + ")"); } configURL = new URL(configFile); if (configURL != null) { if (log.isDebugEnabled()) { log.debug("Found absolute @HandlerChain configuration file: " + configFile); } configStream = configURL.openStream(); } } catch (MalformedURLException e) { // try another method to obtain a stream to the configuration file if (log.isDebugEnabled()) { log.debug("Attempt 1 Failed with exception. Try Attempt 2. " + "The caught exception is : " + e); } } catch (IOException e) { // report this since it was a valid URL but the openStream caused a problem if (log.isDebugEnabled()) { log.debug("The URL was valid, but opening the stream " + "caused a problem : " + e); } throw ExceptionFactory.makeWebServiceException( Messages.getMessage("hcConfigLoadFail", configFile, className, e.toString())); } // Attempt 2: // Try relative uri loading from Classloaders if (configStream == null) { if (log.isDebugEnabled()) { log.debug("@HandlerChain.file attribute refers to a relative location: " + configFile); log.debug("Attempt 2: Try relative uri load of (" + configFile + ") " + "from the classloaders"); } className = className.replace(".", "/"); try { if (log.isDebugEnabled()) { log.debug("Resolving @HandlerChain configuration file: " + configFile + " relative to class file: " + className); } URI uri = new URI(className); uri = uri.resolve(configFile); String resolvedPath = uri.toString(); if (log.isDebugEnabled()) { log.debug("@HandlerChain.file resolved file path location: " + resolvedPath); } configStream = getInputStream_priv(resolvedPath, classLoader); } catch (Throwable e) { if (log.isDebugEnabled()) { log.debug("Attempt 2 Failed with exception. " + "The caught exception is : " + e); } throw ExceptionFactory.makeWebServiceException( Messages.getMessage("hcConfigLoadFail", configFile, className, e.toString())); } } if (configStream == null) { //throw ExceptionFactory.makeWebServiceException(Messages.getMessage("handlerChainNS", // configFile, className)); // No longer throwing an exception here. This method is best-effort, and the caller may // change the class to which the path is relative and try again. The caller is responsible // for determining when to "give up" if (log.isDebugEnabled()) { log.debug("@HandlerChain configuration fail: " + configFile + " in class: " + className + " failed to load."); } } else { if (log.isDebugEnabled()) { log.debug("@HandlerChain configuration file: " + configFile + " in class: " + className + " was successfully loaded."); } } return configStream; }
From source file:org.openhab.binding.ebus.internal.EBusBinding.java
@Override public void updated(Dictionary<String, ?> properties) throws ConfigurationException { logger.info("Update eBus Binding configuration ..."); try {/*from ww w. j av a2 s.c o m*/ // stop last thread if active if (connector != null && connector.isAlive()) { connector.interrupt(); } // check to ensure that it is available checkConfigurationProvider(); // clear current configuration configurationProvider.clear(); // load parser from default url parser = new EBusTelegramParser(configurationProvider); URL configurationUrl = null; String parsers = (String) properties.get("parsers"); if (StringUtils.isEmpty(parsers)) { // set to current stable configurations as default parsers = "common,wolf"; } for (String elem : parsers.split(",")) { configurationUrl = null; // check for keyword custom to load custom configuration if (elem.trim().equals("custom")) { String parserUrl = (String) properties.get("parserUrl"); if (parserUrl != null) { logger.debug("Load custom eBus Parser with url {}", parserUrl); configurationUrl = new URL(parserUrl); } } else { logger.debug("Load eBus Parser Configuration \"{}\" ...", elem.trim()); configurationUrl = this.getClass().getResource("/" + elem.trim() + "-configuration.json"); } if (configurationUrl != null) { configurationProvider.loadConfigurationFile(configurationUrl); } } // check minimal config if (properties.get("serialPort") != null && properties.get("hostname") != null) { throw new ConfigurationException("hostname", "Set property serialPort or hostname, not both!"); } if (StringUtils.isNotEmpty((String) properties.get("serialPort"))) { // use the serial connector connector = new EBusSerialConnector((String) properties.get("serialPort")); } else if (StringUtils.isNotEmpty((String) properties.get("hostname"))) { // use the tcp-ip connector connector = new EBusTCPConnector((String) properties.get("hostname"), Integer.parseInt((String) properties.get("port"))); } // Set eBus sender id or default 0xFF if (StringUtils.isNotEmpty((String) properties.get("senderId"))) { connector.setSenderId(EBusUtils.toByte((String) properties.get("senderId"))); } // add event listener connector.addEBusEventListener(this); // start thread connector.start(); // set the new connector commandProcessor.setConnector(connector); commandProcessor.setConfigurationProvider(configurationProvider); } catch (MalformedURLException e) { logger.error(e.toString(), e); } catch (IOException e) { throw new ConfigurationException("general", e.toString(), e); } }
From source file:se.lu.nateko.edca.svc.DescribeFeatureType.java
/** * Method that calls a geospatial server using a DescribeFeatureType request and, * if successful, forwards the resulting XML object to the XML parser. * @return Returns true if successful, otherwise false. *///from w w w . j a va2s .c o m protected boolean describeFeatureTypeRequest() { // Log.d(TAG, "describeFeatureTypeRequest() called."); /* Execute the HTTP request. */ HttpGet httpGetMethod = new HttpGet(mServerURI); HttpResponse response; try { final HttpParams httpParameters = mHttpClient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, TIME_OUT * 1000); HttpConnectionParams.setSoTimeout(httpParameters, TIME_OUT * 1000); response = mHttpClient.execute(httpGetMethod); Log.v(TAG, "DescribeFeatureType request made to database: " + httpGetMethod.getURI().toString()); InputStream xmlStream = response.getEntity().getContent(); InputStreamReader reader = new InputStreamReader(xmlStream, "UTF-8"); BufferedReader buffReader = new BufferedReader(reader, 2048); try { Log.v(TAG, "Sending response (BufferedReader) to parser..."); return parseXMLResponse(buffReader); // Send the HttpResponse as a Reader to parse its content. } finally { buffReader.close(); } } catch (MalformedURLException e) { Log.e(TAG, e.toString()); return false; } catch (IOException e) { Log.e(TAG, e.toString()); return false; } }
From source file:com.freedomotic.plugins.devices.ipx800.Ipx800.java
private void sendToBoard(Board board, Command c) throws IOException { try {/*from w w w.j a va 2s . c om*/ URL url = null; URLConnection urlConnection; String delimiter = configuration.getProperty("address-delimiter"); String[] address = c.getProperty("address").split(delimiter); Integer relayNumber = Integer.parseInt(address[1]) - 1; if (c.getProperty("command").equals("CHANGE-STATE-DIGITAL-INPUT")) { relayNumber = relayNumber + 100; } // if required set the authentication if (board.getAuthentication().equalsIgnoreCase("true")) { String authString = board.getUsername() + ":" + board.getPassword(); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); //Create a URL for the desired page url = new URL( "http://" + board.getIpAddress() + ":" + board.getPort() + board.getPathAuthentication() + "/" + CHANGE_STATE_RELAY_URL + relayNumber + "=" + c.getProperty("state-value")); urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); } else { //Create a URL for the desired page url = new URL("http://" + board.getIpAddress() + ":" + board.getPort() + "/" + CHANGE_STATE_RELAY_URL + relayNumber + "=" + c.getProperty("state-value")); urlConnection = url.openConnection(); } LOG.info("Freedomotic sends the command " + url); InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int numCharsRead; char[] charArray = new char[1024]; StringBuffer sb = new StringBuffer(); while ((numCharsRead = isr.read(charArray)) > 0) { sb.append(charArray, 0, numCharsRead); } String result = sb.toString(); } catch (MalformedURLException e) { LOG.error("Command malformed URL " + e.toString()); } catch (IOException e) { LOG.error("Command IOexception" + e.toString()); } }