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:com.textuality.keybase.lib.prover.HackerNews.java

@Override
public boolean fetchProofData() {

    try {//from  w  w  w .j  a v  a 2  s.  c  o m
        JSONObject sigJSON = readSig(mProof.getSigId());

        // the magic string is the base64 of the SHA of the raw message
        mShortenedMessageHash = JWalk.getString(sigJSON, "sig_id_short");

        // The api form is off at firebasio, so well use the proof URL
        String hnUrl = mProof.getProofUrl();

        Fetch fetch = new Fetch(hnUrl);
        String problem = fetch.problem();
        if (problem != null) {
            mLog.add(problem);
            return false;
        }

        // Paranoid Interlude:
        // Lets make sure source has right host and there's an id=<nametag> in the path
        //
        String nametag = mProof.getNametag();
        URL url = new URL(hnUrl);
        String scheme = url.getProtocol();
        String host = url.getHost();
        if (!((scheme.equals("http") || scheme.equals("https")) && host.equals("news.ycombinator.com")
                && hnUrl.contains("id=" + nametag))) {
            mLog.add("Proof either doesnt come from news.ycombinator.com or isnt specific to " + nametag);
            return false;
        }

        if (!(fetch.getBody().contains(mShortenedMessageHash))) {
            mLog.add("Hacker News post doesnt contain signed PGP message");
            return false;
        }

        return true;

    } catch (KeybaseException e) {
        mLog.add("Keybase API problem: " + e.getLocalizedMessage());
    } catch (JSONException e) {
        mLog.add("Broken JSON message: " + e.getLocalizedMessage());
    } catch (MalformedURLException e) {
        mLog.add("Malformed URL for proof post");
    }
    return false;
}

From source file:com.digitalpebble.storm.crawler.util.URLPartitioner.java

/**
 * Returns the host, domain, IP of a URL so that it can be partitioned for
 * politeness/*from   w w w  .j  a  v a2 s  .  c o  m*/
 **/
public String getPartition(String url, Metadata metadata) {

    String partitionKey = null;
    String host = "";

    // IP in metadata?
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP)) {
        String ip_provided = metadata.getFirstValue("ip");
        if (StringUtils.isNotBlank(ip_provided)) {
            partitionKey = ip_provided;
        }
    }

    if (partitionKey == null) {
        URL u;
        try {
            u = new URL(url);
            host = u.getHost();
        } catch (MalformedURLException e1) {
            LOG.warn("Invalid URL: {}", url);
            return null;
        }
    }

    // partition by hostname
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_HOST))
        partitionKey = host;

    // partition by domain : needs fixing
    else if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_DOMAIN)) {
        partitionKey = PaidLevelDomain.getPLD(host);
    }

    // partition by IP
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP) && partitionKey == null) {
        try {
            long start = System.currentTimeMillis();
            final InetAddress addr = InetAddress.getByName(host);
            partitionKey = addr.getHostAddress();
            long end = System.currentTimeMillis();
            LOG.debug("Resolved IP {} in {} msec for : {}", partitionKey, (end - start), url);
        } catch (final Exception e) {
            LOG.warn("Unable to resolve IP for: {}", host);
            return null;
        }
    }

    LOG.debug("Partition Key for: {} > {}", url, partitionKey);

    return partitionKey;
}

From source file:com.digitalpebble.stormcrawler.util.URLPartitioner.java

/**
 * Returns the host, domain, IP of a URL so that it can be partitioned for
 * politeness/*from w  ww  .j a  v  a 2  s . co m*/
 **/
public String getPartition(String url, Metadata metadata) {

    String partitionKey = null;
    String host = "";

    // IP in metadata?
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP)) {
        String ip_provided = metadata.getFirstValue("ip");
        if (StringUtils.isNotBlank(ip_provided)) {
            partitionKey = ip_provided;
        }
    }

    if (partitionKey == null) {
        URL u;
        try {
            u = new URL(url);
            host = u.getHost();
        } catch (MalformedURLException e1) {
            LOG.warn("Invalid URL: {}", url);
            return null;
        }
    }

    // partition by hostname
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_HOST))
        partitionKey = host;

    // partition by domain : needs fixing
    else if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_DOMAIN)) {
        partitionKey = PaidLevelDomain.getPLD(host);
    }

    // partition by IP
    if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP) && partitionKey == null) {
        try {
            long start = System.currentTimeMillis();
            final InetAddress addr = InetAddress.getByName(host);
            partitionKey = addr.getHostAddress();
            long end = System.currentTimeMillis();
            LOG.debug("Resolved IP {} in {} msec for : {}", partitionKey, end - start, url);
        } catch (final Exception e) {
            LOG.warn("Unable to resolve IP for: {}", host);
            return null;
        }
    }

    LOG.debug("Partition Key for: {} > {}", url, partitionKey);

    return partitionKey;
}

From source file:org.uiautomation.ios.server.grid.SelfRegisteringRemote.java

private boolean isAlreadyRegistered() {
    HttpClient client = httpClientFactory.getHttpClient();
    try {//from   w ww .  j a  v  a2s  . c  o  m
        URL hubRegistrationURL = new URL(nodeConfig.getRegistrationURL());
        URL api = new URL("http://" + hubRegistrationURL.getHost() + ":" + hubRegistrationURL.getPort()
                + "/grid/api/proxy");
        HttpHost host = new HttpHost(api.getHost(), api.getPort());

        String id = "http://" + nodeConfig.getHost() + ":" + nodeConfig.getPort();
        BasicHttpRequest r = new BasicHttpRequest("GET", api.toExternalForm() + "?id=" + id);

        HttpResponse response = client.execute(host, r);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new GridException(
                    "hub down or not responding. Reason : " + response.getStatusLine().getReasonPhrase());
        }
        JSONObject o = extractObject(response);
        return (Boolean) o.get("success");
    } catch (Exception e) {
        throw new GridException("Problem registering with hub", e);
    }
}

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

/**
 * Splits links of the form target or text|target where target is
 * //  www. j a  v a2 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.apache.metamodel.elasticsearch.rest.ElasticSearchRestDataContextFactory.java

private ElasticSearchRestClient createClient(final DataContextProperties properties)
        throws MalformedURLException {
    final URL url = new URL(properties.getUrl());
    final RestClientBuilder builder = RestClient.builder(new HttpHost(url.getHost(), url.getPort()));

    if (properties.getUsername() != null) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(properties.getUsername(), properties.getPassword()));

        builder.setHttpClientConfigCallback(
                httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    }//ww  w .  j  av a  2  s .c o  m

    return new ElasticSearchRestClient(builder.build());
}

From source file:fr.dutra.confluence2wordpress.xmlrpc.CommonsXmlRpcTransportFactory.java

public CommonsXmlRpcTransportFactory(URL url, String proxyHost, int proxyPort, int maxConnections) {
    this.url = url;
    HostConfiguration hostConf = new HostConfiguration();
    hostConf.setHost(url.getHost(), url.getPort());
    if (proxyHost != null && proxyPort != -1) {
        hostConf.setProxy(proxyHost, proxyPort);
    }/* ww  w.  ja v  a 2s .  c o m*/
    HttpConnectionManagerParams connParam = new HttpConnectionManagerParams();
    connParam.setMaxConnectionsPerHost(hostConf, maxConnections);
    connParam.setMaxTotalConnections(maxConnections);
    MultiThreadedHttpConnectionManager conMgr = new MultiThreadedHttpConnectionManager();
    conMgr.setParams(connParam);
    client = new HttpClient(conMgr);
    client.setHostConfiguration(hostConf);
}

From source file:com.adito.reverseproxy.ReverseProxyMethodHandler.java

/**
 * Encodes a URL //from ww w  . j a va2s  .com
 * @param location
 * @return
 */
public static final String encodeURL(String location) {

    try {
        URL url = new URL(location);

        StringBuffer buf = new StringBuffer();
        buf.append(url.getProtocol());
        buf.append("://");
        if (!Util.isNullOrTrimmedBlank(url.getUserInfo())) {
            buf.append(DAVUtilities.encodeURIUserInfo(url.getUserInfo()));
            buf.append("@");
        }
        buf.append(url.getHost());
        if (url.getPort() != -1) {
            buf.append(":");
            buf.append(url.getPort());
        }
        if (!Util.isNullOrTrimmedBlank(url.getPath())) {
            buf.append(URLUTF8Encoder.encode(url.getPath(), false));
        }
        if (!Util.isNullOrTrimmedBlank(url.getQuery())) {
            buf.append("?");
            buf.append(encodeQuery(url.getQuery()));
        }

        return buf.toString();
    } catch (MalformedURLException e) {

        int idx = location.indexOf('?');
        if (idx > -1 && idx < location.length() - 1) {
            return URLUTF8Encoder.encode(location.substring(0, idx), false) + "?"
                    + encodeQuery(location.substring(idx + 1));
        } else
            return URLUTF8Encoder.encode(location, false);
    }
}

From source file:de.escidoc.core.test.common.fedora.TripleStoreTestBase.java

protected DefaultHttpClient getHttpClient() throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    URL url = new URL(getFedoraUrl());
    AuthScope m_authScope = new AuthScope(url.getHost(), AuthScope.ANY_PORT, AuthScope.ANY_REALM);
    UsernamePasswordCredentials m_creds = new UsernamePasswordCredentials("fedoraAdmin", "fedoraAdmin");
    httpClient.getCredentialsProvider().setCredentials(m_authScope, m_creds);

    return httpClient;
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.StorageImpl.java

private String getKey(final Type type, final HtmlPage page) {
    switch (type) {
    case GLOBAL_STORAGE:
        return page.getUrl().getHost();

    case LOCAL_STORAGE:
        final URL url = page.getUrl();
        return url.getProtocol() + "://" + url.getHost() + ':' + url.getProtocol();

    case SESSION_STORAGE:
        final WebWindow topWindow = page.getEnclosingWindow().getTopWindow();
        return Integer.toHexString(topWindow.hashCode());

    default:/* ww  w. j  av  a  2s  .co  m*/
        return null;
    }
}