List of usage examples for org.apache.commons.net.nntp NNTPClient last
public int last() throws IOException
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 {/* ww w . jav a 2 s .c om*/ /* 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); }