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:Main.java

public static URI unredirect(URI uri) throws IOException {
    if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) {
        return uri;
    }//from  w  w w  .  ja va2  s . c  om
    URL url = uri.toURL();

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setInstanceFollowRedirects(false);
    connection.setDoInput(false);
    connection.setRequestMethod("HEAD");
    connection.setRequestProperty("User-Agent", "ZXing (Android)");
    try {
        connection.connect();
        switch (connection.getResponseCode()) {
        case HttpURLConnection.HTTP_MULT_CHOICE:
        case HttpURLConnection.HTTP_MOVED_PERM:
        case HttpURLConnection.HTTP_MOVED_TEMP:
        case HttpURLConnection.HTTP_SEE_OTHER:
        case 307: // No constant for 307 Temporary Redirect ?
            String location = connection.getHeaderField("Location");
            if (location != null) {
                try {
                    return new URI(location);
                } catch (URISyntaxException e) {
                    // nevermind
                }
            }
        }
        return uri;
    } finally {
        connection.disconnect();
    }
}

From source file:io.gravitee.gateway.standalone.DynamicRoutingGatewayTest.java

private static URI create(URI uri, int port) {
    try {/*  ww  w  .  j a v  a 2s  .c o m*/
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), port, uri.getPath(), uri.getQuery(),
                uri.getFragment());
    } catch (URISyntaxException e) {
        return uri;
    }
}

From source file:architecture.ee.web.util.ServletUtils.java

public static String getDomainName(String url, boolean opt) {

    if (StringUtils.isNotEmpty(url)) {
        try {/*from w w w.  j  ava  2  s  .c o  m*/
            URI uri = new URI(url);
            String domain = uri.getHost();
            if (opt)
                return domain.startsWith("www.") ? domain.substring(4) : domain;
            return domain;
        } catch (URISyntaxException e) {
        }
    }
    return null;
}

From source file:org.thoughtcrime.securesms.mms.MmsSendHelper.java

private static byte[] makePost(MmsConnectionParameters parameters, byte[] mms)
        throws ClientProtocolException, IOException {
    Log.w("MmsSender", "Sending MMS1 of length: " + mms.length);
    try {/*from ww  w  .  java 2 s.c  o  m*/
        HttpClient client = constructHttpClient(parameters);
        URI hostUrl = new URI(parameters.getMmsc());
        HttpHost target = new HttpHost(hostUrl.getHost(), hostUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpPost request = new HttpPost(parameters.getMmsc());
        ByteArrayEntity entity = new ByteArrayEntity(mms);

        entity.setContentType("application/vnd.wap.mms-message");

        request.setEntity(entity);
        request.setParams(client.getParams());
        request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");

        HttpResponse response = client.execute(target, request);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());

        return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
        Log.w("MmsDownlader", use);
        throw new IOException("Bad URI syntax");
    }
}

From source file:org.fdroid.enigtext.mms.MmsDownloadHelper.java

private static byte[] makeRequest(Context context, MmsConnectionParameters connectionParameters, String url)
        throws IOException {
    AndroidHttpClient client = null;/*w  w w  . ja  v  a  2s . co m*/

    try {
        client = constructHttpClient(context, connectionParameters);
        URI targetUrl = new URI(url.trim());
        HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpGet request = new HttpGet(url.trim());

        request.setParams(client.getParams());
        request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");

        HttpResponse response = client.execute(target, request);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());

        return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
        Log.w("MmsDownloadHelper", use);
        throw new IOException("Couldn't parse URI");
    } finally {
        if (client != null)
            client.close();
    }
}

From source file:Main.java

public static String getDomain(String target) {
    Pattern p = Pattern.compile(".*?([^.]+\\.[^.]+)");
    URI uri;

    try {/* ww  w . ja v  a  2  s  .  co m*/
        uri = new URI(target);

    } catch (Exception e) {
        return "http";
    }

    String host = uri.getHost();
    Matcher m = p.matcher(host);

    if (m.matches())
        return m.group(1);
    else
        return host;
}

From source file:com.amazonaws.util.AwsHostNameUtils.java

/**
 * @deprecated in favor of {@link #parseRegionName(String, String)}.
 *///  www .  j a v  a 2 s .  c  o m
@Deprecated
public static String parseRegionName(URI endpoint) {
    return parseRegionName(endpoint.getHost(), null);
}

From source file:org.sonatype.nexus.httpclient.internal.NexusRedirectStrategy.java

/**
 * Return host of given uri or null.//from w  w  w  .j  av  a  2 s .  c om
 */
@Nullable
private static String hostOf(final URI uri) {
    if (uri != null) {
        return uri.getHost();
    }
    return null;
}

From source file:com.opengamma.engine.calcnode.CalculationNodeProcess.java

private static void setConnectionDefaults(final String url) {
    try {//w ww  .  j  a  v a  2s .  c  o m
        final URI uri = new URI(url);
        if (uri.getHost() != null) {
            System.setProperty("opengamma.engine.calcnode.host", uri.getHost());
        }
        if (uri.getPort() != -1) {
            System.setProperty("opengamma.engine.calcnode.port", Integer.toString(uri.getPort()));
        }
    } catch (URISyntaxException e) {
        s_logger.warn("Couldn't set connection defaults", e);
    }
}

From source file:com.amazonaws.util.AwsHostNameUtils.java

/**
 * Parses the service name from an endpoint. Can only handle endpoints of
 * the form 'service.[region.]amazonaws.com'.
 *
 * @deprecated because it's no longer needed by the SDK, and therefore not
 *             maintained./*from  ww  w  .j  av a2s . com*/
 */
@Deprecated
public static String parseServiceName(URI endpoint) {
    String host = endpoint.getHost();

    // If we don't recognize the domain, throw an exception.
    if (!host.endsWith(".amazonaws.com")) {
        throw new IllegalArgumentException(
                "Cannot parse a service name from an unrecognized endpoint (" + host + ").");
    }

    String serviceAndRegion = host.substring(0, host.indexOf(".amazonaws.com"));

    // Special cases for S3 endpoints with bucket names embedded.
    if (serviceAndRegion.endsWith(".s3") || S3_ENDPOINT_PATTERN.matcher(serviceAndRegion).matches()) {
        return "s3";
    }

    char separator = '.';

    // If we don't detect a separator between service name and region, then
    // assume that the region is not included in the hostname, and it's only
    // the service name (ex: "http://iam.amazonaws.com").
    if (serviceAndRegion.indexOf(separator) == -1) {
        return serviceAndRegion;
    }

    String service = serviceAndRegion.substring(0, serviceAndRegion.indexOf(separator));

    return service;
}