001    package org.apache.archiva.rss;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    
028    import com.sun.syndication.feed.synd.SyndContent;
029    import com.sun.syndication.feed.synd.SyndContentImpl;
030    import com.sun.syndication.feed.synd.SyndEntry;
031    import com.sun.syndication.feed.synd.SyndEntryImpl;
032    import com.sun.syndication.feed.synd.SyndFeed;
033    import com.sun.syndication.feed.synd.SyndFeedImpl;
034    import org.springframework.context.annotation.Scope;
035    import org.springframework.stereotype.Service;
036    
037    /**
038     * Generates RSS feeds.
039     *
040     */
041    @Service("rssFeedGenerator#default")
042    @Scope("prototype")
043    public class RssFeedGenerator
044    {
045        private Logger log = LoggerFactory.getLogger( RssFeedGenerator.class );
046    
047        // TODO: make configurable
048        public static String DEFAULT_FEEDTYPE = "rss_2.0";
049    
050        public static String DEFAULT_LANGUAGE = "en-us";
051    
052        public SyndFeed generateFeed( String title, String description, List<RssFeedEntry> dataEntries )
053        {
054            if( dataEntries.size() ==  0 )
055            {
056                log.debug( "No updates found, feed not generated." );
057                return null;
058            }
059            
060            SyndFeed feed = new SyndFeedImpl();
061            feed.setTitle( title );        
062            feed.setDescription( description );
063            feed.setLanguage( DEFAULT_LANGUAGE );
064            feed.setPublishedDate( dataEntries.get( dataEntries.size() - 1 ).getPublishedDate() );
065            feed.setFeedType( DEFAULT_FEEDTYPE );
066            feed.setEntries( getEntries( dataEntries ) );
067    
068            log.debug( "Finished generating the feed \'{}\'.", title );
069            
070            return feed;
071        }
072    
073        private List<SyndEntry> getEntries( List<RssFeedEntry> dataEntries )
074        {
075            List<SyndEntry> entries = new ArrayList<SyndEntry>();
076    
077            SyndEntry entry;
078            SyndContent description;
079    
080            for ( RssFeedEntry dataEntry : dataEntries )
081            {
082                entry = new SyndEntryImpl();
083                entry.setTitle( dataEntry.getTitle() );
084                entry.setPublishedDate( dataEntry.getPublishedDate() );
085    
086                description = new SyndContentImpl();
087                description.setType( "text/plain" );
088                description.setValue( dataEntry.getDescription() );
089                entry.setDescription( description );
090    
091                entries.add( entry );
092            }
093    
094            return entries;
095        }
096    }