You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidateChecksumConsumer.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package org.apache.archiva.consumers.core;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.plexusbridge.DigesterUtils;
  21. import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
  22. import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
  23. import org.apache.archiva.consumers.AbstractMonitoredConsumer;
  24. import org.apache.archiva.consumers.ConsumerException;
  25. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  26. import org.apache.archiva.repository.ManagedRepository;
  27. import org.codehaus.plexus.digest.ChecksumFile;
  28. import org.codehaus.plexus.digest.Digester;
  29. import org.codehaus.plexus.digest.DigesterException;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import org.springframework.context.annotation.Scope;
  33. import org.springframework.stereotype.Service;
  34. import javax.annotation.PostConstruct;
  35. import javax.inject.Inject;
  36. import java.io.FileNotFoundException;
  37. import java.io.IOException;
  38. import java.nio.file.Path;
  39. import java.nio.file.Paths;
  40. import java.util.ArrayList;
  41. import java.util.Date;
  42. import java.util.List;
  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. private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
  54. private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
  55. private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
  56. private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
  57. private String id = "validate-checksums";
  58. private String description = "Validate checksums against file.";
  59. private ChecksumFile checksum;
  60. private List<Digester> allDigesters;
  61. @Inject
  62. private PlexusSisuBridge plexusSisuBridge;
  63. @Inject
  64. private DigesterUtils digesterUtils;
  65. private Path repositoryDir;
  66. private List<String> includes;
  67. @Override
  68. public String getId( )
  69. {
  70. return this.id;
  71. }
  72. @Override
  73. public String getDescription( )
  74. {
  75. return this.description;
  76. }
  77. @Override
  78. public void beginScan( ManagedRepository repository, Date whenGathered )
  79. throws ConsumerException
  80. {
  81. this.repositoryDir = Paths.get( repository.getLocation( ) );
  82. }
  83. @Override
  84. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  85. throws ConsumerException
  86. {
  87. beginScan( repository, whenGathered );
  88. }
  89. @Override
  90. public void completeScan( )
  91. {
  92. /* nothing to do */
  93. }
  94. @Override
  95. public void completeScan( boolean executeOnEntireRepo )
  96. {
  97. completeScan( );
  98. }
  99. @Override
  100. public List<String> getExcludes( )
  101. {
  102. return null;
  103. }
  104. @Override
  105. public List<String> getIncludes( )
  106. {
  107. return this.includes;
  108. }
  109. @Override
  110. public void processFile( String path )
  111. throws ConsumerException
  112. {
  113. Path checksumFile = this.repositoryDir.resolve( path );
  114. try
  115. {
  116. if ( !checksum.isValidChecksum( checksumFile.toFile() ) )
  117. {
  118. log.warn( "The checksum for {} is invalid.", checksumFile );
  119. triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
  120. }
  121. }
  122. catch ( FileNotFoundException e )
  123. {
  124. log.error( "File not found during checksum validation: ", e );
  125. triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage( ) );
  126. }
  127. catch ( DigesterException e )
  128. {
  129. log.error( "Digester failure during checksum validation on {}", checksumFile );
  130. triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
  131. "Digester failure during checksum validation on " + checksumFile );
  132. }
  133. catch ( IOException e )
  134. {
  135. log.error( "Checksum I/O error during validation on {}", checksumFile );
  136. triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
  137. }
  138. }
  139. @Override
  140. public void processFile( String path, boolean executeOnEntireReDpo )
  141. throws Exception
  142. {
  143. processFile( path );
  144. }
  145. @PostConstruct
  146. public void initialize( )
  147. throws PlexusSisuBridgeException
  148. {
  149. checksum = plexusSisuBridge.lookup( ChecksumFile.class );
  150. List<Digester> allDigesters = new ArrayList<>( digesterUtils.getAllDigesters( ) );
  151. includes = new ArrayList<>( allDigesters.size( ) );
  152. for ( Digester digester : allDigesters )
  153. {
  154. includes.add( "**/*" + digester.getFilenameExtension( ) );
  155. }
  156. }
  157. }