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