This project has retired. For details please refer to its
Attic page.
ChecksumPolicy xref
1 package org.apache.archiva.policies;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
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
55
56
57
58 public static final ChecksumOption IGNORE = ChecksumOption.IGNORE;
59
60
61
62
63
64
65 public static final ChecksumOption FAIL = ChecksumOption.FAIL;
66
67
68
69
70
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
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
106 log.debug( "Checksum policy set to IGNORE." );
107 return;
108 }
109
110 if ( !localFile.exists() )
111 {
112
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 }