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:org.elasticsearch.client.sniff.ElasticsearchHostsSniffer.java

private static HttpHost readHost(String nodeId, JsonParser parser, Scheme scheme) throws IOException {
    HttpHost httpHost = null;//  w w  w . ja v  a  2 s .c om
    String fieldName = null;
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
            fieldName = parser.getCurrentName();
        } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
            if ("http".equals(fieldName)) {
                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    if (parser.getCurrentToken() == JsonToken.VALUE_STRING
                            && "publish_address".equals(parser.getCurrentName())) {
                        URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString());
                        httpHost = new HttpHost(boundAddressAsURI.getHost(), boundAddressAsURI.getPort(),
                                boundAddressAsURI.getScheme());
                    } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                        parser.skipChildren();
                    }
                }
            } else {
                parser.skipChildren();
            }
        }
    }
    //http section is not present if http is not enabled on the node, ignore such nodes
    if (httpHost == null) {
        logger.debug("skipping node [" + nodeId + "] with http disabled");
        return null;
    }
    return httpHost;
}

From source file:com.twitter.heron.apiserver.Runtime.java

private static String getErrorMessage(Server server, int port, Exception ex) {
    if (ex instanceof BindException) {
        final URI uri = server.getURI();
        return String.format("%s http://%s:%d", ex.getMessage(), uri.getHost(), port);
    }/*from  www. j a  va 2s. c  o m*/

    return ex.getMessage();
}

From source file:com.collective.celos.Util.java

public static String augmentHdfsPath(String hdfsPrefix, String path) throws URISyntaxException {

    if (hdfsPrefix.isEmpty() || hdfsPrefix.equals("/")) {
        return path;
    }//w  w  w.  ja va  2s .co m

    for (String ch : conversions.keySet()) {
        path = path.replace(ch.toString(), conversions.get(ch).toString());
    }
    URI oldUri = URI.create(path);

    String host = oldUri.getHost();
    if (oldUri.getRawSchemeSpecificPart().startsWith("///") && host == null) {
        host = "";
    }

    URI newUri = new URI(oldUri.getScheme(), oldUri.getUserInfo(), host, oldUri.getPort(),
            hdfsPrefix + oldUri.getPath(), oldUri.getQuery(), oldUri.getFragment());
    path = newUri.toString();
    for (String ch : backConversions.keySet()) {
        path = path.replace(ch.toString(), backConversions.get(ch).toString());
    }
    return path;
}

From source file:Main.java

public static boolean isURI(String str) {

    if (str.indexOf(':') == -1)
        return false;
    str = str.toLowerCase(Locale.ENGLISH).trim();

    if (!str.startsWith("http://") && !str.startsWith("https://") && !str.startsWith("ftp://"))
        return false;

    try {//from  ww  w .  j a  v a 2s. co  m

        URI uri = new URI(str);
        String proto = uri.getScheme();

        if (proto == null)
            return false;

        if (proto.equals("http") || proto.equals("https") || proto.equals("ftp")) {

            if (uri.getHost() == null)
                return false;

            String path = uri.getPath();
            if (path != null) {

                int len = path.length();
                for (int i = 0; i < len; i++) {

                    if ("?<>:*|\"".indexOf(path.charAt(i)) > -1)
                        return false;
                }
            }
        }

        return true;
    } catch (Exception ex) {

        return false;
    }
}

From source file:org.shareok.data.datahandlers.DataHandlersUtil.java

public static String getDomainNameFromUrl(String url) throws URISyntaxException {
    URI uri = new URI(url);
    String domain = uri.getHost();
    return domain.startsWith("www.") ? domain.substring(4) : domain;
}

From source file:cn.hi321.browser.weave.client.WeaveUtil.java

@SuppressWarnings({})
public static void checkNull(URI uri) {
    if (uri == null) {
        Dbg.w(new IllegalArgumentException("checkNull(URI) had null arg"));
    } else if (uri.getHost() == null || uri.getHost().length() < 1) {
        Dbg.w(new IllegalArgumentException("checkNull(URI) had empty host"));
    }//from w  ww  .  j a v  a2  s .com
}

From source file:io.syndesis.credential.BaseCredentialProvider.java

protected static String callbackUrlFor(final URI baseUrl,
        final MultiValueMap<String, String> additionalParams) {
    final String path = baseUrl.getPath();

    final String callbackPath = path + "credentials/callback";

    try {//from  ww  w .  ja v  a2  s.  c o  m
        final URI base = new URI(baseUrl.getScheme(), null, baseUrl.getHost(), baseUrl.getPort(), callbackPath,
                null, null);

        return UriComponentsBuilder.fromUri(base).queryParams(additionalParams).build().toUriString();
    } catch (final URISyntaxException e) {
        throw new IllegalStateException("Unable to generate callback URI", e);
    }
}

From source file:ee.ria.xroad.proxy.clientproxy.FastestConnectionSelectingSSLSocketFactory.java

private static InetSocketAddress toAddress(URI uri) {
    return new InetSocketAddress(uri.getHost(), uri.getPort());
}

From source file:password.pwm.http.client.PwmHttpClient.java

public static HttpClient getHttpClient(final Configuration configuration,
        final PwmHttpClientConfiguration pwmHttpClientConfiguration) throws PwmUnrecoverableException {
    final DefaultHttpClient httpClient;
    try {//from  ww w  .j  av a 2  s  .  c o  m
        if (Boolean.parseBoolean(configuration.readAppProperty(AppProperty.SECURITY_HTTP_PROMISCUOUS_ENABLE))) {
            httpClient = new DefaultHttpClient(makeConnectionManager(new X509Utils.PromiscuousTrustManager()));
        } else if (pwmHttpClientConfiguration != null && pwmHttpClientConfiguration.getCertificates() != null) {
            final TrustManager trustManager = new X509Utils.CertMatchingTrustManager(configuration,
                    pwmHttpClientConfiguration.getCertificates());
            httpClient = new DefaultHttpClient(makeConnectionManager(trustManager));
        } else {
            httpClient = new DefaultHttpClient();
        }
    } catch (Exception e) {
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN,
                "unexpected error creating promiscuous https client: " + e.getMessage()));
    }
    final String strValue = configuration.readSettingAsString(PwmSetting.HTTP_PROXY_URL);
    if (strValue != null && strValue.length() > 0) {
        final URI proxyURI = URI.create(strValue);

        final String host = proxyURI.getHost();
        final int port = proxyURI.getPort();
        final HttpHost proxy = new HttpHost(host, port);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        final String username = proxyURI.getUserInfo();
        if (username != null && username.length() > 0) {
            final String password = (username.contains(":")) ? username.split(":")[1] : "";
            final UsernamePasswordCredentials passwordCredentials = new UsernamePasswordCredentials(username,
                    password);
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port), passwordCredentials);
        }
    }
    final String userAgent = PwmConstants.PWM_APP_NAME + " " + PwmConstants.SERVLET_VERSION;
    httpClient.getParams().setParameter(HttpProtocolParams.USER_AGENT, userAgent);
    return httpClient;
}

From source file:com.ibm.ecod.watson.RetrieveAndRankSolrJExample.java

private static HttpClient createHttpClient(String uri, String username, String password) {
    final URI scopeUri = URI.create(uri);

    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(scopeUri.getHost(), scopeUri.getPort()),
            new UsernamePasswordCredentials(username, password));

    final HttpClientBuilder builder = HttpClientBuilder.create().setMaxConnTotal(128).setMaxConnPerRoute(32)
            .setDefaultRequestConfig(/*  w  ww  . j a  va  2s  .  com*/
                    RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build());
    builder.setDefaultCredentialsProvider(credentialsProvider);

    return builder.build();
}