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

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