/*
* @see org.rssowl.core.connection.IProtocolHandler#reload(java.net.URI,
* org.eclipse.core.runtime.IProgressMonitor, java.util.Map)
*/
public Triple<IFeed, IConditionalGet, URI> 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 (If-None-Match header value) */
Integer lastArticleId = getLastArticleId(properties);
/* Create a new HttpClient */
NNTPClient client = new NNTPClient();
try {
/* 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, link);
/* 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("/", ""); //$NON-NLS-1$ //$NON-NLS-2$
NewsgroupInfo groupInfo = new NewsgroupInfo();
boolean selected = client.selectNewsgroup(newsgroup, groupInfo);
/* Check Authentication Required */
checkAuthenticationRequired(client, link);
/* Check Newsgroup Selected */
if (!selected)
throwConnectionException(Messages.NewsGroupHandler_ERROR_SELECT_NEWSGROUP, client);
/* Support early cancellation */
if (monitor.isCanceled())
return null;
boolean downloadWithoutPointer = (lastArticleId == null); //Last article servers as pointer
/* Subsequent reload: Set pointer to last retrieved News and go from there */
if (lastArticleId != null) {
/* Set Article Pointer to last retrieved News */
int status = client.stat(lastArticleId);
if (status == NO_SUCH_ARTICLE_ERROR)
downloadWithoutPointer = true; //This can happen if the last article id was not found, grab the latest news then
else if (status != STATUS_ARTICLE_POINTER_OK)
throwConnectionException(Messages.NewsGroupHandler_ERROR_RETRIEVE_NEWS, client);
/* Retrieve all the following News */
if (!downloadWithoutPointer) {
while (client.next() == STATUS_ARTICLE_POINTER_OK && !monitor.isCanceled())
createNews(client.retrieveArticle(), feed, monitor);
}
}
/* Retrieve the last 50 News of the group */
if (downloadWithoutPointer) {
/* Set Article Pointer to last Article */
int status = client.stat(groupInfo.getLastArticle());
if (status != STATUS_ARTICLE_POINTER_OK)
throwConnectionException(Messages.NewsGroupHandler_ERROR_RETRIEVE_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;
}
}
/* 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 Triple.create(feed, conditionalGet, link);
}