Example usage for java.net URL getUserInfo

List of usage examples for java.net URL getUserInfo

Introduction

In this page you can find the example usage for java.net URL getUserInfo.

Prototype

public String getUserInfo() 

Source Link

Document

Gets the userInfo part of this URL .

Usage

From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java

/**
 * Creates and returns a new URL identical to the specified URL, except using the specified host.
 * @param u the URL on which to base the returned URL
 * @param newHost the new host to use in the returned URL
 * @param newPort the new port to use in the returned URL
 * @return a new URL identical to the specified URL, except using the specified host
 * @throws MalformedURLException if there is a problem creating the new URL
 *///  w w w. j a  v a2s  .  c o  m
public static URL getUrlWithNewHostAndPort(final URL u, final String newHost, final int newPort)
        throws MalformedURLException {
    return createNewUrl(u.getProtocol(), u.getUserInfo(), newHost, newPort, u.getPath(), u.getRef(),
            u.getQuery());
}

From source file:org.codinjutsu.tools.jenkins.view.validator.UrlValidator.java

public void validate(JTextField component) throws ConfigurationException {
    String value = component.getText();
    if (StringUtils.isEmpty(value)) {
        return;/*from ww w.j a  v  a 2s . c o m*/
    }
    try {
        URL url = new URL(value);
        String userInfo = url.getUserInfo();
        if (StringUtils.isEmpty(userInfo)) {
            return;
        }

        throw new ConfigurationException(
                "Credentials should not be embedded in the url. Use the above form instead.");
    } catch (MalformedURLException ex) {
        throw new ConfigurationException(String.format("URL '%s' is malformed", value));
    }
}

From source file:org.apache.synapse.config.SynapseConfigUtils.java

/**
 * Return an OMElement from a URL source
 *
 * @param urlStr a URL string/*from   w  w  w .  j av a 2 s.  c  om*/
 * @param synapseHome synapse home parameter to be used
 * @return an OMElement of the resource
 * @throws IOException for invalid URL's or IO errors
 */
public static OMNode getOMElementFromURL(String urlStr, String synapseHome) throws IOException {

    URL url = getURLFromPath(urlStr, synapseHome);
    if (url == null) {
        return null;
    }
    URLConnection connection = null;

    //If url contains http basic authentication parameters.
    if (url.getUserInfo() != null) {

        String protocol = url.getProtocol();

        if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {

            //Create new url excluding user info
            URL newUrl = new URL(protocol, url.getHost(), url.getPort(), url.getFile());

            connection = getURLConnection(newUrl);

            String encoding = new String(new Base64().encode(url.getUserInfo().getBytes()));
            connection.setRequestProperty("Authorization", "Basic " + encoding);
        } else {
            handleException("Unsuported protocol [" + protocol + "]. Supports only http and https with "
                    + "basic authentication");
        }
    } else {
        connection = getURLConnection(url);
    }

    if (connection == null) {
        if (log.isDebugEnabled()) {
            log.debug("Cannot create a URLConnection for given URL : " + urlStr);
        }
        return null;
    }
    InputStream inStream = connection.getInputStream();
    try {
        StAXOMBuilder builder = new StAXOMBuilder(inStream);
        OMElement doc = builder.getDocumentElement();
        doc.build();
        return doc;
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.info("Content at URL : " + url + " is non XML..");
        }
        return readNonXML(url);
    } finally {
        try {
            inStream.close();
        } catch (IOException e) {
            log.warn("Error while closing the input stream to: " + url, e);
        }
    }
}

From source file:net.freifunk.autodeploy.selenium.HeadlessDriver.java

@Override
protected void get(final URL url) {
    final String userInfo = url.getUserInfo();
    if (userInfo != null) {
        final Iterable<String> parts = Splitter.on(':').split(userInfo);
        Preconditions.checkState(Iterables.size(parts) == 2, "Expecting format user:password for userinfo.");
        final String username = Iterables.get(parts, 0);
        final String password = Iterables.getLast(parts);
        _headlessDriverCredentialsProvider.set(username, password);
    } else {//from ww w .  j a va2s .  c  om
        _headlessDriverCredentialsProvider.reset();
    }
    super.get(url);
}

From source file:edu.caltech.ipac.firefly.server.util.multipart.MultiPartPostBuilder.java

public void setTargetURL(String targetURL) {
    this.targetURL = targetURL;
    try {//from w w w. j ava2 s. co  m
        URL url = new URL(targetURL);
        if (!StringUtils.isEmpty(url.getUserInfo())) {
            String[] idPass = url.getUserInfo().split(":");
            if (idPass.length == 2) {
                userId = idPass[0];
                passwd = idPass[1];
            }
        }
    } catch (MalformedURLException e) {
        // ignore
    }

}

From source file:net.hillsdon.reviki.wiki.renderer.creole.CreoleLinkContentsSplitter.java

/**
 * Splits links of the form target or text|target where target is
 * /*  w w w. ja  va  2 s . c  om*/
 * PageName wiki:PageName PageName#fragment wiki:PageName#fragment
 * A String representing an absolute URI scheme://valid/absolute/uri
 * Any character not in the `unreserved`, `punct`, `escaped`, or `other` categories (RFC 2396),
 * and not equal '/' or '@', is %-encoded. 
 * 
 * @param in The String to split
 * @return The split LinkParts
 */
LinkParts split(final String in) {
    String target = StringUtils.trimToEmpty(StringUtils.substringBefore(in, "|"));
    String text = StringUtils.trimToNull(StringUtils.substringAfter(in, "|"));
    if (target == null) {
        target = "";
    }
    if (text == null) {
        text = target;
    }
    // Link target can be PageName, wiki:PageName or a URL.
    URI uri = null;
    try {
        URL url = new URL(target);

        uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        if (uri.getPath() == null || !uri.isAbsolute()) {
            uri = null;
        }
    } catch (URISyntaxException e) {
        // uri remains null
    } catch (MalformedURLException e) {
        // uri remains null
    }

    if (uri != null) {
        return new LinkParts(text, uri);
    } else {
        // Split into wiki:pageName
        String[] parts = target.split(":", 2);
        String wiki = null;
        String pageName = target;
        if (parts.length == 2) {
            wiki = parts[0];
            pageName = parts[1];
        }

        // Split into pageName#fragment
        parts = pageName.split("#", 2);
        String fragment = null;
        if (parts.length == 2) {
            pageName = parts[0];
            fragment = parts[1];
        }

        // Split into pageName/attachment
        parts = pageName.split("/", 2);
        String attachment = null;
        if (parts.length == 2) {
            pageName = parts[0];
            attachment = parts[1];
        }

        return new LinkParts(text, wiki, pageName, fragment, attachment);
    }
}

From source file:org.squidy.manager.scanner.PackageScanner.java

/**
 * TODO: out-source me to a FileUtility or FileUtils helper class.
 * /*w  w w  . java  2 s. com*/
 * @param url
 * @return
 */
private static File urlToFile(URL url) {
    URI uri;
    try {
        // this is the step that can fail, and so
        // it should be this step that should be fixed
        uri = url.toURI();
    } catch (URISyntaxException e) {
        // OK if we are here, then obviously the URL did
        // not comply with RFC 2396. This can only
        // happen if we have illegal unescaped characters.
        // If we have one unescaped character, then
        // the only automated fix we can apply, is to assume
        // all characters are unescaped.
        // If we want to construct a URI from unescaped
        // characters, then we have to use the component
        // constructors:
        try {
            uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                    url.getQuery(), url.getRef());
        } catch (URISyntaxException e1) {
            // The URL is broken beyond automatic repair
            throw new IllegalArgumentException("broken URL: " + url);
        }

    }
    return new File(uri);
}

From source file:org.jnode.protocol.ftp.FTPURLConnection.java

/**
 * @param url/*  w  ww. j a va  2  s  .c  o  m*/
 */
public FTPURLConnection(URL url) {
    super(url);
    this.host = url.getHost();
    this.path = url.getPath();
    String userinfo = url.getUserInfo();
    if (userinfo != null) {
        final int pos = userinfo.indexOf(':');
        if (pos != -1) {
            username = userinfo.substring(0, pos);
            password = userinfo.substring(pos + 1);
        } else {
            username = userinfo;
            password = "";
        }
    } else {
        username = "anonymous";
        password = "jnode-user@jnode.org";
    }
}

From source file:org.dasein.cloud.azure.platform.AzureSQLDatabaseSupportRequests.java

private String getEncodedUri(String urlString) throws InternalException {
    try {/*from   ww  w .j a  v a  2s  .co m*/
        URL url = new URL(urlString);
        return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef()).toString();
    } catch (Exception e) {
        throw new InternalException(e.getMessage());
    }
}

From source file:com.cladonia.xngreditor.URLUtilities.java

/**
 * Get the password for this URL./*from  ww  w  . j  ava2  s.co  m*/
 * 
 * @param url the url.
 * 
 * @return the password or null if not found.
 */
public static String getPassword(URL url) {
    String password = null;

    if (url != null) {
        String user = url.getUserInfo();

        if (user != null && user.trim().length() > 0) {
            int index = user.indexOf(':');

            if (index != -1) {
                password = user.substring(index + 1);
            }
        }
    }

    //      System.out.println( "URLUtilities.getPassword( "+url+") ["+password+"]");

    return password;
}