This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.rss.processor;
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.sun.syndication.feed.synd.SyndFeed;
023import com.sun.syndication.io.FeedException;
024import org.apache.archiva.metadata.model.ArtifactMetadata;
025import org.apache.archiva.metadata.repository.MetadataRepository;
026import org.apache.archiva.metadata.repository.MetadataRepositoryException;
027import org.apache.archiva.rss.RssFeedEntry;
028import org.apache.archiva.rss.RssFeedGenerator;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import org.springframework.stereotype.Service;
032
033import javax.inject.Inject;
034import java.util.ArrayList;
035import java.util.Calendar;
036import java.util.Date;
037import java.util.List;
038import java.util.Map;
039import java.util.TimeZone;
040
041/**
042 * Retrieve and process all artifacts of a repository from the database and generate a rss feed.
043 * The artifacts will be grouped by the date when the artifacts were gathered.
044 * Each group will appear as one entry in the feed.
045 *
046 */
047@Service("rssFeedProcessor#new-artifacts")
048public class NewArtifactsRssFeedProcessor
049    extends AbstractArtifactsRssFeedProcessor
050{
051    private int numberOfDaysBeforeNow = 30;
052
053    private static final String title = "New Artifacts in Repository ";
054
055    private static final String desc = "These are the new artifacts found in the repository ";
056
057    /**
058     *
059     */
060    @Inject
061    private RssFeedGenerator generator;
062
063    private Logger log = LoggerFactory.getLogger( NewArtifactsRssFeedProcessor.class );
064
065    private static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone( "GMT" );
066
067    /**
068     * Process the newly discovered artifacts in the repository. Generate feeds for new artifacts in the repository and
069     * new versions of artifact.
070     */
071    @Override
072    public SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository )
073        throws FeedException
074    {
075        log.debug( "Process new artifacts into rss feeds." );
076
077        String repoId = reqParams.get( RssFeedProcessor.KEY_REPO_ID );
078        if ( repoId != null )
079        {
080            return processNewArtifactsInRepo( repoId, metadataRepository );
081        }
082
083        return null;
084    }
085
086    private SyndFeed processNewArtifactsInRepo( String repoId, MetadataRepository metadataRepository )
087        throws FeedException
088    {
089        Calendar greaterThanThisDate = Calendar.getInstance( GMT_TIME_ZONE );
090        greaterThanThisDate.add( Calendar.DATE, -( getNumberOfDaysBeforeNow() ) );
091        greaterThanThisDate.clear( Calendar.MILLISECOND );
092
093        List<ArtifactMetadata> artifacts;
094        try
095        {
096            artifacts = metadataRepository.getArtifactsByDateRange( repoId, greaterThanThisDate.getTime(), null );
097        }
098        catch ( MetadataRepositoryException e )
099        {
100            throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
101                                     e );
102        }
103
104        long tmp = 0;
105        RssFeedEntry entry = null;
106        List<RssFeedEntry> entries = new ArrayList<>();
107        String description = "";
108        int idx = 0;
109        for ( ArtifactMetadata artifact : artifacts )
110        {
111            long whenGathered = artifact.getWhenGathered().getTime();
112
113            String id = artifact.getNamespace() + "/" + artifact.getProject() + "/" + artifact.getId();
114            if ( tmp != whenGathered )
115            {
116                if ( entry != null )
117                {
118                    entry.setDescription( description );
119                    entries.add( entry );
120                    entry = null;
121                }
122
123                String repoId1 = artifact.getRepositoryId();
124                entry = new RssFeedEntry( this.getTitle() + "\'" + repoId1 + "\'" + " as of " + new Date(
125                    whenGathered ) );
126                entry.setPublishedDate( artifact.getWhenGathered() );
127                description = this.getDescription() + "\'" + repoId1 + "\'" + ": \n" + id + " | ";
128            }
129            else
130            {
131                description = description + id + " | ";
132            }
133
134            if ( idx == ( artifacts.size() - 1 ) )
135            {
136                entry.setDescription( description );
137                entries.add( entry );
138            }
139
140            tmp = whenGathered;
141            idx++;
142        }
143
144        return generator.generateFeed( getTitle() + "\'" + repoId + "\'",
145                                       "New artifacts found in repository " + "\'" + repoId + "\'" +
146                                           " during repository scan.", entries );
147    }
148
149    @Override
150    public String getTitle()
151    {
152        return title;
153    }
154
155    @Override
156    public String getDescription()
157    {
158        return desc;
159    }
160
161    public RssFeedGenerator getGenerator()
162    {
163        return generator;
164    }
165
166    public void setGenerator( RssFeedGenerator generator )
167    {
168        this.generator = generator;
169    }
170
171    public int getNumberOfDaysBeforeNow()
172    {
173        return numberOfDaysBeforeNow;
174    }
175
176    public void setNumberOfDaysBeforeNow( int numberOfDaysBeforeNow )
177    {
178        this.numberOfDaysBeforeNow = numberOfDaysBeforeNow;
179    }
180}