選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NewVersionsOfArtifactRssFeedProcessor.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package org.apache.archiva.rss.processor;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import com.sun.syndication.feed.synd.SyndFeed;
  21. import com.sun.syndication.io.FeedException;
  22. import org.apache.archiva.metadata.model.ArtifactMetadata;
  23. import org.apache.archiva.metadata.repository.MetadataRepository;
  24. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  25. import org.apache.archiva.metadata.repository.MetadataResolutionException;
  26. import org.apache.archiva.rss.RssFeedEntry;
  27. import org.apache.archiva.rss.RssFeedGenerator;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.stereotype.Service;
  31. import javax.inject.Inject;
  32. import java.util.ArrayList;
  33. import java.util.Collection;
  34. import java.util.Date;
  35. import java.util.List;
  36. import java.util.Map;
  37. /**
  38. * Retrieve and process new versions of an artifact from the database and
  39. * generate a rss feed. The versions will be grouped by the date when the artifact
  40. * was gathered. Each group will appear as one entry in the feed.
  41. *
  42. */
  43. @Service("rssFeedProcessor#new-versions")
  44. public class NewVersionsOfArtifactRssFeedProcessor
  45. extends AbstractArtifactsRssFeedProcessor
  46. {
  47. private Logger log = LoggerFactory.getLogger( NewVersionsOfArtifactRssFeedProcessor.class );
  48. private static final String title = "New Versions of Artifact ";
  49. private static final String desc = "These are the new versions of artifact ";
  50. /**
  51. *
  52. */
  53. @Inject
  54. private RssFeedGenerator generator;
  55. /**
  56. * Process all versions of the artifact which had a rss feed request.
  57. */
  58. public SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository )
  59. throws FeedException
  60. {
  61. String groupId = reqParams.get( RssFeedProcessor.KEY_GROUP_ID );
  62. String artifactId = reqParams.get( RssFeedProcessor.KEY_ARTIFACT_ID );
  63. if ( groupId != null && artifactId != null )
  64. {
  65. return processNewVersionsOfArtifact( groupId, artifactId, metadataRepository );
  66. }
  67. return null;
  68. }
  69. private SyndFeed processNewVersionsOfArtifact( String groupId, String artifactId,
  70. MetadataRepository metadataRepository )
  71. throws FeedException
  72. {
  73. List<ArtifactMetadata> artifacts = new ArrayList<>();
  74. try
  75. {
  76. for ( String repoId : metadataRepository.getRepositories() )
  77. {
  78. Collection<String> versions = metadataRepository.getProjectVersions( repoId, groupId, artifactId );
  79. for ( String version : versions )
  80. {
  81. artifacts.addAll( metadataRepository.getArtifacts( repoId, groupId, artifactId, version ) );
  82. }
  83. }
  84. }
  85. catch ( MetadataRepositoryException e )
  86. {
  87. throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
  88. e );
  89. }
  90. catch ( MetadataResolutionException e )
  91. {
  92. throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
  93. e );
  94. }
  95. long tmp = 0;
  96. RssFeedEntry entry = null;
  97. List<RssFeedEntry> entries = new ArrayList<>();
  98. String description = "";
  99. int idx = 0;
  100. for ( ArtifactMetadata artifact : artifacts )
  101. {
  102. long whenGathered = artifact.getWhenGathered().getTime();
  103. if ( tmp != whenGathered )
  104. {
  105. if ( entry != null )
  106. {
  107. entry.setDescription( description );
  108. entries.add( entry );
  109. entry = null;
  110. }
  111. entry = new RssFeedEntry(
  112. this.getTitle() + "\'" + groupId + ":" + artifactId + "\'" + " as of " + new Date( whenGathered ) );
  113. entry.setPublishedDate( artifact.getWhenGathered() );
  114. description =
  115. this.getDescription() + "\'" + groupId + ":" + artifactId + "\'" + ": \n" + artifact.getId() +
  116. " | ";
  117. }
  118. else
  119. {
  120. description = description + artifact.getId() + " | ";
  121. }
  122. if ( idx == ( artifacts.size() - 1 ) )
  123. {
  124. entry.setDescription( description );
  125. entries.add( entry );
  126. }
  127. tmp = whenGathered;
  128. idx++;
  129. }
  130. String key = groupId + ":" + artifactId;
  131. return generator.generateFeed( getTitle() + "\'" + key + "\'",
  132. "New versions of artifact " + "\'" + key + "\' found during repository scan.",
  133. entries );
  134. }
  135. public String getTitle()
  136. {
  137. return title;
  138. }
  139. public String getDescription()
  140. {
  141. return desc;
  142. }
  143. public RssFeedGenerator getGenerator()
  144. {
  145. return generator;
  146. }
  147. public void setGenerator( RssFeedGenerator generator )
  148. {
  149. this.generator = generator;
  150. }
  151. }