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.commons.lang.StringUtils;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.springframework.stereotype.Service;
026
027import java.io.File;
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    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<>( 3 );
058
059    public PropagateErrorsDownloadPolicy()
060    {
061        options.add( STOP );
062        options.add( QUEUE );
063        options.add( IGNORE );
064    }
065
066    @Override
067    public boolean applyPolicy( String policySetting, Properties request, File localFile, Exception exception,
068                                Map<String, Exception> previousExceptions )
069        throws PolicyConfigurationException
070    {
071        if ( !options.contains( policySetting ) )
072        {
073            // Not a valid code.
074            throw new PolicyConfigurationException( "Unknown error policy setting [" + policySetting +
075                                                        "], valid settings are [" + StringUtils.join(
076                options.iterator(), "," ) + "]" );
077        }
078
079        if ( IGNORE.equals( policySetting ) )
080        {
081            // Ignore.
082            log.debug( "Error policy set to IGNORE." );
083            return false;
084        }
085
086        String repositoryId = request.getProperty( "remoteRepositoryId" );
087        if ( STOP.equals( policySetting ) )
088        {
089            return true;
090        }
091
092        if ( QUEUE.equals( policySetting ) )
093        {
094            previousExceptions.put( repositoryId, exception );
095            return true;
096        }
097
098        throw new PolicyConfigurationException(
099            "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
100    }
101
102    @Override
103    public String getDefaultOption()
104    {
105        return QUEUE;
106    }
107
108    @Override
109    public String getId()
110    {
111        return "propagate-errors";
112    }
113
114    @Override
115    public String getName()
116    {
117        return "On remote error";
118    }
119
120    @Override
121    public List<String> getOptions()
122    {
123        return options;
124    }
125}