1 package org.apache.maven.archiva.policies;
3 import org.codehaus.plexus.digest.ChecksumFile;
4 import org.codehaus.plexus.digest.Digester;
5 import org.codehaus.plexus.digest.DigesterException;
6 import org.codehaus.plexus.logging.AbstractLogEnabled;
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.util.HashSet;
12 import java.util.Properties;
15 public class ChecksumPolicy
16 extends AbstractLogEnabled
17 implements PostDownloadPolicy
20 * The FAIL policy indicates that if the checksum does not match the
21 * downloaded file, then remove the downloaded artifact, and checksum
22 * files, and fail the transfer to the client side.
24 public static final String FAIL = "fail";
27 * The FIX policy indicates that if the checksum does not match the
28 * downloaded file, then fix the checksum file locally, and return
29 * to the client side the corrected checksum.
31 public static final String FIX = "fix";
34 * The IGNORE policy indicates that the checksum is never tested
35 * and even bad downloads and checksum files are left in place
36 * on the local repository.
38 public static final String IGNORED = "ignored";
41 * @plexus.requirement role-hint="sha1"
43 private Digester digestSha1;
46 * @plexus.requirement role-hint="md5"
48 private Digester digestMd5;
53 private ChecksumFile checksumFile;
55 private Set validPolicyCodes = new HashSet();
57 public ChecksumPolicy()
59 validPolicyCodes.add( FAIL );
60 validPolicyCodes.add( FIX );
61 validPolicyCodes.add( IGNORED );
64 public boolean applyPolicy( String policySetting, Properties request, File localFile )
66 if ( !validPolicyCodes.contains( policySetting ) )
68 // No valid code? false it is then.
69 getLogger().error( "Unknown checksum policyCode [" + policySetting + "]" );
73 if ( IGNORED.equals( policySetting ) )
79 if ( !localFile.exists() )
81 // Local File does not exist.
82 getLogger().debug( "Local file " + localFile.getAbsolutePath() + " does not exist." );
86 File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
87 File md5File = new File( localFile.getAbsolutePath() + ".md5" );
89 if ( FAIL.equals( policySetting ) )
91 if ( !sha1File.exists() && !md5File.exists() )
93 getLogger().error( "File " + localFile.getAbsolutePath() + " has no checksum files (sha1 or md5)." );
98 // Test for sha1 first, then md5
100 if ( sha1File.exists() )
104 return checksumFile.isValidChecksum( sha1File );
106 catch ( FileNotFoundException e )
108 getLogger().warn( "Unable to find sha1 file: " + sha1File.getAbsolutePath(), e );
111 catch ( DigesterException e )
113 getLogger().warn( "Unable to process sha1 file: " + sha1File.getAbsolutePath(), e );
116 catch ( IOException e )
118 getLogger().warn( "Unable to process sha1 file: " + sha1File.getAbsolutePath(), e );
123 if ( md5File.exists() )
127 return checksumFile.isValidChecksum( md5File );
129 catch ( FileNotFoundException e )
131 getLogger().warn( "Unable to find md5 file: " + md5File.getAbsolutePath(), e );
134 catch ( DigesterException e )
136 getLogger().warn( "Unable to process md5 file: " + md5File.getAbsolutePath(), e );
139 catch ( IOException e )
141 getLogger().warn( "Unable to process md5 file: " + md5File.getAbsolutePath(), e );
147 if ( FIX.equals( policySetting ) )
149 if ( !sha1File.exists() )
153 checksumFile.createChecksum( localFile, digestSha1 );
155 catch ( DigesterException e )
157 getLogger().warn( "Unable to create sha1 file: " + e.getMessage(), e );
160 catch ( IOException e )
162 getLogger().warn( "Unable to create sha1 file: " + e.getMessage(), e );
170 checksumFile.isValidChecksum( sha1File );
172 catch ( FileNotFoundException e )
174 getLogger().warn( "Unable to find sha1 file: " + sha1File.getAbsolutePath(), e );
177 catch ( DigesterException e )
179 getLogger().warn( "Unable to process sha1 file: " + sha1File.getAbsolutePath(), e );
182 catch ( IOException e )
184 getLogger().warn( "Unable to process sha1 file: " + sha1File.getAbsolutePath(), e );
189 if ( !md5File.exists() )
193 checksumFile.createChecksum( localFile, digestMd5 );
195 catch ( DigesterException e )
197 getLogger().warn( "Unable to create md5 file: " + e.getMessage(), e );
200 catch ( IOException e )
202 getLogger().warn( "Unable to create md5 file: " + e.getMessage(), e );
210 return checksumFile.isValidChecksum( md5File );
212 catch ( FileNotFoundException e )
214 getLogger().warn( "Unable to find md5 file: " + md5File.getAbsolutePath(), e );
217 catch ( DigesterException e )
219 getLogger().warn( "Unable to process md5 file: " + md5File.getAbsolutePath(), e );
222 catch ( IOException e )
224 getLogger().warn( "Unable to process md5 file: " + md5File.getAbsolutePath(), e );
230 getLogger().error( "Unhandled policyCode [" + policySetting + "]" );
234 public String getDefaultPolicySetting()