1 package org.apache.maven.archiva.policies;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Inject;
36 * {@link PreDownloadPolicy} to check if the requested url has failed before.
39 * plexus.component role="org.apache.maven.archiva.policies.PreDownloadPolicy"
40 * role-hint="cache-failures"
42 @Service("preDownloadPolicy#cache-failures")
43 public class CachedFailuresPolicy
44 implements PreDownloadPolicy
46 private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
49 * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked.
50 * All resource requests are allowed thru to the remote repo.
52 public static final String NO = "no";
55 * The YES policy setting means that the existence of old failures is checked, and will
56 * prevent the request from being performed against the remote repo.
58 public static final String YES = "yes";
64 private UrlFailureCache urlFailureCache;
66 private List<String> options = new ArrayList<String>();
68 public CachedFailuresPolicy()
74 public void applyPolicy( String policySetting, Properties request, File localFile )
75 throws PolicyViolationException, PolicyConfigurationException
77 if ( !options.contains( policySetting ) )
80 throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
81 "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
84 if ( NO.equals( policySetting ) )
87 log.debug( "OK to fetch, check-failures policy set to NO." );
91 String url = request.getProperty( "url" );
93 if ( StringUtils.isNotBlank( url ) )
95 if ( urlFailureCache.hasFailedBefore( url ) )
97 throw new PolicyViolationException(
98 "NO to fetch, check-failures detected previous failure on url: " + url );
102 log.debug( "OK to fetch, check-failures detected no issues." );
105 public String getDefaultOption()
110 public String getId()
112 return "cache-failures";
115 public String getName()
117 return "Cache failures";
120 public List<String> getOptions()