下面列出了java.net.URL#getAuthority ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Encode URL
*
* @param url
* @return
* @throws MalformedURLException
*/
public static URL encode(URL url) throws MalformedURLException
{
try
{
String auth = url.getAuthority();
String host = url.getHost();
int port = url.getPort();
if (StringUtil.isEmpty(auth) || (auth.equals(host) && port == -1) || (auth.equals(host + ":" + port))) //$NON-NLS-1$
{
URI uri = new URI(url.getProtocol(), null, host, port, url.getPath(), url.getQuery(), url.getRef());
return uri.toURL();
}
}
catch (URISyntaxException e)
{
IdeLog.logError(CorePlugin.getDefault(), Messages.URLEncoder_Cannot_Encode_URL + url, e);
}
return url;
}
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;
}
protected String normalizeToString(URL url) throws IOException {
if (!"http".equals(url.getProtocol()) && !"https".equals(url.getProtocol())) {
return url.toExternalForm();
}
try {
URI uri = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(),
url.getRef());
// it is possible that the original url was already (partial) escaped,
// so we must unescape all '%' followed by 2 hexadecimals...
String uriString = uri.normalize().toASCIIString();
// manually escape the '+' character
uriString = uriString.replaceAll("\\+", "%2B");
return ESCAPE_PATTERN.matcher(uriString).replaceAll("%$1");
} catch (URISyntaxException e) {
IOException ioe = new MalformedURLException("Couldn't convert '" + url.toString()
+ "' to a valid URI");
ioe.initCause(e);
throw ioe;
}
}
public void processOpen(String method, URL url, String origin, boolean async, long connectTimeout) throws Exception {
// //LOG.entering(CLASS_NAME, "processOpen", new Object[]{method, url, origin, async});
this.async = async;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setInstanceFollowRedirects(false);
connection.setConnectTimeout((int) connectTimeout);
if (!origin.equalsIgnoreCase("null") && !origin.startsWith("privileged")) {
URL originUrl = new URL(origin);
origin = originUrl.getProtocol() + "://" + originUrl.getAuthority();
}
// connection.addRequestProperty("X-Origin", origin);
connection.addRequestProperty("Origin", origin);
setReadyState(State.OPENED);
listener.opened(new OpenEvent());
}
public static String urltoSchemaHostPortPath(URL u) {
int len = u.getProtocol().length() + 1;
if (u.getAuthority() != null && u.getAuthority().length() > 0)
len += 2 + u.getAuthority().length();
if (u.getPath() != null) {
len += u.getPath().length();
}
if (u.getQuery() != null) {
len += 1 + u.getQuery().length();
}
if (u.getRef() != null)
len += 1 + u.getRef().length();
StringBuffer result = new StringBuffer(len);
result.append(u.getProtocol());
result.append(":");
if (u.getAuthority() != null && u.getAuthority().length() > 0) {
result.append("//");
result.append(u.getAuthority());
}
if (u.getPath() != null) {
result.append(u.getPath());
}
return result.toString();
}
public static String urltoSchemaHostPortFile(URL u) {
int len = u.getProtocol().length() + 1;
if (u.getAuthority() != null && u.getAuthority().length() > 0)
len += 2 + u.getAuthority().length();
if (u.getPath() != null) {
len += u.getPath().length();
}
if (u.getQuery() != null) {
len += 1 + u.getQuery().length();
}
if (u.getRef() != null)
len += 1 + u.getRef().length();
StringBuffer result = new StringBuffer(len);
result.append(u.getProtocol());
result.append(":");
if (u.getAuthority() != null && u.getAuthority().length() > 0) {
result.append("//");
result.append(u.getAuthority());
}
if (u.getPath() != null) {
result.append(u.getFile());
}
return result.toString();
}
private String urltoString(URL u) {
int len = u.getProtocol().length() + 1;
if (u.getAuthority() != null && u.getAuthority().length() > 0)
len += 2 + u.getAuthority().length();
if (u.getPath() != null) {
len += u.getPath().length();
}
if (u.getQuery() != null) {
len += 1 + u.getQuery().length();
}
if (u.getRef() != null)
len += 1 + u.getRef().length();
StringBuffer result = new StringBuffer(len);
result.append(u.getProtocol());
result.append(":");
if (u.getAuthority() != null && u.getAuthority().length() > 0) {
result.append("//");
result.append(u.getAuthority());
}
if (u.getPath() != null) {
result.append(u.getPath());
}
return result.toString();
}
private String urltoString(URL u) {
int len = u.getProtocol().length() + 1;
if (u.getAuthority() != null && u.getAuthority().length() > 0)
len += 2 + u.getAuthority().length();
if (u.getPath() != null) {
len += u.getPath().length();
}
if (u.getQuery() != null) {
len += 1 + u.getQuery().length();
}
if (u.getRef() != null)
len += 1 + u.getRef().length();
StringBuffer result = new StringBuffer(len);
result.append(u.getProtocol());
result.append(":");
if (u.getAuthority() != null && u.getAuthority().length() > 0) {
result.append("//");
result.append(u.getAuthority());
}
if (u.getPath() != null) {
result.append(u.getPath());
}
return result.toString();
}
private static boolean resolve(String base, String path, String checkPath, String suffix) {
try {
URL url = resolveURL(base + path + checkPath + CHECK_FILE + suffix);
if (url != null) {
URL_BASE = url.getProtocol() + "://" + url.getAuthority();
String fullPath = url.getPath();
URL_PATH = fullPath.substring(0, fullPath.indexOf(checkPath));
CHECK_PATH = checkPath;
URL_SUFFIX = fullPath.substring(fullPath.indexOf(CHECK_FILE) +
CHECK_FILE.length());
}
return url != null;
}
catch (IOException ioe) {
return false;
}
}
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;
}
/**
* Parse url and return various components of the URL.
* If accept any null arguments, return null.
*
* @param urlStr URL string.
* @param partToExtract determines which components would return.
* accept values:
* HOST,PATH,QUERY,REF,
* PROTOCOL,FILE,AUTHORITY,USERINFO
* @return target value.
*/
public static String parseUrl(String urlStr, String partToExtract) {
URL url;
try {
url = URL_CACHE.get(urlStr);
} catch (Exception e) {
LOG.error("Parse URL error: " + urlStr, e);
return null;
}
if ("HOST".equals(partToExtract)) {
return url.getHost();
}
if ("PATH".equals(partToExtract)) {
return url.getPath();
}
if ("QUERY".equals(partToExtract)) {
return url.getQuery();
}
if ("REF".equals(partToExtract)) {
return url.getRef();
}
if ("PROTOCOL".equals(partToExtract)) {
return url.getProtocol();
}
if ("FILE".equals(partToExtract)) {
return url.getFile();
}
if ("AUTHORITY".equals(partToExtract)) {
return url.getAuthority();
}
if ("USERINFO".equals(partToExtract)) {
return url.getUserInfo();
}
return null;
}
/**
* if the caller has a URLPermission for connecting to the
* given URL, then return a SocketPermission which permits
* access to that destination. Return null otherwise. The permission
* is cached in a field (which can only be changed by redirects)
*/
SocketPermission URLtoSocketPermission(URL url) throws IOException {
if (socketPermission != null) {
return socketPermission;
}
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
return null;
}
// the permission, which we might grant
SocketPermission newPerm = new SocketPermission(
getHostAndPort(url), "connect"
);
String actions = getRequestMethod()+":" +
getUserSetHeaders().getHeaderNamesInList();
String urlstring = url.getProtocol() + "://" + url.getAuthority()
+ url.getPath();
URLPermission p = new URLPermission(urlstring, actions);
try {
sm.checkPermission(p);
socketPermission = newPerm;
return socketPermission;
} catch (SecurityException e) {
// fall thru
}
return null;
}
/**
* if the caller has a URLPermission for connecting to the
* given URL, then return a SocketPermission which permits
* access to that destination. Return null otherwise. The permission
* is cached in a field (which can only be changed by redirects)
*/
SocketPermission URLtoSocketPermission(URL url) throws IOException {
if (socketPermission != null) {
return socketPermission;
}
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
return null;
}
// the permission, which we might grant
SocketPermission newPerm = new SocketPermission(
getHostAndPort(url), "connect"
);
String actions = getRequestMethod()+":" +
getUserSetHeaders().getHeaderNamesInList();
String urlstring = url.getProtocol() + "://" + url.getAuthority()
+ url.getPath();
URLPermission p = new URLPermission(urlstring, actions);
try {
sm.checkPermission(p);
socketPermission = newPerm;
return socketPermission;
} catch (SecurityException e) {
// fall thru
}
return null;
}
@PostConstruct
public void postConstruct() {
try {
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
final URL aURL = new URL(request.getRequestURL().toString());
redirectUris = aURL.getProtocol() + "://" + aURL.getAuthority() + "/oxauth-rp/home.htm";
backchannelClientNotificationEndpoint = aURL.getProtocol() + "://" + aURL.getAuthority() + "/api/cb";
} catch (MalformedURLException e) {
log.error("Problems processing oxAuth-RP url", e);
}
}
public HttpURLConnection openConnection() throws IOException {
url = new URL(address);
authority = url.getAuthority();
conn = (HttpURLConnection) url.openConnection();
return conn;
}
private static URL validateUrlAtCount(URL url) throws MalformedURLException {
if (url == null || url.getAuthority() == null) {
return url;
}
if (StringUtils.countMatches(url.toString(), "@") > 1) {
throw new MalformedURLException(MessageFormat.format(FileOperationMessages.getString("FileUtils.url_at_count_error"), url.toString()));
}
return url;
}
public HttpURLConnection openConnection() throws IOException {
url = new URL(address);
authority = url.getAuthority();
conn = (HttpURLConnection) url.openConnection();
return conn;
}
private static Permission getURLConnectPermission(URL url) {
String urlString = url.getProtocol() + "://" + url.getAuthority() + url.getPath();
return new URLPermission(urlString);
}
private static Permission getURLConnectPermission(URL url) {
String urlString = url.getProtocol() + "://" + url.getAuthority() + url.getPath();
return new URLPermission(urlString);
}
public static ParsedUrl parseUrl(String url) {
List<PathPart> pathParts = new ArrayList<PathPart>();
List<Query> queries = new ArrayList<Query>();
ParsedUrl parsedUrl;
String base;
try {
URL aURL = new URL(url);
base = aURL.getAuthority();
String protocol = aURL.getProtocol();
parsedUrl = new ParsedUrl();
parsedUrl.setPathWithEndingSlash(aURL.getPath().endsWith("/"));
parsedUrl.setBaseUrl(protocol + "://" + base);
List<NameValuePair> pairs = URLEncodedUtils.parse(aURL.getQuery(),
Charset.defaultCharset());
for (NameValuePair pair : pairs) {
Query query = new Query(pair.getName(), pair.getValue());
queries.add(query);
}
parsedUrl.setQueries(queries);
String[] pathStrings = aURL.getPath().split("/");
for (String pathPart : pathStrings) {
Matcher m = PATH_PARAM_PATTERN.matcher(pathPart);
if (m.find()) {
String paramDef = m.group(1);
String[] paramParts = paramDef.split(":");
if (paramParts.length > 1) {
pathParts.add(new PathPart(paramParts[1].trim(), paramParts[0].trim()));
} else {
pathParts.add(new PathPart(paramParts[0].trim()));
}
} else {
if(!pathPart.isEmpty()) {
pathParts.add(new PathPart(pathPart));
}
}
}
parsedUrl.setPathParts(pathParts);
} catch (Exception ex) {
Logger.error(UrlParser.class, R2MMessages.getMessage("CANNOT_PARSE_URL", url));
return null;
}
return parsedUrl;
}