This project has retired. For details please refer to its Attic page.
Source code
001package 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
022import com.rometools.rome.feed.synd.SyndContent;
023import com.rometools.rome.feed.synd.SyndContentImpl;
024import com.rometools.rome.feed.synd.SyndEntry;
025import com.rometools.rome.feed.synd.SyndEntryImpl;
026import com.rometools.rome.feed.synd.SyndFeed;
027import com.rometools.rome.feed.synd.SyndFeedImpl;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.springframework.context.annotation.Scope;
031import org.springframework.stereotype.Service;
032
033import java.util.ArrayList;
034import java.util.List;
035
036/**
037 * Generates RSS feeds.
038 *
039 */
040@Service("rssFeedGenerator#default")
041@Scope("prototype")
042public class RssFeedGenerator
043{
044    private Logger log = LoggerFactory.getLogger( RssFeedGenerator.class );
045
046    // TODO: make configurable
047    public static String DEFAULT_FEEDTYPE = "rss_2.0";
048
049    public static String DEFAULT_LANGUAGE = "en-us";
050
051    public SyndFeed generateFeed( String title, String description, List<RssFeedEntry> dataEntries )
052    {
053        if( dataEntries.size() ==  0 )
054        {
055            log.debug( "No updates found, feed not generated." );
056            return null;
057        }
058        
059        SyndFeed feed = new SyndFeedImpl();
060        feed.setTitle( title );        
061        feed.setDescription( description );
062        feed.setLanguage( DEFAULT_LANGUAGE );
063        feed.setPublishedDate( dataEntries.get( dataEntries.size() - 1 ).getPublishedDate() );
064        feed.setFeedType( DEFAULT_FEEDTYPE );
065        feed.setEntries( getEntries( dataEntries ) );
066
067        log.debug( "Finished generating the feed \'{}\'.", title );
068        
069        return feed;
070    }
071
072    private List<SyndEntry> getEntries( List<RssFeedEntry> dataEntries )
073    {
074        List<SyndEntry> entries = new ArrayList<>();
075
076        SyndEntry entry;
077        SyndContent description;
078
079        for ( RssFeedEntry dataEntry : dataEntries )
080        {
081            entry = new SyndEntryImpl();
082            entry.setTitle( dataEntry.getTitle() );
083            entry.setPublishedDate( dataEntry.getPublishedDate() );
084
085            description = new SyndContentImpl();
086            description.setType( "text/plain" );
087            description.setValue( dataEntry.getDescription() );
088            entry.setDescription( description );
089
090            entries.add( entry );
091        }
092
093        return entries;
094    }
095}