This project has retired. For details please refer to its Attic page.
NewVersionsOfArtifactRssFeedProcessor 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.*;
26  import org.apache.archiva.repository.Repository;
27  import org.apache.archiva.repository.RepositoryRegistry;
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.ZoneId;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Date;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * Retrieve and process new versions of an artifact from the database and
44   * generate a rss feed. The versions will be grouped by the date when the artifact
45   * was gathered. Each group will appear as one entry in the feed.
46   *
47   */
48  @Service("rssFeedProcessor#new-versions")
49  public class NewVersionsOfArtifactRssFeedProcessor
50      extends AbstractArtifactsRssFeedProcessor
51  {
52      private Logger log = LoggerFactory.getLogger( NewVersionsOfArtifactRssFeedProcessor.class );
53  
54      private static final String title = "New Versions of Artifact ";
55  
56      private static final String desc = "These are the new versions of artifact ";
57  
58      @Inject
59      private RssFeedGenerator generator;
60  
61      @Inject
62      private RepositoryRegistry repositoryRegistry;
63  
64      @Inject
65      private RepositorySessionFactory repositorySessionFactory;
66  
67      /**
68       * Process all versions of the artifact which had a rss feed request.
69       */
70      @Override
71      public SyndFeed process( Map<String, String> reqParams )
72          throws FeedException
73      {
74          String groupId = reqParams.get( RssFeedProcessor.KEY_GROUP_ID );
75          String artifactId = reqParams.get( RssFeedProcessor.KEY_ARTIFACT_ID );
76  
77          if ( groupId != null && artifactId != null )
78          {
79              return processNewVersionsOfArtifact( groupId, artifactId );
80          }
81  
82          return null;
83      }
84  
85      private SyndFeed processNewVersionsOfArtifact( String groupId, String artifactId )
86          throws FeedException
87      {
88          List<ArtifactMetadata> artifacts = new ArrayList<>();
89          try(RepositorySession session = repositorySessionFactory.createSession())
90          {
91              final MetadataRepository metadataRepository = session.getRepository( );
92              for ( Repository repo : repositoryRegistry.getRepositories() )
93              {
94                  final String repoId = repo.getId();
95                  Collection<String> versions = metadataRepository.getProjectVersions( session, repoId, groupId, artifactId );
96                  for ( String version : versions )
97                  {
98                      artifacts.addAll( metadataRepository.getArtifacts(session , repoId, groupId, artifactId, version ) );
99                  }
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 }