如何将 rss 提要添加到 GAE 上的 java webapp?
How to make rss feed into the java webapp on GAE?
我已经根据 https://rometools.github.io/rome/ROMEReleases/ROME0.4Beta/RomeV0.4Tutorials/RomeV0.4TutorialUsingRomeWithinAServletToCreateAndReturnAFeed.html example. However, the rss is not validated on https://validator.w3.org/feed/check.cgi 从头创建的 servlet 准备好了(因此提要未经验证,例如,在 Yandex 小部件策略中),它缺少频道标签中的元素。
有几个提示,如 Atom:link in RSS using Rome 如何启用 atom:link,但它们基于 rome.properties,不可能在 GAE 上使用它们 - 属性文件未加载到 webapp 的类路径中。
那么这是使 atom:link 工作的最简单方法吗?
我们意识到,可以将遗漏的元素添加到 servlet 中的 xml 中,因此对于此特定目的,不需要额外的罗马模块 class:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
try {
SyndFeed feed = getFeed(req, dataEntries, streamGauge);
String feedType = req.getParameter(FEED_TYPE);
feedType = (feedType != null) ? feedType : _defaultFeedType;
feed.setFeedType(feedType);
res.setContentType(MIME_TYPE);
SyndFeedOutput output = new SyndFeedOutput();
Writer writer = new StringWriter();
output.output(feed, writer);
SAXBuilder db = null;
Document doc = null;
db = new SAXBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(writer.toString()));
try {
doc = db.build(is);
} catch (JDOMException e) {
e.printStackTrace();
}
Element root =
doc.getRootElement();
Element channel = root.getChild("channel");
FeedServlet.AtomNSModule atomNSModule = (FeedServlet.AtomNSModule) module;
root.addNamespaceDeclaration(ATOM_NS);
Element atomLink = new Element("link", ATOM_NS);
atomNSModule.setLink("http://volgalevel.appspot.com/feed");
atomLink.setAttribute("href", atomNSModule.getLink());
atomLink.setAttribute("rel", "self");
atomLink.setAttribute("type", "application/rss+xml");
channel.addContent(0, atomLink);
XMLOutputter outputter = new XMLOutputter();
outputter.output(doc, res.getWriter());
} catch (FeedException ex) {
String msg = COULD_NOT_GENERATE_FEED_ERROR;
log(msg, ex);
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
}
}
注意进口:
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.Document;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
实际上可以在那里找到用于 RSS 的工作 GAE Servlet:https://github.com/Eljah/volgalevel/blob/master/src/main/java/com/appspot/FeedServlet.java
它还修复了重复的 pubDate 问题
我已经根据 https://rometools.github.io/rome/ROMEReleases/ROME0.4Beta/RomeV0.4Tutorials/RomeV0.4TutorialUsingRomeWithinAServletToCreateAndReturnAFeed.html example. However, the rss is not validated on https://validator.w3.org/feed/check.cgi 从头创建的 servlet 准备好了(因此提要未经验证,例如,在 Yandex 小部件策略中),它缺少频道标签中的元素。
有几个提示,如 Atom:link in RSS using Rome 如何启用 atom:link,但它们基于 rome.properties,不可能在 GAE 上使用它们 - 属性文件未加载到 webapp 的类路径中。 那么这是使 atom:link 工作的最简单方法吗?
我们意识到,可以将遗漏的元素添加到 servlet 中的 xml 中,因此对于此特定目的,不需要额外的罗马模块 class:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
try {
SyndFeed feed = getFeed(req, dataEntries, streamGauge);
String feedType = req.getParameter(FEED_TYPE);
feedType = (feedType != null) ? feedType : _defaultFeedType;
feed.setFeedType(feedType);
res.setContentType(MIME_TYPE);
SyndFeedOutput output = new SyndFeedOutput();
Writer writer = new StringWriter();
output.output(feed, writer);
SAXBuilder db = null;
Document doc = null;
db = new SAXBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(writer.toString()));
try {
doc = db.build(is);
} catch (JDOMException e) {
e.printStackTrace();
}
Element root =
doc.getRootElement();
Element channel = root.getChild("channel");
FeedServlet.AtomNSModule atomNSModule = (FeedServlet.AtomNSModule) module;
root.addNamespaceDeclaration(ATOM_NS);
Element atomLink = new Element("link", ATOM_NS);
atomNSModule.setLink("http://volgalevel.appspot.com/feed");
atomLink.setAttribute("href", atomNSModule.getLink());
atomLink.setAttribute("rel", "self");
atomLink.setAttribute("type", "application/rss+xml");
channel.addContent(0, atomLink);
XMLOutputter outputter = new XMLOutputter();
outputter.output(doc, res.getWriter());
} catch (FeedException ex) {
String msg = COULD_NOT_GENERATE_FEED_ERROR;
log(msg, ex);
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
}
}
注意进口:
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.Document;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
实际上可以在那里找到用于 RSS 的工作 GAE Servlet:https://github.com/Eljah/volgalevel/blob/master/src/main/java/com/appspot/FeedServlet.java
它还修复了重复的 pubDate 问题