Coverage Report - org.apache.maven.archiva.policies.ChecksumPolicy
 
Classes in this File Line Coverage Branch Coverage Complexity
ChecksumPolicy
0%
0/38
0%
0/20
0
 
 1  
 package org.apache.maven.archiva.policies;
 2  
 
 3  
 /*
 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
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 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
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 import java.util.Properties;
 26  
 
 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  
 
 33  
 /**
 34  
  * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
 35  
  * successfully and completely (or not).
 36  
  *
 37  
  * @version $Id: ChecksumPolicy.java 718864 2008-11-19 06:33:35Z brett $
 38  
  * 
 39  
  * @plexus.component role="org.apache.maven.archiva.policies.PostDownloadPolicy"
 40  
  *                   role-hint="checksum"
 41  
  */
 42  
 public class ChecksumPolicy
 43  
     implements PostDownloadPolicy
 44  
 {
 45  0
     private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
 46  
     
 47  
     /**
 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
 50  
      * checked.
 51  
      */
 52  
     public static final String IGNORE = "ignore";
 53  
     
 54  
     /**
 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.
 58  
      */
 59  
     public static final String FAIL = "fail";
 60  
 
 61  
     /**
 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.
 65  
      */
 66  
     public static final String FIX = "fix";
 67  
 
 68  0
     private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
 69  
 
 70  0
     private List<String> options = new ArrayList<String>();
 71  
 
 72  
     public ChecksumPolicy()
 73  0
     {
 74  0
         options.add( FAIL );
 75  0
         options.add( FIX );
 76  0
         options.add( IGNORE );
 77  0
     }
 78  
 
 79  
     public void applyPolicy( String policySetting, Properties request, File localFile )
 80  
         throws PolicyViolationException, PolicyConfigurationException
 81  
     {
 82  0
         if ( "resource".equals( request.getProperty( "filetype" ) ) )
 83  
         {
 84  0
             return;
 85  
         }
 86  
         
 87  0
         if ( !options.contains( policySetting ) )
 88  
         {
 89  
             // Not a valid code. 
 90  0
             throw new PolicyConfigurationException( "Unknown checksum policy setting [" + policySetting
 91  
                 + "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
 92  
         }
 93  
 
 94  0
         if ( IGNORE.equals( policySetting ) )
 95  
         {
 96  
             // Ignore.
 97  0
             log.debug( "Checksum policy set to IGNORE." );
 98  0
             return;
 99  
         }
 100  
 
 101  0
         if ( !localFile.exists() )
 102  
         {
 103  
             // Local File does not exist.
 104  0
             throw new PolicyViolationException( "Checksum policy failure, local file " + localFile.getAbsolutePath()
 105  
                 + " does not exist to check." );
 106  
         }
 107  
 
 108  0
         if ( FAIL.equals( policySetting ) )
 109  
         {
 110  0
             ChecksummedFile checksum = new ChecksummedFile( localFile );
 111  0
             if ( checksum.isValidChecksums( algorithms ) )
 112  
             {
 113  0
                 return;
 114  
             }
 115  
 
 116  0
             for ( ChecksumAlgorithm algorithm : algorithms )
 117  
             {
 118  0
                 File file = new File( localFile.getAbsolutePath() + "." + algorithm.getExt() );
 119  0
                 if ( file.exists() )
 120  
                 {
 121  0
                     file.delete();
 122  
                 }
 123  
             }
 124  
             
 125  0
             localFile.delete();
 126  0
             throw new PolicyViolationException( "Checksums do not match, policy set to FAIL, "
 127  
                 + "deleting checksum files and local file " + localFile.getAbsolutePath() + "." );
 128  
         }
 129  
 
 130  0
         if ( FIX.equals( policySetting ) )
 131  
         {
 132  0
             ChecksummedFile checksum = new ChecksummedFile( localFile );
 133  0
             if( checksum.fixChecksums( algorithms ) )
 134  
             {
 135  0
                 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
 136  0
                 return;
 137  
             }
 138  
             else
 139  
             {
 140  0
                 throw new PolicyViolationException( "Checksum policy set to FIX, "
 141  
                     + "yet unable to update checksums for local file " + localFile.getAbsolutePath() + "." );
 142  
             }
 143  
         }
 144  
 
 145  0
         throw new PolicyConfigurationException( "Unable to process checksum policy of [" + policySetting
 146  
             + "], please file a bug report." );
 147  
     }
 148  
 
 149  
     public String getDefaultOption()
 150  
     {
 151  0
         return FIX;
 152  
     }
 153  
 
 154  
     public String getId()
 155  
     {
 156  0
         return "checksum";
 157  
     }
 158  
 
 159  
     public String getName()
 160  
     {
 161  0
         return "Checksum";
 162  
     }
 163  
 
 164  
     public List<String> getOptions()
 165  
     {
 166  0
         return options;
 167  
     }
 168  
 }