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.ibm.mobilefirstplatform.clientsdk.android.security.mca.internal.Utils.java

/**
 * Builds rewrite domain from backend route url.
 *
 * @param backendRoute Backend route./* w  w w .ja v a2 s.c o m*/
 * @param subzone      Subzone
 * @return Rewrite domain.
 * @throws MalformedURLException if backendRoute parameter has invalid format.
 */
public static String buildRewriteDomain(String backendRoute, String subzone) throws MalformedURLException {
    if (backendRoute == null || backendRoute.isEmpty()) {
        logger.error("Backend route can't be null.");
        return null;
    }

    String applicationRoute = backendRoute;

    if (!applicationRoute.startsWith(BMSClient.HTTP_SCHEME)) {
        applicationRoute = String.format("%s://%s", BMSClient.HTTPS_SCHEME, applicationRoute);
    } else if (!applicationRoute.startsWith(BMSClient.HTTPS_SCHEME)
            && applicationRoute.contains(BLUEMIX_NAME)) {
        applicationRoute = applicationRoute.replace(BMSClient.HTTP_SCHEME, BMSClient.HTTPS_SCHEME);
    }

    URL url = new URL(applicationRoute);

    String host = url.getHost();
    String rewriteDomain;
    String regionInDomain = "ng";
    int port = url.getPort();

    String serviceUrl = String.format("%s://%s", url.getProtocol(), host);

    if (port != 0) {
        serviceUrl += ":" + String.valueOf(port);
    }

    String[] hostElements = host.split("\\.");

    if (!serviceUrl.contains(STAGE1_NAME)) {
        // Multi-region: myApp.eu-gb.mybluemix.net
        // US: myApp.mybluemix.net
        if (hostElements.length == 4) {
            regionInDomain = hostElements[hostElements.length - 3];
        }

        // this is production, because STAGE1 is not found
        // Multi-Region Eg: eu-gb.bluemix.net
        // US Eg: ng.bluemix.net
        rewriteDomain = String.format("%s.%s", regionInDomain, BLUEMIX_DOMAIN);
    } else {
        // Multi-region: myApp.stage1.eu-gb.mybluemix.net
        // US: myApp.stage1.mybluemix.net
        if (hostElements.length == 5) {
            regionInDomain = hostElements[hostElements.length - 3];
        }

        if (subzone != null && !subzone.isEmpty()) {
            // Multi-region Dev subzone Eg: stage1-Dev.eu-gb.bluemix.net
            // US Dev subzone Eg: stage1-Dev.ng.bluemix.net
            rewriteDomain = String.format("%s-%s.%s.%s", STAGE1_NAME, subzone, regionInDomain, BLUEMIX_DOMAIN);
        } else {
            // Multi-region Eg: stage1.eu-gb.bluemix.net
            // US  Eg: stage1.ng.bluemix.net
            rewriteDomain = String.format("%s.%s.%s", STAGE1_NAME, regionInDomain, BLUEMIX_DOMAIN);
        }
    }

    return rewriteDomain;
}

From source file:org.apache.gobblin.utils.HttpUtils.java

public static String createApacheHttpClientLimiterKey(Config config) {
    try {/*  ww  w.  ja va 2s . c  om*/
        String urlTemplate = config.getString(HttpConstants.URL_TEMPLATE);
        URL url = new URL(urlTemplate);
        String key = url.getProtocol() + "/" + url.getHost();
        if (url.getPort() > 0) {
            key = key + "/" + url.getPort();
        }
        log.info("Get limiter key [" + key + "]");
        return key;
    } catch (MalformedURLException e) {
        throw new IllegalStateException("Cannot get limiter key.", e);
    }
}

From source file:org.silvertunnel.netlib.adapter.httpclient.HttpClientUtil.java

public static byte[] simpleBytesAction(URL url) throws IOException {
    int port = (url.getPort() < 0) ? 80 : url.getPort();
    TcpipNetAddress httpServerNetAddress = new TcpipNetAddress(url.getHost(), port);
    Map<String, Object> localProperties = new HashMap<String, Object>();
    NetSocket lowerLayerNetSocket = lowerNetLayer.createNetSocket(localProperties, /*localAddress*/ null,
            httpServerNetAddress);/*from w w w . j av  a2  s . com*/
    String pathOnHttpServer = url.getPath();
    if (pathOnHttpServer == null || pathOnHttpServer.length() < 1) {
        pathOnHttpServer = "/";
    }
    long timeoutInMs = 10L * 1000L;

    return HttpUtil.getInstance().get(lowerLayerNetSocket, httpServerNetAddress, pathOnHttpServer, timeoutInMs);
}

From source file:org.silvertunnel.netlib.adapter.httpclient.HttpClientUtil.java

public static InputStream simpleAction(URL url) throws IOException {
    int port = (url.getPort() < 0) ? 80 : url.getPort();
    TcpipNetAddress httpServerNetAddress = new TcpipNetAddress(url.getHost(), port);
    Map<String, Object> localProperties = new HashMap<String, Object>();
    NetSocket lowerLayerNetSocket = lowerNetLayer.createNetSocket(localProperties, /*localAddress*/ null,
            httpServerNetAddress);//from www  .  j  ava  2 s  . co m
    String pathOnHttpServer = url.getPath();
    if (pathOnHttpServer == null || pathOnHttpServer.length() < 1) {
        pathOnHttpServer = "/";
    }
    long timeoutInMs = 10L * 1000L;

    return HttpUtil.getInstance().getReponseBodyInputStream(lowerLayerNetSocket, httpServerNetAddress,
            pathOnHttpServer, timeoutInMs);
}

From source file:com.rovemonteux.silvertunnel.netlib.adapter.httpclient.HttpClientUtil.java

public static InputStream simpleAction(final URL url) throws IOException {
    final int port = url.getPort() < 0 ? 80 : url.getPort();
    final TcpipNetAddress httpServerNetAddress = new TcpipNetAddress(url.getHost(), port);
    final Map<String, Object> localProperties = new HashMap<String, Object>();
    final NetSocket lowerLayerNetSocket = lowerNetLayer.createNetSocket(localProperties, /* localAddress */null,
            httpServerNetAddress);/*from   www  . j av a 2 s.  c  o m*/
    String pathOnHttpServer = url.getPath();
    if (pathOnHttpServer == null || pathOnHttpServer.length() < 1) {
        pathOnHttpServer = "/";
    }
    final long timeoutInMs = 10L * 1000L;

    return HttpUtil.getInstance().getReponseBodyInputStream(lowerLayerNetSocket, httpServerNetAddress,
            pathOnHttpServer, timeoutInMs);
}

From source file:com.rovemonteux.silvertunnel.netlib.adapter.httpclient.HttpClientUtil.java

public static byte[] simpleBytesAction(final URL url) throws IOException {
    final int port = url.getPort() < 0 ? 80 : url.getPort();
    final TcpipNetAddress httpServerNetAddress = new TcpipNetAddress(url.getHost(), port);
    final Map<String, Object> localProperties = new HashMap<String, Object>();
    final NetSocket lowerLayerNetSocket = lowerNetLayer.createNetSocket(localProperties, /* localAddress */null,
            httpServerNetAddress);/*from   ww w. j  a v  a2  s .  co m*/
    String pathOnHttpServer = url.getPath();
    if (pathOnHttpServer == null || pathOnHttpServer.length() < 1) {
        pathOnHttpServer = "/";
    }
    final long timeoutInMs = 10L * 1000L;

    HttpUtil.getInstance();
    return HttpUtil.get(lowerLayerNetSocket, httpServerNetAddress, pathOnHttpServer, timeoutInMs);
}

From source file:org.openimaj.web.scraping.images.ImgurClient.java

/**
 * @param url/*from  w ww.  j  av  a 2s  .  c  o  m*/
 * @return the imgur type and hash, or null if the URL was too tricky
 */
public static ImgurTypeHash imgurURLtoHash(URL url) {
    if (!url.getHost().contains("imgur"))
        return null;
    final String path = url.getPath();
    final String[] split = path.split("[/]+");
    if (split.length == 0)
        return null;
    else if (split.length == 2) {
        if (split[1].equals("gallery"))
            return new ImgurTypeHash(ImgurType.GALLERY, null);
        else {
            final Matcher matcher = hashPattern.matcher(split[1]);
            if (matcher.find()) {
                final String hash = split[1].substring(0, matcher.end());
                return new ImgurTypeHash(ImgurType.IMAGE, hash);
            }
            return null;
        }
    } else {
        final String hashPart = split[split.length - 1];
        final String typePart = split[split.length - 2];
        ImgurType type = ImgurType.IMAGE;
        if (typePart.equals("a"))
            type = ImgurType.ALBUM;

        final Matcher matcher = hashPattern.matcher(hashPart);
        matcher.find();
        final String hash = hashPart.substring(0, matcher.end());
        return new ImgurTypeHash(type, hash);
    }

}

From source file:Main.java

private static URL getRootUrlForClass(Class<?> cls) {
    ClassLoader classLoader = cls.getClassLoader();
    String resource = cls.getName().replace('.', '/') + ".class";
    if (classLoader == null) {
        // A null class loader means the bootstrap class loader. In this case we use the
        // system class loader. This is safe since we can assume that the system class
        // loader uses parent first as delegation policy.
        classLoader = ClassLoader.getSystemClassLoader();
    }/*from w w  w.  ja va  2 s  .co m*/
    URL url = classLoader.getResource(resource);
    if (url == null) {
        return null;
    }
    String file = url.getFile();
    if (file.endsWith(resource)) {
        try {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(),
                    file.substring(0, file.length() - resource.length()));
        } catch (MalformedURLException ex) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:wuit.crawler.searcher.Crawler.java

public static String getDNSByURL(String url) {
    String dns = "";
    try {/*from   w  ww  .  j  a va  2s.  c o  m*/
        URL _url = new URL(url);
        dns = _url.getHost() + "";

        if (dns.equals(""))
            return dns;
        String[] name = dns.split("\\.");
        String val = "";
        for (int i = 1; i < name.length; i++) {
            val = val + name[i] + ".";
        }
        dns = val.substring(0, val.length() - 1);

    } catch (Exception e) {
        System.out.println(" crawler   " + e.getMessage());
    }
    return dns;
}

From source file:com.qwazr.utils.HtmlUtils.java

public final static String urlHostPathWrapReduce(String url, int maxSize) {
     URL u;
     try {/*from  ww w . ja v a 2 s  . c  o  m*/
         u = new URL(url);
     } catch (MalformedURLException e) {
         return url;
     }
     String path = StringUtils.fastConcat(u.getHost(), '/', u.getPath());
     String[] frags = StringUtils.split(path, '/');
     if (frags.length < 2)
         return path;
     int startPos = 1;
     int endPos = frags.length - 2;
     StringBuilder sbStart = new StringBuilder(frags[0]);
     StringBuilder sbEnd = new StringBuilder(frags[frags.length - 1]);
     int length = sbStart.length() + sbEnd.length();
     for (;;) {
         boolean bHandled = false;
         if (startPos != -1 && startPos < endPos) {
             if (frags[startPos].length() + length < maxSize) {
                 sbStart.append('/');
                 sbStart.append(frags[startPos++]);
                 bHandled = true;
             }
         }
         if (endPos != -1 && endPos > startPos) {
             if (frags[endPos].length() + length < maxSize) {
                 sbEnd.insert(0, '/');
                 sbEnd.insert(0, frags[endPos--]);
                 bHandled = true;
             }
         }
         if (!bHandled)
             break;
     }
     return StringUtils.fastConcat(sbStart, "//", sbEnd);
 }