Java Code Examples for java.net.URL#getUserInfo()

The following examples show how to use java.net.URL#getUserInfo() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bytecoder   File: FtpURLConnection.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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));
        }
    }
}
 
Example 2
/**
 * 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));
        }
    }
}
 
Example 3
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 "";
	}		
}
 
Example 4
/**
 * 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));
        }
    }
}
 
Example 5
/**
 * 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));
        }
    }
}
 
Example 6
/**
 * 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));
        }
    }
}
 
Example 7
/**
 * 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));
        }
    }
}
 
Example 8
/**
	 * 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;
	}
 
Example 9
Source Project: vertx-stack   File: ResolverImpl.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source Project: htmlunit   File: UrlUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 11
Source Project: konduit-serving   File: S3Handler.java    License: Apache License 2.0 5 votes vote down vote up
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 { }

        };
    }
 
Example 12
Source Project: java-cloudant   File: ClientBuilder.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 13
@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()));
}
 
Example 14
@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);
	
}
 
Example 15
Source Project: mdw   File: GitHubDiscoverer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 16
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));
}
 
Example 17
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));
}
 
Example 18
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));
}
 
Example 19
Source Project: jdk8u_jdk   File: Handler.java    License: GNU General Public License v2.0 4 votes vote down vote up
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));
}
 
Example 20
Source Project: pacbot   File: PacmanUtils.java    License: Apache License 2.0 4 votes vote down vote up
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;
}