This project has retired. For details please refer to its Attic page.
NewArtifactsRssFeedProcessor xref
View Javadoc
1   package org.apache.archiva.rss.processor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.rometools.rome.feed.synd.SyndFeed;
23  import com.rometools.rome.io.FeedException;
24  import org.apache.archiva.metadata.model.ArtifactMetadata;
25  import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26  import org.apache.archiva.metadata.repository.RepositorySession;
27  import org.apache.archiva.metadata.repository.RepositorySessionFactory;
28  import org.apache.archiva.rss.RssFeedEntry;
29  import org.apache.archiva.rss.RssFeedGenerator;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.stereotype.Service;
33  
34  import javax.inject.Inject;
35  import java.time.ZonedDateTime;
36  import java.time.temporal.ChronoUnit;
37  import java.util.ArrayList;
38  import java.util.Date;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.TimeZone;
42  
43  /**
44   * Retrieve and process all artifacts of a repository from the database and generate a rss feed.
45   * The artifacts will be grouped by the date when the artifacts were gathered.
46   * Each group will appear as one entry in the feed.
47   *
48   */
49  @Service("rssFeedProcessor#new-artifacts")
50  public class NewArtifactsRssFeedProcessor
51      extends AbstractArtifactsRssFeedProcessor
52  {
53      private int numberOfDaysBeforeNow = 30;
54  
55      private static final String title = "New Artifacts in Repository ";
56  
57      private static final String desc = "These are the new artifacts found in the repository ";
58  
59      /**
60       *
61       */
62      @Inject
63      private RssFeedGenerator generator;
64  
65      @Inject
66      private RepositorySessionFactory repositorySessionFactory;
67  
68  
69      private Logger log = LoggerFactory.getLogger( NewArtifactsRssFeedProcessor.class );
70  
71  
72      /**
73       * Process the newly discovered artifacts in the repository. Generate feeds for new artifacts in the repository and
74       * new versions of artifact.
75       */
76      @Override
77      public SyndFeed process( Map<String, String> reqParams )
78          throws FeedException
79      {
80          log.debug( "Process new artifacts into rss feeds." );
81  
82          String repoId = reqParams.get( RssFeedProcessor.KEY_REPO_ID );
83          if ( repoId != null )
84          {
85              return processNewArtifactsInRepo( repoId );
86          }
87  
88          return null;
89      }
90  
91      private SyndFeed processNewArtifactsInRepo( String repoId )
92          throws FeedException
93      {
94  
95          ZonedDateTime greaterThanThisDate = ZonedDateTime.now().minusDays(
96                  getNumberOfDaysBeforeNow()
97          ).truncatedTo(ChronoUnit.SECONDS);
98          List<ArtifactMetadata> artifacts;
99          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 }