Example usage for java.net InetSocketAddress isUnresolved

List of usage examples for java.net InetSocketAddress isUnresolved

Introduction

In this page you can find the example usage for java.net InetSocketAddress isUnresolved.

Prototype

public final boolean isUnresolved() 

Source Link

Document

Checks whether the address has been resolved or not.

Usage

From source file:Main.java

public static void printSocketAddress(InetSocketAddress sAddr) {
    System.out.println("Socket  Address: " + sAddr.getAddress());
    System.out.println("Socket Host  Name: " + sAddr.getHostName());
    System.out.println("Socket Port:  " + sAddr.getPort());
    System.out.println("isUnresolved(): " + sAddr.isUnresolved());
    System.out.println();/*  w  ww.  jav a  2  s  .  c o  m*/
}

From source file:Main.java

public static void checkSocketAddress(final InetSocketAddress socketAddress) {
    if (socketAddress == null) {
        throw new IllegalArgumentException("socketAddress can't be null");
    }/*from  ww w.j  ava2s .c  o  m*/
    /** can not use in JNI ; internal InetAddress field is null */
    if (socketAddress.isUnresolved()) {
        throw new IllegalArgumentException(
                "socketAddress is unresolved : " + socketAddress + " : check your DNS settings");
    }
}

From source file:com.evilisn.DAO.CertMapper.java

public static byte[] httpGetBin(URI uri, boolean bActiveCheckUnknownHost) throws Exception

{

    InputStream is = null;/*from  w  w  w  . ja  v  a2s  .  co  m*/

    InputStream is_temp = null;

    try {

        if (uri == null)
            return null;

        URL url = uri.toURL();

        if (bActiveCheckUnknownHost) {

            url.getProtocol();

            String host = url.getHost();

            int port = url.getPort();

            if (port == -1)

                port = url.getDefaultPort();

            InetSocketAddress isa = new InetSocketAddress(host, port);

            if (isa.isUnresolved()) {

                //fix JNLP popup error issue

                throw new UnknownHostException("Host Unknown:" + isa.toString());

            }

        }

        HttpURLConnection uc = (HttpURLConnection) url.openConnection();

        uc.setDoInput(true);

        uc.setAllowUserInteraction(false);

        uc.setInstanceFollowRedirects(true);

        setTimeout(uc);

        String contentEncoding = uc.getContentEncoding();

        int len = uc.getContentLength();

        // is = uc.getInputStream();

        if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("gzip") != -1)

        {

            is_temp = uc.getInputStream();

            is = new GZIPInputStream(is_temp);

        }

        else if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("deflate") != -1)

        {

            is_temp = uc.getInputStream();

            is = new InflaterInputStream(is_temp);

        }

        else

        {

            is = uc.getInputStream();

        }

        if (len != -1) {

            int ch = 0, i = 0;

            byte[] res = new byte[len];

            while ((ch = is.read()) != -1) {

                res[i++] = (byte) (ch & 0xff);

            }

            return res;

        } else {

            ArrayList<byte[]> buffer = new ArrayList<byte[]>();

            int buf_len = 1024;

            byte[] res = new byte[buf_len];

            int ch = 0, i = 0;

            while ((ch = is.read()) != -1) {

                res[i++] = (byte) (ch & 0xff);

                if (i == buf_len) {

                    //rotate

                    buffer.add(res);

                    i = 0;

                    res = new byte[buf_len];

                }

            }

            int total_len = buffer.size() * buf_len + i;

            byte[] buf = new byte[total_len];

            for (int j = 0; j < buffer.size(); j++) {

                System.arraycopy(buffer.get(j), 0, buf, j * buf_len, buf_len);

            }

            if (i > 0) {

                System.arraycopy(res, 0, buf, buffer.size() * buf_len, i);

            }

            return buf;

        }

    } catch (Exception e) {

        e.printStackTrace();

        return null;

    } finally {

        closeInputStream(is_temp);

        closeInputStream(is);

    }

}

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

/**
 * Construct the service key for a token
 *
 * @param addr InetSocketAddress of remote connection with a token
 *
 * @return "ip:port" or "host:port" depending on the value of hadoop.security.token.service.use_ip
 *//*from   w w w . ja va2 s  .c  o m*/
public static Text buildTokenService(InetSocketAddress addr) {
    String host = null;
    if (useIpForTokenService) {
        if (addr.isUnresolved()) { // host has no ip address
            throw new IllegalArgumentException(new UnknownHostException(addr.getHostName()));
        }
        host = addr.getAddress().getHostAddress();
    } else {
        host = StringUtils.toLowerCase(addr.getHostName());
    }
    return new Text(host + ":" + addr.getPort());
}

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

/**
 * Returns an InetSocketAddress that a client can use to connect to the given listening address.
 *
 * @param addr of a listener/*from  w  ww  .j a  v a2 s.  c o m*/
 *
 * @return socket address that a client can use to connect to the server.
 */
public static InetSocketAddress getConnectAddress(InetSocketAddress addr) {
    if (!addr.isUnresolved() && addr.getAddress().isAnyLocalAddress()) {
        try {
            addr = new InetSocketAddress(InetAddress.getLocalHost(), addr.getPort());
        } catch (UnknownHostException uhe) {
            // shouldn't get here unless the host doesn't have a loopback iface
            addr = createSocketAddrForHost("127.0.0.1", addr.getPort());
        }
    }
    return addr;
}

From source file:net.firejack.platform.web.cache.MemcachedClientFactory.java

@Override
public MemcachedClient makeObject() throws Exception {
    MemcachedClient client;//from ww  w  .j  a v a2  s .c  o  m
    if (StringUtils.isNotBlank(memcachedServerUrl) && port != null) {
        logger.info("Adding new memcached client to pool.");
        try {
            InetSocketAddress inetSocketAddress = new InetSocketAddress(memcachedServerUrl, port);
            if (inetSocketAddress.isUnresolved()) {
                client = null;
                logger.error("Memcached server hostname [" + memcachedServerUrl + "] is unresolved.");
            } else {
                List<InetSocketAddress> addresses = new ArrayList<InetSocketAddress>();
                addresses.add(inetSocketAddress);

                client = new MemcachedClient(new OpenFlameMemcachedConnectionFactory(), addresses);
            }
        } catch (IOException e) {
            logger.error("Application failed to obtain connection to memcached server.");
            throw new OpenFlameRuntimeException(e.getMessage(), e);
        }
    } else {
        logger.warn("Memcached server url or port is not set.");
        client = null;
    }

    return client;
}

From source file:org.hydracache.server.httpd.AsyncHttpLightServerTest.java

@Test
public void testBuildInetAddressWithIp() {
    AsyncHttpLightServer server = new AsyncHttpLightServer(id, null, null, TEST_IP, TEST_PORT);

    InetSocketAddress address = server.buildInetSocketAddress();

    assertFalse(address.isUnresolved());
    assertFalse(address.getAddress().isAnyLocalAddress());
    assertEquals(TEST_IP, address.getAddress().getHostAddress());
}

From source file:com.marvelution.hudson.plugins.apiv2.client.connectors.HttpClient3Connector.java

/**
 * Obtains a host from an {@link InetSocketAddress}.
 * //from  w w  w .j a  v  a  2s.  c  o  m
 * @param isa the socket address
 * @return a host string, either as a symbolic name or as a literal IP address string
 */
protected String getHost(InetSocketAddress isa) {
    return isa.isUnresolved() ? isa.getHostName() : isa.getAddress().getHostAddress();
}

From source file:name.persistent.behaviours.RemoteDomainSupport.java

private InetSocketAddress pickService(List<Service> services) {
    int total = 0;
    List<InetSocketAddress> addresses = new ArrayList<InetSocketAddress>();
    for (int i = 0, n = services.size(); i < n; i++) {
        addresses.add(null);//from   w ww .  j av  a  2s.c  om
        Service srv = services.get(i);
        Object url = srv.getPurlServer();
        if (url == null)
            continue;
        ParsedURI parsed = new ParsedURI(((RDFObject) url).getResource().stringValue());
        int port = "https".equalsIgnoreCase(parsed.getScheme()) ? 443 : 80;
        InetSocketAddress server = resolve(parsed.getAuthority(), port);
        if (isBlackListed(server) || server.isUnresolved())
            continue;
        addresses.set(i, server);
        Number weight = srv.getPurlWeight();
        total += weight == null ? 1 : weight.intValue();
    }
    total = random(total);
    for (int i = 0, n = services.size(); i < n; i++) {
        Service srv = services.get(i);
        if (addresses.get(i) == null)
            continue;
        Number weight = srv.getPurlWeight();
        total -= weight == null ? 1 : weight.intValue();
        if (total < 0) {
            return addresses.get(i);
        }
    }
    return null;
}

From source file:IntergrationTest.OCSPIntegrationTest.java

private byte[] httpGetBin(URI uri, boolean bActiveCheckUnknownHost) throws Exception {
    InputStream is = null;/* w  w w .j  a v a  2 s  .c o m*/
    InputStream is_temp = null;
    try {
        if (uri == null)
            return null;
        URL url = uri.toURL();
        if (bActiveCheckUnknownHost) {
            url.getProtocol();
            String host = url.getHost();
            int port = url.getPort();
            if (port == -1)
                port = url.getDefaultPort();
            InetSocketAddress isa = new InetSocketAddress(host, port);
            if (isa.isUnresolved()) {
                //fix JNLP popup error issue
                throw new UnknownHostException("Host Unknown:" + isa.toString());
            }

        }
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setDoInput(true);
        uc.setAllowUserInteraction(false);
        uc.setInstanceFollowRedirects(true);
        setTimeout(uc);
        String contentEncoding = uc.getContentEncoding();
        int len = uc.getContentLength();
        // is = uc.getInputStream();
        if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("gzip") != -1) {
            is_temp = uc.getInputStream();
            is = new GZIPInputStream(is_temp);

        } else if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("deflate") != -1) {
            is_temp = uc.getInputStream();
            is = new InflaterInputStream(is_temp);

        } else {
            is = uc.getInputStream();

        }
        if (len != -1) {
            int ch, i = 0;
            byte[] res = new byte[len];
            while ((ch = is.read()) != -1) {
                res[i++] = (byte) (ch & 0xff);

            }
            return res;

        } else {
            ArrayList<byte[]> buffer = new ArrayList<>();
            int buf_len = 1024;
            byte[] res = new byte[buf_len];
            int ch, i = 0;
            while ((ch = is.read()) != -1) {
                res[i++] = (byte) (ch & 0xff);
                if (i == buf_len) {
                    //rotate
                    buffer.add(res);
                    i = 0;
                    res = new byte[buf_len];

                }

            }
            int total_len = buffer.size() * buf_len + i;
            byte[] buf = new byte[total_len];
            for (int j = 0; j < buffer.size(); j++) {
                System.arraycopy(buffer.get(j), 0, buf, j * buf_len, buf_len);

            }
            if (i > 0) {
                System.arraycopy(res, 0, buf, buffer.size() * buf_len, i);

            }
            return buf;

        }

    } catch (Exception e) {
        e.printStackTrace();
        return null;

    } finally {
        closeInputStream(is_temp);
        closeInputStream(is);

    }

}