List of usage examples for java.net URL getHost
public String getHost()
From source file:de.jlo.talendcomp.jasperrepo.RepositoryClient.java
public static String checkRepositoryUrl(String urlStr) { if (urlStr == null || urlStr.isEmpty()) { throw new IllegalArgumentException("url cannot be null or empty"); }/*www . ja va 2s .co m*/ if (urlStr.endsWith(repositoryUrlPath)) { // everything is fine return urlStr; } else { // extract url parts try { URL url = new URL(urlStr); String host = url.getHost(); String prot = url.getProtocol(); int port = url.getPort(); String path = url.getPath(); if (path.length() > 1) { int pos = path.indexOf('/', 1); if (pos > 0) { path = path.substring(0, pos); } path = path + repositoryUrlPath; } else { path = repositoryUrlPath; } StringBuilder newUrl = new StringBuilder(); newUrl.append(prot); newUrl.append("://"); newUrl.append(host); if (port > 0) { newUrl.append(":"); newUrl.append(port); } newUrl.append(path); System.out.println("Given URL:" + urlStr + " changed to a repository URL:" + newUrl.toString()); return newUrl.toString(); } catch (MalformedURLException e) { throw new IllegalArgumentException("URL: " + urlStr + " is not valied:" + e.getMessage(), e); } } }
From source file:io.cloudslang.content.amazon.utils.InputsUtil.java
public static String getEndpointFromUrl(String input) throws MalformedURLException { URL url = new URL(input); String endpoint = url.getHost(); if (url.getPort() > START_INDEX) { endpoint += COLON + url.getPort(); }//from w ww . j ava2 s. co m return endpoint; }
From source file:com.skcraft.launcher.util.HttpRequest.java
/** * URL may contain spaces and other nasties that will cause a failure. * * @param existing the existing URL to transform * @return the new URL, or old one if there was a failure *///w w w . ja v a2 s .c om private static URL reformat(URL existing) { try { URL url = new URL(existing.toString()); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); return url; } catch (MalformedURLException e) { return existing; } catch (URISyntaxException e) { return existing; } }
From source file:io.seldon.importer.articles.FileItemAttributesImporter.java
public static String getUrlEncodedString(String input) { URL url = null; try {/*from w w w .j a v a 2 s. c o m*/ url = new URL(input); URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null); String encoded = uri.toASCIIString(); return encoded; } catch (MalformedURLException mue) { logger.error("Malformed url " + input); return null; } catch (URISyntaxException e) { logger.error("Failed to tranform url into uri ", e); return null; } }
From source file:hudson.ProxyConfiguration.java
/** * This method should be used wherever {@link URL#openConnection()} to internet URLs is invoked directly. */// w ww .ja v a2 s . c o m public static URLConnection open(URL url) throws IOException { Jenkins h = Jenkins.getInstance(); // this code might run on slaves ProxyConfiguration p = h != null ? h.proxy : null; if (p == null) return url.openConnection(); URLConnection con = url.openConnection(p.createProxy(url.getHost())); if (p.getUserName() != null) { // Add an authenticator which provides the credentials for proxy authentication Authenticator.setDefault(new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { if (getRequestorType() != RequestorType.PROXY) return null; ProxyConfiguration p = Jenkins.getInstance().proxy; return new PasswordAuthentication(p.getUserName(), p.getPassword().toCharArray()); } }); } for (URLConnectionDecorator d : URLConnectionDecorator.all()) d.decorate(con); return con; }
From source file:io.fabric8.apiman.gateway.ApimanGatewayStarter.java
private static URL waitForDependency(URL url, String serviceName, String key, String value, String username, String password) throws InterruptedException { boolean isFoundRunningService = false; ObjectMapper mapper = new ObjectMapper(); int counter = 0; URL endpoint = null;/*www . j a v a2s .c om*/ while (!isFoundRunningService) { endpoint = resolveServiceEndpoint(url.getProtocol(), url.getHost(), String.valueOf(url.getPort())); if (endpoint != null) { String isLive = null; try { URL statusURL = new URL(endpoint.toExternalForm() + url.getPath()); HttpURLConnection urlConnection = (HttpURLConnection) statusURL.openConnection(); urlConnection.setConnectTimeout(500); if (urlConnection instanceof HttpsURLConnection) { try { KeyStoreUtil.Info tPathInfo = new KeyStoreUtil().new Info(TRUSTSTORE_PATH, TRUSTSTORE_PASSWORD_PATH); TrustManager[] tms = KeyStoreUtil.getTrustManagers(tPathInfo); KeyStoreUtil.Info kPathInfo = new KeyStoreUtil().new Info(CLIENT_KEYSTORE_PATH, CLIENT_KEYSTORE_PASSWORD_PATH); KeyManager[] kms = KeyStoreUtil.getKeyManagers(kPathInfo); final SSLContext sc = SSLContext.getInstance("TLS"); sc.init(kms, tms, new java.security.SecureRandom()); final SSLSocketFactory socketFactory = sc.getSocketFactory(); HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory); HttpsURLConnection httpsConnection = (HttpsURLConnection) urlConnection; httpsConnection.setHostnameVerifier(new DefaultHostnameVerifier()); httpsConnection.setSSLSocketFactory(socketFactory); } catch (Exception e) { log.error(e.getMessage(), e); throw e; } } if (Utils.isNotNullOrEmpty(username)) { String encoded = Base64.getEncoder() .encodeToString((username + ":" + password).getBytes("UTF-8")); log.info(username + ":******"); urlConnection.setRequestProperty("Authorization", "Basic " + encoded); } isLive = IOUtils.toString(urlConnection.getInputStream()); Map<String, Object> esResponse = mapper.readValue(isLive, new TypeReference<Map<String, Object>>() { }); if (esResponse.containsKey(key) && value.equals(String.valueOf(esResponse.get(key)))) { isFoundRunningService = true; } else { if (counter % 10 == 0) log.info(endpoint.toExternalForm() + " not yet up (host=" + endpoint.getHost() + ")" + isLive); } } catch (Exception e) { if (counter % 10 == 0) log.info(endpoint.toExternalForm() + " not yet up. (host=" + endpoint.getHost() + ")" + e.getMessage()); } } else { if (counter % 10 == 0) log.info("Could not find " + serviceName + " in namespace, waiting.."); } counter++; Thread.sleep(1000l); } return endpoint; }
From source file:io.fabric8.apiman.ApimanStarter.java
public static void setFabric8Props(URL elasticEndpoint) throws IOException { log.info("** Setting API Manager Configuration Properties **"); setConfigProp("apiman.plugins.repositories", "http://repository.jboss.org/nexus/content/groups/public/"); setConfigProp("apiman.es.protocol", elasticEndpoint.getProtocol()); setConfigProp("apiman.es.host", elasticEndpoint.getHost()); setConfigProp("apiman.es.port", String.valueOf(elasticEndpoint.getPort())); String esIndexPrefix = getSystemPropertyOrEnvVar("apiman.es.index.prefix", ".apiman_"); if (esIndexPrefix != null) { setConfigProp(ApiManagerConfig.APIMAN_MANAGER_STORAGE_ES_INDEX_NAME, esIndexPrefix + "manager"); }/* ww w.j a va2 s . c om*/ setConfigProp(ApiManagerConfig.APIMAN_MANAGER_STORAGE_ES_CLIENT_FACTORY, Fabric8EsClientFactory.class.getName()); setConfigProp(ApiManagerConfig.APIMAN_MANAGER_STORAGE_TYPE, "es"); setConfigProp("apiman-manager.storage.es.protocol", Systems.getEnvVarOrSystemProperty("apiman.es.protocol")); setConfigProp("apiman-manager.storage.es.host", Systems.getEnvVarOrSystemProperty("apiman.es.host")); setConfigProp("apiman-manager.storage.es.port", Systems.getEnvVarOrSystemProperty("apiman.es.port")); setConfigProp("apiman-manager.storage.es.username", Systems.getEnvVarOrSystemProperty("apiman.es.username")); setConfigProp("apiman-manager.storage.es.password", Systems.getEnvVarOrSystemProperty("apiman.es.password")); setConfigProp(ApiManagerConfig.APIMAN_MANAGER_STORAGE_ES_INITIALIZE, "true"); setConfigProp(ApiManagerConfig.APIMAN_MANAGER_METRICS_TYPE, "es"); setConfigProp("apiman-manager.metrics.es.protocol", Systems.getEnvVarOrSystemProperty("apiman.es.protocol")); setConfigProp("apiman-manager.metrics.es.host", Systems.getEnvVarOrSystemProperty("apiman.es.host")); setConfigProp("apiman-manager.metrics.es.port", Systems.getEnvVarOrSystemProperty("apiman.es.port")); setConfigProp("apiman-manager.metrics.es.username", Systems.getEnvVarOrSystemProperty("apiman.es.username")); setConfigProp("apiman-manager.metrics.es.password", Systems.getEnvVarOrSystemProperty("apiman.es.password")); setConfigProp(ApiManagerConfig.APIMAN_MANAGER_API_CATALOG_TYPE, KubernetesServiceCatalog.class.getName()); File gatewayUserFile = new File(ApimanStarter.APIMAN_GATEWAY_USER_PATH); if (gatewayUserFile.exists()) { String[] user = IOUtils.toString(gatewayUserFile.toURI()).split(","); setConfigProp(ApimanStarter.APIMAN_GATEWAY_USERNAME, user[0]); setConfigProp(ApimanStarter.APIMAN_GATEWAY_PASSWORD, user[1]); } log.info("** ******************************************** **"); }
From source file:com.vmware.vchs.publicapi.samples.HttpUtils.java
/** * This method will parse the passed in String which is presumably a complete URL and return the * base URL e.g. https://vchs.vmware.api/ from the component parts of the passed in URL. * // w w w.j a va 2 s . c om * @param vDCUrl * the VDC Href to parse * @return the base url of the vCloud */ public static String getHostname(String completeUrl) { URL baseUrl = null; try { // First create a URL object baseUrl = new URL(completeUrl); // Now use the URL implementation to provide the component parts, leaving out // some parts so that we can build just the base URL without the path and query string return new URL(baseUrl.getProtocol(), baseUrl.getHost(), baseUrl.getPort(), "").toString(); } catch (MalformedURLException e) { throw new RuntimeException("Invalid URL: " + completeUrl); } }
From source file:hudson.ProxyConfiguration.java
public static InputStream getInputStream(URL url) throws IOException { Jenkins h = Jenkins.getInstance(); // this code might run on slaves final ProxyConfiguration p = (h != null) ? h.proxy : null; if (p == null) return new RetryableHttpStream(url); InputStream is = new RetryableHttpStream(url, p.createProxy(url.getHost())); if (p.getUserName() != null) { // Add an authenticator which provides the credentials for proxy authentication Authenticator.setDefault(new Authenticator() { @Override//from w ww.j a v a 2 s. com public PasswordAuthentication getPasswordAuthentication() { if (getRequestorType() != RequestorType.PROXY) { return null; } return new PasswordAuthentication(p.getUserName(), p.getPassword().toCharArray()); } }); } return is; }
From source file:jp.go.nict.langrid.management.logic.service.HttpClientUtil.java
/** * //from w w w . j a v a2 s .c o m * */ public static HttpClient createHttpClientWithHostConfig(URL url) { HttpClient client = new HttpClient(); int port = url.getPort(); if (port == -1) { port = url.getDefaultPort(); if (port == -1) { port = 80; } } if ((url.getProtocol().equalsIgnoreCase("https")) && (sslSocketFactory != null)) { Protocol https = new Protocol("https", (ProtocolSocketFactory) new SSLSocketFactorySSLProtocolSocketFactory(sslSocketFactory), port); client.getHostConfiguration().setHost(url.getHost(), url.getPort(), https); } else { Protocol http = new Protocol("http", new SocketFactoryProtocolSocketFactory(SocketFactory.getDefault()), port); client.getHostConfiguration().setHost(url.getHost(), url.getPort(), http); } try { List<Proxy> proxies = ProxySelector.getDefault().select(url.toURI()); for (Proxy p : proxies) { if (p.equals(Proxy.NO_PROXY)) continue; if (!p.type().equals(Proxy.Type.HTTP)) continue; InetSocketAddress addr = (InetSocketAddress) p.address(); client.getHostConfiguration().setProxy(addr.getHostName(), addr.getPort()); break; } } catch (URISyntaxException e) { e.printStackTrace(); } return client; }