This project has retired. For details please refer to its
Attic page.
ValidateChecksumConsumer xref
1 package org.apache.archiva.consumers.core;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.checksum.ChecksumAlgorithm;
23 import org.apache.archiva.checksum.ChecksumReference;
24 import org.apache.archiva.checksum.ChecksumValidationException;
25 import org.apache.archiva.checksum.ChecksummedFile;
26 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.archiva.consumers.ConsumerException;
28 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.archiva.repository.ManagedRepository;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.context.annotation.Scope;
33 import org.springframework.stereotype.Service;
34
35 import javax.annotation.PostConstruct;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.Date;
40 import java.util.List;
41 import java.util.Set;
42
43 import static org.apache.archiva.checksum.ChecksumValidationException.ValidationError.*;
44
45
46
47
48 @Service( "knownRepositoryContentConsumer#validate-checksums" )
49 @Scope( "prototype" )
50 public class ValidateChecksumConsumer
51 extends AbstractMonitoredConsumer
52 implements KnownRepositoryContentConsumer
53 {
54 private Logger log = LoggerFactory.getLogger( ValidateChecksumConsumer.class );
55
56 private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
57
58 private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
59
60 private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
61
62 private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
63
64 private String id = "validate-checksums";
65
66 private String description = "Validate checksums against file.";
67
68 private Path repositoryDir;
69
70 private List<String> includes;
71
72 @Override
73 public String getId( )
74 {
75 return this.id;
76 }
77
78 @Override
79 public String getDescription( )
80 {
81 return this.description;
82 }
83
84 @Override
85 public void beginScan( ManagedRepository repository, Date whenGathered )
86 throws ConsumerException
87 {
88 this.repositoryDir = Paths.get( repository.getLocation( ) );
89 }
90
91 @Override
92 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
93 throws ConsumerException
94 {
95 beginScan( repository, whenGathered );
96 }
97
98 @Override
99 public void completeScan( )
100 {
101
102 }
103
104 @Override
105 public void completeScan( boolean executeOnEntireRepo )
106 {
107 completeScan( );
108 }
109
110 @Override
111 public List<String> getExcludes( )
112 {
113 return null;
114 }
115
116 @Override
117 public List<String> getIncludes( )
118 {
119 return this.includes;
120 }
121
122 @Override
123 public void processFile( String path )
124 throws ConsumerException
125 {
126 Path checksumFile = this.repositoryDir.resolve( path );
127 try
128 {
129 ChecksumReference cf = ChecksummedFile.getFromChecksumFile( checksumFile );
130 if ( !cf.getFile().isValidChecksum( cf.getAlgorithm(), true ) )
131 {
132 log.warn( "The checksum for {} is invalid.", checksumFile );
133 triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
134 }
135 }
136 catch ( ChecksumValidationException e )
137 {
138 if (e.getErrorType()==READ_ERROR) {
139 log.error( "Checksum read error during validation on {}", checksumFile );
140 triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
141 } else if (e.getErrorType()==INVALID_FORMAT || e.getErrorType()==DIGEST_ERROR) {
142 log.error( "Digester failure during checksum validation on {}", checksumFile );
143 triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
144 "Digester failure during checksum validation on " + checksumFile );
145 } else if (e.getErrorType()==FILE_NOT_FOUND) {
146 log.error( "File not found during checksum validation: ", e );
147 triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage( ) );
148 }
149 }
150 }
151
152 @Override
153 public void processFile( String path, boolean executeOnEntireReDpo )
154 throws Exception
155 {
156 processFile( path );
157 }
158
159 @PostConstruct
160 public void initialize( )
161 {
162 Set<String> extensions = ChecksumAlgorithm.getAllExtensions();
163 includes = new ArrayList<>( extensions.size() );
164 for ( String ext : extensions )
165 {
166 includes.add( "**/*." + ext );
167 }
168 }
169 }