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.rometools.rome.feed.synd.SyndFeed;
023import com.rometools.rome.io.FeedException;
024import org.apache.archiva.metadata.model.ArtifactMetadata;
025import org.apache.archiva.metadata.repository.*;
026import org.apache.archiva.repository.Repository;
027import org.apache.archiva.repository.RepositoryRegistry;
028import org.apache.archiva.rss.RssFeedEntry;
029import org.apache.archiva.rss.RssFeedGenerator;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.springframework.stereotype.Service;
033
034import javax.inject.Inject;
035import java.time.ZoneId;
036import java.util.ArrayList;
037import java.util.Collection;
038import java.util.Date;
039import java.util.List;
040import java.util.Map;
041
042/**
043 * Retrieve and process new versions of an artifact from the database and
044 * generate a rss feed. The versions will be grouped by the date when the artifact
045 * was gathered. Each group will appear as one entry in the feed.
046 *
047 */
048@Service("rssFeedProcessor#new-versions")
049public class NewVersionsOfArtifactRssFeedProcessor
050    extends AbstractArtifactsRssFeedProcessor
051{
052    private Logger log = LoggerFactory.getLogger( NewVersionsOfArtifactRssFeedProcessor.class );
053
054    private static final String title = "New Versions of Artifact ";
055
056    private static final String desc = "These are the new versions of artifact ";
057
058    @Inject
059    private RssFeedGenerator generator;
060
061    @Inject
062    private RepositoryRegistry repositoryRegistry;
063
064    @Inject
065    private RepositorySessionFactory repositorySessionFactory;
066
067    /**
068     * Process all versions of the artifact which had a rss feed request.
069     */
070    @Override
071    public SyndFeed process( Map<String, String> reqParams )
072        throws FeedException
073    {
074        String groupId = reqParams.get( RssFeedProcessor.KEY_GROUP_ID );
075        String artifactId = reqParams.get( RssFeedProcessor.KEY_ARTIFACT_ID );
076
077        if ( groupId != null && artifactId != null )
078        {
079            return processNewVersionsOfArtifact( groupId, artifactId );
080        }
081
082        return null;
083    }
084
085    private SyndFeed processNewVersionsOfArtifact( String groupId, String artifactId )
086        throws FeedException
087    {
088        List<ArtifactMetadata> artifacts = new ArrayList<>();
089        try(RepositorySession session = repositorySessionFactory.createSession())
090        {
091            final MetadataRepository metadataRepository = session.getRepository( );
092            for ( Repository repo : repositoryRegistry.getRepositories() )
093            {
094                final String repoId = repo.getId();
095                Collection<String> versions = metadataRepository.getProjectVersions( session, repoId, groupId, artifactId );
096                for ( String version : versions )
097                {
098                    artifacts.addAll( metadataRepository.getArtifacts(session , repoId, groupId, artifactId, version ) );
099                }
100            }
101        }
102        catch ( MetadataRepositoryException e )
103        {
104            throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
105                                     e );
106        }
107        catch ( MetadataResolutionException e )
108        {
109            throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
110                                     e );
111        }
112
113        long tmp = 0;
114        RssFeedEntry entry = null;
115        List<RssFeedEntry> entries = new ArrayList<>();
116        String description = "";
117        int idx = 0;
118        for ( ArtifactMetadata artifact : artifacts )
119        {
120            long whenGathered = artifact.getWhenGathered().toInstant().toEpochMilli();
121
122            if ( tmp != whenGathered )
123            {
124                if ( entry != null )
125                {
126                    entry.setDescription( description );
127                    entries.add( entry );
128                    entry = null;
129                }
130
131                entry = new RssFeedEntry(
132                    this.getTitle() + "\'" + groupId + ":" + artifactId + "\'" + " as of " + new Date( whenGathered ) );
133                entry.setPublishedDate( Date.from(artifact.getWhenGathered().toInstant()) );
134                description =
135                    this.getDescription() + "\'" + groupId + ":" + artifactId + "\'" + ": \n" + artifact.getId() +
136                        " | ";
137            }
138            else
139            {
140                description = description + artifact.getId() + " | ";
141            }
142
143            if ( idx == ( artifacts.size() - 1 ) )
144            {
145                entry.setDescription( description );
146                entries.add( entry );
147            }
148
149            tmp = whenGathered;
150            idx++;
151        }
152
153        String key = groupId + ":" + artifactId;
154
155        return generator.generateFeed( getTitle() + "\'" + key + "\'",
156                                       "New versions of artifact " + "\'" + key + "\' found during repository scan.",
157                                       entries );
158    }
159
160    @Override
161    public String getTitle()
162    {
163        return title;
164    }
165
166    @Override
167    public String getDescription()
168    {
169        return desc;
170    }
171
172    public RssFeedGenerator getGenerator()
173    {
174        return generator;
175    }
176
177    public void setGenerator( RssFeedGenerator generator )
178    {
179        this.generator = generator;
180    }
181
182    public RepositorySessionFactory getRepositorySessionFactory( )
183    {
184        return repositorySessionFactory;
185    }
186
187    public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
188    {
189        this.repositorySessionFactory = repositorySessionFactory;
190    }
191
192    public RepositoryRegistry getRepositoryRegistry( )
193    {
194        return repositoryRegistry;
195    }
196
197    public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
198    {
199        this.repositoryRegistry = repositoryRegistry;
200    }
201}