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.admin.model.beans.ManagedRepository;
23  import org.apache.archiva.common.plexusbridge.DigesterUtils;
24  import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25  import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
26  import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27  import org.apache.archiva.consumers.ConsumerException;
28  import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
29  import org.codehaus.plexus.digest.ChecksumFile;
30  import org.codehaus.plexus.digest.Digester;
31  import org.codehaus.plexus.digest.DigesterException;
32  import org.springframework.context.annotation.Scope;
33  import org.springframework.stereotype.Service;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import javax.annotation.PostConstruct;
38  import javax.inject.Inject;
39  import java.io.File;
40  import java.io.FileNotFoundException;
41  import java.io.IOException;
42  import java.util.ArrayList;
43  import java.util.Date;
44  import java.util.List;
45  
46  /**
47   * ValidateChecksumConsumer - validate the provided checksum against the file it represents.
48   *
49   *
50   */
51  @Service( "knownRepositoryContentConsumer#validate-checksums" )
52  @Scope( "prototype" )
53  public class ValidateChecksumConsumer
54      extends AbstractMonitoredConsumer
55      implements KnownRepositoryContentConsumer
56  {
57      private Logger log = LoggerFactory.getLogger( ValidateChecksumConsumer.class );
58  
59      private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
60  
61      private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
62  
63      private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
64  
65      private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
66  
67      private String id = "validate-checksums";
68  
69      private String description = "Validate checksums against file.";
70  
71      private ChecksumFile checksum;
72  
73      private List<Digester> allDigesters;
74  
75      @Inject
76      private PlexusSisuBridge plexusSisuBridge;
77  
78      @Inject
79      private DigesterUtils digesterUtils;
80  
81      private File repositoryDir;
82  
83      private List<String> includes;
84  
85      @Override
86      public String getId()
87      {
88          return this.id;
89      }
90  
91      @Override
92      public String getDescription()
93      {
94          return this.description;
95      }
96  
97      @Override
98      public void beginScan( ManagedRepository repository, Date whenGathered )
99          throws ConsumerException
100     {
101         this.repositoryDir = new File( repository.getLocation() );
102     }
103 
104     @Override
105     public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
106         throws ConsumerException
107     {
108         beginScan( repository, whenGathered );
109     }
110 
111     @Override
112     public void completeScan()
113     {
114         /* nothing to do */
115     }
116 
117     @Override
118     public void completeScan( boolean executeOnEntireRepo )
119     {
120         completeScan();
121     }
122 
123     @Override
124     public List<String> getExcludes()
125     {
126         return null;
127     }
128 
129     @Override
130     public List<String> getIncludes()
131     {
132         return this.includes;
133     }
134 
135     @Override
136     public void processFile( String path )
137         throws ConsumerException
138     {
139         File checksumFile = new File( this.repositoryDir, path );
140         try
141         {
142             if ( !checksum.isValidChecksum( checksumFile ) )
143             {
144                 log.warn( "The checksum for {} is invalid.", checksumFile );
145                 triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
146             }
147         }
148         catch ( FileNotFoundException e )
149         {
150             log.error( "File not found during checksum validation: ", e );
151             triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage() );
152         }
153         catch ( DigesterException e )
154         {
155             log.error( "Digester failure during checksum validation on {}", checksumFile );
156             triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
157                                   "Digester failure during checksum validation on " + checksumFile );
158         }
159         catch ( IOException e )
160         {
161             log.error( "Checksum I/O error during validation on {}", checksumFile );
162             triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
163         }
164     }
165 
166     @Override
167     public void processFile( String path, boolean executeOnEntireReDpo )
168         throws Exception
169     {
170         processFile( path );
171     }
172 
173     @PostConstruct
174     public void initialize()
175         throws PlexusSisuBridgeException
176     {
177         checksum = plexusSisuBridge.lookup( ChecksumFile.class );
178         List<Digester> allDigesters = new ArrayList<>( digesterUtils.getAllDigesters() );
179         includes = new ArrayList<>( allDigesters.size() );
180         for ( Digester digester : allDigesters )
181         {
182             includes.add( "**/*" + digester.getFilenameExtension() );
183         }
184     }
185 }