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.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.rss.RssFeedEntry;
28  import org.apache.archiva.rss.RssFeedGenerator;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.stereotype.Service;
32  
33  import javax.inject.Inject;
34  import java.util.ArrayList;
35  import java.util.Calendar;
36  import java.util.Date;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.TimeZone;
40  
41  /**
42   * Retrieve and process all artifacts of a repository from the database and generate a rss feed.
43   * The artifacts will be grouped by the date when the artifacts were gathered.
44   * Each group will appear as one entry in the feed.
45   *
46   */
47  @Service("rssFeedProcessor#new-artifacts")
48  public class NewArtifactsRssFeedProcessor
49      extends AbstractArtifactsRssFeedProcessor
50  {
51      private int numberOfDaysBeforeNow = 30;
52  
53      private static final String title = "New Artifacts in Repository ";
54  
55      private static final String desc = "These are the new artifacts found in the repository ";
56  
57      /**
58       *
59       */
60      @Inject
61      private RssFeedGenerator generator;
62  
63      private Logger log = LoggerFactory.getLogger( NewArtifactsRssFeedProcessor.class );
64  
65      private static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone( "GMT" );
66  
67      /**
68       * Process the newly discovered artifacts in the repository. Generate feeds for new artifacts in the repository and
69       * new versions of artifact.
70       */
71      @Override
72      public SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository )
73          throws FeedException
74      {
75          log.debug( "Process new artifacts into rss feeds." );
76  
77          String repoId = reqParams.get( RssFeedProcessor.KEY_REPO_ID );
78          if ( repoId != null )
79          {
80              return processNewArtifactsInRepo( repoId, metadataRepository );
81          }
82  
83          return null;
84      }
85  
86      private SyndFeed processNewArtifactsInRepo( String repoId, MetadataRepository metadataRepository )
87          throws FeedException
88      {
89          Calendar greaterThanThisDate = Calendar.getInstance( GMT_TIME_ZONE );
90          greaterThanThisDate.add( Calendar.DATE, -( getNumberOfDaysBeforeNow() ) );
91          greaterThanThisDate.clear( Calendar.MILLISECOND );
92  
93          List<ArtifactMetadata> artifacts;
94          try
95          {
96              artifacts = metadataRepository.getArtifactsByDateRange( repoId, greaterThanThisDate.getTime(), null );
97          }
98          catch ( MetadataRepositoryException e )
99          {
100             throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
101                                      e );
102         }
103 
104         long tmp = 0;
105         RssFeedEntry entry = null;
106         List<RssFeedEntry> entries = new ArrayList<>();
107         String description = "";
108         int idx = 0;
109         for ( ArtifactMetadata artifact : artifacts )
110         {
111             long whenGathered = artifact.getWhenGathered().getTime();
112 
113             String id = artifact.getNamespace() + "/" + artifact.getProject() + "/" + artifact.getId();
114             if ( tmp != whenGathered )
115             {
116                 if ( entry != null )
117                 {
118                     entry.setDescription( description );
119                     entries.add( entry );
120                     entry = null;
121                 }
122 
123                 String repoId1 = artifact.getRepositoryId();
124                 entry = new RssFeedEntry( this.getTitle() + "\'" + repoId1 + "\'" + " as of " + new Date(
125                     whenGathered ) );
126                 entry.setPublishedDate( artifact.getWhenGathered() );
127                 description = this.getDescription() + "\'" + repoId1 + "\'" + ": \n" + id + " | ";
128             }
129             else
130             {
131                 description = description + id + " | ";
132             }
133 
134             if ( idx == ( artifacts.size() - 1 ) )
135             {
136                 entry.setDescription( description );
137                 entries.add( entry );
138             }
139 
140             tmp = whenGathered;
141             idx++;
142         }
143 
144         return generator.generateFeed( getTitle() + "\'" + repoId + "\'",
145                                        "New artifacts found in repository " + "\'" + repoId + "\'" +
146                                            " during repository scan.", entries );
147     }
148 
149     @Override
150     public String getTitle()
151     {
152         return title;
153     }
154 
155     @Override
156     public String getDescription()
157     {
158         return desc;
159     }
160 
161     public RssFeedGenerator getGenerator()
162     {
163         return generator;
164     }
165 
166     public void setGenerator( RssFeedGenerator generator )
167     {
168         this.generator = generator;
169     }
170 
171     public int getNumberOfDaysBeforeNow()
172     {
173         return numberOfDaysBeforeNow;
174     }
175 
176     public void setNumberOfDaysBeforeNow( int numberOfDaysBeforeNow )
177     {
178         this.numberOfDaysBeforeNow = numberOfDaysBeforeNow;
179     }
180 }