You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CachedFailuresPolicy.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package org.apache.archiva.policies;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.policies.urlcache.UrlFailureCache;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.stereotype.Service;
  25. import javax.inject.Inject;
  26. import java.io.File;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Properties;
  30. /**
  31. * {@link PreDownloadPolicy} to check if the requested url has failed before.
  32. */
  33. @Service( "preDownloadPolicy#cache-failures" )
  34. public class CachedFailuresPolicy
  35. implements PreDownloadPolicy
  36. {
  37. private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
  38. /**
  39. * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked.
  40. * All resource requests are allowed thru to the remote repo.
  41. */
  42. public static final String NO = "no";
  43. /**
  44. * The YES policy setting means that the existence of old failures is checked, and will
  45. * prevent the request from being performed against the remote repo.
  46. */
  47. public static final String YES = "yes";
  48. @Inject
  49. private UrlFailureCache urlFailureCache;
  50. private List<String> options = new ArrayList<>( 2 );
  51. public CachedFailuresPolicy()
  52. {
  53. options.add( NO );
  54. options.add( YES );
  55. }
  56. public void applyPolicy( String policySetting, Properties request, File localFile )
  57. throws PolicyViolationException, PolicyConfigurationException
  58. {
  59. if ( !options.contains( policySetting ) )
  60. {
  61. // Not a valid code.
  62. throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
  63. "], valid settings are [" + StringUtils.join(
  64. options.iterator(), "," ) + "]" );
  65. }
  66. if ( NO.equals( policySetting ) )
  67. {
  68. // Skip.
  69. log.debug( "OK to fetch, check-failures policy set to NO." );
  70. return;
  71. }
  72. String url = request.getProperty( "url" );
  73. if ( StringUtils.isNotBlank( url ) )
  74. {
  75. if ( urlFailureCache.hasFailedBefore( url ) )
  76. {
  77. throw new PolicyViolationException(
  78. "NO to fetch, check-failures detected previous failure on url: " + url );
  79. }
  80. }
  81. log.debug( "OK to fetch, check-failures detected no issues." );
  82. }
  83. public String getDefaultOption()
  84. {
  85. return NO;
  86. }
  87. public String getId()
  88. {
  89. return "cache-failures";
  90. }
  91. public String getName()
  92. {
  93. return "Cache failures";
  94. }
  95. public List<String> getOptions()
  96. {
  97. return options;
  98. }
  99. }