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.archiva.repository.storage.StorageAsset;
24import org.apache.commons.lang3.StringUtils;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27import org.springframework.stereotype.Service;
2829import javax.inject.Inject;
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" )
38publicclassCachedFailuresPolicy39extendsAbstractPolicyimplementsPreDownloadPolicy40 {
41private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
42privatestaticfinal String ID = "cache-failures";
4344/**45 * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked.46 * All resource requests are allowed thru to the remote repo.47 */48publicstaticfinalStandardOption NO = StandardOption.NO;
4950/**51 * The YES policy setting means that the existence of old failures is checked, and will52 * prevent the request from being performed against the remote repo.53 */54publicstaticfinalStandardOption YES = StandardOption.YES;
5556 @Inject
57privateUrlFailureCache urlFailureCache;
5859private List<PolicyOption> options = new ArrayList<>( 2 );
6061publicCachedFailuresPolicy()
62 {
63super();
64 options.add( NO );
65 options.add( YES );
66 }
6768 @Override
69publicvoid applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile )
70throws PolicyViolationException, PolicyConfigurationException71 {
72if ( !options.contains( policySetting ) )
73 {
74// Not a valid code.75thrownewPolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
76"], valid settings are [" + StringUtils.join(
77 options.iterator(), "," ) + "]" );
78 }
7980if ( NO.equals( policySetting ) )
81 {
82// Skip.83 log.debug( "OK to fetch, check-failures policy set to NO." );
84return;
85 }
8687 String url = request.getProperty( "url" );
8889if ( StringUtils.isNotBlank( url ) )
90 {
91if ( urlFailureCache.hasFailedBefore( url ) )
92 {
93thrownewPolicyViolationException(
94"NO to fetch, check-failures detected previous failure on url: " + url );
95 }
96 }
9798 log.debug( "OK to fetch, check-failures detected no issues." );
99 }
100101 @Override
102publicPolicyOption getDefaultOption()
103 {
104return NO;
105 }
106107 @Override
108public String getId()
109 {
110return ID;
111 }
112113114 @Override
115public List<PolicyOption> getOptions()
116 {
117return options;
118 }
119 }