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

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

Introduction

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

Prototype

public boolean isConnected() 

Source Link

Document

Returns true if the client is currently connected to a server.

Usage

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

/**
 * Whois look up./*  w  w  w .  ja v  a 2  s  . com*/
 *
 * @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:org.archive.modules.fetcher.FetchWhois.java

protected void fetch(CrawlURI curi, String whoisServer, String whoisQuery) {
    WhoisClient client = new WhoisClient();
    Recorder recorder = curi.getRecorder();

    try {/*ww w.j a  va2  s  .com*/
        client.setConnectTimeout(getSoTimeoutMs());
        client.setDefaultTimeout(getSoTimeoutMs());

        if (curi.getUURI().getPort() > 0) {
            client.connect(whoisServer, curi.getUURI().getPort());
        } else {
            client.connect(whoisServer);
        }

        client.setSoTimeout(getSoTimeoutMs()); // must be after connect()

        curi.getData().put(CoreAttributeConstants.A_WHOIS_SERVER_IP,
                client.getRemoteAddress().getHostAddress());

        recorder.inputWrap(client.getInputStream(whoisQuery));

        // look for info about whois server in the response
        // XXX run regex on the whole thing, rather than line by line?
        BufferedReader reader = new BufferedReader(new InputStreamReader(recorder.getRecordedInput(), "ASCII"));
        for (String line = reader.readLine(); line != null; line = reader.readLine()) {
            Matcher matcher = TextUtils.getMatcher(WHOIS_SERVER_REGEX, line);
            if (matcher.find()) {
                // gets rid of "domain " for whois.verisign-grs.com queries
                String key = whoisQuery.replaceFirst("(\\S+\\s+)+", "").toLowerCase();
                referralServers.put(key, matcher.group(1).toLowerCase());
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("added referral server " + matcher.group(1) + " to server list for " + key);
                }
            }
        }

        curi.setContentType("text/plain");
        curi.setFetchStatus(S_WHOIS_SUCCESS);
    } catch (IOException e) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("failed to connect to whois server for uri " + curi + ": " + e);
        }
        curi.getNonFatalFailures().add(e);
        curi.setFetchStatus(S_CONNECT_FAILED);
    } finally {
        recorder.close();
        curi.setContentSize(recorder.getRecordedInput().getSize());
        logger.fine(curi + ": " + recorder.getRecordedInput().getSize() + " bytes read");

        if (client != null && client.isConnected())
            try {
                client.disconnect();
            } catch (IOException e) {
                logger.fine("problem closing connection to whois server for uri " + curi + ": " + e);
            }

        urlProgress.put(curi.toString(), UrlStatus.DONE.ordinal());
    }
}