1 package org.apache.archiva.policies;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.checksum.ChecksumAlgorithm;
23 import org.apache.archiva.checksum.ChecksummedFile;
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.stereotype.Service;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Properties;
37 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
38 * successfully and completely (or not).
42 @Service( "postDownloadPolicy#checksum" )
43 public class ChecksumPolicy
44 implements PostDownloadPolicy
46 private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
49 * The IGNORE policy indicates that if the checksum policy is ignored, and
50 * the state of, contents of, or validity of the checksum files are not
53 public static final String IGNORE = "ignore";
56 * The FAIL policy indicates that if the checksum does not match the
57 * downloaded file, then remove the downloaded artifact, and checksum
58 * files, and fail the transfer to the client side.
60 public static final String FAIL = "fail";
63 * The FIX policy indicates that if the checksum does not match the
64 * downloaded file, then fix the checksum file locally, and return
65 * to the client side the corrected checksum.
67 public static final String FIX = "fix";
69 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
71 private List<String> options = new ArrayList<>( 3 );
73 public ChecksumPolicy()
77 options.add( IGNORE );
81 public void applyPolicy( String policySetting, Properties request, Path localFile )
82 throws PolicyViolationException, PolicyConfigurationException
84 if ( "resource".equals( request.getProperty( "filetype" ) ) )
89 if ( !options.contains( policySetting ) )
92 throw new PolicyConfigurationException(
93 "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
94 options.iterator(), "," ) + "]" );
97 if ( IGNORE.equals( policySetting ) )
100 log.debug( "Checksum policy set to IGNORE." );
104 if ( !Files.exists(localFile) )
106 // Local File does not exist.
107 throw new PolicyViolationException(
108 "Checksum policy failure, local file " + localFile.toAbsolutePath() + " does not exist to check." );
111 if ( FAIL.equals( policySetting ) )
113 ChecksummedFile checksum = new ChecksummedFile( localFile );
114 if ( checksum.isValidChecksums( algorithms ) )
119 for ( ChecksumAlgorithm algorithm : algorithms )
121 Path file = localFile.toAbsolutePath().resolveSibling( localFile.getFileName() + "." + algorithm.getExt() );
124 Files.deleteIfExists( file );
126 catch ( IOException e )
128 log.error("Could not delete file {}", file);
134 Files.deleteIfExists( localFile );
136 catch ( IOException e )
138 log.error("Could not delete file {}", localFile);
140 throw new PolicyViolationException(
141 "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "
142 + localFile.toAbsolutePath() + "." );
145 if ( FIX.equals( policySetting ) )
147 ChecksummedFile checksum = new ChecksummedFile( localFile );
148 if ( checksum.fixChecksums( algorithms ) )
150 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
155 throw new PolicyViolationException(
156 "Checksum policy set to FIX, " + "yet unable to update checksums for local file "
157 + localFile.toAbsolutePath() + "." );
161 throw new PolicyConfigurationException(
162 "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
166 public String getDefaultOption()
172 public String getId()
178 public String getName()
184 public List<String> getOptions()