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.commons.lang.StringUtils;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.stereotype.Service;
27  
28  import javax.inject.Inject;
29  import java.io.File;
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      implements PreDownloadPolicy
40  {
41      private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
42  
43      /**
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       */
47      public static final String NO = "no";
48  
49      /**
50       * The YES policy setting means that the existence of old failures is checked, and will
51       * prevent the request from being performed against the remote repo.
52       */
53      public static final String YES = "yes";
54  
55      @Inject
56      private UrlFailureCache urlFailureCache;
57  
58      private List<String> options = new ArrayList<>( 2 );
59  
60      public CachedFailuresPolicy()
61      {
62          options.add( NO );
63          options.add( YES );
64      }
65  
66      @Override
67      public void applyPolicy( String policySetting, Properties request, File localFile )
68          throws PolicyViolationException, PolicyConfigurationException
69      {
70          if ( !options.contains( policySetting ) )
71          {
72              // Not a valid code.
73              throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
74                                                          "], valid settings are [" + StringUtils.join(
75                  options.iterator(), "," ) + "]" );
76          }
77  
78          if ( NO.equals( policySetting ) )
79          {
80              // Skip.
81              log.debug( "OK to fetch, check-failures policy set to NO." );
82              return;
83          }
84  
85          String url = request.getProperty( "url" );
86  
87          if ( StringUtils.isNotBlank( url ) )
88          {
89              if ( urlFailureCache.hasFailedBefore( url ) )
90              {
91                  throw new PolicyViolationException(
92                      "NO to fetch, check-failures detected previous failure on url: " + url );
93              }
94          }
95  
96          log.debug( "OK to fetch, check-failures detected no issues." );
97      }
98  
99      @Override
100     public String getDefaultOption()
101     {
102         return NO;
103     }
104 
105     @Override
106     public String getId()
107     {
108         return "cache-failures";
109     }
110 
111     @Override
112     public String getName()
113     {
114         return "Cache failures";
115     }
116 
117     @Override
118     public List<String> getOptions()
119     {
120         return options;
121     }
122 }