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.sun.syndication.feed.synd.SyndFeed;
23  import com.sun.syndication.io.FeedException;
24  import org.apache.archiva.metadata.model.ArtifactMetadata;
25  import org.apache.archiva.metadata.repository.MetadataRepository;
26  import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27  import org.apache.archiva.metadata.repository.MetadataResolutionException;
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.util.ArrayList;
36  import java.util.Collection;
37  import java.util.Date;
38  import java.util.List;
39  import java.util.Map;
40  
41  /**
42   * Retrieve and process new versions of an artifact from the database and
43   * generate a rss feed. The versions will be grouped by the date when the artifact
44   * was gathered. Each group will appear as one entry in the feed.
45   *
46   */
47  @Service("rssFeedProcessor#new-versions")
48  public class NewVersionsOfArtifactRssFeedProcessor
49      extends AbstractArtifactsRssFeedProcessor
50  {
51      private Logger log = LoggerFactory.getLogger( NewVersionsOfArtifactRssFeedProcessor.class );
52  
53      private static final String title = "New Versions of Artifact ";
54  
55      private static final String desc = "These are the new versions of artifact ";
56  
57      @Inject
58      private RssFeedGenerator generator;
59  
60      /**
61       * Process all versions of the artifact which had a rss feed request.
62       */
63      @Override
64      public SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository )
65          throws FeedException
66      {
67          String groupId = reqParams.get( RssFeedProcessor.KEY_GROUP_ID );
68          String artifactId = reqParams.get( RssFeedProcessor.KEY_ARTIFACT_ID );
69  
70          if ( groupId != null && artifactId != null )
71          {
72              return processNewVersionsOfArtifact( groupId, artifactId, metadataRepository );
73          }
74  
75          return null;
76      }
77  
78      private SyndFeed processNewVersionsOfArtifact( String groupId, String artifactId,
79                                                     MetadataRepository metadataRepository )
80          throws FeedException
81      {
82          List<ArtifactMetadata> artifacts = new ArrayList<>();
83          try
84          {
85              for ( String repoId : metadataRepository.getRepositories() )
86              {
87                  Collection<String> versions = metadataRepository.getProjectVersions( repoId, groupId, artifactId );
88                  for ( String version : versions )
89                  {
90                      artifacts.addAll( metadataRepository.getArtifacts( repoId, groupId, artifactId, version ) );
91                  }
92              }
93          }
94          catch ( MetadataRepositoryException e )
95          {
96              throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
97                                       e );
98          }
99          catch ( MetadataResolutionException e )
100         {
101             throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
102                                      e );
103         }
104 
105         long tmp = 0;
106         RssFeedEntry entry = null;
107         List<RssFeedEntry> entries = new ArrayList<>();
108         String description = "";
109         int idx = 0;
110         for ( ArtifactMetadata artifact : artifacts )
111         {
112             long whenGathered = artifact.getWhenGathered().getTime();
113 
114             if ( tmp != whenGathered )
115             {
116                 if ( entry != null )
117                 {
118                     entry.setDescription( description );
119                     entries.add( entry );
120                     entry = null;
121                 }
122 
123                 entry = new RssFeedEntry(
124                     this.getTitle() + "\'" + groupId + ":" + artifactId + "\'" + " as of " + new Date( whenGathered ) );
125                 entry.setPublishedDate( artifact.getWhenGathered() );
126                 description =
127                     this.getDescription() + "\'" + groupId + ":" + artifactId + "\'" + ": \n" + artifact.getId() +
128                         " | ";
129             }
130             else
131             {
132                 description = description + artifact.getId() + " | ";
133             }
134 
135             if ( idx == ( artifacts.size() - 1 ) )
136             {
137                 entry.setDescription( description );
138                 entries.add( entry );
139             }
140 
141             tmp = whenGathered;
142             idx++;
143         }
144 
145         String key = groupId + ":" + artifactId;
146 
147         return generator.generateFeed( getTitle() + "\'" + key + "\'",
148                                        "New versions of artifact " + "\'" + key + "\' found during repository scan.",
149                                        entries );
150     }
151 
152     @Override
153     public String getTitle()
154     {
155         return title;
156     }
157 
158     @Override
159     public String getDescription()
160     {
161         return desc;
162     }
163 
164     public RssFeedGenerator getGenerator()
165     {
166         return generator;
167     }
168 
169     public void setGenerator( RssFeedGenerator generator )
170     {
171         this.generator = generator;
172     }
173 }