Example usage for org.apache.commons.net.whois WhoisClient disconnect

List of usage examples for org.apache.commons.net.whois WhoisClient disconnect

Introduction

In this page you can find the example usage for org.apache.commons.net.whois WhoisClient disconnect.

Prototype

public void disconnect() throws IOException 

Source Link

Document

Disconnects the socket connection.

Usage

From source file:examples.fwhois.java

public static final void main(String[] args) {
    int index;//  w  w w.j  a v a 2s.  c om
    String handle, host;
    InetAddress address = null;
    WhoisClient whois;

    if (args.length != 1) {
        System.err.println("usage: fwhois handle[@<server>]");
        System.exit(1);
    }

    index = args[0].lastIndexOf("@");

    whois = new WhoisClient();
    // We want to timeout if a response takes longer than 60 seconds
    whois.setDefaultTimeout(60000);

    if (index == -1) {
        handle = args[0];
        host = WhoisClient.DEFAULT_HOST;
    } else {
        handle = args[0].substring(0, index);
        host = args[0].substring(index + 1);
    }

    try {
        address = InetAddress.getByName(host);
    } catch (UnknownHostException e) {
        System.err.println("Error unknown host: " + e.getMessage());
        System.exit(1);
    }

    System.out.println("[" + address.getHostName() + "]");

    try {
        whois.connect(address);
        System.out.print(whois.query(handle));
        whois.disconnect();
    } catch (IOException e) {
        System.err.println("Error I/O exception: " + e.getMessage());
        System.exit(1);
    }
}

From source file:examples.unix.fwhois.java

public static void main(String[] args) {
    int index;/*from  www. j a v a2 s .c o  m*/
    String handle, host;
    InetAddress address = null;
    WhoisClient whois;

    if (args.length != 1) {
        System.err.println("usage: fwhois handle[@<server>]");
        System.exit(1);
    }

    index = args[0].lastIndexOf("@");

    whois = new WhoisClient();
    // We want to timeout if a response takes longer than 60 seconds
    whois.setDefaultTimeout(60000);

    if (index == -1) {
        handle = args[0];
        host = WhoisClient.DEFAULT_HOST;
    } else {
        handle = args[0].substring(0, index);
        host = args[0].substring(index + 1);
    }

    try {
        address = InetAddress.getByName(host);
        System.out.println("[" + address.getHostName() + "]");
    } catch (UnknownHostException e) {
        System.err.println("Error unknown host: " + e.getMessage());
        System.exit(1);
    }

    try {
        whois.connect(address);
        System.out.print(whois.query(handle));
        whois.disconnect();
    } catch (IOException e) {
        System.err.println("Error I/O exception: " + e.getMessage());
        System.exit(1);
    }
}

From source file:com.vish.phishDetect.Web_Utilities.java

public static int domainAge(String domainName) {
    StringBuilder whoisResult = new StringBuilder("");

    WhoisClient crunchifyWhois = new WhoisClient();
    try {//  w w  w  . j  a v a 2s .c om
        // The WhoisClient class implements the client side of the Internet
        // Whois Protocol defined in RFC 954. To query a host you create a
        // WhoisClient instance, connect to the host, query the host, and
        // finally disconnect from the host. If the whois service you want
        // to query is on a non-standard port, connect to the host at that
        // port.
        crunchifyWhois.connect(WhoisClient.DEFAULT_HOST);
        String whoisData = crunchifyWhois.query("=" + domainName);
        whoisResult.append(whoisData);
        crunchifyWhois.disconnect();

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String patternString1 = "No\\smatch\\sfor";
    Pattern pattern = Pattern.compile(patternString1);
    Matcher matcher = pattern.matcher(whoisResult);
    if (!matcher.find()) {
        System.out.println(domainName + "\n" + whoisResult);
    }

    return 0;//whoisResult.toString();
}

From source file:at.tlphotography.jAbuseReport.Reporter.java

/**
 * Whois look up./*from  w  w  w  .  java2 s . c om*/
 *
 * @param ip
 *          the ip
 * @return the string
 */
private static String whoIsLookUp(String ip) {
    String[] serverList = { "whois.ripe.net", "whois.lacnic.net", "whois.registro.br" };

    WhoisClient whois = new WhoisClient();
    try {
        for (String server : serverList) {
            whois.connect(server);
            String whoisData1 = whois.query(ip);
            StringBuilder result = new StringBuilder("");
            ;
            result.append(whoisData1);

            String mail = extractEMail(result.toString());

            if (mail != null)
                return mail;
        }
        return "not found";
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (whois.isConnected())
                whois.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.ecrimebureau.Web.Entity.WhoisS.java

private String queryWithWhoisServer(String domainName, String whoisServer) {

    String result = "";
    WhoisClient whois = new WhoisClient();
    try {//  www .j  a v  a 2s.  co m

        whois.connect(whoisServer);
        result = whois.query(domainName);
        whois.disconnect();

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;

}

From source file:com.thunder.network.WhoisCommand.java

@Override
public void runCommand(final String host, final OnPartialResultListener listener) {
    new Thread(new Runnable() {
        @Override//from w  w w.jav a 2  s.  c  om
        public void run() {
            StringBuilder result = new StringBuilder("");
            WhoisClient whois = new WhoisClient();
            try {
                whois.connect(WhoisClient.DEFAULT_HOST);
                String whoisData1 = whois.query("=" + host);
                result.append(whoisData1);
                listener.onPartialResult(result.toString());
                listener.onFinish();
                whois.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                listener.onError(e);
            }

        }
    }).start();
}

From source file:com.ecrimebureau.Web.Entity.WhoisS.java

public String getWhois(String domainName) {

    StringBuilder result = new StringBuilder("");

    WhoisClient whois = new WhoisClient();
    try {//w  w w. j av  a2  s .  co  m

        whois.connect(WhoisClient.DEFAULT_HOST);

        // whois =google.com
        String whoisData1 = whois.query("=" + domainName);

        // append first result
        result.append(whoisData1);
        whois.disconnect();

        // get the google.com whois server - whois.markmonitor.com
        String whoisServerUrl = getWhoisServer(whoisData1);
        if (!whoisServerUrl.equals("")) {

            // whois -h whois.markmonitor.com google.com
            String whoisData2 = queryWithWhoisServer(domainName, whoisServerUrl);

            // append 2nd result
            result.append(whoisData2);
        }

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result.toString();

}

From source file:net.spfbl.core.Server.java

/**
 * Consulta de identificao no WHOIS./*from w w w .ja  v  a2  s  .co  m*/
 * Controla a taxa de 30 consultas no intervalo de 24 horas.
 * @param query a consulta a ser realizada.
 * @param server o servidor que contm a informao.
 * @return o resultado do WHOIS para a consulta.
 * @throws ProcessException se houver falha no processamento da informao.
 */
public static String whoisID(String query, String server) throws ProcessException {
    long time = System.currentTimeMillis();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        if (WHOIS_CONNECTION_SEMAPHORE.tryAcquire()) {
            try {
                acquireWhoisIDQuery();
                WhoisClient whoisClient = new WhoisClient();
                try {
                    whoisClient.setDefaultTimeout(3000);
                    whoisClient.connect(server);
                    InputStream inputStream = whoisClient.getInputStream(query);
                    int code;
                    while ((code = inputStream.read()) != -1) {
                        outputStream.write(code);
                    }
                } finally {
                    whoisClient.disconnect();
                }
            } finally {
                WHOIS_CONNECTION_SEMAPHORE.release();
            }
        } else {
            throw new ProcessException("ERROR: TOO MANY CONNECTIONS");
        }
    } catch (ProcessException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new ProcessException("ERROR: WHOIS CONNECTION FAIL", ex);
    }
    try {
        String result = outputStream.toString("ISO-8859-1");
        logWhois(time, server, query, result);
        return result;
    } catch (UnsupportedEncodingException ex) {
        throw new ProcessException("ERROR: ENCODING", ex);
    }
}

From source file:net.spfbl.core.Server.java

/**
 * Consulta comum no WHOIS.//from w w  w .  j  av  a2  s. c o  m
 * Controla a taxa de 30 consultas no intervalo de 5 minutos.
 * @param query a consulta a ser realizada.
 * @param server o servidor que contm a informao.
 * @return o resultado do WHOIS para a consulta.
 * @throws ProcessException se houver falha no processamento da informao.
 */
public static String whois(String query, String server) throws ProcessException {
    long time = System.currentTimeMillis();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        if (WHOIS_CONNECTION_SEMAPHORE.tryAcquire()) {
            try {
                acquireWhoisQuery();
                WhoisClient whoisClient = new WhoisClient();
                try {
                    whoisClient.setDefaultTimeout(3000);
                    whoisClient.connect(server);
                    InputStream inputStream = whoisClient.getInputStream(query);
                    try {
                        int code;
                        while ((code = inputStream.read()) != -1) {
                            outputStream.write(code);
                        }
                    } finally {
                        inputStream.close();
                    }
                } finally {
                    whoisClient.disconnect();
                }
            } finally {
                WHOIS_CONNECTION_SEMAPHORE.release();
            }
        } else {
            throw new ProcessException("ERROR: TOO MANY CONNECTIONS");
        }
    } catch (ProcessException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new ProcessException("ERROR: WHOIS CONNECTION FAIL", ex);
    }
    try {
        String result = outputStream.toString("ISO-8859-1");
        result = result.replace("\r", "");
        logWhois(time, server, query, result);
        return result;
    } catch (UnsupportedEncodingException ex) {
        throw new ProcessException("ERROR: ENCODING", ex);
    }
}

From source file:org.apache.common.net.examples.unix.fwhois.java

public static final void main(String[] args) {
    int index;//  ww w  .  j  av  a  2s.  c o  m
    String handle, host;
    InetAddress address = null;
    WhoisClient whois;

    if (args.length != 1) {
        System.err.println("usage: fwhois handle[@<server>]");
        System.exit(1);
    }

    index = args[0].lastIndexOf("@");

    whois = new WhoisClient();
    // We want to timeout if a response takes longer than 60 seconds
    whois.setDefaultTimeout(60000);

    if (index == -1) {
        handle = args[0];
        host = WhoisClient.DEFAULT_HOST;
    } else {
        handle = args[0].substring(0, index);
        host = args[0].substring(index + 1);
    }

    try {
        address = InetAddress.getByName(host);
        System.out.println("[" + address.getHostName() + "]");
    } catch (UnknownHostException e) {
        System.err.println("Error unknown host: " + e.getMessage());
        System.exit(1);
    }

    try {
        whois.connect(address);
        System.out.print(whois.query(handle));
        whois.disconnect();
    } catch (IOException e) {
        System.err.println("Error I/O exception: " + e.getMessage());
        System.exit(1);
    }
}