1package org.apache.archiva.policies;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.checksum.ChecksumAlgorithm;
23import org.apache.archiva.checksum.ChecksummedFile;
24import org.apache.archiva.checksum.UpdateStatus;
25import org.apache.archiva.repository.storage.StorageAsset;
26import org.apache.commons.lang3.StringUtils;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29import org.springframework.stereotype.Service;
3031import java.io.IOException;
32import java.nio.file.Files;
33import java.nio.file.Path;
34import java.util.ArrayList;
35import java.util.Arrays;
36import java.util.List;
37import java.util.Properties;
3839/**40 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded41 * successfully and completely (or not).42 *43 *44 */45 @Service( "postDownloadPolicy#checksum" )
46publicclassChecksumPolicy47extendsAbstractPolicyimplementsPostDownloadPolicy48 {
49private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
5051privatestaticfinal String ID = "checksum";
5253/**54 * The IGNORE policy indicates that if the checksum policy is ignored, and55 * the state of, contents of, or validity of the checksum files are not56 * checked.57 */58publicstaticfinalChecksumOption IGNORE = ChecksumOption.IGNORE;
5960/**61 * The FAIL policy indicates that if the checksum does not match the62 * downloaded file, then remove the downloaded artifact, and checksum63 * files, and fail the transfer to the client side.64 */65publicstaticfinalChecksumOption FAIL = ChecksumOption.FAIL;
6667/**68 * The FIX policy indicates that if the checksum does not match the69 * downloaded file, then fix the checksum file locally, and return70 * to the client side the corrected checksum.71 */72publicstaticfinalChecksumOption FIX = ChecksumOption.FIX;
7374private List<ChecksumAlgorithm> algorithms = Arrays.asList( ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
7576private List<PolicyOption> options = new ArrayList<>( 3 );
7778publicChecksumPolicy()
79 {
80super();
81 options.add( FAIL );
82 options.add( FIX );
83 options.add( IGNORE );
84 }
8586 @Override
87publicvoid applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile )
88throws PolicyViolationException, PolicyConfigurationException89 {
90if ( "resource".equals( request.getProperty( "filetype" ) ) )
91 {
92return;
93 }
9495if ( !options.contains( policySetting ) )
96 {
97// Not a valid code. 98thrownewPolicyConfigurationException(
99"Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
100 options.iterator(), "," ) + "]" );
101 }
102103if ( IGNORE.equals( policySetting ) )
104 {
105// Ignore.106 log.debug( "Checksum policy set to IGNORE." );
107return;
108 }
109110if ( !localFile.exists() )
111 {
112// Local File does not exist.113thrownewPolicyViolationException(
114"Checksum policy failure, local file " + localFile.getPath() + " does not exist to check." );
115 }
116117if ( FAIL.equals( policySetting ) && localFile.isFileBased() )
118 {
119 ChecksummedFile checksum = new ChecksummedFile( localFile.getFilePath() );
120if ( checksum.isValidChecksums( algorithms ) )
121 {
122return;
123 }
124125for ( ChecksumAlgorithm algorithm : algorithms )
126 {
127 Path file = checksum.getChecksumFile( algorithm );
128try129 {
130 Files.deleteIfExists( file );
131 }
132catch ( IOException e )
133 {
134 log.error("Could not delete file {}", file);
135 }
136 }
137138try139 {
140 localFile.getStorage().removeAsset( localFile );
141 }
142catch ( IOException e )
143 {
144 log.error("Could not delete file {}", localFile);
145 }
146thrownewPolicyViolationException(
147"Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "148 + localFile.getPath() + "." );
149 }
150151if ( FIX.equals( policySetting ) && localFile.isFileBased())
152 {
153 ChecksummedFile checksum = new ChecksummedFile( localFile.getFilePath() );
154if ( checksum.fixChecksums( algorithms ).getTotalStatus() != UpdateStatus.ERROR )
155 {
156 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
157return;
158 }
159else160 {
161thrownewPolicyViolationException(
162"Checksum policy set to FIX, " + "yet unable to update checksums for local file "163 + localFile.getPath() + "." );
164 }
165 }
166167thrownewPolicyConfigurationException(
168"Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
169 }
170171 @Override
172publicPolicyOption getDefaultOption()
173 {
174return FIX;
175 }
176177 @Override
178public String getId()
179 {
180return ID;
181 }
182183 @Override
184public List<PolicyOption> getOptions()
185 {
186return options;
187 }
188 }