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.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.springframework.stereotype.Service;
026    
027    import java.io.File;
028    import java.util.ArrayList;
029    import java.util.List;
030    import java.util.Map;
031    import java.util.Properties;
032    
033    /**
034     * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error.
035     */
036    @Service( "downloadErrorPolicy#propagate-errors" )
037    public class PropagateErrorsDownloadPolicy
038        implements DownloadErrorPolicy
039    {
040        private Logger log = LoggerFactory.getLogger( PropagateErrorsDownloadPolicy.class );
041    
042        /**
043         * Signifies any error should stop searching for other proxies.
044         */
045        public static final String STOP = "stop";
046    
047        /**
048         * Propagate errors at the end after all are gathered, if there was no successful download from other proxies.
049         */
050        public static final String QUEUE = "queue error";
051    
052        /**
053         * Ignore errors and treat as if it were not found.
054         */
055        public static final String IGNORE = "ignore";
056    
057        private List<String> options = new ArrayList<String>( 3 );
058    
059        public PropagateErrorsDownloadPolicy()
060        {
061            options.add( STOP );
062            options.add( QUEUE );
063            options.add( IGNORE );
064        }
065    
066        public boolean applyPolicy( String policySetting, Properties request, File localFile, Exception exception,
067                                    Map<String, Exception> previousExceptions )
068            throws PolicyConfigurationException
069        {
070            if ( !options.contains( policySetting ) )
071            {
072                // Not a valid code.
073                throw new PolicyConfigurationException( "Unknown error policy setting [" + policySetting +
074                                                            "], valid settings are [" + StringUtils.join(
075                    options.iterator(), "," ) + "]" );
076            }
077    
078            if ( IGNORE.equals( policySetting ) )
079            {
080                // Ignore.
081                log.debug( "Error policy set to IGNORE." );
082                return false;
083            }
084    
085            String repositoryId = request.getProperty( "remoteRepositoryId" );
086            if ( STOP.equals( policySetting ) )
087            {
088                return true;
089            }
090    
091            if ( QUEUE.equals( policySetting ) )
092            {
093                previousExceptions.put( repositoryId, exception );
094                return true;
095            }
096    
097            throw new PolicyConfigurationException(
098                "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
099        }
100    
101        public String getDefaultOption()
102        {
103            return QUEUE;
104        }
105    
106        public String getId()
107        {
108            return "propagate-errors";
109        }
110    
111        public String getName()
112        {
113            return "On remote error";
114        }
115    
116        public List<String> getOptions()
117        {
118            return options;
119        }
120    }