2017-02-25 67 views
0

我已經準備好了根據https://rometools.github.io/rome/ROMEReleases/ROME0.4Beta/RomeV0.4Tutorials/RomeV0.4TutorialUsingRomeWithinAServletToCreateAndReturnAFeed.html示例的servlet。但是,在從頭創建的https://validator.w3.org/feed/check.cgi上未驗證rss(因此未驗證Feed(例如,在Yandex窗口小部件策略中),它會忽略通道標記中的元素。如何在GAE上將RSS饋入Java Web應用程序?

有幾個提示,如Atom:link in RSS using Rome如何啓用atom:link,但它們基於rome.properties,在GAE上不可能使用它們 - 屬性文件未加載到webapp的類路徑中。 所以這是使原子最簡單的方法:鏈接工作?

回答

0

人們認識,即未接元件可被添加到XML右在servlet,不需要用於該特定目的爲羅馬所以附加模塊類:

公共無效的doGet(HttpServletRequest的REQ,HttpServletResponse的水庫)拋出IOException {嘗試{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; 

實際工作GAE的Servlet的RSS可以在那裏達到:https://github.com/Eljah/volgalevel/blob/master/src/main/java/com/appspot/FeedServlet.java

它還修正重複pubdate的問題

相關問題