This project has retired. For details please refer to its Attic page.
ChecksumPolicy 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.checksum.ChecksumAlgorithm;
23  import org.apache.archiva.checksum.ChecksummedFile;
24  import org.apache.archiva.checksum.UpdateStatus;
25  import org.apache.archiva.repository.storage.StorageAsset;
26  import org.apache.commons.lang3.StringUtils;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.stereotype.Service;
30  
31  import java.io.IOException;
32  import java.nio.file.Files;
33  import java.nio.file.Path;
34  import java.util.ArrayList;
35  import java.util.Arrays;
36  import java.util.List;
37  import java.util.Properties;
38  
39  /**
40   * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
41   * successfully and completely (or not).
42   *
43   *
44   */
45  @Service( "postDownloadPolicy#checksum" )
46  public class ChecksumPolicy
47      extends AbstractPolicy implements PostDownloadPolicy
48  {
49      private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
50  
51      private static final String ID = "checksum";
52  
53      /**
54       * The IGNORE policy indicates that if the checksum policy is ignored, and
55       * the state of, contents of, or validity of the checksum files are not
56       * checked.
57       */
58      public static final ChecksumOption IGNORE = ChecksumOption.IGNORE;
59  
60      /**
61       * The FAIL policy indicates that if the checksum does not match the
62       * downloaded file, then remove the downloaded artifact, and checksum
63       * files, and fail the transfer to the client side.
64       */
65      public static final ChecksumOption FAIL = ChecksumOption.FAIL;
66  
67      /**
68       * The FIX policy indicates that if the checksum does not match the
69       * downloaded file, then fix the checksum file locally, and return
70       * to the client side the corrected checksum.
71       */
72      public static final ChecksumOption FIX = ChecksumOption.FIX;
73  
74      private List<ChecksumAlgorithm> algorithms = Arrays.asList( ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
75  
76      private List<PolicyOption> options = new ArrayList<>( 3 );
77  
78      public ChecksumPolicy()
79      {
80          super();
81          options.add( FAIL );
82          options.add( FIX );
83          options.add( IGNORE );
84      }
85  
86      @Override
87      public void applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile )
88          throws PolicyViolationException, PolicyConfigurationException
89      {
90          if ( "resource".equals( request.getProperty( "filetype" ) ) )
91          {
92              return;
93          }
94  
95          if ( !options.contains( policySetting ) )
96          {
97              // Not a valid code. 
98              throw new PolicyConfigurationException(
99                  "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
100                     options.iterator(), "," ) + "]" );
101         }
102 
103         if ( IGNORE.equals( policySetting ) )
104         {
105             // Ignore.
106             log.debug( "Checksum policy set to IGNORE." );
107             return;
108         }
109 
110         if ( !localFile.exists() )
111         {
112             // Local File does not exist.
113             throw new PolicyViolationException(
114                 "Checksum policy failure, local file " + localFile.getPath() + " does not exist to check." );
115         }
116 
117         if ( FAIL.equals( policySetting ) && localFile.isFileBased() )
118         {
119             ChecksummedFileFile.html#ChecksummedFile">ChecksummedFile checksum = new ChecksummedFile( localFile.getFilePath() );
120             if ( checksum.isValidChecksums( algorithms ) )
121             {
122                 return;
123             }
124 
125             for ( ChecksumAlgorithm algorithm : algorithms )
126             {
127                 Path file = checksum.getChecksumFile( algorithm );
128                 try
129                 {
130                     Files.deleteIfExists( file );
131                 }
132                 catch ( IOException e )
133                 {
134                     log.error("Could not delete file {}", file);
135                 }
136             }
137 
138             try
139             {
140                 localFile.getStorage().removeAsset( localFile );
141             }
142             catch ( IOException e )
143             {
144                 log.error("Could not delete file {}", localFile);
145             }
146             throw new PolicyViolationException(
147                 "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "
148                     + localFile.getPath() + "." );
149         }
150 
151         if ( FIX.equals( policySetting ) && localFile.isFileBased())
152         {
153             ChecksummedFileFile.html#ChecksummedFile">ChecksummedFile checksum = new ChecksummedFile( localFile.getFilePath() );
154             if ( checksum.fixChecksums( algorithms ).getTotalStatus() != UpdateStatus.ERROR )
155             {
156                 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
157                 return;
158             }
159             else
160             {
161                 throw new PolicyViolationException(
162                     "Checksum policy set to FIX, " + "yet unable to update checksums for local file "
163                         + localFile.getPath() + "." );
164             }
165         }
166 
167         throw new PolicyConfigurationException(
168             "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
169     }
170 
171     @Override
172     public PolicyOption getDefaultOption()
173     {
174         return FIX;
175     }
176 
177     @Override
178     public String getId()
179     {
180         return ID;
181     }
182 
183     @Override
184     public List<PolicyOption> getOptions()
185     {
186         return options;
187     }
188 }