001 package org.apache.archiva.policies; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import org.apache.archiva.checksum.ChecksumAlgorithm; 023 import org.apache.archiva.checksum.ChecksummedFile; 024 import org.apache.commons.lang.StringUtils; 025 import org.slf4j.Logger; 026 import org.slf4j.LoggerFactory; 027 import org.springframework.stereotype.Service; 028 029 import java.io.File; 030 import java.util.ArrayList; 031 import java.util.List; 032 import java.util.Properties; 033 034 /** 035 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded 036 * successfully and completely (or not). 037 * 038 * 039 */ 040 @Service( "postDownloadPolicy#checksum" ) 041 public class ChecksumPolicy 042 implements PostDownloadPolicy 043 { 044 private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class ); 045 046 /** 047 * The IGNORE policy indicates that if the checksum policy is ignored, and 048 * the state of, contents of, or validity of the checksum files are not 049 * checked. 050 */ 051 public static final String IGNORE = "ignore"; 052 053 /** 054 * The FAIL policy indicates that if the checksum does not match the 055 * downloaded file, then remove the downloaded artifact, and checksum 056 * files, and fail the transfer to the client side. 057 */ 058 public static final String FAIL = "fail"; 059 060 /** 061 * The FIX policy indicates that if the checksum does not match the 062 * downloaded file, then fix the checksum file locally, and return 063 * to the client side the corrected checksum. 064 */ 065 public static final String FIX = "fix"; 066 067 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 }; 068 069 private List<String> options = new ArrayList<String>( 3 ); 070 071 public ChecksumPolicy() 072 { 073 options.add( FAIL ); 074 options.add( FIX ); 075 options.add( IGNORE ); 076 } 077 078 public void applyPolicy( String policySetting, Properties request, File localFile ) 079 throws PolicyViolationException, PolicyConfigurationException 080 { 081 if ( "resource".equals( request.getProperty( "filetype" ) ) ) 082 { 083 return; 084 } 085 086 if ( !options.contains( policySetting ) ) 087 { 088 // Not a valid code. 089 throw new PolicyConfigurationException( 090 "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join( 091 options.iterator(), "," ) + "]" ); 092 } 093 094 if ( IGNORE.equals( policySetting ) ) 095 { 096 // Ignore. 097 log.debug( "Checksum policy set to IGNORE." ); 098 return; 099 } 100 101 if ( !localFile.exists() ) 102 { 103 // Local File does not exist. 104 throw new PolicyViolationException( 105 "Checksum policy failure, local file " + localFile.getAbsolutePath() + " does not exist to check." ); 106 } 107 108 if ( FAIL.equals( policySetting ) ) 109 { 110 ChecksummedFile checksum = new ChecksummedFile( localFile ); 111 if ( checksum.isValidChecksums( algorithms ) ) 112 { 113 return; 114 } 115 116 for ( ChecksumAlgorithm algorithm : algorithms ) 117 { 118 File file = new File( localFile.getAbsolutePath() + "." + algorithm.getExt() ); 119 if ( file.exists() ) 120 { 121 file.delete(); 122 } 123 } 124 125 localFile.delete(); 126 throw new PolicyViolationException( 127 "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file " 128 + localFile.getAbsolutePath() + "." ); 129 } 130 131 if ( FIX.equals( policySetting ) ) 132 { 133 ChecksummedFile checksum = new ChecksummedFile( localFile ); 134 if ( checksum.fixChecksums( algorithms ) ) 135 { 136 log.debug( "Checksum policy set to FIX, checksum files have been updated." ); 137 return; 138 } 139 else 140 { 141 throw new PolicyViolationException( 142 "Checksum policy set to FIX, " + "yet unable to update checksums for local file " 143 + localFile.getAbsolutePath() + "." ); 144 } 145 } 146 147 throw new PolicyConfigurationException( 148 "Unable to process checksum policy of [" + policySetting + "], please file a bug report." ); 149 } 150 151 public String getDefaultOption() 152 { 153 return FIX; 154 } 155 156 public String getId() 157 { 158 return "checksum"; 159 } 160 161 public String getName() 162 { 163 return "Checksum"; 164 } 165 166 public List<String> getOptions() 167 { 168 return options; 169 } 170 }