You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SyndicationUtils.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.io.OutputStreamWriter;
  20. import java.text.MessageFormat;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import org.eclipse.jgit.revwalk.RevCommit;
  24. import com.sun.syndication.feed.synd.SyndContent;
  25. import com.sun.syndication.feed.synd.SyndContentImpl;
  26. import com.sun.syndication.feed.synd.SyndEntry;
  27. import com.sun.syndication.feed.synd.SyndEntryImpl;
  28. import com.sun.syndication.feed.synd.SyndFeed;
  29. import com.sun.syndication.feed.synd.SyndFeedImpl;
  30. import com.sun.syndication.io.FeedException;
  31. import com.sun.syndication.io.SyndFeedOutput;
  32. public class SyndicationUtils {
  33. public static void toRSS(String hostUrl, String title, String description, String repository, List<RevCommit> commits, OutputStream os)
  34. throws IOException, FeedException {
  35. SyndFeed feed = new SyndFeedImpl();
  36. feed.setFeedType("rss_1.0");
  37. feed.setTitle(title);
  38. feed.setLink(MessageFormat.format("{0}/summary/{1}", hostUrl, repository));
  39. feed.setDescription(description);
  40. List<SyndEntry> entries = new ArrayList<SyndEntry>();
  41. for (RevCommit commit : commits) {
  42. SyndEntry entry = new SyndEntryImpl();
  43. entry.setTitle(commit.getShortMessage());
  44. entry.setAuthor(commit.getAuthorIdent().getName());
  45. entry.setLink(MessageFormat.format("{0}/commit/{1}/{2}", hostUrl, repository, commit.getName()));
  46. entry.setPublishedDate(commit.getCommitterIdent().getWhen());
  47. SyndContent content = new SyndContentImpl();
  48. content.setType("text/html");
  49. String html = StringUtils.escapeForHtml(commit.getFullMessage(), false);
  50. content.setValue(StringUtils.breakLinesForHtml(html));
  51. entry.setDescription(content);
  52. entries.add(entry);
  53. }
  54. feed.setEntries(entries);
  55. OutputStreamWriter writer = new OutputStreamWriter(os);
  56. SyndFeedOutput output = new SyndFeedOutput();
  57. output.output(feed, writer);
  58. writer.close();
  59. }
  60. }