This project has retired. For details please refer to its Attic page.
CachedFailuresPolicy xref
View Javadoc
1   package org.apache.archiva.policies;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.archiva.policies.urlcache.UrlFailureCache;
23  import org.apache.archiva.repository.storage.StorageAsset;
24  import org.apache.commons.lang3.StringUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.stereotype.Service;
28  
29  import javax.inject.Inject;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Properties;
33  
34  /**
35   * {@link PreDownloadPolicy} to check if the requested url has failed before.
36   */
37  @Service( "preDownloadPolicy#cache-failures" )
38  public class CachedFailuresPolicy
39      extends AbstractPolicy implements PreDownloadPolicy
40  {
41      private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
42      private static final String ID = "cache-failures";
43  
44      /**
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       */
48      public static final StandardOption NO = StandardOption.NO;
49  
50      /**
51       * The YES policy setting means that the existence of old failures is checked, and will
52       * prevent the request from being performed against the remote repo.
53       */
54      public static final StandardOption YES = StandardOption.YES;
55  
56      @Inject
57      private UrlFailureCache urlFailureCache;
58  
59      private List<PolicyOption> options = new ArrayList<>( 2 );
60  
61      public CachedFailuresPolicy()
62      {
63          super();
64          options.add( NO );
65          options.add( YES );
66      }
67  
68      @Override
69      public void applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile )
70          throws PolicyViolationException, PolicyConfigurationException
71      {
72          if ( !options.contains( policySetting ) )
73          {
74              // Not a valid code.
75              throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
76                                                          "], valid settings are [" + StringUtils.join(
77                  options.iterator(), "," ) + "]" );
78          }
79  
80          if ( NO.equals( policySetting ) )
81          {
82              // Skip.
83              log.debug( "OK to fetch, check-failures policy set to NO." );
84              return;
85          }
86  
87          String url = request.getProperty( "url" );
88  
89          if ( StringUtils.isNotBlank( url ) )
90          {
91              if ( urlFailureCache.hasFailedBefore( url ) )
92              {
93                  throw new PolicyViolationException(
94                      "NO to fetch, check-failures detected previous failure on url: " + url );
95              }
96          }
97  
98          log.debug( "OK to fetch, check-failures detected no issues." );
99      }
100 
101     @Override
102     public PolicyOption getDefaultOption()
103     {
104         return NO;
105     }
106 
107     @Override
108     public String getId()
109     {
110         return ID;
111     }
112 
113 
114     @Override
115     public List<PolicyOption> getOptions()
116     {
117         return options;
118     }
119 }