This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.policies;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.checksum.ChecksumAlgorithm;
023import org.apache.archiva.checksum.ChecksummedFile;
024import org.apache.commons.lang.StringUtils;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.springframework.stereotype.Service;
028
029import java.io.File;
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Properties;
033
034/**
035 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
036 * successfully and completely (or not).
037 *
038 *
039 */
040@Service( "postDownloadPolicy#checksum" )
041public class ChecksumPolicy
042    implements PostDownloadPolicy
043{
044    private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
045
046    /**
047     * The IGNORE policy indicates that if the checksum policy is ignored, and
048     * the state of, contents of, or validity of the checksum files are not
049     * checked.
050     */
051    public static final String IGNORE = "ignore";
052
053    /**
054     * The FAIL policy indicates that if the checksum does not match the
055     * downloaded file, then remove the downloaded artifact, and checksum
056     * files, and fail the transfer to the client side.
057     */
058    public static final String FAIL = "fail";
059
060    /**
061     * The FIX policy indicates that if the checksum does not match the
062     * downloaded file, then fix the checksum file locally, and return
063     * to the client side the corrected checksum.
064     */
065    public static final String FIX = "fix";
066
067    private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
068
069    private List<String> options = new ArrayList<>( 3 );
070
071    public ChecksumPolicy()
072    {
073        options.add( FAIL );
074        options.add( FIX );
075        options.add( IGNORE );
076    }
077
078    @Override
079    public void applyPolicy( String policySetting, Properties request, File localFile )
080        throws PolicyViolationException, PolicyConfigurationException
081    {
082        if ( "resource".equals( request.getProperty( "filetype" ) ) )
083        {
084            return;
085        }
086
087        if ( !options.contains( policySetting ) )
088        {
089            // Not a valid code. 
090            throw new PolicyConfigurationException(
091                "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
092                    options.iterator(), "," ) + "]" );
093        }
094
095        if ( IGNORE.equals( policySetting ) )
096        {
097            // Ignore.
098            log.debug( "Checksum policy set to IGNORE." );
099            return;
100        }
101
102        if ( !localFile.exists() )
103        {
104            // Local File does not exist.
105            throw new PolicyViolationException(
106                "Checksum policy failure, local file " + localFile.getAbsolutePath() + " does not exist to check." );
107        }
108
109        if ( FAIL.equals( policySetting ) )
110        {
111            ChecksummedFile checksum = new ChecksummedFile( localFile );
112            if ( checksum.isValidChecksums( algorithms ) )
113            {
114                return;
115            }
116
117            for ( ChecksumAlgorithm algorithm : algorithms )
118            {
119                File file = new File( localFile.getAbsolutePath() + "." + algorithm.getExt() );
120                if ( file.exists() )
121                {
122                    file.delete();
123                }
124            }
125
126            localFile.delete();
127            throw new PolicyViolationException(
128                "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "
129                    + localFile.getAbsolutePath() + "." );
130        }
131
132        if ( FIX.equals( policySetting ) )
133        {
134            ChecksummedFile checksum = new ChecksummedFile( localFile );
135            if ( checksum.fixChecksums( algorithms ) )
136            {
137                log.debug( "Checksum policy set to FIX, checksum files have been updated." );
138                return;
139            }
140            else
141            {
142                throw new PolicyViolationException(
143                    "Checksum policy set to FIX, " + "yet unable to update checksums for local file "
144                        + localFile.getAbsolutePath() + "." );
145            }
146        }
147
148        throw new PolicyConfigurationException(
149            "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
150    }
151
152    @Override
153    public String getDefaultOption()
154    {
155        return FIX;
156    }
157
158    @Override
159    public String getId()
160    {
161        return "checksum";
162    }
163
164    @Override
165    public String getName()
166    {
167        return "Checksum";
168    }
169
170    @Override
171    public List<String> getOptions()
172    {
173        return options;
174    }
175}