List of usage examples for java.net URL getProtocol
public String getProtocol()
From source file:org.fcrepo.localservices.saxon.SaxonServlet.java
/** * Return a URL string in which the port is always specified. *//*from w w w .j a v a 2 s . co m*/ private static String normalizeURL(String urlString) throws MalformedURLException { URL url = new URL(urlString); if (url.getPort() == -1) { return url.getProtocol() + "://" + url.getHost() + ":" + url.getDefaultPort() + url.getFile() + (url.getRef() != null ? "#" + url.getRef() : ""); } else { return urlString; } }
From source file:mobi.jenkinsci.ci.client.JenkinsFormAuthHttpClient.java
public static final String getDomainFromUrl(final URL url) { return String.format("%s://%s%s", url.getProtocol(), url.getHost(), url.getPort() > 0 ? ":" + url.getPort() : ""); }
From source file:com.dmsl.anyplace.utils.NetworkUtils.java
public static String encodeURL(String urlStr) throws URISyntaxException, MalformedURLException { URL url = new URL(urlStr); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL();/*from w w w . j a v a 2 s . com*/ return url.toString(); }
From source file:net.daporkchop.porkselfbot.util.HTTPUtils.java
/** * Concatenates the given {@link java.net.URL} and query. * * @param url URL to base off/*from w w w .j av a 2 s . co m*/ * @param query Query to append to URL * @return URL constructed */ public static URL concatenateURL(final URL url, final String query) { try { if (url.getQuery() != null && url.getQuery().length() > 0) { return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "&" + query); } else { return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "?" + query); } } catch (final MalformedURLException ex) { throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex); } }
From source file:net.rim.ejde.internal.model.BlackBerryVMInstallType.java
/** * Returns the default javadoc location specified in the properties or <code>null</code> if none. * * @param properties/*from w w w .j a v a 2 s.c om*/ * properties map * * @return javadoc location specified in the properties or <code>null</code> if none */ public static URL getJavadocLocation(final Map properties) { final String javadoc = BlackBerryVMInstallType.getProperty(ExecutionEnvironmentDescription.JAVADOC_LOC, properties); if ((javadoc != null) && (javadoc.length() > 0)) { try { URL url = new URL(javadoc); if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$ final File file = new File(url.getFile()); url = file.getCanonicalFile().toURL(); } return url; } catch (final MalformedURLException e) { BlackBerryVMInstallType.log.error("", e); //$NON-NLS-1$ return null; } catch (final IOException e) { BlackBerryVMInstallType.log.error("", e); //$NON-NLS-1$ return null; } } final String version = BlackBerryVMInstallType.getProperty(ExecutionEnvironmentDescription.LANGUAGE_LEVEL, properties); if (version != null) return StandardVMType.getDefaultJavadocLocation(version); return null; }
From source file:com.netflix.nicobar.core.utils.ClassPathUtils.java
/** * Find the root path for the given resource. If the resource is found in a Jar file, then the * result will be an absolute path to the jar file. If the resource is found in a directory, * then the result will be the parent path of the given resource. * * For example, if the resourceName is given as "scripts/myscript.groovy", and the path to the file is * "/root/sub1/script/myscript.groovy", then this method will return "/root/sub1" * * @param resourceName relative path of the resource to search for. E.G. "scripts/myscript.groovy" * @param classLoader the {@link ClassLoader} to search * @return absolute path of the root of the resource. *//*www .j av a 2s.co m*/ @Nullable public static Path findRootPathForResource(String resourceName, ClassLoader classLoader) { Objects.requireNonNull(resourceName, "resourceName"); Objects.requireNonNull(classLoader, "classLoader"); URL resource = classLoader.getResource(resourceName); if (resource != null) { String protocol = resource.getProtocol(); if (protocol.equals("jar")) { return getJarPathFromUrl(resource); } else if (protocol.equals("file")) { return getRootPathFromDirectory(resourceName, resource); } else { throw new IllegalStateException("Unsupported URL protocol: " + protocol); } } return null; }
From source file:com.base.httpclient.HttpJsonClient.java
/** * get//w w w.ja va 2s .c o m * * @param url * URL * @param params * getkey-value * @return JSON * @throws ClientProtocolException * @throws IOException * @throws URISyntaxException */ public static String get(String url, Map<String, ?> params) throws ClientProtocolException, IOException, URISyntaxException { DefaultHttpClient httpclient = getHttpClient(); try { if (params != null && !(params.isEmpty())) { List<NameValuePair> values = new ArrayList<NameValuePair>(); for (Map.Entry<String, ?> entity : params.entrySet()) { BasicNameValuePair pare = new BasicNameValuePair(entity.getKey(), entity.getValue().toString()); values.add(pare); } String str = URLEncodedUtils.format(values, "UTF-8"); if (url.indexOf("?") > -1) { url += "&" + str; } else { url += "?" + str; } } URL pageURL = new URL(url); //pageUrl?httpget URI uri = new URI(pageURL.getProtocol(), pageURL.getHost(), pageURL.getPath(), pageURL.getQuery(), null); HttpGet httpget = createHttpUriRequest(HttpMethod.GET, uri); httpget.setHeader("Pragma", "no-cache"); httpget.setHeader("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate"); httpget.setHeader("Connection", "keep-alive"); httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); httpget.setHeader("Accept-Charset", "gbk,utf-8;q=0.7,*;q=0.7"); httpget.setHeader("Referer", url); /*httpget.setHeader("Content-Encoding", "gzip"); httpget.setHeader("Accept-Encoding", "gzip, deflate");*/ //httpget.setHeader("Host", "s.taobao.com"); /*int temp = Integer.parseInt(Math.round(Math.random()*(MingSpiderService.UserAgent.length-1))+"");//??? httpget.setHeader("User-Agent", MingSpiderService.UserAgent[temp]);*/ HttpResponse response = httpclient.execute(httpget); httpclient.getCookieStore().clear(); int resStatu = response.getStatusLine().getStatusCode(); String html = ""; if (resStatu == HttpStatus.SC_OK) {//200 HttpEntity entity = response.getEntity(); if (entity != null) { html = EntityUtils.toString(entity, "GBK"); EntityUtils.consume(entity); } } return html; } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:URLUtils.java
/** * Checks that the protocol://host:port part of two URLs are equal. * /*from w ww. j a va 2 s .co m*/ * @param u1, * the first URL to check * @param u2, * the second URL to check * @return a boolean, true if the protocol://host:port part of the URL are * equals, false otherwise */ public static boolean equalsProtocolHostPort(URL u1, URL u2) { if ((u1 == null) || (u2 == null)) { return false; } // check that the protocol are the same (as it impacts the // default port check if (!u1.getProtocol().equalsIgnoreCase(u2.getProtocol())) { return false; } // check that both hostnames are equal if (!u1.getHost().equalsIgnoreCase(u2.getHost())) { return false; } int u1p = u1.getPort(); int u2p = u2.getPort(); // if port is ok, it's good! if (u1p == u2p) { return true; } else if ((u1p > 0) && (u2p > 0)) { return false; } // otherwise, the painful comparison of -1 and such if (url_defport != null) { if (u1p == -1) { try { int u1dp; u1dp = ((Integer) url_defport.invoke(u1, (Object[]) null)).intValue(); return (u2p == u1dp); } catch (InvocationTargetException ex) { } catch (IllegalAccessException iex) { } } else { try { int u2dp; u2dp = ((Integer) url_defport.invoke(u2, (Object[]) null)).intValue(); return (u1p == u2dp); } catch (InvocationTargetException ex) { } catch (IllegalAccessException iex) { } } } // no JDK 1.4 this is becoming painful... if (u1p == -1) { String s = u1.getProtocol(); int u1dp = 0; if (s.equalsIgnoreCase("http")) { u1dp = 80; } else if (s.equalsIgnoreCase("https")) { u1dp = 443; } // FIXME do others? return (u2p == u1dp); } else { String s = u2.getProtocol(); int u2dp = 0; if (s.equalsIgnoreCase("http")) { u2dp = 80; } else if (s.equalsIgnoreCase("https")) { u2dp = 443; } // FIXME do others? return (u1p == u2dp); } }
From source file:com.yahoo.storm.yarn.StormOnYarn.java
/** * Find a jar that contains a class of the same name, if any. * It will return a jar file, even if that is not the first thing * on the class path that has a class with the same name. * //from w w w .j a va2s . com * @param my_class the class to find. * @return a jar file that contains the class, or null. * @throws IOException on any error */ public static String findContainingJar(Class<?> my_class) throws IOException { ClassLoader loader = my_class.getClassLoader(); String class_file = my_class.getName().replaceAll("\\.", "/") + ".class"; for (Enumeration<URL> itr = loader.getResources(class_file); itr.hasMoreElements();) { URL url = itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } // URLDecoder is a misnamed class, since it actually decodes // x-www-form-urlencoded MIME type rather than actual // URL encoding (which the file path has). Therefore it would // decode +s to ' 's which is incorrect (spaces are actually // either unencoded or encoded as "%20"). Replace +s first, so // that they are kept sacred during the decoding process. toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } throw new IOException("Fail to locat a JAR for class: " + my_class.getName()); }
From source file:com.tc.util.ProductInfo.java
static InputStream getData(String name) { CodeSource codeSource = ProductInfo.class.getProtectionDomain().getCodeSource(); if (codeSource != null && codeSource.getLocation() != null) { URL source = codeSource.getLocation(); if (source.getProtocol().equals("file") && source.toExternalForm().endsWith(".jar")) { URL res;// www . j a v a 2s . c om try { res = new URL("jar:" + source.toExternalForm() + "!" + name); InputStream in = res.openStream(); if (in != null) { return in; } } catch (MalformedURLException e) { throw new AssertionError(e); } catch (IOException e) { // must not be embedded in this jar -- resolve via loader path } } else if (source.getProtocol().equals("file") && (new File(source.getPath()).isDirectory())) { File local = new File(source.getPath(), name); if (local.isFile()) { try { return new FileInputStream(local); } catch (FileNotFoundException e) { throw new AssertionError(e); } } } } return ProductInfo.class.getResourceAsStream(name); }