forked from I2P_Developers/i2p.i2p
News: New /news page and helper to show all news
WIP, not yet hooked in
This commit is contained in:
@ -57,6 +57,8 @@ public class NewsManager implements RouterApp {
|
||||
if (!_currentNews.isEmpty())
|
||||
return new ArrayList(_currentNews);
|
||||
// load old news.xml
|
||||
if (_log.shouldWarn())
|
||||
_log.warn("no real XML, falling back to news.xml");
|
||||
List<NewsEntry> rv = parseOldNews();
|
||||
if (!rv.isEmpty()) {
|
||||
_currentNews = rv;
|
||||
@ -65,6 +67,8 @@ public class NewsManager implements RouterApp {
|
||||
}
|
||||
// load and translate initialnews
|
||||
// We don't save it to _currentNews, as the language may change
|
||||
if (_log.shouldWarn())
|
||||
_log.warn("no news.xml, falling back to initialNews");
|
||||
return parseInitialNews();
|
||||
}
|
||||
|
||||
@ -112,6 +116,8 @@ public class NewsManager implements RouterApp {
|
||||
public synchronized void startup() {
|
||||
changeState(STARTING);
|
||||
_currentNews = PersistNews.load(_context);
|
||||
if (_log.shouldWarn())
|
||||
_log.warn("Initialized with " + _currentNews.size() + " entries");
|
||||
changeState(RUNNING);
|
||||
if (_cmgr != null)
|
||||
_cmgr.register(this);
|
||||
|
@ -33,7 +33,7 @@ import org.cybergarage.xml.ParserException;
|
||||
*
|
||||
* @since 0.9.23
|
||||
*/
|
||||
public class PersistNews {
|
||||
class PersistNews {
|
||||
|
||||
private static final String DIR = "docs/news";
|
||||
private static final String PFX = "news-";
|
||||
|
@ -0,0 +1,81 @@
|
||||
package net.i2p.router.web;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.app.ClientAppManager;
|
||||
import net.i2p.router.news.NewsEntry;
|
||||
import net.i2p.router.news.NewsManager;
|
||||
|
||||
|
||||
/**
|
||||
* HTML-formatted full news entries
|
||||
*
|
||||
* @since 0.9.23
|
||||
*/
|
||||
public class NewsFeedHelper extends HelperBase {
|
||||
|
||||
private int _start = 0;
|
||||
private int _limit = 3;
|
||||
|
||||
/**
|
||||
* @param limit less than or equal to zero means all
|
||||
*/
|
||||
public void setLimit(int limit) {
|
||||
_limit = limit;
|
||||
}
|
||||
|
||||
public void setStart(int start) {
|
||||
_start = start;
|
||||
}
|
||||
|
||||
public String getEntries() {
|
||||
return getEntries(_context, _start, _limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param max less than or equal to zero means all
|
||||
* @return non-null, "" if none
|
||||
*/
|
||||
static String getEntries(I2PAppContext ctx, int start, int max) {
|
||||
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();
|
||||
}
|
||||
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
|
||||
String systemTimeZone = ctx.getProperty("i2p.systemTimeZone");
|
||||
if (systemTimeZone != null)
|
||||
fmt.setTimeZone(TimeZone.getTimeZone(systemTimeZone));
|
||||
int i = 0;
|
||||
for (NewsEntry entry : entries) {
|
||||
if (i++ < start)
|
||||
continue;
|
||||
buf.append("<h3>");
|
||||
if (entry.updated > 0) {
|
||||
Date date = new Date(entry.updated);
|
||||
buf.append(fmt.format(date))
|
||||
.append(": ");
|
||||
}
|
||||
buf.append(entry.title)
|
||||
.append("</h3>\n")
|
||||
.append(entry.content)
|
||||
.append("\n");
|
||||
if (i >= start + max)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
18
apps/routerconsole/jsp/news.jsp
Normal file
18
apps/routerconsole/jsp/news.jsp
Normal file
@ -0,0 +1,18 @@
|
||||
<%@page contentType="text/html"%>
|
||||
<%@page pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
|
||||
<html><head>
|
||||
<%@include file="css.jsi" %>
|
||||
<%=intl.title("News")%>
|
||||
<script src="/js/ajax.js" type="text/javascript"></script>
|
||||
<%@include file="summaryajax.jsi" %>
|
||||
</head><body onload="initAjax()">
|
||||
<%@include file="summary.jsi" %>
|
||||
<h1><%=intl._("Latest News")%></h1>
|
||||
<div class="main" id="main">
|
||||
<jsp:useBean class="net.i2p.router.web.NewsFeedHelper" id="feedHelper" scope="request" />
|
||||
<jsp:setProperty name="feedHelper" property="contextId" value="<%=(String)session.getAttribute(\"i2p.contextId\")%>" />
|
||||
<% feedHelper.setLimit(0); %>
|
||||
<jsp:getProperty name="feedHelper" property="entries" />
|
||||
</div></body></html>
|
Reference in New Issue
Block a user