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;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
35 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
36 * successfully and completely (or not).
40 @Service( "postDownloadPolicy#checksum" )
41 public class ChecksumPolicy
42 implements PostDownloadPolicy
44 private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
47 * The IGNORE policy indicates that if the checksum policy is ignored, and
48 * the state of, contents of, or validity of the checksum files are not
51 public static final String IGNORE = "ignore";
54 * The FAIL policy indicates that if the checksum does not match the
55 * downloaded file, then remove the downloaded artifact, and checksum
56 * files, and fail the transfer to the client side.
58 public static final String FAIL = "fail";
61 * The FIX policy indicates that if the checksum does not match the
62 * downloaded file, then fix the checksum file locally, and return
63 * to the client side the corrected checksum.
65 public static final String FIX = "fix";
67 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
69 private List<String> options = new ArrayList<String>( 3 );
71 public ChecksumPolicy()
75 options.add( IGNORE );
78 public void applyPolicy( String policySetting, Properties request, File localFile )
79 throws PolicyViolationException, PolicyConfigurationException
81 if ( "resource".equals( request.getProperty( "filetype" ) ) )
86 if ( !options.contains( policySetting ) )
89 throw new PolicyConfigurationException(
90 "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
91 options.iterator(), "," ) + "]" );
94 if ( IGNORE.equals( policySetting ) )
97 log.debug( "Checksum policy set to IGNORE." );
101 if ( !localFile.exists() )
103 // Local File does not exist.
104 throw new PolicyViolationException(
105 "Checksum policy failure, local file " + localFile.getAbsolutePath() + " does not exist to check." );
108 if ( FAIL.equals( policySetting ) )
110 ChecksummedFile checksum = new ChecksummedFile( localFile );
111 if ( checksum.isValidChecksums( algorithms ) )
116 for ( ChecksumAlgorithm algorithm : algorithms )
118 File file = new File( localFile.getAbsolutePath() + "." + algorithm.getExt() );
126 throw new PolicyViolationException(
127 "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "
128 + localFile.getAbsolutePath() + "." );
131 if ( FIX.equals( policySetting ) )
133 ChecksummedFile checksum = new ChecksummedFile( localFile );
134 if ( checksum.fixChecksums( algorithms ) )
136 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
141 throw new PolicyViolationException(
142 "Checksum policy set to FIX, " + "yet unable to update checksums for local file "
143 + localFile.getAbsolutePath() + "." );
147 throw new PolicyConfigurationException(
148 "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
151 public String getDefaultOption()
156 public String getId()
161 public String getName()
166 public List<String> getOptions()