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.

ChecksumPolicy.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package org.apache.archiva.policies;
  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.checksum.ChecksumAlgorithm;
  21. import org.apache.archiva.checksum.ChecksummedFile;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.stereotype.Service;
  26. import java.io.File;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Properties;
  30. /**
  31. * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
  32. * successfully and completely (or not).
  33. *
  34. *
  35. */
  36. @Service( "postDownloadPolicy#checksum" )
  37. public class ChecksumPolicy
  38. implements PostDownloadPolicy
  39. {
  40. private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
  41. /**
  42. * The IGNORE policy indicates that if the checksum policy is ignored, and
  43. * the state of, contents of, or validity of the checksum files are not
  44. * checked.
  45. */
  46. public static final String IGNORE = "ignore";
  47. /**
  48. * The FAIL policy indicates that if the checksum does not match the
  49. * downloaded file, then remove the downloaded artifact, and checksum
  50. * files, and fail the transfer to the client side.
  51. */
  52. public static final String FAIL = "fail";
  53. /**
  54. * The FIX policy indicates that if the checksum does not match the
  55. * downloaded file, then fix the checksum file locally, and return
  56. * to the client side the corrected checksum.
  57. */
  58. public static final String FIX = "fix";
  59. private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
  60. private List<String> options = new ArrayList<>( 3 );
  61. public ChecksumPolicy()
  62. {
  63. options.add( FAIL );
  64. options.add( FIX );
  65. options.add( IGNORE );
  66. }
  67. public void applyPolicy( String policySetting, Properties request, File localFile )
  68. throws PolicyViolationException, PolicyConfigurationException
  69. {
  70. if ( "resource".equals( request.getProperty( "filetype" ) ) )
  71. {
  72. return;
  73. }
  74. if ( !options.contains( policySetting ) )
  75. {
  76. // Not a valid code.
  77. throw new PolicyConfigurationException(
  78. "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
  79. options.iterator(), "," ) + "]" );
  80. }
  81. if ( IGNORE.equals( policySetting ) )
  82. {
  83. // Ignore.
  84. log.debug( "Checksum policy set to IGNORE." );
  85. return;
  86. }
  87. if ( !localFile.exists() )
  88. {
  89. // Local File does not exist.
  90. throw new PolicyViolationException(
  91. "Checksum policy failure, local file " + localFile.getAbsolutePath() + " does not exist to check." );
  92. }
  93. if ( FAIL.equals( policySetting ) )
  94. {
  95. ChecksummedFile checksum = new ChecksummedFile( localFile );
  96. if ( checksum.isValidChecksums( algorithms ) )
  97. {
  98. return;
  99. }
  100. for ( ChecksumAlgorithm algorithm : algorithms )
  101. {
  102. File file = new File( localFile.getAbsolutePath() + "." + algorithm.getExt() );
  103. if ( file.exists() )
  104. {
  105. file.delete();
  106. }
  107. }
  108. localFile.delete();
  109. throw new PolicyViolationException(
  110. "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "
  111. + localFile.getAbsolutePath() + "." );
  112. }
  113. if ( FIX.equals( policySetting ) )
  114. {
  115. ChecksummedFile checksum = new ChecksummedFile( localFile );
  116. if ( checksum.fixChecksums( algorithms ) )
  117. {
  118. log.debug( "Checksum policy set to FIX, checksum files have been updated." );
  119. return;
  120. }
  121. else
  122. {
  123. throw new PolicyViolationException(
  124. "Checksum policy set to FIX, " + "yet unable to update checksums for local file "
  125. + localFile.getAbsolutePath() + "." );
  126. }
  127. }
  128. throw new PolicyConfigurationException(
  129. "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
  130. }
  131. public String getDefaultOption()
  132. {
  133. return FIX;
  134. }
  135. public String getId()
  136. {
  137. return "checksum";
  138. }
  139. public String getName()
  140. {
  141. return "Checksum";
  142. }
  143. public List<String> getOptions()
  144. {
  145. return options;
  146. }
  147. }