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.3KB

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.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.springframework.context.annotation.Scope;
  31. import org.springframework.stereotype.Service;
  32. import javax.annotation.PostConstruct;
  33. import javax.inject.Inject;
  34. import java.io.File;
  35. import java.io.FileNotFoundException;
  36. import java.io.IOException;
  37. import java.util.ArrayList;
  38. import java.util.Date;
  39. import java.util.List;
  40. /**
  41. * ValidateChecksumConsumer - validate the provided checksum against the file it represents.
  42. *
  43. *
  44. */
  45. @Service( "knownRepositoryContentConsumer#validate-checksums" )
  46. @Scope( "prototype" )
  47. public class ValidateChecksumConsumer
  48. extends AbstractMonitoredConsumer
  49. implements KnownRepositoryContentConsumer
  50. {
  51. private static final String NOT_VALID_CHECKSUM = "checksum-not-valid";
  52. private static final String CHECKSUM_NOT_FOUND = "checksum-not-found";
  53. private static final String CHECKSUM_DIGESTER_FAILURE = "checksum-digester-failure";
  54. private static final String CHECKSUM_IO_ERROR = "checksum-io-error";
  55. /**
  56. * default-value="validate-checksums"
  57. */
  58. private String id = "validate-checksums";
  59. /**
  60. * default-value="Validate checksums against file."
  61. */
  62. private String description = "Validate checksums against file.";
  63. /**
  64. *
  65. */
  66. private ChecksumFile checksum;
  67. /**
  68. *
  69. */
  70. private List<Digester> allDigesters;
  71. @Inject
  72. private PlexusSisuBridge plexusSisuBridge;
  73. @Inject
  74. private DigesterUtils digesterUtils;
  75. private File repositoryDir;
  76. private List<String> includes;
  77. public String getId()
  78. {
  79. return this.id;
  80. }
  81. public String getDescription()
  82. {
  83. return this.description;
  84. }
  85. public boolean isPermanent()
  86. {
  87. return false;
  88. }
  89. public void beginScan( ManagedRepository repository, Date whenGathered )
  90. throws ConsumerException
  91. {
  92. this.repositoryDir = new File( repository.getLocation() );
  93. }
  94. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  95. throws ConsumerException
  96. {
  97. beginScan( repository, whenGathered );
  98. }
  99. public void completeScan()
  100. {
  101. /* nothing to do */
  102. }
  103. public void completeScan( boolean executeOnEntireRepo )
  104. {
  105. completeScan();
  106. }
  107. public List<String> getExcludes()
  108. {
  109. return null;
  110. }
  111. public List<String> getIncludes()
  112. {
  113. return this.includes;
  114. }
  115. public void processFile( String path )
  116. throws ConsumerException
  117. {
  118. File checksumFile = new File( this.repositoryDir, path );
  119. try
  120. {
  121. if ( !checksum.isValidChecksum( checksumFile ) )
  122. {
  123. triggerConsumerWarning( NOT_VALID_CHECKSUM, "The checksum for " + checksumFile + " is invalid." );
  124. }
  125. }
  126. catch ( FileNotFoundException e )
  127. {
  128. triggerConsumerError( CHECKSUM_NOT_FOUND, "File not found during checksum validation: " + e.getMessage() );
  129. }
  130. catch ( DigesterException e )
  131. {
  132. triggerConsumerError( CHECKSUM_DIGESTER_FAILURE,
  133. "Digester failure during checksum validation on " + checksumFile );
  134. }
  135. catch ( IOException e )
  136. {
  137. triggerConsumerError( CHECKSUM_IO_ERROR, "Checksum I/O error during validation on " + checksumFile );
  138. }
  139. }
  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. }