1 17 package org.apache.activemq.web.view; 18 19 import com.sun.syndication.feed.synd.SyndContent; 20 import com.sun.syndication.feed.synd.SyndContentImpl; 21 import com.sun.syndication.feed.synd.SyndEntry; 22 import com.sun.syndication.feed.synd.SyndEntryImpl; 23 import com.sun.syndication.feed.synd.SyndFeed; 24 import com.sun.syndication.feed.synd.SyndFeedImpl; 25 import com.sun.syndication.io.FeedException; 26 import com.sun.syndication.io.SyndFeedOutput; 27 28 import javax.jms.JMSException ; 29 import javax.jms.Message ; 30 import javax.jms.QueueBrowser ; 31 import javax.jms.TextMessage ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import java.io.IOException ; 37 import java.io.PrintWriter ; 38 import java.util.Date ; 39 import java.util.List ; 40 41 46 public class RssMessageRenderer extends SimpleMessageRenderer { 47 48 private String feedType = "rss_2.0"; 50 private SyndFeed feed; 51 private String description = "This feed is auto-generated by Apache ActiveMQ"; 52 private String entryContentType = "text/plain"; 53 54 public void renderMessage(PrintWriter writer, HttpServletRequest request, HttpServletResponse response, 55 QueueBrowser browser, Message message) throws JMSException { 56 SyndFeed feed = getFeed(browser, request); 57 58 List entries = feed.getEntries(); 59 SyndEntry entry = createEntry(browser, message, request); 60 SyndContent description = createEntryContent(browser, message, request); 61 entry.setDescription(description); 62 entries.add(entry); 63 } 64 65 public String getDescription() { 68 return description; 69 } 70 71 public void setDescription(String feedDescription) { 72 this.description = feedDescription; 73 } 74 75 public String getFeedType() { 76 return feedType; 77 } 78 79 public void setFeedType(String feedType) { 80 this.feedType = feedType; 81 } 82 83 public String getEntryContentType() { 84 return entryContentType; 85 } 86 87 public void setEntryContentType(String entryContentType) { 88 this.entryContentType = entryContentType; 89 } 90 91 94 protected void printFooter(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) 95 throws IOException , JMSException , ServletException { 96 SyndFeed feed = getFeed(browser, request); 98 SyndFeedOutput output = new SyndFeedOutput(); 99 try { 100 output.output(feed, writer); 101 } 102 catch (FeedException e) { 103 throw new ServletException (e); 104 } 105 } 106 107 protected void printHeader(PrintWriter writer, QueueBrowser browser, HttpServletRequest request) 108 throws IOException , JMSException { 109 } 110 111 public SyndFeed getFeed(QueueBrowser browser, HttpServletRequest request) throws JMSException { 112 if (feed == null) { 113 feed = createFeed(browser, request); 114 } 115 return feed; 116 } 117 118 protected SyndEntry createEntry(QueueBrowser browser, Message message, HttpServletRequest request) throws JMSException { 119 SyndEntry entry = new SyndEntryImpl(); 120 String title = message.getJMSMessageID(); 121 entry.setTitle(title); 122 String link = request.getRequestURI() + "/" + title; 123 entry.setLink(link); 124 entry.setPublishedDate(new Date ()); 125 return entry; 126 } 127 128 protected SyndContent createEntryContent(QueueBrowser browser, Message message, HttpServletRequest request) throws JMSException { 129 SyndContent description = new SyndContentImpl(); 130 description.setType(entryContentType); 131 132 if (message instanceof TextMessage ) { 133 String text = ((TextMessage ) message).getText(); 134 description.setValue(text); 135 } 136 return description; 137 } 138 139 protected SyndFeed createFeed(QueueBrowser browser, HttpServletRequest request) throws JMSException { 140 SyndFeed feed = new SyndFeedImpl(); 141 feed.setFeedType(feedType); 142 143 String title = browser.getQueue().toString(); 144 String selector = browser.getMessageSelector(); 145 if (selector != null) { 146 title += " with selector: " + selector; 147 } 148 feed.setTitle(title); 149 feed.setLink(request.getRequestURI()); 150 feed.setDescription(getDescription()); 151 return feed; 152 } 153 154 } 155 | Popular Tags |