Example usage for org.apache.commons.net.nntp NNTPClient isConnected

List of usage examples for org.apache.commons.net.nntp NNTPClient isConnected

Introduction

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

Prototype

public boolean isConnected() 

Source Link

Document

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

Usage

From source file:examples.nntp.newsgroups.java

public final static void main(String[] args) {
    NNTPClient client;
    NewsgroupInfo[] list;/* w w  w . j a  v  a  2s. c o m*/

    if (args.length < 1) {
        System.err.println("Usage: newsgroups newsserver");
        System.exit(1);
    }

    client = new NNTPClient();

    try {
        client.connect(args[0]);

        list = client.listNewsgroups();

        if (list != null) {
            for (int i = 0; i < list.length; i++)
                System.out.println(list[i].getNewsgroup());
        } else {
            System.err.println("LIST command failed.");
            System.err.println("Server reply: " + client.getReplyString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (client.isConnected())
                client.disconnect();
        } catch (IOException e) {
            System.err.println("Error disconnecting from server.");
            e.printStackTrace();
            System.exit(1);
        }
    }

}

From source file:examples.nntp.ListNewsgroups.java

public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("Usage: newsgroups newsserver [pattern]");
        return;//  www .  j  av a  2  s .  c  om
    }

    NNTPClient client = new NNTPClient();
    String pattern = args.length >= 2 ? args[1] : "";

    try {
        client.connect(args[0]);

        int j = 0;
        try {
            for (String s : client.iterateNewsgroupListing(pattern)) {
                j++;
                System.out.println(s);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        System.out.println(j);

        j = 0;
        for (NewsgroupInfo n : client.iterateNewsgroups(pattern)) {
            j++;
            System.out.println(n.getNewsgroup());
        }
        System.out.println(j);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (client.isConnected()) {
                client.disconnect();
            }
        } catch (IOException e) {
            System.err.println("Error disconnecting from server.");
            e.printStackTrace();
            System.exit(1);
        }
    }

}

From source file:org.apache.common.net.examples.nntp.ListNewsgroups.java

public final static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("Usage: newsgroups newsserver [pattern]");
        return;/*w  w w  .ja  va 2s  . co m*/
    }

    NNTPClient client = new NNTPClient();
    String pattern = args.length >= 2 ? args[1] : "";

    try {
        client.connect(args[0]);

        int j = 0;
        try {
            for (String s : client.iterateNewsgroupListing(pattern)) {
                j++;
                System.out.println(s);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        System.out.println(j);

        j = 0;
        for (NewsgroupInfo n : client.iterateNewsgroups(pattern)) {
            j++;
            System.out.println(n.getNewsgroup());
        }
        System.out.println(j);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (client.isConnected()) {
                client.disconnect();
            }
        } catch (IOException e) {
            System.err.println("Error disconnecting from server.");
            e.printStackTrace();
            System.exit(1);
        }
    }

}

From source file:org.rssowl.contrib.internal.nntp.NewsGroupHandler.java

public Pair<IFeed, IConditionalGet> reload(URI link, IProgressMonitor monitor, Map<Object, Object> properties)
        throws CoreException {
    IModelFactory factory = Owl.getModelFactory();

    /* Create a new empty feed from the existing one */
    IFeed feed = factory.createFeed(null, link);

    /* Retrieve last article pointer */
    Integer lastArticleId = getLastArticleId(properties);

    /* Create a new HttpClient */
    NNTPClient client = new NNTPClient();

    try {//  www  . j  a  v  a2  s. co  m

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* Connect to Server */
        if (link.getPort() != -1)
            client.connect(link.getHost(), link.getPort());
        else
            client.connect(link.getHost());

        /* Set Timeout */
        setTimeout(client, properties);

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* Authentication if provided */
        setupAuthentication(link, client);

        /* Check Authentication Required */
        checkAuthenticationRequired(client);

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* Enable Reader Mode */
        client.sendCommand(MODE_READER);

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* Select Newsgroup */
        String newsgroup = link.getPath().replace("/", "");
        NewsgroupInfo groupInfo = new NewsgroupInfo();
        boolean selected = client.selectNewsgroup(newsgroup, groupInfo);

        /* Check Authentication Required */
        checkAuthenticationRequired(client);

        /* Check Newsgroup Selected */
        if (!selected)
            throwConnectionException("Unable to select Newsgroup", client);

        /* Support early cancellation */
        if (monitor.isCanceled())
            return null;

        /* First reload: Retrieve an initial amount of News */
        if (lastArticleId == null) {

            /* Set Article Pointer to last Article */
            int status = client.stat(groupInfo.getLastArticle());
            if (status != STATUS_ARTICLE_POINTER_OK)
                throwConnectionException("Unable to retrieve any News", client);

            /* Retrieve initial news */
            for (int i = 0; i < INITIAL_NEWS && !monitor.isCanceled(); i++) {
                createNews(client.retrieveArticle(), feed, monitor);

                /* Goto previous news if provided */
                int result = client.last();
                if (result != STATUS_ARTICLE_POINTER_OK)
                    break;
            }
        }

        /* Subsequent reload: Set pointer to last retrieved News */
        else {

            /* Set Article Pointer to last retrieved News */
            int status = client.stat(lastArticleId);
            if (status != STATUS_ARTICLE_POINTER_OK)
                throwConnectionException("Unable to retrieve any News", client);

            /* Retrieve all the following News */
            while (client.next() == STATUS_ARTICLE_POINTER_OK && !monitor.isCanceled())
                createNews(client.retrieveArticle(), feed, monitor);
        }

        /* Remember last article's ID */
        lastArticleId = groupInfo.getLastArticle();
    }

    /* Wrap Exceptions */
    catch (IOException e) {
        throw new ConnectionException(Activator.createErrorStatus(e.getMessage(), e));
    }

    /* Disconnect */
    finally {
        try {
            if (client.isConnected())
                client.disconnect();
        } catch (IOException e) {
            throw new ConnectionException(Activator.createErrorStatus(e.getMessage(), e));
        }
    }

    /* Create Conditional Get Object */
    IConditionalGet conditionalGet = null;
    if (lastArticleId != null)
        conditionalGet = factory.createConditionalGet(null, link, String.valueOf(lastArticleId));

    return Pair.create(feed, conditionalGet);
}