use of net.i2p.router.news.NewsEntry in project i2p.i2p by i2p.
the class NewsFetcher method outputOldNewsXML.
/**
* Output in the old format.
*
* @since 0.9.17
*/
private void outputOldNewsXML(NewsMetadata data, List<NewsEntry> entries, String sudVersion, String signingKeyName, File to) throws IOException {
NewsMetadata.Release latestRelease = data.releases.get(0);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(to), "UTF-8"));
out.write("<!--\n");
// update metadata in old format
out.write("<i2p.release ");
if (latestRelease.i2pVersion != null)
out.write(" version=\"" + latestRelease.i2pVersion + '"');
if (latestRelease.minVersion != null)
out.write(" minVersion=\"" + latestRelease.minVersion + '"');
if (latestRelease.minJavaVersion != null)
out.write(" minJavaVersion=\"" + latestRelease.minJavaVersion + '"');
String su3Torrent = "";
String su2Torrent = "";
for (NewsMetadata.Update update : latestRelease.updates) {
if (update.torrent != null) {
if ("su3".equals(update.type))
su3Torrent = update.torrent;
else if ("su2".equals(update.type))
su2Torrent = update.torrent;
}
}
if (!su2Torrent.isEmpty())
out.write(" su2Torrent=\"" + su2Torrent + '"');
if (!su3Torrent.isEmpty())
out.write(" su3Torrent=\"" + su3Torrent + '"');
out.write("/>\n");
// su3 and feed metadata for debugging
out.write("** News version:\t" + DataHelper.stripHTML(sudVersion) + '\n');
out.write("** Signed by:\t" + signingKeyName + '\n');
out.write("** Feed:\t" + DataHelper.stripHTML(data.feedTitle) + '\n');
out.write("** Feed ID:\t" + DataHelper.stripHTML(data.feedID) + '\n');
out.write("** Feed Date:\t" + (new Date(data.feedUpdated)) + '\n');
out.write("-->\n");
if (entries == null)
return;
DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT);
// the router sets the JVM time zone to UTC but saves the original here so we can get it
fmt.setTimeZone(SystemVersion.getSystemTimeZone(_context));
for (NewsEntry e : entries) {
if (e.title == null || e.content == null)
continue;
Date date = new Date(e.updated);
out.write("<!-- Entry Date: " + date + " -->\n");
out.write("<h3>");
out.write(fmt.format(date));
out.write(": ");
out.write(e.title);
out.write("</h3>\n");
out.write(e.content);
out.write("\n\n");
}
} finally {
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
}
use of net.i2p.router.news.NewsEntry in project i2p.i2p by i2p.
the class NewsFeedHelper method getEntries.
/**
* @param max less than or equal to zero means all
* @param ageLimit time before now, less than or equal to zero means all (after the first)
* @return non-null, "" if none
*/
static String getEntries(I2PAppContext ctx, int start, int max, long ageLimit) {
if (max <= 0)
max = Integer.MAX_VALUE;
StringBuilder buf = new StringBuilder(512);
List<NewsEntry> entries = Collections.emptyList();
ClientAppManager cmgr = ctx.clientAppManager();
if (cmgr != null) {
NewsManager nmgr = (NewsManager) cmgr.getRegisteredApp(NewsManager.APP_NAME);
if (nmgr != null) {
entries = nmgr.getEntries();
NewsEntry init = nmgr.getInitialNews();
if (init != null) {
// crude check to see if it's already in there
if (entries.size() != 1 || !DataHelper.eq(entries.get(0).title, init.title))
if (entries.isEmpty())
// in case it was an emtpyList
entries = Collections.singletonList(init);
else
entries.add(init);
}
}
}
if (!entries.isEmpty()) {
DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT);
// the router sets the JVM time zone to UTC but saves the original here so we can get it
fmt.setTimeZone(SystemVersion.getSystemTimeZone(ctx));
int i = 0;
for (NewsEntry entry : entries) {
if (i < start)
continue;
if (i > start && entry.updated > 0 && ageLimit > 0 && entry.updated < ctx.clock().now() - ageLimit)
break;
buf.append("<div class=\"newsentry\"><h3>");
if (entry.updated > 0) {
Date date = new Date(entry.updated);
buf.append("<span class=\"newsDate\">").append(fmt.format(date)).append("</span> ");
}
if (entry.link != null)
buf.append("<a href=\"").append(DataHelper.escapeHTML(entry.link)).append("\">");
buf.append(entry.title);
if (entry.link != null)
buf.append("</a>");
if (entry.authorName != null) {
// FIXME translate
buf.append(" <span class=\"newsAuthor\" title=\"Post author\"><i>").append(DataHelper.escapeHTML(entry.authorName)).append("</i></span>\n");
}
buf.append("</h3>\n<div class=\"newscontent\">\n").append(entry.content).append("\n</div></div>\n");
if (++i >= start + max)
break;
}
}
return buf.toString();
}
use of net.i2p.router.news.NewsEntry in project i2p.i2p by i2p.
the class NewsFetcher method processSU3.
/**
* Process the fetched su3 news file _tempFile.
* Handles 3 types of contained files: xml.gz (preferred), xml, and html (old format fake xml)
*
* @return the temp file contining the HTML-format news.xml
* @since 0.9.17
*/
private File processSU3() throws IOException {
SU3File su3 = new SU3File(_context, _tempFile);
// real xml, maybe gz, maybe not
File to1 = new File(_context.getTempDir(), "tmp-" + _context.random().nextInt() + ".xml");
// real xml
File to2 = new File(_context.getTempDir(), "tmp2-" + _context.random().nextInt() + ".xml");
try {
su3.verifyAndMigrate(to1);
int type = su3.getFileType();
if (su3.getContentType() != SU3File.CONTENT_NEWS)
throw new IOException("bad content type: " + su3.getContentType());
if (type == SU3File.TYPE_HTML)
return to1;
if (type != SU3File.TYPE_XML && type != SU3File.TYPE_XML_GZ)
throw new IOException("bad file type: " + type);
File xml;
if (type == SU3File.TYPE_XML_GZ) {
gunzip(to1, to2);
xml = to2;
to1.delete();
} else {
xml = to1;
}
NewsXMLParser parser = new NewsXMLParser(_context);
Node root = parser.parse(xml);
xml.delete();
NewsMetadata data = parser.getMetadata();
List<NewsEntry> entries = parser.getEntries();
// add entries to the news manager
ClientAppManager cmgr = _context.clientAppManager();
if (cmgr != null) {
NewsManager nmgr = (NewsManager) cmgr.getRegisteredApp(NewsManager.APP_NAME);
if (nmgr != null) {
nmgr.addEntries(entries);
List<Node> nodes = NewsXMLParser.getNodes(root, "entry");
nmgr.storeEntries(nodes);
}
}
// Persist any new CRL entries
List<CRLEntry> crlEntries = parser.getCRLEntries();
if (crlEntries != null)
persistCRLEntries(crlEntries);
else
_log.info("No CRL entries found in news feed");
// Block any new blocklist entries
BlocklistEntries ble = parser.getBlocklistEntries();
if (ble != null && ble.isVerified())
processBlocklistEntries(ble);
else
_log.info("No blocklist entries found in news feed");
// store entries and metadata in old news.xml format
String sudVersion = su3.getVersionString();
String signingKeyName = su3.getSignerString();
File to3 = new File(_context.getTempDir(), "tmp3-" + _context.random().nextInt() + ".xml");
outputOldNewsXML(data, entries, sudVersion, signingKeyName, to3);
return to3;
} finally {
to2.delete();
}
}
use of net.i2p.router.news.NewsEntry in project i2p.i2p by i2p.
the class SummaryBarRenderer method renderNewsHeadingsHTML.
/**
* @since 0.9.1
*/
public String renderNewsHeadingsHTML() {
if (_helper == null)
return "";
NewsHelper newshelper = _helper.getNewsHelper();
if (newshelper == null || newshelper.shouldShowNews())
return "";
StringBuilder buf = new StringBuilder(512);
String consoleNonce = CSSHelper.getNonce();
if (consoleNonce != null) {
// Set up title and pre-headings stuff.
// buf.append("<h3><a href=\"/configupdate\">")
buf.append("<h3><a href=\"/news\">").append(_t("News & Updates")).append("</a></h3><hr class=\"b\"><div class=\"sb_newsheadings\">\n");
// Get news content.
List<NewsEntry> entries = Collections.emptyList();
ClientAppManager cmgr = _context.clientAppManager();
if (cmgr != null) {
NewsManager nmgr = (NewsManager) cmgr.getRegisteredApp(NewsManager.APP_NAME);
if (nmgr != null)
entries = nmgr.getEntries();
}
if (!entries.isEmpty()) {
buf.append("<table>\n");
DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT);
// the router sets the JVM time zone to UTC but saves the original here so we can get it
fmt.setTimeZone(SystemVersion.getSystemTimeZone(_context));
int i = 0;
// show a min of 1, max of 3, none older than 60 days over min
final int min = 1;
final int max = 3;
for (NewsEntry entry : entries) {
if (i >= min && entry.updated > 0 && entry.updated < _context.clock().now() - 60 * 24 * 60 * 60 * 1000L)
break;
buf.append("<tr><td><a href=\"/?news=1&consoleNonce=").append(consoleNonce).append("\"");
if (entry.updated > 0) {
Date date = new Date(entry.updated);
buf.append(" title=\"").append(_t("Published")).append(": ").append(fmt.format(date)).append("\"");
}
buf.append(">");
buf.append(entry.title).append("</a></td></tr>\n");
if (++i >= max)
break;
}
buf.append("</table>\n");
} else {
buf.append("<center><i>").append(_t("none")).append("</i></center>");
}
// Add post-headings stuff.
// buf.append("<a href=\"/news\">")
// .append(_t("Show all news"))
// .append("</a>\n");
buf.append("</div>\n");
}
return buf.toString();
}
Aggregations