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

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

Introduction

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

Prototype

@Override
public void disconnect() throws IOException 

Source Link

Document

Closes the connection to the NNTP server and sets to null some internal data so that the memory may be reclaimed by the garbage collector.

Usage

From source file:org.ossmeter.platform.communicationchannel.nntp.local.NntpUtil.java

public static void disconnectFromNntpServer(NNTPClient nntpClient) {
    try {/*  w  ww .ja  v a 2 s. c  om*/
        nntpClient.disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.println("IOException while disconnectiong from NNTP server: " + e.getMessage());
    }
}

From source file:org.ossmeter.platform.communicationchannel.nntp.NntpManager.java

@Override
public CommunicationChannelDelta getDelta(DB db, NntpNewsGroup newsgroup, Date date) throws Exception {
    NNTPClient nntpClient = NntpUtil.connectToNntpServer(newsgroup);

    NewsgroupInfo newsgroupInfo = NntpUtil.selectNewsgroup(nntpClient, newsgroup);
    int lastArticle = newsgroupInfo.getLastArticle();

    //       The following statement is not really needed, but I added it to speed up running,
    //       in the date is far latter than the first day of the newsgroup.
    //      if (Integer.parseInt(newsgroup.getLastArticleChecked())<134500)
    //         newsgroup.setLastArticleChecked("134500"); //137500");

    String lac = newsgroup.getLastArticleChecked();
    if (lac == null || lac.equals("") || lac.equals("null"))
        lac = "-1";
    int lastArticleChecked = Integer.parseInt(lac);
    if (lastArticleChecked < 0)
        lastArticleChecked = newsgroupInfo.getFirstArticle();

    // FIXME: certain eclipse newsgroups return 0 for both FirstArticle and LastArticle which causes exceptions
    if (lastArticleChecked == 0)
        return null;

    CommunicationChannelDelta delta = new CommunicationChannelDelta();
    delta.setNewsgroup(newsgroup);/*from w  ww  . j  a v  a  2s . c o m*/

    int retrievalStep = RETRIEVAL_STEP;
    Boolean dayCompleted = false;
    while (!dayCompleted) {
        if (lastArticleChecked + retrievalStep > lastArticle) {
            retrievalStep = lastArticle - lastArticleChecked;
            dayCompleted = true;
        }
        Article[] articles;
        Date articleDate = date;
        // The following loop discards messages for days earlier than the required one.
        do {
            articles = NntpUtil.getArticleInfo(nntpClient, lastArticleChecked + 1,
                    lastArticleChecked + retrievalStep);
            if (articles.length > 0) {
                Article lastArticleRetrieved = articles[articles.length - 1];
                java.util.Date javaArticleDate = NntpUtil.parseDate(lastArticleRetrieved.getDate());
                articleDate = new Date(javaArticleDate);
                if (date.compareTo(articleDate) > 0)
                    lastArticleChecked = lastArticleRetrieved.getArticleNumber();
            }
        } while (date.compareTo(articleDate) > 0);

        for (Article article : articles) {
            java.util.Date javaArticleDate = NntpUtil.parseDate(article.getDate());
            if (javaArticleDate != null) {
                articleDate = new Date(javaArticleDate);
                if (date.compareTo(articleDate) < 0) {
                    dayCompleted = true;
                    //                  System.out.println("dayCompleted");
                } else if (date.compareTo(articleDate) == 0) {
                    CommunicationChannelArticle communicationChannelArticle = new CommunicationChannelArticle();
                    communicationChannelArticle.setArticleId(article.getArticleId());
                    communicationChannelArticle.setArticleNumber(article.getArticleNumber());
                    communicationChannelArticle.setDate(javaArticleDate);
                    //                  I haven't seen any messageThreadIds on NNTP servers, yet.
                    //                  communicationChannelArticle.setMessageThreadId(article.messageThreadId());
                    NntpNewsGroup newNewsgroup = new NntpNewsGroup();
                    newNewsgroup.setUrl(newsgroup.getUrl());
                    newNewsgroup.setAuthenticationRequired(newsgroup.getAuthenticationRequired());
                    newNewsgroup.setUsername(newsgroup.getUsername());
                    newNewsgroup.setPassword(newsgroup.getPassword());
                    newNewsgroup.setNewsGroupName(newsgroup.getNewsGroupName());
                    newNewsgroup.setPort(newsgroup.getPort());
                    newNewsgroup.setInterval(newsgroup.getInterval());
                    communicationChannelArticle.setNewsgroup(newNewsgroup);
                    communicationChannelArticle.setReferences(article.getReferences());
                    communicationChannelArticle.setSubject(article.getSubject());
                    communicationChannelArticle.setUser(article.getFrom());
                    communicationChannelArticle
                            .setText(getContents(db, newNewsgroup, communicationChannelArticle));
                    delta.getArticles().add(communicationChannelArticle);
                    lastArticleChecked = article.getArticleNumber();
                    //                  System.out.println("dayNOTCompleted");
                } else {

                    //TODO: In this case, there are unprocessed articles whose date is earlier than the date requested.
                    //      This means that the deltas of those article dates are incomplete, 
                    //      i.e. the deltas did not contain all articles of those dates.
                }
            } else {
                // If an article has no correct date, then ignore it
                System.err.println("\t\tUnparsable article date: " + article.getDate());
            }
        }
    }
    nntpClient.disconnect();
    newsgroup.setLastArticleChecked(lastArticleChecked + "");
    System.out.println(
            "delta (" + date.toString() + ") contains:\t" + delta.getArticles().size() + " nntp articles");

    return delta;
}

From source file:org.ossmeter.platform.communicationchannel.nntp.NntpManager.java

@Override
public Date getFirstDate(DB db, NntpNewsGroup newsgroup) throws Exception {
    NNTPClient nntpClient = NntpUtil.connectToNntpServer(newsgroup);
    NewsgroupInfo newsgroupInfo = NntpUtil.selectNewsgroup(nntpClient, newsgroup);
    int firstArticleNumber = newsgroupInfo.getFirstArticle();

    if (firstArticleNumber == 0) {
        return null; // This is to deal with message-less newsgroups.
    }/*from ww w . j  a v  a2  s.  c  om*/

    Reader reader = nntpClient.retrieveArticle(firstArticleNumber);
    while (reader == null) {
        firstArticleNumber++;
        reader = nntpClient.retrieveArticle(firstArticleNumber);
        if (firstArticleNumber >= newsgroupInfo.getLastArticle())
            break;
    }

    ArticleHeader articleHeader = new ArticleHeader(reader);
    //      Article article = NntpUtil.getArticleInfo(nntpClient, articleId);
    nntpClient.disconnect();
    //      String date = article.getDate();

    return new Date(NntpUtil.parseDate(articleHeader.getDate().trim()));
}

From source file:org.ossmeter.platform.communicationchannel.nntp.NntpManager.java

@Override
public String getContents(DB db, NntpNewsGroup newsgroup, CommunicationChannelArticle article)
        throws Exception {
    NNTPClient nntpClient = NntpUtil.connectToNntpServer(newsgroup);
    NntpUtil.selectNewsgroup(nntpClient, newsgroup);
    String contents = NntpUtil.getArticleBody(nntpClient, article.getArticleNumber());
    nntpClient.disconnect();
    return contents;
}

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 {/*from  w  ww  .  j a v a  2s  .  com*/

        /* 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);
}