java.net.URL#getProtocol ( )源码实例Demo

下面列出了java.net.URL#getProtocol ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Bytecoder   文件: ParseUtil.java
public static java.net.URI toURI(URL url) {
    String protocol = url.getProtocol();
    String auth = url.getAuthority();
    String path = url.getPath();
    String query = url.getQuery();
    String ref = url.getRef();
    if (path != null && !(path.startsWith("/")))
        path = "/" + path;

    //
    // In java.net.URI class, a port number of -1 implies the default
    // port number. So get it stripped off before creating URI instance.
    //
    if (auth != null && auth.endsWith(":-1"))
        auth = auth.substring(0, auth.length() - 3);

    java.net.URI uri;
    try {
        uri = createURI(protocol, auth, path, query, ref);
    } catch (java.net.URISyntaxException e) {
        uri = null;
    }
    return uri;
}
 
源代码2 项目: development   文件: PortFactory.java
/**
 * Creates a new URL object that retrieves the content specified by the
 * given URL with credentials.. Internally the content is pre-fetched with
 * the given credentials. The returned URL has an custom protocol handler
 * that simply returns the pre-fetched content.
 * 
 * @param url
 * @param username
 * @param password
 * @return
 * @throws IOException
 */
private static URL withCredentials(final URL url, final String username,
        final String password) throws IOException {

    final byte[] content = getUrlContent(url, username, password);

    final URLStreamHandler handler = new URLStreamHandler() {

        @Override
        protected URLConnection openConnection(URL u) throws IOException {
            return new URLConnection(url) {
                @Override
                public void connect() throws IOException {
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    return new ByteArrayInputStream(content);
                }
            };
        }
    };
    return new URL(url.getProtocol(), url.getHost(), url.getPort(),
            url.getFile(), handler);
}
 
源代码3 项目: jdk8u-jdk   文件: HttpURLConnection.java
private String getHostAndPort(URL url) {
    String host = url.getHost();
    final String hostarg = host;
    try {
        // lookup hostname and use IP address if available
        host = AccessController.doPrivileged(
            new PrivilegedExceptionAction<String>() {
                public String run() throws IOException {
                        InetAddress addr = InetAddress.getByName(hostarg);
                        return addr.getHostAddress();
                }
            }
        );
    } catch (PrivilegedActionException e) {}
    int port = url.getPort();
    if (port == -1) {
        String scheme = url.getProtocol();
        if ("http".equals(scheme)) {
            return host + ":80";
        } else { // scheme must be https
            return host + ":443";
        }
    }
    return host + ":" + Integer.toString(port);
}
 
源代码4 项目: common-utils   文件: PackageUtil.java
/**
 * 获取包中所有资源,如果字符串为<code>“空”</code>则返回<code>null</code>。
 * 
 * @param packageName 包名
 * @return 包中所有资源文件,如果字符串为<code>“空”</code>则返回<code>null</code>。
 * @throws IOException
 */
public static List<String> getResourceInPackage(String packageName) throws IOException {
    if (StringUtil.isBlank(packageName)) {
        return null;
    }

    boolean recursive = packageName.endsWith(".*");
    String packagePath = getPackagePath(packageName);
    List<String> resources = CollectionUtil.createArrayList();
    String packageDirName = packagePath.replace('.', '/');

    URL[] dirs = ClassLoaderUtil.getResources(packageDirName);
    for (URL url : dirs) {
        String protocol = url.getProtocol();
        if ("file".equals(protocol)) {
            findResourceInDirPackage(packagePath, URLDecoder.decode(url.getFile(), "UTF-8"), resources);
        } else if ("jar".equals(protocol)) {
            findResourceInJarPackage(url, packageName, packageDirName, recursive, resources);
        }

    }
    return resources;
}
 
源代码5 项目: knopflerfish.org   文件: BundleURLStreamHandler.java
/**
 * Compare two urls to see whether they refer to the same file,
 * i.e., having the same protocol, host, port, and path.
 * @return true if u1 and u2 refer to the same file
 */
@Override
protected boolean sameFile(URL u1, URL u2) {
  final String p1 = u1.getProtocol();
  if (PROTOCOL.equals(p1)) {
    if (!p1.equals(u2.getProtocol()))
      return false;

    if (!hostsEqual(u1, u2))
      return false;

    if (!(u1.getFile() == u2.getFile() ||
          (u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
      return false;

    if (u1.getPort() != u2.getPort())
      return false;

    return true;
  } else {
    return u1.equals(u2);
  }
}
 
源代码6 项目: olca-app   文件: PlatformUtils.java
protected static String getBundleJarPathInternal(String MANIFESTPath,
		URL pluginManifLoc) {
	String protocol = pluginManifLoc.getProtocol();
	String path = pluginManifLoc.getPath();
	if (protocol.equals("file")) {
		// ok, can use like this
	} else if (protocol.equals("jar") && path.startsWith("file:")) {
		path = path.substring("file:".length());
	} else {
		log.warn("Plugin protocol not file but {}", protocol);
		throw new RuntimeException(
				"Cannot determine path - protocol indicates non-local URL.");
	}

	if (path.endsWith(MANIFESTPath)) {
		path = path.substring(0, path.length() - MANIFESTPath.length());
	}
	if (path.endsWith("!")) {
		path = path.substring(0, path.length() - 1);
	}
	return path;
}
 
源代码7 项目: hadoop   文件: StreamUtil.java
static URL qualifyHost(URL url) {
  try {
    InetAddress a = InetAddress.getByName(url.getHost());
    String qualHost = a.getCanonicalHostName();
    URL q = new URL(url.getProtocol(), qualHost, url.getPort(), url.getFile());
    return q;
  } catch (IOException io) {
    return url;
  }
}
 
源代码8 项目: ache   文件: BipartiteGraphRepository.java
/**
 * This method retrieves the the backlinks of a given url.
 * 
 * @param url
 * @return
 * @throws IOException
 */

public BackLinkNeighborhood[] getBacklinks(URL url) throws IOException {
    URL normalizedURL = new URL(url.getProtocol(), url.getHost(), "/");
    String urlId = url2id.get(normalizedURL.toString());
    if (urlId == null) {
        return null;
    }
    String strLinks = authGraph.get(urlId);
    if (strLinks == null) {
        return null;
    } else {
        List<BackLinkNeighborhood> tempBacklinks = new ArrayList<BackLinkNeighborhood>();
        String[] backlinkIds = strLinks.split("###");
        for (int i = 0; i < backlinkIds.length; i++) {
            String url_title = hubID.get(backlinkIds[i]);
            if (url_title != null) {
                BackLinkNeighborhood bln = new BackLinkNeighborhood();
                String[] fields = url_title.split(":::");
                bln.setLink(fields[0]);
                if (fields.length > 1) {
                    bln.setTitle(fields[1]);
                }
                tempBacklinks.add(bln);
            }
        }
        BackLinkNeighborhood[] blns = new BackLinkNeighborhood[tempBacklinks.size()];
        tempBacklinks.toArray(blns);
        return blns;
    }
}
 
源代码9 项目: openjdk-8-source   文件: Handler.java
/**
 * This method is called to parse the string spec into URL u for a
 * mailto protocol.
 *
 * @param   u the URL to receive the result of parsing the spec
 * @param   spec the URL string to parse
 * @param   start the character position to start parsing at.  This is
 *          just past the ':'.
 * @param   limit the character position to stop parsing at.
 */
public void parseURL(URL u, String spec, int start, int limit) {

    String protocol = u.getProtocol();
    String host = "";
    int port = u.getPort();
    String file = "";

    if (start < limit) {
        file = spec.substring(start, limit);
    }
    /*
     * Let's just make sure we DO have an Email address in the URL.
     */
    boolean nogood = false;
    if (file == null || file.equals(""))
        nogood = true;
    else {
        boolean allwhites = true;
        for (int i = 0; i < file.length(); i++)
            if (!Character.isWhitespace(file.charAt(i)))
                allwhites = false;
        if (allwhites)
            nogood = true;
    }
    if (nogood)
        throw new RuntimeException("No email address");
    setURLHandler(u, protocol, host, port, file, null);
}
 
源代码10 项目: Android-Basics-Codes   文件: AsyncHttpClient.java
/**
 * Will encode url, if not disabled, and adds params on the end of it
 *
 * @param url             String with URL, should be valid URL without params
 * @param params          RequestParams to be appended on the end of URL
 * @param shouldEncodeUrl whether url should be encoded (replaces spaces with %20)
 * @return encoded url if requested with params appended if any available
 */
public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url, RequestParams params) {
    if (url == null)
        return null;

    if (shouldEncodeUrl) {
        try {
            String decodedURL = URLDecoder.decode(url, "UTF-8");
            URL _url = new URL(decodedURL);
            URI _uri = new URI(_url.getProtocol(), _url.getUserInfo(), _url.getHost(), _url.getPort(), _url.getPath(), _url.getQuery(), _url.getRef());
            url = _uri.toASCIIString();
        } catch (Exception ex) {
            // Should not really happen, added just for sake of validity
            Log.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex);
        }
    }

    if (params != null) {
        // Construct the query string and trim it, in case it
        // includes any excessive white spaces.
        String paramString = params.getParamString().trim();

        // Only add the query string if it isn't empty and it
        // isn't equal to '?'.
        if (!paramString.equals("") && !paramString.equals("?")) {
            url += url.contains("?") ? "&" : "?";
            url += paramString;
        }
    }

    return url;
}
 
源代码11 项目: krpc   文件: DefaultHttpClient.java
String getKey(HttpClientReq req) {
    URL url = req.getUrlObj();
    String schema = url.getProtocol();
    String host = url.getHost();
    int port = url.getPort();
    if (port == -1) port = url.getDefaultPort();
    return schema + "//" + host + ":" + port;
}
 
源代码12 项目: incubator-gobblin   文件: HttpUtils.java
public static String createApacheHttpClientLimiterKey(Config config) {
  try {
    String urlTemplate = config.getString(HttpConstants.URL_TEMPLATE);
    URL url = new URL(urlTemplate);
    String key = url.getProtocol() + "/" + url.getHost();
    if (url.getPort() > 0) {
      key = key + "/" + url.getPort();
    }
    log.info("Get limiter key [" + key + "]");
    return key;
  } catch (MalformedURLException e) {
    throw new IllegalStateException("Cannot get limiter key.", e);
  }
}
 
源代码13 项目: openjdk-jdk9   文件: Handler.java
/**
 * This method is called to parse the string spec into URL u for a
 * mailto protocol.
 *
 * @param   u the URL to receive the result of parsing the spec
 * @param   spec the URL string to parse
 * @param   start the character position to start parsing at.  This is
 *          just past the ':'.
 * @param   limit the character position to stop parsing at.
 */
public void parseURL(URL u, String spec, int start, int limit) {

    String protocol = u.getProtocol();
    String host = "";
    int port = u.getPort();
    String file = "";

    if (start < limit) {
        file = spec.substring(start, limit);
    }
    /*
     * Let's just make sure we DO have an Email address in the URL.
     */
    boolean nogood = false;
    if (file == null || file.equals(""))
        nogood = true;
    else {
        boolean allwhites = true;
        for (int i = 0; i < file.length(); i++)
            if (!Character.isWhitespace(file.charAt(i)))
                allwhites = false;
        if (allwhites)
            nogood = true;
    }
    if (nogood)
        throw new RuntimeException("No email address");
    setURLHandler(u, protocol, host, port, file, null);
}
 
源代码14 项目: trekarta   文件: URLHandler.java
protected static URLHandler getDefault(URL url) {
    URLHandler handler = null;

    String protocol = url.getProtocol();
    try {
        if (protocol.equals("file")) {
            handler = new FileURLHandler(url);
        } else if (protocol.equals("jar") || protocol.equals("wsjar")) {
            handler = new JarURLHandler(url);
        }
    } catch (Exception e) {
        // ignore - just return null
    }
    return handler;
}
 
源代码15 项目: jdk8u_jdk   文件: HttpCallerInfo.java
/**
 * Constructor an un-schemed object for proxy access.
 */
public HttpCallerInfo(URL url, String host, int port) {
    this.url= url;
    this.host = host;
    this.port = port;
    prompt = "";
    addr = null;
    protocol = url.getProtocol();
    authType = RequestorType.PROXY;
    scheme = "";
}
 
源代码16 项目: openjdk-jdk8u-backup   文件: URLUtil.java
private static Permission getURLConnectPermission(URL url) {
    String urlString = url.getProtocol() + "://" + url.getAuthority() + url.getPath();
    return new URLPermission(urlString);
}
 
源代码17 项目: lams   文件: StandardArchiveDescriptorFactory.java
@Override
public URL adjustJarFileEntryUrl(URL url, URL rootUrl) {
	final String protocol = url.getProtocol();
	final boolean check = StringHelper.isEmpty( protocol )
			|| "file".equals( protocol )
			|| "vfszip".equals( protocol )
			|| "vfsfile".equals( protocol );
	if ( !check ) {
		return url;
	}

	final String filePart = extractLocalFilePath( url );
	if ( filePart.startsWith( "/" ) || new File(url.getFile()).isAbsolute() ) {
		// the URL is already an absolute form
		return url;
	}
	else {
		// prefer to resolve the relative URL relative to the root PU URL per
		// JPA 2.0 clarification.
		final File rootUrlFile = new File( extractLocalFilePath( rootUrl ) );
		try {
			if ( rootUrlFile.isDirectory() ) {
				// The PU root is a directory (exploded).  Here we can just build
				// the relative File reference and use the Filesystem API to convert
				// to URI and then a URL
				final File combined = new File( rootUrlFile, filePart );
				// make sure it exists..
				if ( combined.exists() ) {
					return combined.toURI().toURL();
				}
			}
			else {
				// The PU root is an archive.  Here we have to build a JAR URL to properly
				// handle the nested entry reference (the !/ part).
				return new URL(
						"jar:" + protocol + "://" + rootUrlFile.getAbsolutePath() + "!/" + filePart
				);
			}
		}
		catch (MalformedURLException e) {
			// allow to pass through to return the original URL
			log.debugf(
					e,
					"Unable to adjust relative <jar-file/> URL [%s] relative to root URL [%s]",
					filePart,
					rootUrlFile.getAbsolutePath()
			);
		}

		return url;
	}
}
 
源代码18 项目: Knowage-Server   文件: XSSRequestWrapper.java
private static String checkAnchorTags(String value) {
	logger.debug("IN");
	Pattern aPattern = Pattern.compile("<a([^>]+)>(.+?)</a>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
	Pattern hrefPattern = Pattern.compile("\\s*href\\s*=\\s*['\"]([^'\"]+)['\"]", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

	Matcher aTagMatcher = aPattern.matcher(value);

	while (aTagMatcher.find()) {
		String aTag = aTagMatcher.group();
		String href = aTagMatcher.group(1);

		// In <a> tag find href attribute
		Matcher hrefMatcher = hrefPattern.matcher(href);

		while (hrefMatcher.find()) {
			String link = hrefMatcher.group(1);

			try {
				URL url = new URL(link);
				String baseUrl = url.getProtocol() + "://" + url.getHost();

				if (!whitelist.getExternalServices().contains(baseUrl)) {
					logger.warn("Provided anchor's href is: " + url + ". Anchor base url is not in Whitelist and therefore anchor will be deleted");
					value = value.replace(aTag, "");
				}

			} catch (MalformedURLException e) {
				logger.debug("URL [" + link + "] is malformed. Trying to see if it is a valid relative URL...");
				if (isValidRelativeURL(link) && isTrustedRelativePath(link)) {
					logger.debug("URL " + link + " is recognized to be a valid URL");
				} else {
					logger.error("Malformed URL [" + link + "]", e);
					value = value.replace(aTag, "");
				}
			}
		}

	}

	logger.debug("OUT");
	return value;
}
 
源代码19 项目: tomee   文件: DeploymentsResolver.java
public static void processUrls(final String caller,
                               final List<URL> urls,
                               final ClassLoader classLoader,
                               final Set<RequireDescriptors> requireDescriptors,
                               final List<URL> jarList) {
    for (final URL url : urls) {

        final String urlProtocol = url.getProtocol();
        //Currently, we only support jar and file protocol
        final boolean isValidURL = urlProtocol.equals("jar") || urlProtocol.equals("file");
        if (!isValidURL) {
            logger.warning("Unknown protocol " + urlProtocol);
            continue;
        }

        if (logger.isDebugEnabled()) {
            logger.debug(caller + ".processing: " + url);
        }

        try {

            final DeploymentLoader deploymentLoader = new DeploymentLoader();

            final Class<? extends DeploymentModule> moduleType = deploymentLoader.discoverModuleType(url, classLoader, requireDescriptors);
            if (AppModule.class.isAssignableFrom(moduleType) ||
                    EjbModule.class.isAssignableFrom(moduleType) ||
                    PersistenceModule.class.isAssignableFrom(moduleType) ||
                    ConnectorModule.class.isAssignableFrom(moduleType) ||
                    ClientModule.class.isAssignableFrom(moduleType)) {

                final URL archive = toFileUrl(url);

                if (!jarList.contains(archive)) {
                    jarList.add(archive);
                    final File file = toFile(archive);
                    logger.info("Found " + moduleType.getSimpleName() + " in classpath: " + file.getAbsolutePath());
                }
            }
        } catch (final IOException e) {
            logger.warning("Unable to determine the module type of " + url.toExternalForm() + ": Exception: " + e.getMessage(), e);
        } catch (final UnknownModuleTypeException ignore) {
            // no-op
        }
    }
}
 
源代码20 项目: stategen   文件: ResourceHelper.java
/**
 * Determine whether the given URL points to a resource in the file system,
 * that is, has protocol "file" or "vfs".
 * @param url the URL to check
 * @return whether the URL has been identified as a file system URL
 */
public static boolean isFileURL(URL url) {
	String protocol = url.getProtocol();
	return (URL_PROTOCOL_FILE.equals(protocol) || protocol.startsWith(URL_PROTOCOL_VFS));
}