001    package 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    
022    import org.apache.commons.lang.StringUtils;
023    import org.springframework.stereotype.Service;
024    
025    import java.io.File;
026    import java.util.ArrayList;
027    import java.util.List;
028    import java.util.Map;
029    import java.util.Properties;
030    
031    /**
032     * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error.
033     */
034    @Service( "downloadErrorPolicy#propagate-errors-on-update" )
035    public class PropagateErrorsOnUpdateDownloadPolicy
036        implements DownloadErrorPolicy
037    {
038        /**
039         * Signifies any error should cause a failure whether the artifact is already present or not.
040         */
041        public static final String ALWAYS = "always";
042    
043        /**
044         * Signifies any error should cause a failure only if the artifact is not already present.
045         */
046        public static final String NOT_PRESENT = "artifact not already present";
047    
048        private List<String> options = new ArrayList<String>( 2 );
049    
050        public PropagateErrorsOnUpdateDownloadPolicy()
051        {
052            options.add( ALWAYS );
053            options.add( NOT_PRESENT );
054        }
055    
056        public boolean applyPolicy( String policySetting, Properties request, File localFile, Exception exception,
057                                    Map<String, Exception> previousExceptions )
058            throws PolicyConfigurationException
059        {
060            if ( !options.contains( policySetting ) )
061            {
062                // Not a valid code.
063                throw new PolicyConfigurationException(
064                    "Unknown error policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
065                        options.iterator(), "," ) + "]" );
066            }
067    
068            if ( ALWAYS.equals( policySetting ) )
069            {
070                // throw ther exception regardless
071                return true;
072            }
073    
074            if ( NOT_PRESENT.equals( policySetting ) )
075            {
076                // cancel the exception if the file exists
077                return !localFile.exists();
078            }
079    
080            throw new PolicyConfigurationException(
081                "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
082        }
083    
084        public String getDefaultOption()
085        {
086            return NOT_PRESENT;
087        }
088    
089        public String getId()
090        {
091            return "propagate-errors-on-update";
092        }
093    
094        public String getName()
095        {
096            return "Return error when";
097        }
098    
099        public List<String> getOptions()
100        {
101            return options;
102        }
103    }