This project has retired. For details please refer to its Attic page.
RssFeedGenerator xref
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 com.rometools.rome.feed.synd.SyndContent;
23  import com.rometools.rome.feed.synd.SyndContentImpl;
24  import com.rometools.rome.feed.synd.SyndEntry;
25  import com.rometools.rome.feed.synd.SyndEntryImpl;
26  import com.rometools.rome.feed.synd.SyndFeed;
27  import com.rometools.rome.feed.synd.SyndFeedImpl;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.context.annotation.Scope;
31  import org.springframework.stereotype.Service;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /**
37   * Generates RSS feeds.
38   *
39   */
40  @Service("rssFeedGenerator#default")
41  @Scope("prototype")
42  public class RssFeedGenerator
43  {
44      private Logger log = LoggerFactory.getLogger( RssFeedGenerator.class );
45  
46      // TODO: make configurable
47      public static String DEFAULT_FEEDTYPE = "rss_2.0";
48  
49      public static String DEFAULT_LANGUAGE = "en-us";
50  
51      public SyndFeed generateFeed( String title, String description, List<RssFeedEntry> dataEntries )
52      {
53          if( dataEntries.size() ==  0 )
54          {
55              log.debug( "No updates found, feed not generated." );
56              return null;
57          }
58          
59          SyndFeed feed = new SyndFeedImpl();
60          feed.setTitle( title );        
61          feed.setDescription( description );
62          feed.setLanguage( DEFAULT_LANGUAGE );
63          feed.setPublishedDate( dataEntries.get( dataEntries.size() - 1 ).getPublishedDate() );
64          feed.setFeedType( DEFAULT_FEEDTYPE );
65          feed.setEntries( getEntries( dataEntries ) );
66  
67          log.debug( "Finished generating the feed \'{}\'.", title );
68          
69          return feed;
70      }
71  
72      private List<SyndEntry> getEntries( List<RssFeedEntry> dataEntries )
73      {
74          List<SyndEntry> entries = new ArrayList<>();
75  
76          SyndEntry entry;
77          SyndContent description;
78  
79          for ( RssFeedEntry dataEntry : dataEntries )
80          {
81              entry = new SyndEntryImpl();
82              entry.setTitle( dataEntry.getTitle() );
83              entry.setPublishedDate( dataEntry.getPublishedDate() );
84  
85              description = new SyndContentImpl();
86              description.setType( "text/plain" );
87              description.setValue( dataEntry.getDescription() );
88              entry.setDescription( description );
89  
90              entries.add( entry );
91          }
92  
93          return entries;
94      }
95  }