List of usage examples for java.net URL getProtocol
public String getProtocol()
From source file:com.amazonaws.mws.MarketplaceWebServiceOrdersClient.java
private static boolean usesHttps(String url) { try {/*from ww w . j a v a 2s . c o m*/ URL u = new URL(url); return u.getProtocol().equalsIgnoreCase("https"); } catch (MalformedURLException e) { throw new RuntimeException(url + " is not a URL", e); } }
From source file:com.adguard.commons.web.UrlUtils.java
/** * Extracts port from URI/*from w w w. jav a 2 s . c om*/ * * @param url URL to get port from * @return port value. */ public static int getPort(URL url) { int port = url.getPort(); if (port >= 0) { return port; } else if ("http".equals(url.getProtocol())) { return 80; } else if ("https".equals(url.getProtocol())) { return 443; } else if ("ftp".equals(url.getProtocol())) { return 21; } return port; }
From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java
/** * Creates and returns a new URL identical to the specified URL, except using the specified host. * @param u the URL on which to base the returned URL * @param newHost the new host to use in the returned URL * @param newPort the new port to use in the returned URL * @return a new URL identical to the specified URL, except using the specified host * @throws MalformedURLException if there is a problem creating the new URL *///w w w. j ava 2 s .co m public static URL getUrlWithNewHostAndPort(final URL u, final String newHost, final int newPort) throws MalformedURLException { return createNewUrl(u.getProtocol(), u.getUserInfo(), newHost, newPort, u.getPath(), u.getRef(), u.getQuery()); }
From source file:com.gargoylesoftware.htmlunit.HttpWebConnection.java
/** * Returns a new HttpClient host configuration, initialized based on the specified request. * @param webRequest the request to use to initialize the returned host configuration * @return a new HttpClient host configuration, initialized based on the specified request *//*from w w w . j av a 2 s . co m*/ private static HttpHost getHostConfiguration(final WebRequest webRequest) { final URL url = webRequest.getUrl(); return new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); }
From source file:com.adito.core.RequestParameterMap.java
private static URL parseProxiedURL(String location, MultiMap map) { try {//from w ww. ja va2 s . c o m URL proxiedURL = new URL(location); URL proxiedURLBase = proxiedURL; // Extract parameters from the proxied URL String query = proxiedURL.getQuery(); if (query != null) { proxiedURLBase = new URL(proxiedURL.getProtocol(), proxiedURL.getHost(), proxiedURL.getPort() < 1 ? -1 : proxiedURL.getPort(), proxiedURL.getPath()); parseQuery(map, query); } return proxiedURLBase; } catch (MalformedURLException murle) { log.error("Invalid proxied URL '" + location + "'"); } return null; }
From source file:brooklyn.util.ResourceUtils.java
public static URL getContainerUrl(URL url, String resourceInThatDir) { //Switching from manual parsing of jar: and file: URLs to java provided functionality. //The old code was breaking on any Windows path and instead of fixing it, using //the provided Java APIs seemed like the better option since they are already tested //on multiple platforms. boolean isJar = "jar".equals(url.getProtocol()); if (isJar) {//from ww w .j av a 2s.c o m try { //let java handle the parsing of jar URL, no network connection is established. //Strips the jar protocol: // jar:file:/<path to jar>!<resourceInThatDir> // becomes // file:/<path to jar> JarURLConnection connection = (JarURLConnection) url.openConnection(); url = connection.getJarFileURL(); } catch (IOException e) { throw new IllegalStateException(e); } } else { //Remove the trailing resouceInThatDir path from the URL, thus getting the parent folder. String path = url.toString(); int i = path.indexOf(resourceInThatDir); if (i == -1) throw new IllegalStateException( "Resource path (" + resourceInThatDir + ") not in url substring (" + url + ")"); String parent = path.substring(0, i); try { url = new URL(parent); } catch (MalformedURLException e) { throw new IllegalStateException( "Resource (" + resourceInThatDir + ") found at invalid URL parent (" + parent + ")", e); } } return url; }
From source file:jsaf.protocol.tftp.TftpURLStreamHandler.java
@Override public URLConnection openConnection(URL url) throws IOException { if ("tftp".equals(url.getProtocol())) { return new TftpURLConnection(url); } else {// w ww.j ava 2 s.co m throw new MalformedURLException(Message.getMessage(Message.ERROR_PROTOCOL, url.getProtocol())); } }
From source file:fm.last.peyote.cacti.JmxInputProcessor.java
private String convertToCactiUrl(String urlString) throws MalformedURLException { URL url = new URL(urlString); return url.getProtocol() + "://<host>:" + url.getPort() + url.getPath(); }
From source file:com.ms.commons.test.classloader.IntlTestURLClassPath.java
private static URL translateURL(URL url) throws Exception { URLClassLoader ucl = new URLClassLoader(new URL[] { url }); // URL?autoconfig List<URL> autoConfigXmlList = AutoConfigUtil.loadAllAutoConfigFilesWithFind(ucl); if ((autoConfigXmlList == null) || (autoConfigXmlList.size() == 0)) { return url; }/* w w w . jav a 2s .com*/ AutoConfigMap acm = AutoConfigUtil.loadAutoConfigMapWithFind(ucl, IntlTestProperties.PROPERTIES); // ??URL boolean urlTranslated = false; // String outputPath; if ("file".equals(url.getProtocol()) && !url.toString().toLowerCase().endsWith(".jar")) { outputPath = FileUtil.convertURLToFilePath(url); } else if (url.toString().toLowerCase().endsWith(".jar")) { String fileN = FileUtil.convertURLToFile(url).getName(); String jarUrlPath = fileN + "@" + url.toString().hashCode(); jarUrlPath = jarUrlPath.replace(".jar", "").replace("alibaba", ""); outputPath = TESTCASE_JAR_TEMP_DIR + File.separator + StringUtil.replaceNoWordChars(jarUrlPath); // ?????? File signature = new File(outputPath + File.separator + TESTCASE_JAR_SIGNATURE); StringBuilder refAst = new StringBuilder(); if (isOutOfDate(signature, url, refAst)) { // JAR??JAR?JAR?? FileUtil.clearAndMakeDirs(outputPath); expandJarTo(new JarFile(FileUtil.convertURLToFile(url)), outputPath); FileUtils.writeStringToFile(signature, refAst.toString()); } urlTranslated = true; } else { throw new RuntimeException("URL protocol unknow:" + url); } // ?autoconfig? System.err.println("Auto config for:" + url); for (AutoConfigItem antxConfigResourceItem : acm.values()) { AutoConfigUtil.autoConfigFile(ucl, IntlTestProperties.PROPERTIES, antxConfigResourceItem, outputPath, true); } return urlTranslated ? (new File(outputPath)).toURI().toURL() : url; }
From source file:br.bireme.tb.URLS.java
static URL withDomain(final URL furl, final String url) throws IOException { assert furl != null; assert url != null; final String url2 = url.trim(); final URL ret; if (url2.startsWith("http://") || (url2.startsWith("www"))) { ret = new URL(url2); } else {/*from w w w . j av a 2 s. co m*/ if (url2.charAt(0) == '/') { ret = new URL(furl.getProtocol() + "://" + furl.getHost() + url2); } else { final String path0 = furl.getPath(); final String path = path0.substring(0, path0.lastIndexOf('/')); ret = new URL(furl.getProtocol() + "://" + furl.getHost() + path + "/" + url2); } } return ret; }