001package org.apache.archiva.rss.processor; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import com.sun.syndication.feed.synd.SyndFeed; 023import com.sun.syndication.io.FeedException; 024import org.apache.archiva.metadata.model.ArtifactMetadata; 025import org.apache.archiva.metadata.repository.MetadataRepository; 026import org.apache.archiva.metadata.repository.MetadataRepositoryException; 027import org.apache.archiva.metadata.repository.MetadataResolutionException; 028import org.apache.archiva.rss.RssFeedEntry; 029import org.apache.archiva.rss.RssFeedGenerator; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032import org.springframework.stereotype.Service; 033 034import javax.inject.Inject; 035import java.util.ArrayList; 036import java.util.Collection; 037import java.util.Date; 038import java.util.List; 039import java.util.Map; 040 041/** 042 * Retrieve and process new versions of an artifact from the database and 043 * generate a rss feed. The versions will be grouped by the date when the artifact 044 * was gathered. Each group will appear as one entry in the feed. 045 * 046 */ 047@Service("rssFeedProcessor#new-versions") 048public class NewVersionsOfArtifactRssFeedProcessor 049 extends AbstractArtifactsRssFeedProcessor 050{ 051 private Logger log = LoggerFactory.getLogger( NewVersionsOfArtifactRssFeedProcessor.class ); 052 053 private static final String title = "New Versions of Artifact "; 054 055 private static final String desc = "These are the new versions of artifact "; 056 057 @Inject 058 private RssFeedGenerator generator; 059 060 /** 061 * Process all versions of the artifact which had a rss feed request. 062 */ 063 @Override 064 public SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository ) 065 throws FeedException 066 { 067 String groupId = reqParams.get( RssFeedProcessor.KEY_GROUP_ID ); 068 String artifactId = reqParams.get( RssFeedProcessor.KEY_ARTIFACT_ID ); 069 070 if ( groupId != null && artifactId != null ) 071 { 072 return processNewVersionsOfArtifact( groupId, artifactId, metadataRepository ); 073 } 074 075 return null; 076 } 077 078 private SyndFeed processNewVersionsOfArtifact( String groupId, String artifactId, 079 MetadataRepository metadataRepository ) 080 throws FeedException 081 { 082 List<ArtifactMetadata> artifacts = new ArrayList<>(); 083 try 084 { 085 for ( String repoId : metadataRepository.getRepositories() ) 086 { 087 Collection<String> versions = metadataRepository.getProjectVersions( repoId, groupId, artifactId ); 088 for ( String version : versions ) 089 { 090 artifacts.addAll( metadataRepository.getArtifacts( repoId, groupId, artifactId, version ) ); 091 } 092 } 093 } 094 catch ( MetadataRepositoryException e ) 095 { 096 throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(), 097 e ); 098 } 099 catch ( MetadataResolutionException e ) 100 { 101 throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(), 102 e ); 103 } 104 105 long tmp = 0; 106 RssFeedEntry entry = null; 107 List<RssFeedEntry> entries = new ArrayList<>(); 108 String description = ""; 109 int idx = 0; 110 for ( ArtifactMetadata artifact : artifacts ) 111 { 112 long whenGathered = artifact.getWhenGathered().getTime(); 113 114 if ( tmp != whenGathered ) 115 { 116 if ( entry != null ) 117 { 118 entry.setDescription( description ); 119 entries.add( entry ); 120 entry = null; 121 } 122 123 entry = new RssFeedEntry( 124 this.getTitle() + "\'" + groupId + ":" + artifactId + "\'" + " as of " + new Date( whenGathered ) ); 125 entry.setPublishedDate( artifact.getWhenGathered() ); 126 description = 127 this.getDescription() + "\'" + groupId + ":" + artifactId + "\'" + ": \n" + artifact.getId() + 128 " | "; 129 } 130 else 131 { 132 description = description + artifact.getId() + " | "; 133 } 134 135 if ( idx == ( artifacts.size() - 1 ) ) 136 { 137 entry.setDescription( description ); 138 entries.add( entry ); 139 } 140 141 tmp = whenGathered; 142 idx++; 143 } 144 145 String key = groupId + ":" + artifactId; 146 147 return generator.generateFeed( getTitle() + "\'" + key + "\'", 148 "New versions of artifact " + "\'" + key + "\' found during repository scan.", 149 entries ); 150 } 151 152 @Override 153 public String getTitle() 154 { 155 return title; 156 } 157 158 @Override 159 public String getDescription() 160 { 161 return desc; 162 } 163 164 public RssFeedGenerator getGenerator() 165 { 166 return generator; 167 } 168 169 public void setGenerator( RssFeedGenerator generator ) 170 { 171 this.generator = generator; 172 } 173}