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.microsoft.tfs.core.util.URIUtils.java

/**
 * Returns a new {@link URI} containing only the scheme, user info, host,
 * and port of the given {@link URI}.//from w w  w .j  a v  a  2  s  .  c o  m
 *
 * @param uri
 *        the {@link URI} to remove the path and query parts from (must not
 *        be <code>null</code>)
 * @return a new {@link URI} containing only the scheme, user info, host,
 *         and port of the given {@link URI}
 */
public static URI removePathAndQueryParts(final URI uri) {
    Check.notNull(uri, "uri"); //$NON-NLS-1$

    try {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
    } catch (final URISyntaxException e) {
        final IllegalArgumentException e2 = new IllegalArgumentException(
                MessageFormat.format(Messages.getString("URIUtils.IllegalURIFormat"), uri)); //$NON-NLS-1$
        e2.initCause(e);
        throw e2;
    }
}

From source file:com.microsoft.tfs.core.util.URIUtils.java

/**
 * Returns a new {@link URI} containing only the scheme, user info, host,
 * port, and path of the given {@link URI}.
 *
 * @param uri//w w  w  .  j  a va2 s . com
 *        the {@link URI} to remove the query parts from (must not be
 *        <code>null</code>)
 * @return a new {@link URI} containing only the scheme, user info, host,
 *         port, and path of the given {@link URI}
 */
public static URI removeQueryParts(final URI uri) {
    Check.notNull(uri, "uri"); //$NON-NLS-1$

    try {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null,
                null);
    } catch (final URISyntaxException e) {
        final IllegalArgumentException e2 = new IllegalArgumentException(
                MessageFormat.format(Messages.getString("URIUtils.IllegalURIFormat"), uri)); //$NON-NLS-1$
        e2.initCause(e);
        throw e2;
    }
}

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

/**
 * Performs a sanity check on the list of hostnames/IPs to verify they at least appear to be valid.
 *
 * @param names - List of hostnames/IPs/*from  ww w . java 2 s  . c  o m*/
 *
 * @throws UnknownHostException
 */
public static void verifyHostnames(String[] names) throws UnknownHostException {
    for (String name : names) {
        if (name == null) {
            throw new UnknownHostException("null hostname found");
        }
        // The first check supports URL formats (e.g. hdfs://, etc.).
        // java.net.URI requires a schema, so we add a dummy one if it doesn't
        // have one already.
        URI uri = null;
        try {
            uri = new URI(name);
            if (uri.getHost() == null) {
                uri = new URI("http://" + name);
            }
        } catch (URISyntaxException e) {
            uri = null;
        }
        if (uri == null || uri.getHost() == null) {
            throw new UnknownHostException(name + " is not a valid Inet address");
        }
    }
}

From source file:com.sworddance.util.UriFactoryImpl.java

/**
 * Purpose of this method is to process redirect case. A lot of sites are NOT compatible to HTTP RFCs. It means
 * that they may specify redirect in wrong way. For correct way refer to http://www.ietf.org/rfc/rfc2616.txt 14.30.
 *
 * Let's say we received redirect directive from external server after requested http://www.w3.org/Whois page.
 * Below are the variants we may've received and the ways we will treat them:
 * <code>//from  ww w .  j a  v a  2  s  .c  om
 * <ol>
 * <li> "http://www.w3.org/pub/WWW/People.html" - correct redirect, no questions
 * <li> "/pub/WWW/People.html" - resolve to http://www.w3.org/pub/WWW/People.html
 * <li> "pub/WWW/People.html" - resolve to http://www.w3.org/pub/WWW/People.html
 * <li> "" - resolve to http://www.w3.org/
 * </ol>
 * </code>
 *
 * Please add the cases if you found anything else we may suffer from on redirect matter.
 *
 * One more note. Why this couldn't be done in a regular method {@link #createUriWithSchemaAndPath(Object)} ,
 * why we need separate one. Regular one deals with user input with no URI context built in. Redirect case
 * ALWAYS has context - the page it was redirected from. User meaning of something.page is (try it in browser!)
 * http://something.page. Redirect meaning of the same is most likely http://theHostRedirectCameFrom/something.page.
 *
 * @param redirectTo string saying where we should be redirected
 * @param hostRedirectedFrom host we received redirect from
 * @return resolved URI
 */
public static URI createUriForRedirect(String redirectTo, String hostRedirectedFrom) {
    // empty redirect. Redirect to the main page.
    if (isEmpty(redirectTo)) {
        return createUriWithPath(hostRedirectedFrom);
    }

    URI idealCaseURI = createUriWithPath(redirectTo);
    if (idealCaseURI.getScheme() != null && idealCaseURI.getHost() != null && idealCaseURI.getPath() != null) {
        // that's it. Thanks our remote server for following RFC!
        return idealCaseURI;
    }

    // if we're failed with ideal case - let's try other ways
    notNull(hostRedirectedFrom,
            "Host we came from shouldn't be null because redirectTo doesn't have host information. redirectTo=",
            redirectTo);
    if (!redirectTo.startsWith(PATH_SEPARATOR)) {
        redirectTo = PATH_SEPARATOR + redirectTo;
    }
    return createUriWithSchema(hostRedirectedFrom + redirectTo);
}

From source file:io.personium.core.rs.cell.MessageODataResource.java

/**
 * To????baseUrl??????.//from  w w  w .ja v a 2 s.c  o m
 * @param toValue to???
 * @param baseUrl baseUrl
 */
public static void validateToValue(String toValue, String baseUrl) {
    if (toValue == null) {
        return;
    }
    // URL??
    String checkBaseUrl = null;
    String[] uriList = toValue.split(",");
    for (String uriStr : uriList) {
        try {
            URI uri = new URI(uriStr);
            checkBaseUrl = uri.getScheme() + "://" + uri.getHost();
            int port = uri.getPort();
            if (port != -1) {
                checkBaseUrl += ":" + Integer.toString(port);
            }
            checkBaseUrl += "/";
        } catch (URISyntaxException e) {
            log.info(e.getMessage());
            throw PersoniumCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(SentMessage.P_TO.getName());
        }

        if (checkBaseUrl == null || !checkBaseUrl.equals(baseUrl)) {
            throw PersoniumCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(SentMessage.P_TO.getName());
        }
    }

}

From source file:com.daskiworks.ghwatch.backend.RemoteSystemClient.java

protected static DefaultHttpClient prepareHttpClient(URI uri, GHCredentials apiCredentials) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    httpClient.addRequestInterceptor(preemptiveAuth, 0);

    if (apiCredentials != null) {
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(uri.getHost(), uri.getPort(), AuthScope.ANY_SCHEME),
                new UsernamePasswordCredentials(apiCredentials.getUsername(), apiCredentials.getPassword()));
    }/*from  w w  w  .j  a  v  a2 s  . c om*/
    return httpClient;
}

From source file:com.vmware.thinapp.common.util.AfUtil.java

/**
 * Given a URI, return a new URI with the query, fragment, and top level of
 * the path removed.//from  ww w. j  a va 2s.c om
 *
 * @param uri the input URI
 * @return the base URI
 */
public static URI parentUri(URI uri) throws URISyntaxException {
    if (uri == null) {
        return null;
    }
    String protocol = uri.getScheme();
    String host = uri.getHost();

    // Process the port number, if any
    int port = uri.getPort();

    // Process the path, if any
    String path = uri.getPath();
    if (!StringUtils.hasLength(path)) {
        path = "";
    } else {
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        if (!path.contains("/")) {
            path = "";
        } else {
            int lastSlash = path.lastIndexOf('/');
            path = path.substring(0, lastSlash);
        }
    }

    // Build the final URL and return it
    return new URI(protocol, null, host, port, path, null, null);
}

From source file:com.sworddance.util.UriFactoryImpl.java

/**
 * suitable for namespacing//from   ww  w.  j a  v a2 s .c o m
 * @return the uri with relative schema ( ex. //facebook.com/path/to/stuff?query )
 */
public static URI createNamespaceUri(Object uriStr) {
    URI temp = createUriWithOptions(uriStr, true, true);
    try {
        URI namesUri = new URI(null, temp.getUserInfo(), temp.getHost(), -1, temp.getPath(), temp.getQuery(),
                temp.getFragment());
        return namesUri;
    } catch (URISyntaxException e) {
        return null;
    }
}

From source file:com.spotify.helios.cli.CliConfig.java

public static CliConfig fromEnvVar(Map<String, Object> config) throws URISyntaxException {
    final String master = environment.get("HELIOS_MASTER");
    if (master == null) {
        return fromMap(config);
    }/*from  w  ww.  j ava 2 s  .com*/

    // Specifically only include relevant bits according to the env var setting, rather than
    // strictly overlaying config so that if the config file has a setting for master endpoints, the
    // file doesn't override the env var if it's a domain:// as masterEndpoints takes precedence
    // over domains.
    final URI uri = new URI(master);
    final Builder<String, Object> builder = ImmutableMap.<String, Object>builder();
    // Always include the srvName bit if it's specified, so it can be specified in the file and
    // a domain flag could be passed on the command line, and it would work as you'd hope.
    if (config.containsKey(SRV_NAME_KEY)) {
        builder.put(SRV_NAME_KEY, config.get(SRV_NAME_KEY));
    }

    // TODO (dxia) Remove DEPRECATED_SITE_SCHEME after a period of time
    final String scheme = uri.getScheme();
    switch (scheme) {
    case DOMAIN_SCHEME:
    case DEPRECATED_SITE_SCHEME:
        builder.put(DOMAINS_KEY, ImmutableList.of(uri.getHost())).build();
        break;
    case HTTP_SCHEME:
        builder.put(MASTER_ENDPOINTS_KEY, ImmutableList.of(master));
        break;
    default:
        throw new RuntimeException(
                "Unknown Scheme " + scheme + " in HELIOS_MASTER env variable setting of [" + master + "]");
    }

    return fromMap(builder.build());
}

From source file:org.hl7.fhir.client.ResourceAddress.java

public static URI appendHttpParameters(URI basePath, Map<String, String> parameters) {
    try {//from  w  ww. j  a v  a2s .c om
        Set<String> httpParameterNames = parameters.keySet();
        String query = basePath.getQuery();

        for (String httpParameterName : httpParameterNames) {
            if (query != null) {
                query += "&";
            } else {
                query = "";
            }
            query += httpParameterName + "=" + parameters.get(httpParameterName);
        }

        return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(), basePath.getPort(),
                basePath.getPath(), query, basePath.getFragment());
    } catch (Exception e) {
        throw new EFhirClientException("Error appending http parameter", e);
    }
}