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.MetadataRepositoryException;
026import org.apache.archiva.metadata.repository.RepositorySession;
027import org.apache.archiva.metadata.repository.RepositorySessionFactory;
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.ZonedDateTime;
036import java.time.temporal.ChronoUnit;
037import java.util.ArrayList;
038import java.util.Date;
039import java.util.List;
040import java.util.Map;
041import java.util.TimeZone;
042
043/**
044 * Retrieve and process all artifacts of a repository from the database and generate a rss feed.
045 * The artifacts will be grouped by the date when the artifacts were gathered.
046 * Each group will appear as one entry in the feed.
047 *
048 */
049@Service("rssFeedProcessor#new-artifacts")
050public class NewArtifactsRssFeedProcessor
051    extends AbstractArtifactsRssFeedProcessor
052{
053    private int numberOfDaysBeforeNow = 30;
054
055    private static final String title = "New Artifacts in Repository ";
056
057    private static final String desc = "These are the new artifacts found in the repository ";
058
059    /**
060     *
061     */
062    @Inject
063    private RssFeedGenerator generator;
064
065    @Inject
066    private RepositorySessionFactory repositorySessionFactory;
067
068
069    private Logger log = LoggerFactory.getLogger( NewArtifactsRssFeedProcessor.class );
070
071
072    /**
073     * Process the newly discovered artifacts in the repository. Generate feeds for new artifacts in the repository and
074     * new versions of artifact.
075     */
076    @Override
077    public SyndFeed process( Map<String, String> reqParams )
078        throws FeedException
079    {
080        log.debug( "Process new artifacts into rss feeds." );
081
082        String repoId = reqParams.get( RssFeedProcessor.KEY_REPO_ID );
083        if ( repoId != null )
084        {
085            return processNewArtifactsInRepo( repoId );
086        }
087
088        return null;
089    }
090
091    private SyndFeed processNewArtifactsInRepo( String repoId )
092        throws FeedException
093    {
094
095        ZonedDateTime greaterThanThisDate = ZonedDateTime.now().minusDays(
096                getNumberOfDaysBeforeNow()
097        ).truncatedTo(ChronoUnit.SECONDS);
098        List<ArtifactMetadata> artifacts;
099        try(RepositorySession session = repositorySessionFactory.createSession())
100        {
101            artifacts = session.getRepository().getArtifactsByDateRange(session , repoId, greaterThanThisDate, null );
102        }
103        catch ( MetadataRepositoryException e )
104        {
105            throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
106                                     e );
107        }
108
109        long tmp = 0;
110        RssFeedEntry entry = null;
111        List<RssFeedEntry> entries = new ArrayList<>();
112        String description = "";
113        int idx = 0;
114        for ( ArtifactMetadata artifact : artifacts )
115        {
116            long whenGathered = artifact.getWhenGathered().toInstant().toEpochMilli();
117
118            String id = artifact.getNamespace() + "/" + artifact.getProject() + "/" + artifact.getId();
119            if ( tmp != whenGathered )
120            {
121                if ( entry != null )
122                {
123                    entry.setDescription( description );
124                    entries.add( entry );
125                    entry = null;
126                }
127
128                String repoId1 = artifact.getRepositoryId();
129                entry = new RssFeedEntry( this.getTitle() + "\'" + repoId1 + "\'" + " as of " + new Date(
130                    whenGathered ) );
131                entry.setPublishedDate( Date.from(artifact.getWhenGathered().toInstant()) );
132                description = this.getDescription() + "\'" + repoId1 + "\'" + ": \n" + id + " | ";
133            }
134            else
135            {
136                description = description + id + " | ";
137            }
138
139            if ( idx == ( artifacts.size() - 1 ) )
140            {
141                entry.setDescription( description );
142                entries.add( entry );
143            }
144
145            tmp = whenGathered;
146            idx++;
147        }
148
149        return generator.generateFeed( getTitle() + "\'" + repoId + "\'",
150                                       "New artifacts found in repository " + "\'" + repoId + "\'" +
151                                           " during repository scan.", entries );
152    }
153
154    @Override
155    public String getTitle()
156    {
157        return title;
158    }
159
160    @Override
161    public String getDescription()
162    {
163        return desc;
164    }
165
166    public RssFeedGenerator getGenerator()
167    {
168        return generator;
169    }
170
171    public void setGenerator( RssFeedGenerator generator )
172    {
173        this.generator = generator;
174    }
175
176    public int getNumberOfDaysBeforeNow()
177    {
178        return numberOfDaysBeforeNow;
179    }
180
181    public void setNumberOfDaysBeforeNow( int numberOfDaysBeforeNow )
182    {
183        this.numberOfDaysBeforeNow = numberOfDaysBeforeNow;
184    }
185
186    public RepositorySessionFactory getRepositorySessionFactory( )
187    {
188        return repositorySessionFactory;
189    }
190
191    public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
192    {
193        this.repositorySessionFactory = repositorySessionFactory;
194    }
195}