This project has retired. For details please refer to its
Attic page.
ChecksummedFile xref
1 package org.apache.archiva.checksum;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.Files;
31 import java.nio.file.StandardOpenOption;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class ChecksummedFile
51 {
52 private final Logger log = LoggerFactory.getLogger( ChecksummedFile.class );
53
54 private static final Pattern METADATA_PATTERN = Pattern.compile( "maven-metadata-\\S*.xml" );
55
56 private final File referenceFile;
57
58
59
60
61
62
63 public ChecksummedFile( final File referenceFile )
64 {
65 this.referenceFile = referenceFile;
66 }
67
68
69
70
71
72
73
74
75 public String calculateChecksum( ChecksumAlgorithm checksumAlgorithm )
76 throws IOException
77 {
78
79 try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
80 {
81 Checksum checksum = new Checksum( checksumAlgorithm );
82 checksum.update( fis );
83 return checksum.getChecksum();
84 }
85 }
86
87
88
89
90
91
92
93
94 public File createChecksum( ChecksumAlgorithm checksumAlgorithm )
95 throws IOException
96 {
97 File checksumFile = new File( referenceFile.getAbsolutePath() + "." + checksumAlgorithm.getExt() );
98 Files.deleteIfExists( checksumFile.toPath() );
99 String checksum = calculateChecksum( checksumAlgorithm );
100 Files.write( checksumFile.toPath(),
101 ( checksum + " " + referenceFile.getName() ).getBytes(),
102 StandardOpenOption.CREATE_NEW );
103 return checksumFile;
104 }
105
106
107
108
109
110
111
112 public File getChecksumFile( ChecksumAlgorithm checksumAlgorithm )
113 {
114 return new File( referenceFile.getAbsolutePath() + "." + checksumAlgorithm.getExt() );
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public boolean isValidChecksum( ChecksumAlgorithm algorithm )
130 throws IOException
131 {
132 return isValidChecksums( new ChecksumAlgorithm[]{ algorithm } );
133 }
134
135
136
137
138
139
140
141
142 public boolean isValidChecksums( ChecksumAlgorithm algorithms[] )
143 {
144
145 try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
146 {
147 List<Checksum> checksums = new ArrayList<>( algorithms.length );
148
149 for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
150 {
151 File checksumFile = getChecksumFile( checksumAlgorithm );
152
153
154 if ( checksumFile.exists() )
155 {
156 checksums.add( new Checksum( checksumAlgorithm ) );
157 }
158 }
159
160
161 if ( checksums.isEmpty() )
162 {
163
164 return false;
165 }
166
167
168 try
169 {
170 Checksum.update( checksums, fis );
171 }
172 catch ( IOException e )
173 {
174 log.warn( "Unable to update checksum:{}", e.getMessage() );
175 return false;
176 }
177
178 boolean valid = true;
179
180
181 try
182 {
183 for ( Checksum checksum : checksums )
184 {
185 ChecksumAlgorithm checksumAlgorithm = checksum.getAlgorithm();
186 File checksumFile = getChecksumFile( checksumAlgorithm );
187
188 String rawChecksum = FileUtils.readFileToString( checksumFile );
189 String expectedChecksum = parseChecksum( rawChecksum, checksumAlgorithm, referenceFile.getName() );
190
191 if ( !StringUtils.equalsIgnoreCase( expectedChecksum, checksum.getChecksum() ) )
192 {
193 valid = false;
194 }
195 }
196 }
197 catch ( IOException e )
198 {
199 log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
200 return false;
201 }
202
203 return valid;
204 }
205 catch ( IOException e )
206 {
207 log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
208 return false;
209 }
210 }
211
212
213
214
215
216
217
218 public boolean fixChecksums( ChecksumAlgorithm[] algorithms )
219 {
220 List<Checksum> checksums = new ArrayList<>( algorithms.length );
221
222 for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
223 {
224 checksums.add( new Checksum( checksumAlgorithm ) );
225 }
226
227
228 if ( checksums.isEmpty() )
229 {
230
231 return true;
232 }
233
234 try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
235 {
236
237 Checksum.update( checksums, fis );
238 }
239 catch ( IOException e )
240 {
241 log.warn( e.getMessage(), e );
242 return false;
243 }
244
245 boolean valid = true;
246
247
248 for ( Checksum checksum : checksums )
249 {
250 ChecksumAlgorithm checksumAlgorithm = checksum.getAlgorithm();
251 try
252 {
253 File checksumFile = getChecksumFile( checksumAlgorithm );
254 String actualChecksum = checksum.getChecksum();
255
256 if ( checksumFile.exists() )
257 {
258 String rawChecksum = FileUtils.readFileToString( checksumFile );
259 String expectedChecksum = parseChecksum( rawChecksum, checksumAlgorithm, referenceFile.getName() );
260
261 if ( !StringUtils.equalsIgnoreCase( expectedChecksum, actualChecksum ) )
262 {
263
264 FileUtils.writeStringToFile( checksumFile, actualChecksum + " " + referenceFile.getName() );
265 }
266 }
267 else
268 {
269 FileUtils.writeStringToFile( checksumFile, actualChecksum + " " + referenceFile.getName() );
270 }
271 }
272 catch ( IOException e )
273 {
274 log.warn( e.getMessage(), e );
275 valid = false;
276 }
277 }
278
279 return valid;
280
281 }
282
283 private boolean isValidChecksumPattern( String filename, String path )
284 {
285
286
287 Matcher m = METADATA_PATTERN.matcher( path );
288 if ( m.matches() )
289 {
290 return filename.endsWith( path ) || ( "-".equals( filename ) ) || filename.endsWith( "maven-metadata.xml" );
291 }
292
293 return filename.endsWith( path ) || ( "-".equals( filename ) );
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 public String parseChecksum( String rawChecksumString, ChecksumAlgorithm expectedHash, String expectedPath )
310 throws IOException
311 {
312 String trimmedChecksum = rawChecksumString.replace( '\n', ' ' ).trim();
313
314
315 String regex = expectedHash.getType() + "\\s*\\(([^)]*)\\)\\s*=\\s*([a-fA-F0-9]+)";
316 Matcher m = Pattern.compile( regex ).matcher( trimmedChecksum );
317 if ( m.matches() )
318 {
319 String filename = m.group( 1 );
320 if ( !isValidChecksumPattern( filename, expectedPath ) )
321 {
322 throw new IOException(
323 "Supplied checksum file '" + filename + "' does not match expected file: '" + expectedPath + "'" );
324 }
325 trimmedChecksum = m.group( 2 );
326 }
327 else
328 {
329
330 m = Pattern.compile( "([a-fA-F0-9]+)\\s+\\*?(.+)" ).matcher( trimmedChecksum );
331 if ( m.matches() )
332 {
333 String filename = m.group( 2 );
334 if ( !isValidChecksumPattern( filename, expectedPath ) )
335 {
336 throw new IOException(
337 "Supplied checksum file '" + filename + "' does not match expected file: '" + expectedPath
338 + "'" );
339 }
340 trimmedChecksum = m.group( 1 );
341 }
342 }
343 return trimmedChecksum;
344 }
345 }