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.

RssFeedGenerator.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package org.apache.archiva.rss;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  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 org.springframework.context.annotation.Scope;
  31. import org.springframework.stereotype.Service;
  32. /**
  33. * Generates RSS feeds.
  34. *
  35. */
  36. @Service("rssFeedGenerator#default")
  37. @Scope("prototype")
  38. public class RssFeedGenerator
  39. {
  40. private Logger log = LoggerFactory.getLogger( RssFeedGenerator.class );
  41. // TODO: make configurable
  42. public static String DEFAULT_FEEDTYPE = "rss_2.0";
  43. public static String DEFAULT_LANGUAGE = "en-us";
  44. public SyndFeed generateFeed( String title, String description, List<RssFeedEntry> dataEntries )
  45. {
  46. if( dataEntries.size() == 0 )
  47. {
  48. log.debug( "No updates found, feed not generated." );
  49. return null;
  50. }
  51. SyndFeed feed = new SyndFeedImpl();
  52. feed.setTitle( title );
  53. feed.setDescription( description );
  54. feed.setLanguage( DEFAULT_LANGUAGE );
  55. feed.setPublishedDate( dataEntries.get( dataEntries.size() - 1 ).getPublishedDate() );
  56. feed.setFeedType( DEFAULT_FEEDTYPE );
  57. feed.setEntries( getEntries( dataEntries ) );
  58. log.debug( "Finished generating the feed \'{}\'.", title );
  59. return feed;
  60. }
  61. private List<SyndEntry> getEntries( List<RssFeedEntry> dataEntries )
  62. {
  63. List<SyndEntry> entries = new ArrayList<>();
  64. SyndEntry entry;
  65. SyndContent description;
  66. for ( RssFeedEntry dataEntry : dataEntries )
  67. {
  68. entry = new SyndEntryImpl();
  69. entry.setTitle( dataEntry.getTitle() );
  70. entry.setPublishedDate( dataEntry.getPublishedDate() );
  71. description = new SyndContentImpl();
  72. description.setType( "text/plain" );
  73. description.setValue( dataEntry.getDescription() );
  74. entry.setDescription( description );
  75. entries.add( entry );
  76. }
  77. return entries;
  78. }
  79. }