This project has retired. For details please refer to its Attic page.
PropagateErrorsDownloadPolicy 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.repository.storage.StorageAsset;
23  import org.apache.commons.lang3.StringUtils;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.stereotype.Service;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Properties;
32  
33  /**
34   * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error.
35   */
36  @Service( "downloadErrorPolicy#propagate-errors" )
37  public class PropagateErrorsDownloadPolicy
38      extends AbstractPolicy implements DownloadErrorPolicy
39  {
40      private Logger log = LoggerFactory.getLogger( PropagateErrorsDownloadPolicy.class );
41      private static final String ID = "propagate-errors";
42  
43  
44      /**
45       * Signifies any error should stop searching for other proxies.
46       */
47      public static final DownloadErrorOption STOP = DownloadErrorOption.STOP;
48  
49      /**
50       * Propagate errors at the end after all are gathered, if there was no successful download from other proxies.
51       */
52      public static final DownloadErrorOption QUEUE = DownloadErrorOption.QUEUE;
53  
54      /**
55       * Ignore errors and treat as if it were not found.
56       */
57      public static final DownloadErrorOption IGNORE = DownloadErrorOption.IGNORE;
58  
59      private List<PolicyOption> options = new ArrayList<>( 3 );
60  
61      public PropagateErrorsDownloadPolicy()
62      {
63          options.add( STOP );
64          options.add( QUEUE );
65          options.add( IGNORE );
66      }
67  
68      @Override
69      public boolean applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile, Exception exception,
70                                  Map<String, Exception> previousExceptions )
71          throws PolicyConfigurationException
72      {
73          if ( !options.contains( policySetting ) )
74          {
75              // Not a valid code.
76              throw new PolicyConfigurationException( "Unknown error policy setting [" + policySetting +
77                                                          "], valid settings are [" + StringUtils.join(
78                  options.iterator(), "," ) + "]" );
79          }
80  
81          if ( IGNORE.equals( policySetting ) )
82          {
83              // Ignore.
84              log.debug( "Error policy set to IGNORE." );
85              return false;
86          }
87  
88          String repositoryId = request.getProperty( "remoteRepositoryId" );
89          if ( STOP.equals( policySetting ) )
90          {
91              return true;
92          }
93  
94          if ( QUEUE.equals( policySetting ) )
95          {
96              previousExceptions.put( repositoryId, exception );
97              return true;
98          }
99  
100         throw new PolicyConfigurationException(
101             "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
102     }
103 
104     @Override
105     public PolicyOption getDefaultOption()
106     {
107         return QUEUE;
108     }
109 
110     @Override
111     public String getId()
112     {
113         return ID ;
114     }
115 
116     @Override
117     public List<PolicyOption> getOptions()
118     {
119         return options;
120     }
121 }