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.commons.lang.StringUtils;
28 import org.apache.maven.archiva.common.utils.Checksums;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
34 * successfully and completely (or not).
36 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
39 * @plexus.component role="org.apache.maven.archiva.policies.PostDownloadPolicy"
40 * role-hint="checksum"
42 public class ChecksumPolicy
43 implements PostDownloadPolicy
45 private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
48 * The IGNORE policy indicates that if the checksum policy is ignored, and
49 * the state of, contents of, or validity of the checksum files are not
52 public static final String IGNORE = "ignore";
55 * The FAIL policy indicates that if the checksum does not match the
56 * downloaded file, then remove the downloaded artifact, and checksum
57 * files, and fail the transfer to the client side.
59 public static final String FAIL = "fail";
62 * The FIX policy indicates that if the checksum does not match the
63 * downloaded file, then fix the checksum file locally, and return
64 * to the client side the corrected checksum.
66 public static final String FIX = "fix";
71 private Checksums checksums;
73 private List<String> options = new ArrayList<String>();
75 public ChecksumPolicy()
79 options.add( IGNORE );
82 public void applyPolicy( String policySetting, Properties request, File localFile )
83 throws PolicyViolationException, PolicyConfigurationException
85 if ( !options.contains( policySetting ) )
88 throw new PolicyConfigurationException( "Unknown checksum policy setting [" + policySetting
89 + "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
92 if ( IGNORE.equals( policySetting ) )
95 log.debug( "Checksum policy set to IGNORE." );
99 if ( !localFile.exists() )
101 // Local File does not exist.
102 throw new PolicyViolationException( "Checksum policy failure, local file " + localFile.getAbsolutePath()
103 + " does not exist to check." );
106 if ( FAIL.equals( policySetting ) )
108 if( checksums.check( localFile ) )
113 File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
114 File md5File = new File( localFile.getAbsolutePath() + ".md5" );
116 // On failure. delete files.
117 if ( sha1File.exists() )
122 if ( md5File.exists() )
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 if( checksums.update( localFile ) )
136 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
141 throw new PolicyViolationException( "Checksum policy set to FIX, "
142 + "yet unable to update checksums for local file " + localFile.getAbsolutePath() + "." );
146 throw new PolicyConfigurationException( "Unable to process checksum policy of [" + policySetting
147 + "], please file a bug report." );
150 public String getDefaultOption()
155 public String getId()
160 public List<String> getOptions()