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.hypersocket.client.hosts.HostsFileManager.java

public static URL sanitizeURL(String url) throws MalformedURLException {

    URL u = new URL(url);
    String hostname = IPAddressValidator.getInstance().getGuaranteedHostname(u.getHost());
    return new URL(u.getProtocol(), hostname, u.getPort(), u.getFile());
}

From source file:crow.weibo.util.WeiboUtil.java

/**
 * ?URL,URL Path URL ???/*from   w  ww .  j  a va 2s  .c  om*/
 * 
 * @param url
 * @return
 */
public static String getNormalizedUrl(String url) {
    try {
        URL ul = new URL(url);
        StringBuilder buf = new StringBuilder();
        buf.append(ul.getProtocol());
        buf.append("://");
        buf.append(ul.getHost());
        if ((ul.getProtocol().equals("http") || ul.getProtocol().equals("https")) && ul.getPort() != -1) {
            buf.append(":");
            buf.append(ul.getPort());
        }
        buf.append(ul.getPath());
        return buf.toString();
    } catch (Exception e) {
    }
    return null;
}

From source file:com.icloud.framework.http.URLUtil.java

/** Partitions of the hostname of the url by "." */
public static String[] getHostSegments(URL url) {
    String host = url.getHost();
    // return whole hostname, if it is an ipv4
    // TODO : handle ipv6
    if (IP_PATTERN.matcher(host).matches())
        return new String[] { host };
    return host.split("\\.");
}

From source file:com.db.comserv.main.utilities.HttpCaller.java

private static void logExtAccess(final String type, final URL url, final String method,
        final int responseStatus, final int responseBytes, final long takenMs) {
    LogUtil.logExtAccess(url.getHost(), type, method, responseStatus, responseBytes, takenMs,
            ServletUtil.filterUrl(url.getPath() + (url.getQuery() == null ? "" : "?" + url.getQuery())));
}

From source file:cn.ctyun.amazonaws.regions.RegionUtils.java

/**
 * Searches through all known regions to find one with any service at the
 * specified endpoint. If no region is found with a service at that
 * endpoint, an exception is thrown.// w  ww . j av  a 2 s .c o m
 * 
 * @param endpoint
 *            The endpoint for any service residing in the desired region.
 * @return The region containing any service running at the specified
 *         endpoint, otherwise an exception is thrown if no region is found
 *         with a service at the specified endpoint.
 * @throws MalformedURLException
 *             If the given URL is malformed, or if the one of the service
 *             URLs on record is malformed.
 */
public static Region getRegionByEndpoint(String endpoint) throws MalformedURLException {
    URL targetEndpointUrl = null;
    try {
        targetEndpointUrl = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new RuntimeException("Unable to parse service endpoint: " + e.getMessage());
    }

    String targetHost = targetEndpointUrl.getHost();
    for (Region region : getRegions()) {
        for (String serviceEndpoint : region.getServiceEndpoints().values()) {
            URL serviceEndpointUrl = new URL(serviceEndpoint);
            if (serviceEndpointUrl.getHost().equals(targetHost))
                return region;
        }
    }

    throw new RuntimeException("No region found with any service for endpoint " + endpoint);
}

From source file:lucee.commons.net.http.httpclient3.HTTPEngine3Impl.java

/**
 * FUNKTIONIERT NICHT, HOST WIRD NICHT UEBERNOMMEN
 * Clones a http method and sets a new url
 * @param src//from   w  ww  .  java  2s .c o  m
 * @param url
 * @return
 */
private static HttpMethod clone(HttpMethod src, URL url) {
    HttpMethod trg = HttpMethodCloner.clone(src);
    HostConfiguration trgConfig = trg.getHostConfiguration();
    trgConfig.setHost(url.getHost(), url.getPort(), url.getProtocol());
    trg.setPath(url.getPath());
    trg.setQueryString(url.getQuery());

    return trg;
}

From source file:com.celamanzi.liferay.portlets.rails286.OnlineUtils.java

/** 
 * @author Reinaldo Silva /*from   w ww  .j  a  v  a2  s.  c om*/
 */
protected static Cookie[] getRequestCookies(RenderRequest request, java.net.URL url) {
    javax.servlet.http.Cookie[] sr_cookies;
    org.apache.commons.httpclient.Cookie[] cookies;

    sr_cookies = request.getCookies();
    cookies = new org.apache.commons.httpclient.Cookie[sr_cookies.length];

    log.debug("Servlet request cookies -------v");

    for (int i = 0; i < sr_cookies.length; i++) {
        cookies[i] = new org.apache.commons.httpclient.Cookie(url.getHost(), sr_cookies[i].getName(),
                sr_cookies[i].getValue(), url.getPath(), sr_cookies[i].getMaxAge(), sr_cookies[i].getSecure());

        log.debug("Servlet-Cookie: " + cookies[i].toString() + ", original-domain=" + sr_cookies[i].getDomain()
                + ", domain=" + url.getHost() + ", original-path=" + sr_cookies[i].getPath() + ", path="
                + cookies[i].getPath() + ", max-age=" + cookies[i].getExpiryDate() + ", secure="
                + cookies[i].getSecure());
    }

    return cookies;
}

From source file:org.apache.metron.dataloads.taxii.TaxiiHandler.java

private static HttpClientContext createContext(URL endpoint, String username, String password, int port) {
    HttpClientContext context = null;//  ww  w.j a v a 2  s .c o m
    HttpHost target = new HttpHost(endpoint.getHost(), port, endpoint.getProtocol());
    if (username != null && password != null) {

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials(username, password));

        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
        AuthCache authCache = new BasicAuthCache();
        authCache.put(target, new BasicScheme());

        // Add AuthCache to the execution context
        context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);
        context.setAuthCache(authCache);
    } else {
        context = null;
    }
    return context;
}

From source file:org.eclipse.wst.json.core.internal.download.HttpClientProvider.java

private static File download(File file, URL url) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    OutputStream out = null;/*w  w w.j  a  v  a2s .  c  om*/
    file.getParentFile().mkdirs();
    try {
        HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
        Builder builder = RequestConfig.custom();
        HttpHost proxy = getProxy(target);
        if (proxy != null) {
            builder = builder.setProxy(proxy);
        }
        RequestConfig config = builder.build();
        HttpGet request = new HttpGet(url.toURI());
        request.setConfig(config);
        response = httpclient.execute(target, request);
        InputStream in = response.getEntity().getContent();
        out = new BufferedOutputStream(new FileOutputStream(file));
        copy(in, out);
        return file;
    } catch (Exception e) {
        logWarning(e);
        ;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
        try {
            httpclient.close();
        } catch (IOException e) {
        }
    }
    return null;
}

From source file:it.uniud.ailab.dcore.wrappers.external.OpenNlpBootstrapperAnnotator.java

private static boolean isLocalFile(URL url) {
    String scheme = url.getProtocol();
    String host = url.getHost();
    return "file".equalsIgnoreCase(scheme) && (host == null || "".equals(host));
}