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.springframework.stereotype.Service; 025 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 extends AbstractPolicy implements DownloadErrorPolicy 037{ 038 private static final String ID = "propagate-errors-on-update"; 039 040 /** 041 * Signifies any error should cause a failure whether the artifact is already present or not. 042 */ 043 public static final DownloadErrorOption ALWAYS = DownloadErrorOption.ALWAYS; 044 045 /** 046 * Signifies any error should cause a failure only if the artifact is not already present. 047 */ 048 public static final DownloadErrorOption NOT_PRESENT = DownloadErrorOption.NOT_PRESENT; 049 050 private static final List<PolicyOption> options = new ArrayList<>( 2 ); 051 052 public PropagateErrorsOnUpdateDownloadPolicy() 053 { 054 options.add( ALWAYS ); 055 options.add( NOT_PRESENT ); 056 } 057 058 @Override 059 public boolean applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile, Exception exception, 060 Map<String, Exception> previousExceptions ) 061 throws PolicyConfigurationException 062 { 063 if ( !options.contains( policySetting ) ) 064 { 065 // Not a valid code. 066 throw new PolicyConfigurationException( 067 "Unknown error policy setting [" + policySetting + "], valid settings are [" + StringUtils.join( 068 options.iterator(), "," ) + "]" ); 069 } 070 071 if ( ALWAYS.equals( policySetting ) ) 072 { 073 // throw ther exception regardless 074 return true; 075 } 076 077 if ( NOT_PRESENT.equals( policySetting ) ) 078 { 079 // cancel the exception if the file exists 080 return !localFile.exists(); 081 } 082 083 throw new PolicyConfigurationException( 084 "Unable to process checksum policy of [" + policySetting + "], please file a bug report." ); 085 } 086 087 @Override 088 public PolicyOption getDefaultOption() 089 { 090 return NOT_PRESENT; 091 } 092 093 @Override 094 public String getId() 095 { 096 return ID; 097 } 098 099 100 @Override 101 public List<PolicyOption> getOptions() 102 { 103 return options; 104 } 105}