This project has retired. For details please refer to its
Attic page.
NewArtifactsRssFeedProcessor xref
1 package org.apache.archiva.rss.processor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
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
74
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 }