1 package org.apache.maven.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
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
27 import org.apache.archiva.checksum.ChecksumAlgorithm;
28 import org.apache.archiva.checksum.ChecksummedFile;
29 import org.apache.commons.lang.StringUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Service;
35 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
36 * successfully and completely (or not).
40 * plexus.component role="org.apache.maven.archiva.policies.PostDownloadPolicy"
41 * role-hint="checksum"
43 @Service("postDownloadPolicy#checksum")
44 public class ChecksumPolicy
45 implements PostDownloadPolicy
47 private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
50 * The IGNORE policy indicates that if the checksum policy is ignored, and
51 * the state of, contents of, or validity of the checksum files are not
54 public static final String IGNORE = "ignore";
57 * The FAIL policy indicates that if the checksum does not match the
58 * downloaded file, then remove the downloaded artifact, and checksum
59 * files, and fail the transfer to the client side.
61 public static final String FAIL = "fail";
64 * The FIX policy indicates that if the checksum does not match the
65 * downloaded file, then fix the checksum file locally, and return
66 * to the client side the corrected checksum.
68 public static final String FIX = "fix";
70 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
72 private List<String> options = new ArrayList<String>();
74 public ChecksumPolicy()
78 options.add( IGNORE );
81 public void applyPolicy( String policySetting, Properties request, File localFile )
82 throws PolicyViolationException, PolicyConfigurationException
84 if ( "resource".equals( request.getProperty( "filetype" ) ) )
89 if ( !options.contains( policySetting ) )
92 throw new PolicyConfigurationException( "Unknown checksum policy setting [" + policySetting
93 + "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
96 if ( IGNORE.equals( policySetting ) )
99 log.debug( "Checksum policy set to IGNORE." );
103 if ( !localFile.exists() )
105 // Local File does not exist.
106 throw new PolicyViolationException( "Checksum policy failure, local file " + localFile.getAbsolutePath()
107 + " does not exist to check." );
110 if ( FAIL.equals( policySetting ) )
112 ChecksummedFile checksum = new ChecksummedFile( localFile );
113 if ( checksum.isValidChecksums( algorithms ) )
118 for ( ChecksumAlgorithm algorithm : algorithms )
120 File file = new File( localFile.getAbsolutePath() + "." + algorithm.getExt() );
128 throw new PolicyViolationException( "Checksums do not match, policy set to FAIL, "
129 + "deleting checksum files and local file " + localFile.getAbsolutePath() + "." );
132 if ( FIX.equals( policySetting ) )
134 ChecksummedFile checksum = new ChecksummedFile( localFile );
135 if( checksum.fixChecksums( algorithms ) )
137 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
142 throw new PolicyViolationException( "Checksum policy set to FIX, "
143 + "yet unable to update checksums for local file " + localFile.getAbsolutePath() + "." );
147 throw new PolicyConfigurationException( "Unable to process checksum policy of [" + policySetting
148 + "], please file a bug report." );
151 public String getDefaultOption()
156 public String getId()
161 public String getName()
166 public List<String> getOptions()