1 17 package org.apache.servicemix.components.rss; 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.SyndFeedInput; 27 import com.sun.syndication.io.SyndFeedOutput; 28 import com.sun.syndication.io.XmlReader; 29 30 import org.apache.servicemix.components.util.OutBinding; 31 import org.apache.servicemix.expression.Expression; 32 import org.apache.servicemix.expression.ExpressionHelper; 33 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 34 35 import javax.jbi.JBIException; 36 import javax.jbi.messaging.MessageExchange; 37 import javax.jbi.messaging.MessagingException; 38 import javax.jbi.messaging.NormalizedMessage; 39 import javax.xml.transform.Source ; 40 import javax.xml.transform.TransformerException ; 41 42 import java.io.File ; 43 import java.io.FileWriter ; 44 import java.io.IOException ; 45 import java.io.Writer ; 46 import java.util.ArrayList ; 47 import java.util.Date ; 48 import java.util.List ; 49 50 55 public class FeedWriter extends OutBinding { 56 57 private String feedType = "atom_0.3"; 58 private SyndFeed cachedFeed; 59 private String title; 60 private String link = "http://servicemix.org/RSS"; 61 private Expression entryTitle; 62 private Expression entryValue; 63 private String feedDescription = "This feed is auto-generated by ServiceMix"; 64 private int maximumEntryCount = 200; 65 private File feedFile; 66 private String contentType = "text/plain"; 67 private SourceTransformer sourceTransformer = new SourceTransformer(); 68 private boolean loadOnStartup = true; 69 70 public SyndFeed getCachedFeed() throws IllegalArgumentException , FeedException, IOException { 71 if (cachedFeed == null) { 72 cachedFeed = loadOrCreateFeed(); 73 } 74 return cachedFeed; 75 } 76 77 public void setCachedFeed(SyndFeed feed) { 78 this.cachedFeed = feed; 79 } 80 81 public String getFeedType() { 82 return feedType; 83 } 84 85 public void setFeedType(String feedType) { 86 this.feedType = feedType; 87 } 88 89 public String getFeedDescription() { 90 return feedDescription; 91 } 92 93 public void setFeedDescription(String feedDescription) { 94 this.feedDescription = feedDescription; 95 } 96 97 public String getTitle() { 98 if (title == null) { 99 title = "ServiceMix feed for service: " + getService(); 100 } 101 return title; 102 } 103 104 public void setTitle(String title) { 105 this.title = title; 106 } 107 108 public String getLink() { 109 return link; 110 } 111 112 public void setLink(String link) { 113 this.link = link; 114 } 115 116 public Expression getEntryTitle() { 117 return entryTitle; 118 } 119 120 public void setEntryTitle(Expression title) { 121 this.entryTitle = title; 122 } 123 124 public Expression getEntryValue() { 125 return entryValue; 126 } 127 128 public void setEntryValue(Expression entryValue) { 129 this.entryValue = entryValue; 130 } 131 132 public int getMaximumEntryCount() { 133 return maximumEntryCount; 134 } 135 136 public void setMaximumEntryCount(int maximumEntryCount) { 137 this.maximumEntryCount = maximumEntryCount; 138 } 139 140 public File getFeedFile() { 141 if (feedFile == null) { 142 throw new IllegalArgumentException ("You must set the 'feedFile' property"); 143 } 144 return feedFile; 145 } 146 147 public void setFeedFile(File feedFile) { 148 this.feedFile = feedFile; 149 } 150 151 public String getContentType() { 152 return contentType; 153 } 154 155 public void setContentType(String contentType) { 156 this.contentType = contentType; 157 } 158 159 public SourceTransformer getSourceTransformer() { 160 return sourceTransformer; 161 } 162 163 public void setSourceTransformer(SourceTransformer sourceTransformer) { 164 this.sourceTransformer = sourceTransformer; 165 } 166 167 public boolean isLoadOnStartup() { 168 return loadOnStartup; 169 } 170 171 public void setLoadOnStartup(boolean loadOnStartup) { 172 this.loadOnStartup = loadOnStartup; 173 } 174 175 178 protected void init() throws JBIException { 179 super.init(); 180 181 File file = getFeedFile(); 183 if (file.exists() && file.isDirectory()) { 184 throw new IllegalArgumentException ("Cannot generate the cachedFeed as the cachedFeed file is a directory: " + file); 185 } 186 } 187 188 protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception { 189 SyndFeed feed = getCachedFeed(); 190 addMessageToFeed(feed, exchange, message); 191 removeExpiredEntries(feed); 192 writeFeed(feed, exchange, message); 193 done(exchange); 194 } 195 196 protected void addMessageToFeed(SyndFeed feed, MessageExchange exchange, NormalizedMessage message) throws TransformerException , MessagingException { 197 List entries = feed.getEntries(); 198 SyndEntry entry = createEntry(exchange, message); 199 SyndContent description = createEntryContent(exchange, message); 200 entry.setDescription(description); 201 202 205 List temp = new ArrayList (); 206 temp.add(entry); 207 temp.addAll(entries); 208 feed.setEntries(temp); 209 } 210 211 protected SyndEntry createEntry(MessageExchange exchange, NormalizedMessage message) throws MessagingException { 212 SyndEntry entry = new SyndEntryImpl(); 213 214 entry.setTitle(ExpressionHelper.asString(getEntryTitle(), exchange, message, "ServiceMix Feed")); 215 entry.setLink(getLink()); 216 entry.setPublishedDate(new Date ()); 217 return entry; 218 } 219 220 protected SyndContent createEntryContent(MessageExchange exchange, NormalizedMessage message) throws TransformerException , MessagingException { 221 SyndContent description = new SyndContentImpl(); 222 description.setType(contentType); 223 Source content = message.getContent(); 224 225 String value = ExpressionHelper.asString(getEntryValue(), exchange, message, null); 226 if (value == null && content != null) { 227 value = getSourceTransformer().toString(content); 228 value = encodeContent(value); 229 } 230 if (value != null) { 231 description.setValue(value); 232 } 233 return description; 234 } 235 236 239 protected String encodeContent(String value) { 240 return "<![CDATA[" + value + "]]>"; 241 } 242 243 protected void writeFeed(SyndFeed feed, MessageExchange messageExchange, NormalizedMessage message) throws IOException , FeedException { 244 Writer writer = new FileWriter (feedFile); 245 SyndFeedOutput output = new SyndFeedOutput(); 246 output.output(feed, writer); 247 writer.close(); 248 } 249 250 255 protected void removeExpiredEntries(SyndFeed feed) { 256 if (maximumEntryCount > 0) { 258 List entries = feed.getEntries(); 259 int size = entries.size(); 260 if (size > maximumEntryCount) { 261 entries.subList(maximumEntryCount, size).clear(); 262 } 263 } 264 } 265 266 protected SyndFeed loadOrCreateFeed() throws IllegalArgumentException , FeedException, IOException { 267 if (isLoadOnStartup()) { 268 File file = getFeedFile(); 269 if (file.exists() && file.isFile()) { 270 SyndFeedInput input = new SyndFeedInput(); 271 XmlReader xmlReader = new XmlReader(file); 272 return input.build(xmlReader); 273 } 274 } 275 return createFeed(); 276 } 277 278 protected SyndFeed createFeed() { 279 SyndFeed feed = new SyndFeedImpl(); 280 feed.setFeedType(feedType); 281 282 feed.setTitle(getTitle()); 283 feed.setLink(getLink()); 284 feed.setDescription(getFeedDescription()); 285 return feed; 286 } 287 288 } 289 | Popular Tags |