下面列出了java.net.URL#getUserInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Same as FtpURLconnection(URL) with a per connection proxy specified
*/
FtpURLConnection(URL url, Proxy p) {
super(checkURL(url));
instProxy = p;
host = url.getHost();
port = url.getPort();
String userInfo = url.getUserInfo();
if (userInfo != null) { // get the user and password
int delimiter = userInfo.indexOf(':');
if (delimiter == -1) {
user = ParseUtil.decode(userInfo);
password = null;
} else {
user = ParseUtil.decode(userInfo.substring(0, delimiter++));
password = ParseUtil.decode(userInfo.substring(delimiter));
}
}
}
/**
* Same as FtpURLconnection(URL) with a per connection proxy specified
*/
FtpURLConnection(URL url, Proxy p) {
super(checkURL(url));
instProxy = p;
host = url.getHost();
port = url.getPort();
String userInfo = url.getUserInfo();
if (userInfo != null) { // get the user and password
int delimiter = userInfo.indexOf(':');
if (delimiter == -1) {
user = ParseUtil.decode(userInfo);
password = null;
} else {
user = ParseUtil.decode(userInfo.substring(0, delimiter++));
password = ParseUtil.decode(userInfo.substring(delimiter));
}
}
}
public static String getPassword(URL url) throws UnsupportedEncodingException {
String userInfo = url.getUserInfo();
if (userInfo != null) {
userInfo = URLDecoder.decode(userInfo, UTF_8);
int colon = userInfo.indexOf(':');
if (colon == -1) {
return "";
}
else {
return userInfo.substring(colon+1);
}
}
else {
return "";
}
}
/**
* Same as FtpURLconnection(URL) with a per connection proxy specified
*/
FtpURLConnection(URL url, Proxy p) {
super(checkURL(url));
instProxy = p;
host = url.getHost();
port = url.getPort();
String userInfo = url.getUserInfo();
if (userInfo != null) { // get the user and password
int delimiter = userInfo.indexOf(':');
if (delimiter == -1) {
user = ParseUtil.decode(userInfo);
password = null;
} else {
user = ParseUtil.decode(userInfo.substring(0, delimiter++));
password = ParseUtil.decode(userInfo.substring(delimiter));
}
}
}
/**
* Same as FtpURLconnection(URL) with a per connection proxy specified
*/
FtpURLConnection(URL url, Proxy p) {
super(url);
instProxy = p;
host = url.getHost();
port = url.getPort();
String userInfo = url.getUserInfo();
if (userInfo != null) { // get the user and password
int delimiter = userInfo.indexOf(':');
if (delimiter == -1) {
user = ParseUtil.decode(userInfo);
password = null;
} else {
user = ParseUtil.decode(userInfo.substring(0, delimiter++));
password = ParseUtil.decode(userInfo.substring(delimiter));
}
}
}
/**
* Same as FtpURLconnection(URL) with a per connection proxy specified
*/
FtpURLConnection(URL url, Proxy p) {
super(checkURL(url));
instProxy = p;
host = url.getHost();
port = url.getPort();
String userInfo = url.getUserInfo();
if (userInfo != null) { // get the user and password
int delimiter = userInfo.indexOf(':');
if (delimiter == -1) {
user = ParseUtil.decode(userInfo);
password = null;
} else {
user = ParseUtil.decode(userInfo.substring(0, delimiter++));
password = ParseUtil.decode(userInfo.substring(delimiter));
}
}
}
/**
* Same as FtpURLconnection(URL) with a per connection proxy specified
*/
FtpURLConnection(URL url, Proxy p) {
super(checkURL(url));
instProxy = p;
host = url.getHost();
port = url.getPort();
String userInfo = url.getUserInfo();
if (userInfo != null) { // get the user and password
int delimiter = userInfo.indexOf(':');
if (delimiter == -1) {
user = ParseUtil.decode(userInfo);
password = null;
} else {
user = ParseUtil.decode(userInfo.substring(0, delimiter++));
password = ParseUtil.decode(userInfo.substring(delimiter));
}
}
}
/**
* Encodes a full URL.
*
* @param rawUrl the URL to encode.
*/
private String encodeUrl(String rawUrl) {
if (StringUtils.isBlank(rawUrl)) {
return null;
}
String encodedUrl = null;
try {
URL url = new URL(rawUrl);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
encodedUrl = uri.toASCIIString();
} catch (Exception e) {
log.warn("encoding url: " + rawUrl + ", " + e.getMessage(), e);
}
return encodedUrl;
}
private static Authentication extractAuth(URL url) {
String userInfo = url.getUserInfo();
if (userInfo != null) {
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
int sep = userInfo.indexOf(':');
if (sep != -1) {
authBuilder.addUsername(userInfo.substring(0, sep));
authBuilder.addPassword(userInfo.substring(sep + 1));
} else {
authBuilder.addUsername(userInfo);
}
return authBuilder.build();
}
return null;
}
/**
* Creates and returns a new URL identical to the specified URL but with a changed user name.
* @param u the URL on which to base the returned URL
* @param newUserName the new user name
* @return a new URL identical to the specified URL; only user name updated
* @throws MalformedURLException if there is a problem creating the new URL
*/
public static URL getUrlWithNewUserName(final URL u, final String newUserName) throws MalformedURLException {
String newUserInfo = newUserName;
final String userInfo = u.getUserInfo();
if (org.apache.commons.lang3.StringUtils.isNotBlank(userInfo)) {
final int colonIdx = userInfo.indexOf(':');
if (colonIdx > -1) {
newUserInfo = newUserName + userInfo.substring(colonIdx);
}
}
return createNewUrl(u.getProtocol(), newUserInfo,
u.getHost(), u.getPort(), u.getPath(), u.getRef(), u.getQuery());
}
protected URLConnection openConnection(URL url) throws IOException {
return new URLConnection(url) {
@Override
public InputStream getInputStream() throws IOException {
String accessKey = null;
String secretKey = null;
if (url.getUserInfo() != null) {
String[] credentials = url.getUserInfo().split("[:]");
accessKey = credentials[0];
secretKey = credentials[1];
}
String bucket = url.getHost().substring(0, url.getHost().indexOf("."));
String key = url.getPath().substring(1);
/*try {
RestS3Service s3Service = new RestS3Service(new AWSCredentials(accessKey, secretKey));
S3Object s3obj = s3Service.getObject(bucket, key);
return s3obj.getDataInputStream();
} catch (ServiceException e) {
throw new IOException(e);
}*/
return null;
}
@Override
public void connect() throws IOException { }
};
}
private ClientBuilder(URL url) {
logger.config("URL: " + url);
String urlProtocol = url.getProtocol();
String urlHost = url.getHost();
//Check if port exists
int urlPort = url.getPort();
if (urlPort < 0) {
urlPort = url.getDefaultPort();
}
if (url.getUserInfo() != null) {
//Get username and password and replace credential variables
try {
this.username = URLDecoder.decode(url.getUserInfo().substring(0, url
.getUserInfo()
.indexOf(":")), "UTF-8");
this.password = URLDecoder.decode(url.getUserInfo().substring(url
.getUserInfo()
.indexOf(":") + 1), "UTF-8");
} catch (UnsupportedEncodingException e) {
// Should never happen UTF-8 is required in JVM
throw new RuntimeException(e);
}
}
// Check if a path exists and sanitize it by removing whitespace and any trailing /
String urlPath = url.getPath().trim();
urlPath = urlPath.endsWith("/") ? urlPath.substring(0, urlPath.length() - 1) : urlPath;
// Reconstruct URL without user credentials
this.url = convertStringToURL(urlProtocol
+ "://"
+ urlHost
+ ":"
+ urlPort
+ urlPath);
}
@Override
public PasswordAuthentication
getAuthentication(
String realm,
URL tracker )
{
if ( user_name == null || password == null ){
String user_info = tracker.getUserInfo();
if ( user_info == null ){
return( null );
}
String user_bit = user_info;
String pw_bit = "";
int pos = user_info.indexOf(':');
if ( pos != -1 ){
user_bit = user_info.substring(0,pos);
pw_bit = user_info.substring(pos+1);
}
return( new PasswordAuthentication( user_bit, pw_bit.toCharArray()));
}
return( new PasswordAuthentication( user_name, password.toCharArray()));
}
@Override
public URLConnection openConnection(URL url) throws IOException{
SingletonContext context = SingletonContext.getInstance();
NtlmPasswordAuthentication ntlmPasswordAuthentication =
new NtlmPasswordAuthentication(context, url.getUserInfo());
CIFSContext credentials =
SingletonContext.getInstance().withCredentials(ntlmPasswordAuthentication);
return new SmbFile(url, credentials);
}
/**
* Must be a public repository.
*/
public GitHubDiscoverer(URL repoUrl) throws IOException {
super(repoUrl);
String apiBaseUrl = "https://";
if (repoUrl.getUserInfo() != null) {
apiBaseUrl += repoUrl.getUserInfo() + "@";
}
apiBase = new URL(apiBaseUrl + "api.github.com/repos");
String path = repoUrl.getPath();
repoPath = path.substring(1, path.lastIndexOf('.'));
repoName = repoPath.substring(repoPath.lastIndexOf('/') + 1);
}
protected boolean equals(URL u1, URL u2) {
String userInfo1 = u1.getUserInfo();
String userInfo2 = u2.getUserInfo();
return super.equals(u1, u2) &&
(userInfo1 == null? userInfo2 == null: userInfo1.equals(userInfo2));
}
protected boolean equals(URL u1, URL u2) {
String userInfo1 = u1.getUserInfo();
String userInfo2 = u2.getUserInfo();
return super.equals(u1, u2) &&
(userInfo1 == null? userInfo2 == null: userInfo1.equals(userInfo2));
}
protected boolean equals(URL u1, URL u2) {
String userInfo1 = u1.getUserInfo();
String userInfo2 = u2.getUserInfo();
return super.equals(u1, u2) &&
(userInfo1 == null? userInfo2 == null: userInfo1.equals(userInfo2));
}
protected boolean equals(URL u1, URL u2) {
String userInfo1 = u1.getUserInfo();
String userInfo2 = u2.getUserInfo();
return super.equals(u1, u2) &&
(userInfo1 == null? userInfo2 == null: userInfo1.equals(userInfo2));
}
public static List<String> getVolumeIdFromElasticSearch(String id, String esUrl, String attributeName) {
JsonParser jsonParser = new JsonParser();
List<String> volList = new ArrayList<>();
try {
HttpClient client = HttpClientBuilder.create().build();
URL url = new URL(esUrl);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
url.getQuery(), url.getRef());
// prepare Json pay load for GET query.
JsonObject innerJson = new JsonObject();
JsonObject matchPhrase = new JsonObject();
JsonObject must = new JsonObject();
JsonObject bool = new JsonObject();
JsonObject query = new JsonObject();
innerJson.addProperty(attributeName, id);
matchPhrase.add(PacmanRuleConstants.MATCH_PHRASE, innerJson);
must.add("must", matchPhrase);
bool.add("bool", must);
query.add(PacmanRuleConstants.QUERY, bool);
StringEntity strjson = new StringEntity(query.toString());
// Qurying the ES
HttpPost httpPost = new HttpPost();
httpPost.setURI(uri);
httpPost.setEntity(strjson);
httpPost.setHeader(PacmanRuleConstants.CONTENT_TYPE, PacmanRuleConstants.APPLICATION_JSON);
HttpResponse response = client.execute(httpPost);
String jsonString = EntityUtils.toString(response.getEntity());
JsonObject resultJson = (JsonObject) jsonParser.parse(jsonString);
String hitsJsonString = resultJson.get(PacmanRuleConstants.HITS).toString();
JsonObject hitsJson = (JsonObject) jsonParser.parse(hitsJsonString);
JsonArray jsonArray = hitsJson.getAsJsonObject().get(PacmanRuleConstants.HITS).getAsJsonArray();
volList = getVolumeList(jsonArray);
} catch (Exception me) {
logger.error(me.getMessage());
}
return volList;
}