]> source.dussan.org Git - archiva.git/blob
06930bd4e3626332ab59366804218e2d21831394
[archiva.git] /
1 package org.apache.archiva.consumers.core;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.apache.archiva.checksum.ChecksumValidationException;
23 import org.apache.archiva.checksum.ChecksumValidator;
24 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
25 import org.apache.archiva.consumers.ConsumerException;
26 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.context.annotation.Scope;
31 import org.springframework.stereotype.Service;
32
33 import javax.annotation.PostConstruct;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.Date;
38 import java.util.List;
39 import java.util.Set;
40
41 import static org.apache.archiva.checksum.ChecksumValidationException.ValidationError.*;
42
43 /**
44  * ValidateChecksumConsumer - validate the provided checksum against the file it represents.
45  */
46 @Service( "knownRepositoryContentConsumer#validate-checksums" )
47 @Scope( "prototype" )
48 public class ValidateChecksumConsumer
49     extends AbstractMonitoredConsumer
50     implements KnownRepositoryContentConsumer
51 {
52     private Logger log = LoggerFactory.getLogger( ValidateChecksumConsumer.class );
53
54     private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
55
56     private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
57
58     private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
59
60     private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
61
62     private String id = "validate-checksums";
63
64     private String description = "Validate checksums against file.";
65
66     ThreadLocal<ChecksumValidator> validatorThreadLocal = new ThreadLocal<>();
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         /* nothing to do */
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         ChecksumValidator validator;
127         if ((validator=validatorThreadLocal.get())==null) {
128             validator = new ChecksumValidator();
129             validatorThreadLocal.set(validator);
130         }
131         Path checksumFile = this.repositoryDir.resolve( path );
132         try
133         {
134
135             if ( !validator.isValidChecksum( checksumFile ) )
136             {
137                 log.warn( "The checksum for {} is invalid.", checksumFile );
138                 triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
139             }
140         }
141         catch ( ChecksumValidationException e )
142         {
143             if (e.getErrorType()==READ_ERROR) {
144                 log.error( "Checksum read error during validation on {}", checksumFile );
145                 triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
146             } else if (e.getErrorType()==INVALID_FORMAT || e.getErrorType()==DIGEST_ERROR) {
147                 log.error( "Digester failure during checksum validation on {}", checksumFile );
148                 triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
149                     "Digester failure during checksum validation on " + checksumFile );
150             } else if (e.getErrorType()==FILE_NOT_FOUND) {
151                 log.error( "File not found during checksum validation: ", e );
152                 triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage( ) );
153             }
154         }
155     }
156
157     @Override
158     public void processFile( String path, boolean executeOnEntireReDpo )
159         throws Exception
160     {
161         processFile( path );
162     }
163
164     @PostConstruct
165     public void initialize( )
166     {
167         ChecksumValidator validator;
168         if ((validator=validatorThreadLocal.get())==null) {
169             validator = new ChecksumValidator();
170             validatorThreadLocal.set(validator);
171         }
172         Set<String> extensions = validator.getSupportedExtensions();
173         includes = new ArrayList<>( extensions.size() );
174         for ( String ext : extensions )
175         {
176             includes.add( "**/*." + ext );
177         }
178     }
179 }