1 package org.apache.archiva.consumers.core;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.plexusbridge.DigesterUtils;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
25 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
26 import org.apache.archiva.consumers.ConsumerException;
27 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.archiva.repository.ManagedRepository;
29 import org.codehaus.plexus.digest.ChecksumFile;
30 import org.codehaus.plexus.digest.Digester;
31 import org.codehaus.plexus.digest.DigesterException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Service;
37 import javax.annotation.PostConstruct;
38 import javax.inject.Inject;
39 import java.io.FileNotFoundException;
40 import java.io.IOException;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.ArrayList;
44 import java.util.Date;
45 import java.util.List;
48 * ValidateChecksumConsumer - validate the provided checksum against the file it represents.
50 @Service( "knownRepositoryContentConsumer#validate-checksums" )
52 public class ValidateChecksumConsumer
53 extends AbstractMonitoredConsumer
54 implements KnownRepositoryContentConsumer
56 private Logger log = LoggerFactory.getLogger( ValidateChecksumConsumer.class );
58 private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
60 private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
62 private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
64 private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
66 private String id = "validate-checksums";
68 private String description = "Validate checksums against file.";
70 private ChecksumFile checksum;
72 private List<Digester> allDigesters;
75 private PlexusSisuBridge plexusSisuBridge;
78 private DigesterUtils digesterUtils;
80 private Path repositoryDir;
82 private List<String> includes;
85 public String getId( )
91 public String getDescription( )
93 return this.description;
97 public void beginScan( ManagedRepository repository, Date whenGathered )
98 throws ConsumerException
100 this.repositoryDir = Paths.get( repository.getLocation( ) );
104 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
105 throws ConsumerException
107 beginScan( repository, whenGathered );
111 public void completeScan( )
117 public void completeScan( boolean executeOnEntireRepo )
123 public List<String> getExcludes( )
129 public List<String> getIncludes( )
131 return this.includes;
135 public void processFile( String path )
136 throws ConsumerException
138 Path checksumFile = this.repositoryDir.resolve( path );
141 if ( !checksum.isValidChecksum( checksumFile.toFile() ) )
143 log.warn( "The checksum for {} is invalid.", checksumFile );
144 triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
147 catch ( FileNotFoundException e )
149 log.error( "File not found during checksum validation: ", e );
150 triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage( ) );
152 catch ( DigesterException e )
154 log.error( "Digester failure during checksum validation on {}", checksumFile );
155 triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
156 "Digester failure during checksum validation on " + checksumFile );
158 catch ( IOException e )
160 log.error( "Checksum I/O error during validation on {}", checksumFile );
161 triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
166 public void processFile( String path, boolean executeOnEntireReDpo )
173 public void initialize( )
174 throws PlexusSisuBridgeException
176 checksum = plexusSisuBridge.lookup( ChecksumFile.class );
177 List<Digester> allDigesters = new ArrayList<>( digesterUtils.getAllDigesters( ) );
178 includes = new ArrayList<>( allDigesters.size( ) );
179 for ( Digester digester : allDigesters )
181 includes.add( "**/*" + digester.getFilenameExtension( ) );