1 package org.apache.archiva.policies; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.archiva.repository.storage.StorageAsset; 23 import org.apache.commons.lang3.StringUtils; 24 import org.springframework.stereotype.Service; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 import java.util.Map; 29 import java.util.Properties; 30 31 /** 32 * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error. 33 */ 34 @Service( "downloadErrorPolicy#propagate-errors-on-update" ) 35 public class PropagateErrorsOnUpdateDownloadPolicy 36 extends AbstractPolicy implements DownloadErrorPolicy 37 { 38 private static final String ID = "propagate-errors-on-update"; 39 40 /** 41 * Signifies any error should cause a failure whether the artifact is already present or not. 42 */ 43 public static final DownloadErrorOption ALWAYS = DownloadErrorOption.ALWAYS; 44 45 /** 46 * Signifies any error should cause a failure only if the artifact is not already present. 47 */ 48 public static final DownloadErrorOption NOT_PRESENT = DownloadErrorOption.NOT_PRESENT; 49 50 private static final List<PolicyOption> options = new ArrayList<>( 2 ); 51 52 public PropagateErrorsOnUpdateDownloadPolicy() 53 { 54 options.add( ALWAYS ); 55 options.add( NOT_PRESENT ); 56 } 57 58 @Override 59 public boolean applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile, Exception exception, 60 Map<String, Exception> previousExceptions ) 61 throws PolicyConfigurationException 62 { 63 if ( !options.contains( policySetting ) ) 64 { 65 // Not a valid code. 66 throw new PolicyConfigurationException( 67 "Unknown error policy setting [" + policySetting + "], valid settings are [" + StringUtils.join( 68 options.iterator(), "," ) + "]" ); 69 } 70 71 if ( ALWAYS.equals( policySetting ) ) 72 { 73 // throw ther exception regardless 74 return true; 75 } 76 77 if ( NOT_PRESENT.equals( policySetting ) ) 78 { 79 // cancel the exception if the file exists 80 return !localFile.exists(); 81 } 82 83 throw new PolicyConfigurationException( 84 "Unable to process checksum policy of [" + policySetting + "], please file a bug report." ); 85 } 86 87 @Override 88 public PolicyOption getDefaultOption() 89 { 90 return NOT_PRESENT; 91 } 92 93 @Override 94 public String getId() 95 { 96 return ID; 97 } 98 99 100 @Override 101 public List<PolicyOption> getOptions() 102 { 103 return options; 104 } 105 }