Example usage for java.net URL getHost

List of usage examples for java.net URL getHost

Introduction

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

Prototype

public String getHost() 

Source Link

Document

Gets the host name of this URL , if applicable.

Usage

From source file:Main.java

private static String escapeUrlString(String urlString) throws MalformedURLException, URISyntaxException {
    URL url = new URL(urlString);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef());
    return uri.toURL().toString();
}

From source file:Main.java

/**
 * Return the base URL from the given URL.  Example:
 * http://foo.org/abc.html -> http://foo.org/
 * @param surl/*www  .  j a  v  a  2  s.c  o m*/
 * @return The base URL.
 */
public static String getBaseUrl(String surl) {
    URL url;
    try {
        url = new URL(surl);
        System.out.println("getHost: " + url.getHost());
        return "http://" + url.getHost() + "/";
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static String zzb(String... strArr) {
    Builder builder = new Builder();
    int length = strArr.length;
    int i = 0;//from w  ww  . ja  v  a2s.c o  m
    while (i < length) {
        String str = strArr[i];
        try {
            URL url = new URL(str);
            builder.appendQueryParameter("url", url.getProtocol() + "://" + url.getHost());
            i++;
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Invalid URL: " + str);
        }
    }
    return "weblogin:" + builder.build().getQuery();
}

From source file:org.talend.components.elasticsearch.runtime_2_4.ElasticsearchConnection.java

public static RestClient createClient(ElasticsearchDatastoreProperties datastore) throws MalformedURLException {
    String urlStr = datastore.nodes.getValue();
    String[] urls = urlStr.split(",");
    HttpHost[] hosts = new HttpHost[urls.length];
    int i = 0;// ww w  . j  a  v a2s . c  o m
    for (String address : urls) {
        URL url = new URL("http://" + address);
        hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
        i++;
    }
    RestClientBuilder restClientBuilder = RestClient.builder(hosts);
    if (datastore.auth.useAuth.getValue()) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
                datastore.auth.userId.getValue(), datastore.auth.password.getValue()));
        restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }
        });
    }
    return restClientBuilder.build();
}

From source file:Main.java

public static String realUrl(String target) {
    try {// ww w  .j a  v  a2 s.c om
        URL url = new URL(target);

        String protocol = url.getProtocol();
        String host = url.getHost();
        String path = url.getPath();
        String query = url.getQuery();

        path = URLEncoder.encode(path, "utf-8").replace("%3A", ":").replace("%2B", "+").replace("%2C", ",")
                .replace("%5E", "^").replace("%2F", "/").replace("%21", "!").replace("%24", "$")
                .replace("%25", "%").replace("%26", "&").replace("%28", "(").replace("%29", ")")
                .replace("%40", "@").replace("%60", "`");
        // .replace("", "#"); // not support.

        StringBuilder urlBuild = new StringBuilder(protocol).append("://").append(host).append(path);
        if (query != null)
            urlBuild.append("?").append(query);
        return urlBuild.toString();
    } catch (IOException e) {
        return target;
    }
}

From source file:com.esri.geoportal.commons.utils.HttpClientContextBuilder.java

/**
 * Creates client context./*from   ww w  . j  a  v a  2s  .c  o m*/
 * @param url url
 * @param cred credentials
 * @return client context
 */
public static HttpClientContext createHttpClientContext(URL url, SimpleCredentials cred) {
    HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(cred.getUserName(), cred.getPassword()));

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);

    return context;
}

From source file:Main.java

public static String encodeDocumentUrl(String urlString) {
    try {/*from   w  w w.  j  av  a2s. c  o  m*/

        URL url = new URL(urlString);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());

        return uri.toASCIIString();

    } catch (MalformedURLException e) {
        return null;
    } catch (URISyntaxException e) {
        return null;
    }

}

From source file:Main.java

public static void checkForBlacklistedURLs(URL url)

        throws IOException {
    for (int i = 0; i < BLACKLISTED_HOSTS.length; i++) {

        if (url.getHost().equalsIgnoreCase(BLACKLISTED_HOSTS[i]) && url.getPort() == BLACKLISTED_PORTS[i]) {

            throw (new IOException(
                    "http://" + BLACKLISTED_HOSTS[i] + ":" + BLACKLISTED_PORTS[i] + "/ is not a tracker"));
        }//from  ww  w  . j  a  va  2  s  . c  o  m
    }
}

From source file:de.fraunhofer.iosb.ilt.tests.Constants.java

public static SensorThingsService createService(URL serviceUrl)
        throws MalformedURLException, URISyntaxException {
    SensorThingsService service = new SensorThingsService(serviceUrl);
    if (USE_OPENID_CONNECT) {
        service.setTokenManager(new TokenManagerOpenIDConnect().setTokenServerUrl(TOKEN_SERVER_URL)
                .setClientId(CLIENT_ID).setUserName(USERNAME).setPassword(PASSWORD));
    }//from w w w  .  j  av a 2 s.c  om
    if (USE_BASIC_AUTH) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        URL url = new URL(BASE_URL);
        credsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()),
                new UsernamePasswordCredentials(USERNAME, PASSWORD));
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
                .build();
        service.setClient(httpclient);
    }
    return service;
}

From source file:fr.mael.microrss.util.Tools.java

public static String getBaseUrl(String completeUrl) throws MalformedURLException {
    URL url = new URL(completeUrl);
    String host = url.getHost();
    return new StringBuffer("http://").append(host).toString();
}