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