Example usage for java.net URI getHost

List of usage examples for java.net URI getHost

Introduction

In this page you can find the example usage for java.net URI getHost.

Prototype

public String getHost() 

Source Link

Document

Returns the host component of this URI.

Usage

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Create an InetSocketAddress from the given target string and default port. If the string cannot be parsed
 * correctly, the <code>configName</code> parameter is used as part of the exception message, allowing the user to
 * better diagnose the misconfiguration.
 *
 * @param target      a string of either "host" or "host:port"
 * @param defaultPort the default port if <code>target</code> does not include a port number
 * @param configName  the name of the configuration from which <code>target</code> was loaded. This is used in the
 *                    exception message in the case that parsing fails.
 *///from   w  w w .jav a 2s.  c  o  m
public static InetSocketAddress createSocketAddr(String target, int defaultPort, String configName) {
    String helpText = "";
    if (configName != null) {
        helpText = " (configuration property '" + configName + "')";
    }
    if (target == null) {
        throw new IllegalArgumentException("Target address cannot be null." + helpText);
    }
    target = target.trim();
    boolean hasScheme = target.contains("://");
    URI uri = null;
    try {
        uri = hasScheme ? URI.create(target) : URI.create("dummyscheme://" + target);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                "Does not contain a valid host:port authority: " + target + helpText);
    }

    String host = uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        port = defaultPort;
    }
    String path = uri.getPath();

    if ((host == null) || (port < 0) || (!hasScheme && path != null && !path.isEmpty())) {
        throw new IllegalArgumentException(
                "Does not contain a valid host:port authority: " + target + helpText);
    }
    return createSocketAddrForHost(host, port);
}

From source file:com.netflix.spinnaker.kork.jedis.JedisPoolFactory.java

public Pool<Jedis> build(String name, JedisDriverProperties properties) {
    if (properties.connection == null || "".equals(properties.connection)) {
        throw new MissingRequiredConfiguration("Jedis client must have a connection defined");
    }/*ww w  .ja v a 2s . co  m*/

    URI redisConnection = URI.create(properties.connection);

    String host = redisConnection.getHost();
    int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort();
    int database = parseDatabase(redisConnection.getPath());
    String password = parsePassword(redisConnection.getUserInfo());
    GenericObjectPoolConfig objectPoolConfig = properties.poolConfig;
    boolean isSSL = redisConnection.getScheme().equals("rediss");

    return new InstrumentedJedisPool(registry,
            // Pool name should always be "null", as setting this is incompat with some SaaS Redis offerings
            new JedisPool(objectPoolConfig, host, port, properties.timeoutMs, password, database, null, isSSL),
            name);
}

From source file:beadsan.AppConfig.java

@ConfigurationProperties("spring.datasource")
@Bean(destroyMethod = "close")
DataSource realDataSource() throws URISyntaxException {
    String url;//w ww .j av  a 2 s  .  com
    String username;
    String password;

    String databaseUrl = System.getenv("DATABASE_URL");
    if (databaseUrl != null) {
        URI dbUri = new URI(databaseUrl);
        url = "jdbc:postgresql://" + dbUri.getHost() + ":" + dbUri.getPort() + dbUri.getPath();
        username = dbUri.getUserInfo().split(":")[0];
        password = dbUri.getUserInfo().split(":")[1];
    } else {
        url = this.properties.getUrl();
        username = this.properties.getUsername();
        password = this.properties.getPassword();
    }

    DataSourceBuilder factory = DataSourceBuilder.create(this.properties.getClassLoader()).url(url)
            .username(username).password(password);
    this.dataSource = factory.build();
    return this.dataSource;
}

From source file:com.splunk.shuttl.archiver.copy.CallCopyBucketEndpointTest.java

public void _givenBucket_callsCopyBucketEndpointWithClientAndConfigurationWithSuccess() throws IOException {
    String host = "host";
    when(shuttlMBean.getHttpHost()).thenReturn(host);
    int port = 1234;
    when(shuttlMBean.getHttpPort()).thenReturn(port);
    LocalBucket bucket = TUtilsBucket.createBucket();

    copyBucketEndpoint.call(bucket);//from  ww  w.  j a v  a2s. co m

    HttpPost capturedRequest = getHttpClientsExecutedRequest();
    URI capturedUri = capturedRequest.getURI();
    assertEquals(host, capturedUri.getHost());
    assertEquals(capturedUri.getPort(), port);
}

From source file:org.geosdi.geoplatform.connector.server.security.AbstractSecurityConnector.java

/**
 * Bind Credentials for {@link CredentialsProvider} class
 *
 * @param credentialsProvider/*from www  .j  a  v a  2s.c  o  m*/
 */
protected void bindCredentials(CredentialsProvider credentialsProvider, URI targetURI) {
    if (this.authScope == null) {
        this.authScope = new AuthScope(targetURI.getHost(), targetURI.getPort());
    }

    credentialsProvider.setCredentials(authScope, new UsernamePasswordCredentials(username, password));
}

From source file:com.cognifide.qa.bb.provider.http.HttpClientProvider.java

private AuthScope getAuthScope(String urlString) {
    String host = AuthScope.ANY_HOST;
    int port = AuthScope.ANY_PORT;
    try {//from   www .  j av  a2 s  .  c o m
        URI uri = new URI(urlString);
        host = StringUtils.defaultString(uri.getHost(), AuthScope.ANY_HOST);
        port = uri.getPort();
    } catch (URISyntaxException e) {
        LOG.error("Could not parse '{}' as a valid URI", urlString, e);
    }
    return new AuthScope(host, port);
}

From source file:com.jaeksoft.searchlib.web.AbstractServlet.java

protected static URI buildUri(URI uri, String additionalPath, String indexName, String login, String apiKey,
        String additionnalQuery) throws URISyntaxException {
    StringBuilder path = new StringBuilder(uri.getPath());
    if (additionalPath != null)
        path.append(additionalPath);/*from  ww  w.  ja  v  a 2 s .c o m*/
    StringBuilder query = new StringBuilder();
    if (indexName != null) {
        query.append("index=");
        query.append(indexName);
    }
    if (login != null) {
        query.append("&login=");
        query.append(login);
    }
    if (apiKey != null) {
        query.append("&key=");
        query.append(apiKey);
    }
    if (additionnalQuery != null) {
        if (query.length() > 0)
            query.append("&");
        query.append(additionnalQuery);
    }
    return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), path.toString(),
            query.toString(), uri.getFragment());

}

From source file:com.autentia.web.rest.wadl.zipper.WadlZipper.java

private String composesGrammarFileNameWith(URI grammarUri) {
    String pathName = "";

    final String host = grammarUri.getHost();
    if (host != null) {
        pathName = host + '/';
    }/*from w  w  w .j  av  a 2  s .  c  o  m*/

    String uriPath = grammarUri.getPath();
    if (uriPath.startsWith("/")) {
        uriPath = uriPath.substring(1);
    }
    pathName += uriPath;

    if (!pathName.toLowerCase().endsWith(DEFAULT_SCHEMA_EXTENSION)) {
        pathName += DEFAULT_SCHEMA_EXTENSION;
    }
    return pathName;
}

From source file:be.fedict.trust.client.XKMS2ProxySelector.java

@Override
public List<Proxy> select(URI uri) {
    LOG.debug("select: " + uri);
    String hostname = uri.getHost();
    Proxy proxy = this.proxies.get(hostname);
    if (null != proxy) {
        LOG.debug("using proxy: " + proxy);
        return Collections.singletonList(proxy);
    }/*  ww  w. j ava2 s  .  co m*/
    List<Proxy> proxyList = this.defaultProxySelector.select(uri);
    return proxyList;
}

From source file:de.betterform.connector.xmlrpc.XMLRPCURIResolver.java

/**
 * Parses the URI into three elements.//from www  .  jav  a 2 s. c o  m
 * The xmlrpc URL, the function and the params
 *
 * @return a Vector
 */
private Vector parseURI(URI uri) {
    String host = uri.getHost();
    int port = uri.getPort();
    String path = uri.getPath();
    String query = uri.getQuery();

    /* Split the path up into basePath and function */
    int finalSlash = path.lastIndexOf('/');
    String basePath = "";
    if (finalSlash > 0) {
        basePath = path.substring(1, finalSlash);
    }
    String function = path.substring(finalSlash + 1, path.length());

    String rpcURL = "http://" + host + ":" + port + "/" + basePath;

    log.debug("New URL  = " + rpcURL);
    log.debug("Function = " + function);

    Hashtable paramHash = new Hashtable();

    if (query != null) {
        String[] params = query.split("&");

        for (int i = 0; i < params.length; i++) {
            log.debug("params[" + i + "] = " + params[i]);
            String[] keyval = params[i].split("=");
            log.debug("\t" + keyval[0] + " -> " + keyval[1]);
            paramHash.put(keyval[0], keyval[1]);
        }
    }

    Vector ret = new Vector();
    ret.add(rpcURL);
    ret.add(function);
    ret.add(paramHash);
    return ret;
}