You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewArtifactsRssFeedProcessor.java 5.8KB

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