This project has retired. For details please refer to its
Attic page.
NewVersionsOfArtifactRssFeedProcessor 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.*;
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
44
45
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
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 }