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

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

Introduction

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

Prototype

public void connect(InetAddress host, int port) throws SocketException, IOException 

Source Link

Document

Opens a Socket connected to a remote host at the specified port and originating from the current host at a system assigned port.

Usage

From source file:me.lehrner.newsgroupsndy.presenter.tasks.UpdateGroupListAsyncTask.java

@Override
protected Boolean doInBackground(Server... servers) {
    mServerId = servers[0].getId();// ww w  .  j ava  2  s . com

    String hostName = servers[0].getServerUrl();
    int lastVisitInt = servers[0].getLastVisit();

    NewsgroupInfo[] newsgroupInfos;
    ArrayList<String> groupNames = new ArrayList<>();

    try {
        NNTPClient nntpClient = new NNTPClient();
        nntpClient.connect(hostName, 119);

        Log.d(LOG_TAG, "lastVisitInt: " + lastVisitInt);

        if (lastVisitInt != 0) {
            Calendar lastVisit = Calendar.getInstance();
            lastVisit.setTimeInMillis(lastVisitInt * 1000L);

            NewGroupsOrNewsQuery query = new NewGroupsOrNewsQuery(lastVisit, false);
            newsgroupInfos = nntpClient.listNewNewsgroups(query);

            Log.d(LOG_TAG, "listNewNewsgroups");

        } else {
            newsgroupInfos = nntpClient.listNewsgroups();

            Log.d(LOG_TAG, "listNewsgroups");

        }

        nntpClient.disconnect();
    } catch (IOException e) {
        Log.d(LOG_TAG, "IOException");

        GroupPresenter groupPresenter = mGroupPresenterWeakReference.get();

        if (groupPresenter != null) {
            groupPresenter.updateNotSuccessful();
        }
        return false;
    }

    if (newsgroupInfos == null) {
        Log.d(LOG_TAG, "newsgroupInfos is null");
        return false;
    }

    for (NewsgroupInfo newsgroupInfo : newsgroupInfos) {
        groupNames.add(newsgroupInfo.getNewsgroup());
    }

    return mGroupRepository.saveGroups(mServerId, groupNames);
}

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.  jav  a 2s  .c o 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);
}