content: package org.apache.archiva.policies;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an
   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   * KIND, either express or implied.  See the License for the
   * specific language governing permissions and limitations
   * under the License.
   */
  
  import org.apache.archiva.repository.storage.StorageAsset;
  import org.apache.commons.lang3.StringUtils;
  import org.springframework.stereotype.Service;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import java.util.Properties;
  
  /**
   * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error.
   */
  @Service( "downloadErrorPolicy#propagate-errors-on-update" )
  public class PropagateErrorsOnUpdateDownloadPolicy
      extends AbstractPolicy implements DownloadErrorPolicy
  {
      private static final String ID = "propagate-errors-on-update";
  
      /**
       * Signifies any error should cause a failure whether the artifact is already present or not.
       */
      public static final DownloadErrorOption ALWAYS = DownloadErrorOption.ALWAYS;
  
      /**
       * Signifies any error should cause a failure only if the artifact is not already present.
       */
      public static final DownloadErrorOption NOT_PRESENT = DownloadErrorOption.NOT_PRESENT;
  
      private static final List<PolicyOption> options = new ArrayList<>( 2 );
  
      public PropagateErrorsOnUpdateDownloadPolicy()
      {
          options.add( ALWAYS );
          options.add( NOT_PRESENT );
      }
  
      @Override
      public boolean applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile, Exception exception,
                                  Map<String, Exception> previousExceptions )
          throws PolicyConfigurationException
      {
          if ( !options.contains( policySetting ) )
          {
              // Not a valid code.
              throw new PolicyConfigurationException(
                  "Unknown error policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
                      options.iterator(), "," ) + "]" );
          }
  
          if ( ALWAYS.equals( policySetting ) )
          {
              // throw ther exception regardless
              return true;
          }
  
          if ( NOT_PRESENT.equals( policySetting ) )
          {
              // cancel the exception if the file exists
              return !localFile.exists();
          }
  
          throw new PolicyConfigurationException(
              "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
      }
  
      @Override
      public PolicyOption getDefaultOption()
      {
          return NOT_PRESENT;
      }
  
      @Override
      public String getId()
      {
          return ID;
      }
  
  
      @Override
      public List<PolicyOption> getOptions()
      {
          return options;
      }
  }