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.archiva.policies.urlcache.UrlFailureCache;
23import org.apache.commons.lang.StringUtils;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26import org.springframework.stereotype.Service;
2728import javax.inject.Inject;
29import java.io.File;
30import java.util.ArrayList;
31import java.util.List;
32import java.util.Properties;
3334/**35 * {@link PreDownloadPolicy} to check if the requested url has failed before.36 */37 @Service( "preDownloadPolicy#cache-failures" )
38publicclassCachedFailuresPolicy39implementsPreDownloadPolicy40 {
41private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
4243/**44 * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked.45 * All resource requests are allowed thru to the remote repo.46 */47publicstaticfinal String NO = "no";
4849/**50 * The YES policy setting means that the existence of old failures is checked, and will51 * prevent the request from being performed against the remote repo.52 */53publicstaticfinal String YES = "yes";
5455 @Inject
56privateUrlFailureCache urlFailureCache;
5758private List<String> options = new ArrayList<>( 2 );
5960publicCachedFailuresPolicy()
61 {
62 options.add( NO );
63 options.add( YES );
64 }
6566 @Override
67publicvoid applyPolicy( String policySetting, Properties request, File localFile )
68throws PolicyViolationException, PolicyConfigurationException69 {
70if ( !options.contains( policySetting ) )
71 {
72// Not a valid code.73thrownewPolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
74"], valid settings are [" + StringUtils.join(
75 options.iterator(), "," ) + "]" );
76 }
7778if ( NO.equals( policySetting ) )
79 {
80// Skip.81 log.debug( "OK to fetch, check-failures policy set to NO." );
82return;
83 }
8485 String url = request.getProperty( "url" );
8687if ( StringUtils.isNotBlank( url ) )
88 {
89if ( urlFailureCache.hasFailedBefore( url ) )
90 {
91thrownewPolicyViolationException(
92"NO to fetch, check-failures detected previous failure on url: " + url );
93 }
94 }
9596 log.debug( "OK to fetch, check-failures detected no issues." );
97 }
9899 @Override
100public String getDefaultOption()
101 {
102return NO;
103 }
104105 @Override
106public String getId()
107 {
108return"cache-failures";
109 }
110111 @Override
112public String getName()
113 {
114return"Cache failures";
115 }
116117 @Override
118public List<String> getOptions()
119 {
120return options;
121 }
122 }