Example usage for java.net InetAddress getByName

List of usage examples for java.net InetAddress getByName

Introduction

In this page you can find the example usage for java.net InetAddress getByName.

Prototype

public static InetAddress getByName(String host) throws UnknownHostException 

Source Link

Document

Determines the IP address of a host, given the host's name.

Usage

From source file:co.cask.common.security.server.ExternalAuthenticationServerTest.java

@BeforeClass
public static void beforeClass() throws Exception {
    SecurityConfiguration cConf = SecurityConfiguration.create();
    cConf.set(Constants.SSL_ENABLED, "false");
    configuration = cConf;/* w  w w  .j  ava 2  s  . co m*/
    ldapListenerConfig = InMemoryListenerConfig.createLDAPConfig("LDAP", InetAddress.getByName("127.0.0.1"),
            ldapPort, null);
    setup();
}

From source file:org.archive.modules.deciderules.ExternalGeoLocationDecideRule.java

@Override
protected boolean evaluate(CrawlURI uri) {
    ExternalGeoLookupInterface impl = getLookup();
    if (impl == null) {
        return false;
    }/* w  w  w.j a  v  a  2  s .c  om*/
    CrawlHost crawlHost = null;
    String host;
    InetAddress address;
    try {
        host = uri.getUURI().getHost();
        crawlHost = serverCache.getHostFor(host);
        if (crawlHost.getCountryCode() != null) {
            return countryCodes.contains(crawlHost.getCountryCode());
        }
        address = crawlHost.getIP();
        if (address == null) {
            // TODO: handle transient lookup failures better
            address = Address.getByName(host);
        }
        crawlHost.setCountryCode((String) impl.lookup(address));
        if (countryCodes.contains(crawlHost.getCountryCode())) {
            LOGGER.fine("Country Code Lookup: " + " " + host + crawlHost.getCountryCode());
            return true;
        }
    } catch (UnknownHostException e) {
        LOGGER.log(Level.FINE, "Failed dns lookup " + uri, e);
        if (crawlHost != null) {
            crawlHost.setCountryCode("--");
        }
    } catch (URIException e) {
        LOGGER.log(Level.FINE, "Failed to parse hostname " + uri, e);
    }

    return false;
}

From source file:com.fuensalida.utils.FechaExternaNTP.java

public static Date getNTPDateROA() {
    String servidor = "minuto.roa.es";
    //Se le da un valor nulo por defecto a la variable

    Date fechaRecibida = null;//  w  w w . j  a v  a2  s.  c  o m
    //Se crea un objeto de tipo NTPUDPClient Clase de la libreria Commons

    NTPUDPClient cliente = new NTPUDPClient();
    //Tiempo de Espera Antes De Mandar Error.

    cliente.setDefaultTimeout(5000);
    try {
        //Obtenemos la direccion IP por medio de el host previamente Asignado

        InetAddress hostAddr = InetAddress.getByName(servidor);
        //Solicitamos la fecha al servidor

        TimeInfo fecha = cliente.getTime(hostAddr);
        //Recibimos y convertimos la fecha a formato DATE

        fechaRecibida = new Date(fecha.getMessage().getTransmitTimeStamp().getTime());
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
    //Cerramos la comunicacin con el cliente

    cliente.close();
    //Retornamos la fecha ya convertida si no es nula , de ser nula regresa la fecha Local

    return fechaRecibida == null ? new Date() : fechaRecibida;

}

From source file:co.cask.cdap.security.server.ExternalAuthenticationServerTest.java

@BeforeClass
public static void beforeClass() throws Exception {
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.Security.AUTH_SERVER_BIND_ADDRESS, "127.0.0.1");
    cConf.set(Constants.Security.SSL_ENABLED, "false");
    cConf.set(Constants.Security.AUTH_SERVER_BIND_PORT, "0");

    configuration = cConf;/*  w w w .  ja v a2  s .  c  o m*/
    sConfiguration = SConfiguration.create();

    ldapListenerConfig = InMemoryListenerConfig.createLDAPConfig("LDAP", InetAddress.getByName("127.0.0.1"),
            ldapPort, null);
    setup();
}

From source file:com.cyberway.issue.crawler.deciderules.ExternalGeoLocationDecideRule.java

protected boolean evaluate(Object obj) {
    ExternalGeoLookupInterface impl = getConfiguredImplementation(obj);
    if (impl == null) {
        return false;
    }//from  w w  w.jav  a 2 s .c  o m
    CrawlHost crawlHost = null;
    String host;
    InetAddress address;
    try {
        if (obj instanceof CandidateURI) {
            host = ((CandidateURI) obj).getUURI().getHost();
            crawlHost = getSettingsHandler().getOrder().getController().getServerCache().getHostFor(host);
            if (crawlHost.getCountryCode() != null) {
                return (crawlHost.getCountryCode().equals(countryCode)) ? true : false;
            }
            address = crawlHost.getIP();
            if (address == null) {
                address = Address.getByName(host);
            }
            crawlHost.setCountryCode((String) impl.lookup(address));
            if (crawlHost.getCountryCode().equals(countryCode)) {
                LOGGER.fine("Country Code Lookup: " + " " + host + crawlHost.getCountryCode());
                return true;
            }
        }
    } catch (UnknownHostException e) {
        LOGGER.log(Level.FINE, "Failed dns lookup " + obj, e);
        if (crawlHost != null) {
            crawlHost.setCountryCode(DEFAULT_COUNTRY_CODE);
        }
    } catch (URIException e) {
        LOGGER.log(Level.FINE, "Failed to parse hostname " + obj, e);
    }

    return false;
}

From source file:experts.net.nic.MACAddr.java

/**
 * Obtains the hardware address (usually MAC) of an interface which is from an IP address or a host name (e.g. "10.0.0.1" or "example").
 *
 * @param address//  ww  w  . jav a 2  s . c  o m
 *            the String of the IP address or the host name
 * @return a byte array contains the address
 * @throws java.net.SocketException
 * @throws java.net.UnknownHostException
 */
public static byte[] getMACAddress(String address) throws SocketException, UnknownHostException {
    // Stored in a NIC network interface corresponding to the IP address or the host name
    NetworkInterface nic = NetworkInterface.getByInetAddress(InetAddress.getByName(address));

    // Returns byte[] the hardware address
    return nic.getHardwareAddress();
}

From source file:Main.java

static InetAddress parseLinuxSubnetMask(String line) throws UnknownHostException {
    int e = line.indexOf("broadcast");
    if (e == -1)//from  w  w w  . j a  v a 2s  .c o  m
        return null;
    e--;
    String v = line.substring(line.indexOf("netmask") + 8, e);
    if (v.startsWith("0x")) {
        try {
            long address = Long.parseLong(v.substring(2), 16);
            return InetAddress.getByAddress(new byte[] { (byte) (address >> 24),
                    (byte) ((address >> 16) & 0xff), (byte) ((address >> 8) & 0xff), (byte) (address & 0xff) });
        } catch (NumberFormatException e2) {
        }
    } else {
        return InetAddress.getByName(v);
    }
    return null;
}

From source file:Main.java

public static DatagramPacket getPacket(String msg) throws UnknownHostException {
    int PACKET_MAX_LENGTH = 1024;
    byte[] msgBuffer = msg.getBytes();

    int length = msgBuffer.length;
    if (length > PACKET_MAX_LENGTH) {
        length = PACKET_MAX_LENGTH;//www .  j a  v  a  2s.co  m
    }
    DatagramPacket packet = new DatagramPacket(msgBuffer, length);
    InetAddress serverIPAddress = InetAddress.getByName("localhost");
    packet.setAddress(serverIPAddress);
    packet.setPort(15900);
    return packet;
}

From source file:com.predic8.membrane.core.interceptor.acl.Hostname.java

private static InetAddress initV6() {
    try {/*from w w  w  . j av a 2  s  . c om*/
        //could this fail if the machine has no IPv6 support?
        return InetAddress.getByName("0:0:0:0:0:0:0:1");
    } catch (UnknownHostException e) {
        e.printStackTrace(); //if you must... maybe logging framework is not set up yet.
        log.error("Failed resolving localhost IPv6 0:0:0:0:0:0:0:1!");
        return null;
    }
}

From source file:it.holiday69.phpwebserver.httpd.fastcgi.impl.PoolFactory.java

/**
 * build a ConnDesc from a "host:port" string.
 * @param s//  w ww. j a va  2 s .  com
 * @return
 */
private static ConnDesc makeConndesc(String s) {
    Matcher m = Pattern.compile("([^:]+):([1-9][0-9]*)$").matcher(s);
    if (m.matches()) {
        try {
            InetAddress addr = InetAddress.getByName(m.group(1));
            int port = Integer.parseInt(m.group(2));
            return new ConnDesc(addr, port);
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException(e);
        }
    } else
        throw new IllegalArgumentException(s);
}