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.springframework.stereotype.Service;
024
025import java.io.File;
026import java.util.ArrayList;
027import java.util.List;
028import java.util.Map;
029import 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" )
035public 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<>( 2 );
049
050    public PropagateErrorsOnUpdateDownloadPolicy()
051    {
052        options.add( ALWAYS );
053        options.add( NOT_PRESENT );
054    }
055
056    @Override
057    public boolean applyPolicy( String policySetting, Properties request, File localFile, Exception exception,
058                                Map<String, Exception> previousExceptions )
059        throws PolicyConfigurationException
060    {
061        if ( !options.contains( policySetting ) )
062        {
063            // Not a valid code.
064            throw new PolicyConfigurationException(
065                "Unknown error policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
066                    options.iterator(), "," ) + "]" );
067        }
068
069        if ( ALWAYS.equals( policySetting ) )
070        {
071            // throw ther exception regardless
072            return true;
073        }
074
075        if ( NOT_PRESENT.equals( policySetting ) )
076        {
077            // cancel the exception if the file exists
078            return !localFile.exists();
079        }
080
081        throw new PolicyConfigurationException(
082            "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
083    }
084
085    @Override
086    public String getDefaultOption()
087    {
088        return NOT_PRESENT;
089    }
090
091    @Override
092    public String getId()
093    {
094        return "propagate-errors-on-update";
095    }
096
097    @Override
098    public String getName()
099    {
100        return "Return error when";
101    }
102
103    @Override
104    public List<String> getOptions()
105    {
106        return options;
107    }
108}