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
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.common.utils.Checksums;
24 import org.codehaus.plexus.logging.AbstractLogEnabled;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Properties;
32 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
33 * successfully and completely (or not).
35 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
38 * @plexus.component role="org.apache.maven.archiva.policies.PostDownloadPolicy"
39 * role-hint="checksum"
41 public class ChecksumPolicy
42 extends AbstractLogEnabled
43 implements PostDownloadPolicy
46 * The IGNORE policy indicates that if the checksum policy is ignored, and
47 * the state of, contents of, or validity of the checksum files are not
50 public static final String IGNORE = "ignore";
53 * The FAIL policy indicates that if the checksum does not match the
54 * downloaded file, then remove the downloaded artifact, and checksum
55 * files, and fail the transfer to the client side.
57 public static final String FAIL = "fail";
60 * The FIX policy indicates that if the checksum does not match the
61 * downloaded file, then fix the checksum file locally, and return
62 * to the client side the corrected checksum.
64 public static final String FIX = "fix";
69 private Checksums checksums;
71 private List<String> options = new ArrayList<String>();
73 public ChecksumPolicy()
77 options.add( IGNORE );
80 public void applyPolicy( String policySetting, Properties request, File localFile )
81 throws PolicyViolationException, PolicyConfigurationException
83 if ( !options.contains( policySetting ) )
86 throw new PolicyConfigurationException( "Unknown checksum policy setting [" + policySetting
87 + "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
90 if ( IGNORE.equals( policySetting ) )
93 getLogger().debug( "Checksum policy set to IGNORE." );
97 if ( !localFile.exists() )
99 // Local File does not exist.
100 throw new PolicyViolationException( "Checksum policy failure, local file " + localFile.getAbsolutePath()
101 + " does not exist to check." );
104 if ( FAIL.equals( policySetting ) )
106 if( checksums.check( localFile ) )
111 File sha1File = new File( localFile.getAbsolutePath() + ".sha1" );
112 File md5File = new File( localFile.getAbsolutePath() + ".md5" );
114 // On failure. delete files.
115 if ( sha1File.exists() )
120 if ( md5File.exists() )
126 throw new PolicyViolationException( "Checksums do not match, policy set to FAIL, "
127 + "deleting checksum files and local file " + localFile.getAbsolutePath() + "." );
130 if ( FIX.equals( policySetting ) )
132 if( checksums.update( localFile ) )
134 getLogger().debug( "Checksum policy set to FIX, checksum files have been updated." );
139 throw new PolicyViolationException( "Checksum policy set to FIX, "
140 + "yet unable to update checksums for local file " + localFile.getAbsolutePath() + "." );
144 throw new PolicyConfigurationException( "Unable to process checksum policy of [" + policySetting
145 + "], please file a bug report." );
148 public String getDefaultOption()
153 public String getId()
158 public List<String> getOptions()