List of usage examples for java.net URL getHost
public String getHost()
From source file:javarestart.WebClassLoaderRegistry.java
public static WebClassLoader resolveClassLoader(URL url) { if (url.getPath().endsWith("/..")) { return null; }//from ww w . j a v a 2s . c o m WebClassLoader cl = null; URL baseURL = normalizeURL(url); try { URL rootURL = new URL(baseURL, "/"); while (((cl = associatedClassloaders.get(baseURL)) == null) && !baseURL.equals(rootURL)) { baseURL = new URL(baseURL, ".."); } } catch (MalformedURLException e) { } if (cl == null) { try { JSONObject desc = Utils.getJSON(new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath() + "?getAppDescriptor")); if (desc != null) { cl = new WebClassLoader(url, desc); } } catch (Exception e) { } } associatedClassloaders.put(normalizeURL(url), cl); if (cl != null) { URL clURL = normalizeURL(cl.getBaseURL()); associatedClassloaders.put(clURL, cl); classloaders.put(clURL, cl); } return cl; }
From source file:wuit.crawler.searcher.Crawler.java
public static DSCrawlerUrl parsePageUrl(String url, String pageUrl) { DSCrawlerUrl info = new DSCrawlerUrl(); try {/*www. j a v a2s.c om*/ if (url.toLowerCase().indexOf("http:") == 0) { info.url = url; getUrlInfo(info); } else { URL _pageUrl = new URL(pageUrl); int index = pageUrl.lastIndexOf("/"); //url = url.replaceAll("\\.\\.", ""); while (url.indexOf(".") == 0) { url = url.substring(1, url.length()); } if (url.indexOf("/") == 0) info.url = _pageUrl.getProtocol() + "://" + _pageUrl.getHost() + url; else info.url = _pageUrl.getProtocol() + "://" + _pageUrl.getHost() + "/" + url; ///////////////// // System.out.println(url); // System.out.println(info.url); /////////// getUrlInfo(info); } } catch (Exception e) { return null; } return info; }
From source file:jp.go.nict.langrid.servicesupervisor.invocationprocessor.executor.intragrid.HttpClientUtil.java
/** * /*from www .j a v a2 s . c om*/ * */ 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()); client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", "")); break; } } catch (URISyntaxException e) { e.printStackTrace(); } return client; }
From source file:elaborate.util.StringUtil.java
/** * change ULRs in <code>textWithURLs</code> to links * /*from ww w. ja v a 2 s. c om*/ * @param textWithURLs * text with URLs * @return text with links */ public static String activateURLs(String textWithURLs) { StringTokenizer tokenizer = new StringTokenizer(textWithURLs, "<> ", true); StringBuilder replaced = new StringBuilder(textWithURLs.length()); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); // Log.info("token={}", token); try { URL url = new URL(token); // If possible then replace with anchor... String linktext = token; String file = url.getFile(); if (StringUtils.isNotBlank(file)) { linktext = file; } String protocol = url.getProtocol(); if (hostProtocols.contains(protocol)) { linktext = url.getHost() + linktext; } replaced.append("<a target=\"_blank\" href=\"" + url + "\">" + linktext + "</a>"); } catch (MalformedURLException e) { replaced.append(token); } } return replaced.toString(); }
From source file:jp.sf.fess.solr.plugin.suggest.util.SolrConfigUtil.java
public static SuggestUpdateConfig getUpdateHandlerConfig(final SolrConfig config) { final SuggestUpdateConfig suggestUpdateConfig = new SuggestUpdateConfig(); final Node solrServerNode = config.getNode("updateHandler/suggest/solrServer", false); if (solrServerNode != null) { try {//from w ww . ja v a2s. c o m final Node classNode = solrServerNode.getAttributes().getNamedItem("class"); String className; if (classNode != null) { className = classNode.getTextContent(); } else { className = "org.codelibs.solr.lib.server.SolrLibHttpSolrServer"; } @SuppressWarnings("unchecked") final Class<? extends SolrServer> clazz = (Class<? extends SolrServer>) Class.forName(className); final String arg = config.getVal("updateHandler/suggest/solrServer/arg", false); SolrServer solrServer; if (StringUtils.isNotBlank(arg)) { final Constructor<? extends SolrServer> constructor = clazz.getConstructor(String.class); solrServer = constructor.newInstance(arg); } else { solrServer = clazz.newInstance(); } final String username = config.getVal("updateHandler/suggest/solrServer/credentials/username", false); final String password = config.getVal("updateHandler/suggest/solrServer/credentials/password", false); if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password) && solrServer instanceof SolrLibHttpSolrServer) { final SolrLibHttpSolrServer solrLibHttpSolrServer = (SolrLibHttpSolrServer) solrServer; final URL u = new URL(arg); final AuthScope authScope = new AuthScope(u.getHost(), u.getPort()); final Credentials credentials = new UsernamePasswordCredentials(username, password); solrLibHttpSolrServer.setCredentials(authScope, credentials); solrLibHttpSolrServer.addRequestInterceptor(new PreemptiveAuthInterceptor()); } final NodeList childNodes = solrServerNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { final Node node = childNodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { final String name = node.getNodeName(); if (!"arg".equals(name) && !"credentials".equals(name)) { final String value = node.getTextContent(); final Node typeNode = node.getAttributes().getNamedItem("type"); final Method method = clazz.getMethod( "set" + name.substring(0, 1).toUpperCase() + name.substring(1), getMethodArgClass(typeNode)); method.invoke(solrServer, getMethodArgValue(typeNode, value)); } } } if (solrServer instanceof SolrLibHttpSolrServer) { ((SolrLibHttpSolrServer) solrServer).init(); } suggestUpdateConfig.setSolrServer(solrServer); } catch (final Exception e) { throw new FessSuggestException("Failed to load SolrServer.", e); } } final String labelFields = config.getVal("updateHandler/suggest/labelFields", false); if (StringUtils.isNotBlank(labelFields)) { suggestUpdateConfig.setLabelFields(labelFields.trim().split(",")); } final String roleFields = config.getVal("updateHandler/suggest/roleFields", false); if (StringUtils.isNotBlank(roleFields)) { suggestUpdateConfig.setRoleFields(roleFields.trim().split(",")); } final String expiresField = config.getVal("updateHandler/suggest/expiresField", false); if (StringUtils.isNotBlank(expiresField)) { suggestUpdateConfig.setExpiresField(expiresField); } final String segmentField = config.getVal("updateHandler/suggest/segmentField", false); if (StringUtils.isNotBlank(segmentField)) { suggestUpdateConfig.setSegmentField(segmentField); } final String updateInterval = config.getVal("updateHandler/suggest/updateInterval", false); if (StringUtils.isNotBlank(updateInterval) && StringUtils.isNumeric(updateInterval)) { suggestUpdateConfig.setUpdateInterval(Long.parseLong(updateInterval)); } //set suggestFieldInfo final NodeList nodeList = config.getNodeList("updateHandler/suggest/suggestFieldInfo", true); for (int i = 0; i < nodeList.getLength(); i++) { try { final SuggestUpdateConfig.FieldConfig fieldConfig = new SuggestUpdateConfig.FieldConfig(); final Node fieldInfoNode = nodeList.item(i); final NamedNodeMap fieldInfoAttributes = fieldInfoNode.getAttributes(); final Node fieldNameNode = fieldInfoAttributes.getNamedItem("fieldName"); final String fieldName = fieldNameNode.getNodeValue(); if (StringUtils.isBlank(fieldName)) { continue; } fieldConfig.setTargetFields(fieldName.trim().split(",")); if (logger.isInfoEnabled()) { for (final String s : fieldConfig.getTargetFields()) { logger.info("fieldName : " + s); } } final NodeList fieldInfoChilds = fieldInfoNode.getChildNodes(); for (int j = 0; j < fieldInfoChilds.getLength(); j++) { final Node fieldInfoChildNode = fieldInfoChilds.item(j); final String fieldInfoChildNodeName = fieldInfoChildNode.getNodeName(); if ("tokenizerFactory".equals(fieldInfoChildNodeName)) { //field tokenier settings final SuggestUpdateConfig.TokenizerConfig tokenizerConfig = new SuggestUpdateConfig.TokenizerConfig(); final NamedNodeMap tokenizerFactoryAttributes = fieldInfoChildNode.getAttributes(); final Node tokenizerClassNameNode = tokenizerFactoryAttributes.getNamedItem("class"); final String tokenizerClassName = tokenizerClassNameNode.getNodeValue(); tokenizerConfig.setClassName(tokenizerClassName); if (logger.isInfoEnabled()) { logger.info("tokenizerFactory : " + tokenizerClassName); } final Map<String, String> args = new HashMap<String, String>(); for (int k = 0; k < tokenizerFactoryAttributes.getLength(); k++) { final Node attribute = tokenizerFactoryAttributes.item(k); final String key = attribute.getNodeName(); final String value = attribute.getNodeValue(); if (!"class".equals(key)) { args.put(key, value); } } if (!args.containsKey(USER_DICT_PATH)) { final String userDictPath = System.getProperty(SuggestConstants.USER_DICT_PATH, ""); if (StringUtils.isNotBlank(userDictPath)) { args.put(USER_DICT_PATH, userDictPath); } final String userDictEncoding = System.getProperty(SuggestConstants.USER_DICT_ENCODING, ""); if (StringUtils.isNotBlank(userDictEncoding)) { args.put(USER_DICT_ENCODING, userDictEncoding); } } tokenizerConfig.setArgs(args); fieldConfig.setTokenizerConfig(tokenizerConfig); } else if ("suggestReadingConverter".equals(fieldInfoChildNodeName)) { //field reading converter settings final NodeList converterNodeList = fieldInfoChildNode.getChildNodes(); for (int k = 0; k < converterNodeList.getLength(); k++) { final SuggestUpdateConfig.ConverterConfig converterConfig = new SuggestUpdateConfig.ConverterConfig(); final Node converterNode = converterNodeList.item(k); if (!"converter".equals(converterNode.getNodeName())) { continue; } final NamedNodeMap converterAttributes = converterNode.getAttributes(); final Node classNameNode = converterAttributes.getNamedItem("class"); final String className = classNameNode.getNodeValue(); converterConfig.setClassName(className); if (logger.isInfoEnabled()) { logger.info("converter : " + className); } final Map<String, String> properties = new HashMap<String, String>(); for (int l = 0; l < converterAttributes.getLength(); l++) { final Node attribute = converterAttributes.item(l); final String key = attribute.getNodeName(); final String value = attribute.getNodeValue(); if (!"class".equals(key)) { properties.put(key, value); } } converterConfig.setProperties(properties); if (logger.isInfoEnabled()) { logger.info("converter properties = " + properties); } fieldConfig.addConverterConfig(converterConfig); } } else if ("suggestNormalizer".equals(fieldInfoChildNodeName)) { //field normalizer settings final NodeList normalizerNodeList = fieldInfoChildNode.getChildNodes(); for (int k = 0; k < normalizerNodeList.getLength(); k++) { final SuggestUpdateConfig.NormalizerConfig normalizerConfig = new SuggestUpdateConfig.NormalizerConfig(); final Node normalizerNode = normalizerNodeList.item(k); if (!"normalizer".equals(normalizerNode.getNodeName())) { continue; } final NamedNodeMap normalizerAttributes = normalizerNode.getAttributes(); final Node classNameNode = normalizerAttributes.getNamedItem("class"); final String className = classNameNode.getNodeValue(); normalizerConfig.setClassName(className); if (logger.isInfoEnabled()) { logger.info("normalizer : " + className); } final Map<String, String> properties = new HashMap<String, String>(); for (int l = 0; l < normalizerAttributes.getLength(); l++) { final Node attribute = normalizerAttributes.item(l); final String key = attribute.getNodeName(); final String value = attribute.getNodeValue(); if (!"class".equals(key)) { properties.put(key, value); } } normalizerConfig.setProperties(properties); if (logger.isInfoEnabled()) { logger.info("normalize properties = " + properties); } fieldConfig.addNormalizerConfig(normalizerConfig); } } } suggestUpdateConfig.addFieldConfig(fieldConfig); } catch (final Exception e) { throw new FessSuggestException("Failed to load Suggest Field Info.", e); } } return suggestUpdateConfig; }
From source file:it.geosolutions.figis.requester.HTTPUtils.java
private static void setAuth(HttpClient client, String url, String username, String pw) throws MalformedURLException { URL u = new URL(url); if ((username != null) && (pw != null)) { Credentials defaultcreds = new UsernamePasswordCredentials(username, pw); client.getState().setCredentials(new AuthScope(u.getHost(), u.getPort()), defaultcreds); client.getParams().setAuthenticationPreemptive(true); // GS2 by default always requires authentication } else {//from w ww . ja va2s .c o m if (LOGGER.isDebugEnabled()) { LOGGER.debug("Not setting credentials to access to " + url); } } }
From source file:com.redhat.rcm.version.util.InputUtils.java
public static File getFile(final String location, final File downloadsDir, final boolean deleteExisting) throws VManException { if (client == null) { setupClient();/* www . j a va 2 s. c o m*/ } File result = null; if (location.startsWith("http")) { logger.info("Downloading: '" + location + "'..."); try { final URL url = new URL(location); final String userpass = url.getUserInfo(); if (!isEmpty(userpass)) { final AuthScope scope = new AuthScope(url.getHost(), url.getPort()); final Credentials creds = new UsernamePasswordCredentials(userpass); client.getCredentialsProvider().setCredentials(scope, creds); } } catch (final MalformedURLException e) { logger.error("Malformed URL: '" + location + "'", e); throw new VManException("Failed to download: %s. Reason: %s", e, location, e.getMessage()); } final File downloaded = new File(downloadsDir, new File(location).getName()); if (deleteExisting && downloaded.exists()) { downloaded.delete(); } if (!downloaded.exists()) { HttpGet get = new HttpGet(location); OutputStream out = null; try { HttpResponse response = null; // Work around for scenario where we are loading from a server // that does a refresh e.g. gitweb final int tries = 0; do { get = new HttpGet(location); response = client.execute(get); if (response.containsHeader("Cache-control")) { logger.info("Waiting for server to generate cache..."); get.abort(); try { Thread.sleep(3000); } catch (final InterruptedException e) { } } else { break; } } while (tries < MAX_RETRIES); if (response.containsHeader("Cache-control")) { throw new VManException( "Failed to read: %s. Cache-control header was present in final attempt.", location); } final int code = response.getStatusLine().getStatusCode(); if (code == 200) { final InputStream in = response.getEntity().getContent(); out = new FileOutputStream(downloaded); copy(in, out); } else { logger.info("Received status: '{}' while downloading: {}", response.getStatusLine(), location); throw new VManException("Received status: '%s' while downloading: %s", response.getStatusLine(), location); } } catch (final ClientProtocolException e) { throw new VManException("Failed to download: '%s'. Error: %s", e, location, e.getMessage()); } catch (final IOException e) { throw new VManException("Failed to download: '%s'. Error: %s", e, location, e.getMessage()); } finally { closeQuietly(out); get.abort(); } } result = downloaded; } else { logger.info("Using local file: '" + location + "'..."); result = new File(location); } return result; }
From source file:com.reizes.shiva.utils.CommonUtil.java
/** * http https URL ?? //from w w w. j av a 2 s . c o m * @param url * @return * @throws MalformedURLException */ public static boolean isValidHttpUrl(String url) { if (url.length() > 255) { // 255? url ? ? return false; } String[] schemes = { "http", "https" }; UrlValidator urlValidator = new UrlValidator(schemes); if (urlValidator.isValid(url)) { return true; } // ? ?? URL urlTemp; try { urlTemp = new URL(url); } catch (MalformedURLException e) { return false; } String forUnicodeUrl = urlTemp.getProtocol() + "://" + IDN.toASCII(urlTemp.getHost()); if (urlValidator.isValid(forUnicodeUrl)) { // ??? http://.com www ? return true; } String regex = "([a-zA-Z0-9-.\\-&/%=?:#$(),.+;~\\_]+)"; // ? ?? if (urlTemp.getHost().startsWith("\"")) { // ?? ? ?? ? URL return false; } else if (urlTemp.getHost().startsWith(".")) { // ?? ? ?? ? URL return false; } else if (urlTemp.getProtocol().startsWith("http") && urlTemp.getHost().matches(regex)) { return true; } return false; }
From source file:org.frontcache.core.FCUtils.java
public static HttpHost getHttpHost(URL host) { HttpHost httpHost = new HttpHost(host.getHost(), host.getPort(), host.getProtocol()); return httpHost; }
From source file:it.geosolutions.geonetwork.util.HTTPUtils.java
private static void setAuth(HttpClient client, String url, String username, String pw) throws MalformedURLException { URL u = new URL(url); if (username != null && pw != null) { Credentials defaultcreds = new UsernamePasswordCredentials(username, pw); client.getState().setCredentials(new AuthScope(u.getHost(), u.getPort()), defaultcreds); client.getParams().setAuthenticationPreemptive(true); // GS2 by default always requires authentication } else {// www.j a va2 s.c om if (LOGGER.isTraceEnabled()) { LOGGER.trace("Not setting credentials to access to " + url); } } }