This project has retired. For details please refer to its Attic page.
ValidateChecksumConsumer xref
View Javadoc
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.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   * ValidateChecksumConsumer - validate the provided checksum against the file it represents.
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         /* 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         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 }