001 package 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 022 import org.apache.archiva.policies.urlcache.UrlFailureCache; 023 import org.apache.commons.lang.StringUtils; 024 import org.slf4j.Logger; 025 import org.slf4j.LoggerFactory; 026 import org.springframework.stereotype.Service; 027 028 import javax.inject.Inject; 029 import java.io.File; 030 import java.util.ArrayList; 031 import java.util.List; 032 import java.util.Properties; 033 034 /** 035 * {@link PreDownloadPolicy} to check if the requested url has failed before. 036 */ 037 @Service( "preDownloadPolicy#cache-failures" ) 038 public class CachedFailuresPolicy 039 implements PreDownloadPolicy 040 { 041 private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class ); 042 043 /** 044 * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked. 045 * All resource requests are allowed thru to the remote repo. 046 */ 047 public static final String NO = "no"; 048 049 /** 050 * The YES policy setting means that the existence of old failures is checked, and will 051 * prevent the request from being performed against the remote repo. 052 */ 053 public static final String YES = "yes"; 054 055 /** 056 * 057 */ 058 @Inject 059 private UrlFailureCache urlFailureCache; 060 061 private List<String> options = new ArrayList<String>( 2 ); 062 063 public CachedFailuresPolicy() 064 { 065 options.add( NO ); 066 options.add( YES ); 067 } 068 069 public void applyPolicy( String policySetting, Properties request, File localFile ) 070 throws PolicyViolationException, PolicyConfigurationException 071 { 072 if ( !options.contains( policySetting ) ) 073 { 074 // Not a valid code. 075 throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting + 076 "], valid settings are [" + StringUtils.join( 077 options.iterator(), "," ) + "]" ); 078 } 079 080 if ( NO.equals( policySetting ) ) 081 { 082 // Skip. 083 log.debug( "OK to fetch, check-failures policy set to NO." ); 084 return; 085 } 086 087 String url = request.getProperty( "url" ); 088 089 if ( StringUtils.isNotBlank( url ) ) 090 { 091 if ( urlFailureCache.hasFailedBefore( url ) ) 092 { 093 throw new PolicyViolationException( 094 "NO to fetch, check-failures detected previous failure on url: " + url ); 095 } 096 } 097 098 log.debug( "OK to fetch, check-failures detected no issues." ); 099 } 100 101 public String getDefaultOption() 102 { 103 return NO; 104 } 105 106 public String getId() 107 { 108 return "cache-failures"; 109 } 110 111 public String getName() 112 { 113 return "Cache failures"; 114 } 115 116 public List<String> getOptions() 117 { 118 return options; 119 } 120 }