1package org.apache.archiva.policies;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.commons.lang.StringUtils;
23import org.springframework.stereotype.Service;
2425import java.io.File;
26import java.util.ArrayList;
27import java.util.List;
28import java.util.Map;
29import java.util.Properties;
3031/**32 * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error.33 */34 @Service( "downloadErrorPolicy#propagate-errors-on-update" )
35publicclassPropagateErrorsOnUpdateDownloadPolicy36implementsDownloadErrorPolicy37 {
38/**39 * Signifies any error should cause a failure whether the artifact is already present or not.40 */41publicstaticfinal String ALWAYS = "always";
4243/**44 * Signifies any error should cause a failure only if the artifact is not already present.45 */46publicstaticfinal String NOT_PRESENT = "artifact not already present";
4748private List<String> options = new ArrayList<>( 2 );
4950publicPropagateErrorsOnUpdateDownloadPolicy()
51 {
52 options.add( ALWAYS );
53 options.add( NOT_PRESENT );
54 }
5556 @Override
57publicboolean applyPolicy( String policySetting, Properties request, File localFile, Exception exception,
58 Map<String, Exception> previousExceptions )
59throwsPolicyConfigurationException60 {
61if ( !options.contains( policySetting ) )
62 {
63// Not a valid code.64thrownewPolicyConfigurationException(
65"Unknown error policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
66 options.iterator(), "," ) + "]" );
67 }
6869if ( ALWAYS.equals( policySetting ) )
70 {
71// throw ther exception regardless72returntrue;
73 }
7475if ( NOT_PRESENT.equals( policySetting ) )
76 {
77// cancel the exception if the file exists78return !localFile.exists();
79 }
8081thrownewPolicyConfigurationException(
82"Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
83 }
8485 @Override
86public String getDefaultOption()
87 {
88return NOT_PRESENT;
89 }
9091 @Override
92public String getId()
93 {
94return"propagate-errors-on-update";
95 }
9697 @Override
98public String getName()
99 {
100return"Return error when";
101 }
102103 @Override
104public List<String> getOptions()
105 {
106return options;
107 }
108 }