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.commons.lang.StringUtils;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27import org.springframework.stereotype.Service;
2829import java.io.File;
30import java.util.ArrayList;
31import java.util.List;
32import java.util.Properties;
3334/**35 * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded36 * successfully and completely (or not).37 *38 *39 */40 @Service( "postDownloadPolicy#checksum" )
41publicclassChecksumPolicy42implementsPostDownloadPolicy43 {
44private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
4546/**47 * The IGNORE policy indicates that if the checksum policy is ignored, and48 * the state of, contents of, or validity of the checksum files are not49 * checked.50 */51publicstaticfinal String IGNORE = "ignore";
5253/**54 * The FAIL policy indicates that if the checksum does not match the55 * downloaded file, then remove the downloaded artifact, and checksum56 * files, and fail the transfer to the client side.57 */58publicstaticfinal String FAIL = "fail";
5960/**61 * The FIX policy indicates that if the checksum does not match the62 * downloaded file, then fix the checksum file locally, and return63 * to the client side the corrected checksum.64 */65publicstaticfinal String FIX = "fix";
6667private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
6869private List<String> options = new ArrayList<>( 3 );
7071publicChecksumPolicy()
72 {
73 options.add( FAIL );
74 options.add( FIX );
75 options.add( IGNORE );
76 }
7778 @Override
79publicvoid applyPolicy( String policySetting, Properties request, File localFile )
80throws PolicyViolationException, PolicyConfigurationException81 {
82if ( "resource".equals( request.getProperty( "filetype" ) ) )
83 {
84return;
85 }
8687if ( !options.contains( policySetting ) )
88 {
89// Not a valid code. 90thrownewPolicyConfigurationException(
91"Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
92 options.iterator(), "," ) + "]" );
93 }
9495if ( IGNORE.equals( policySetting ) )
96 {
97// Ignore.98 log.debug( "Checksum policy set to IGNORE." );
99return;
100 }
101102if ( !localFile.exists() )
103 {
104// Local File does not exist.105thrownewPolicyViolationException(
106"Checksum policy failure, local file " + localFile.getAbsolutePath() + " does not exist to check." );
107 }
108109if ( FAIL.equals( policySetting ) )
110 {
111 ChecksummedFile checksum = new ChecksummedFile( localFile );
112if ( checksum.isValidChecksums( algorithms ) )
113 {
114return;
115 }
116117for ( ChecksumAlgorithm algorithm : algorithms )
118 {
119 File file = new File( localFile.getAbsolutePath() + "." + algorithm.getExt() );
120if ( file.exists() )
121 {
122 file.delete();
123 }
124 }
125126 localFile.delete();
127thrownewPolicyViolationException(
128"Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "129 + localFile.getAbsolutePath() + "." );
130 }
131132if ( FIX.equals( policySetting ) )
133 {
134 ChecksummedFile checksum = new ChecksummedFile( localFile );
135if ( checksum.fixChecksums( algorithms ) )
136 {
137 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
138return;
139 }
140else141 {
142thrownewPolicyViolationException(
143"Checksum policy set to FIX, " + "yet unable to update checksums for local file "144 + localFile.getAbsolutePath() + "." );
145 }
146 }
147148thrownewPolicyConfigurationException(
149"Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
150 }
151152 @Override
153public String getDefaultOption()
154 {
155return FIX;
156 }
157158 @Override
159public String getId()
160 {
161return"checksum";
162 }
163164 @Override
165public String getName()
166 {
167return"Checksum";
168 }
169170 @Override
171public List<String> getOptions()
172 {
173return options;
174 }
175 }