1 package org.apache.maven.repository.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.repository.ArtifactRepository;
20 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
21 import org.apache.maven.repository.digest.Digester;
22 import org.codehaus.plexus.util.FileUtils;
25 import java.io.IOException;
26 import java.security.NoSuchAlgorithmException;
29 * This class reports invalid and mismatched checksums of artifacts and metadata files.
30 * It validates MD5 and SHA-1 checksums.
32 * @plexus.component role="org.apache.maven.repository.reporting.MetadataReportProcessor" role-hint="checksum-metadata"
34 public class ChecksumMetadataReporter
35 implements MetadataReportProcessor
37 /** @plexus.requirement */
38 private Digester digester;
41 * Validate the checksums of the metadata. Get the metadata file from the
42 * repository then validate the checksum.
44 public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter )
46 if ( !"file".equals( repository.getProtocol() ) )
48 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
49 throw new UnsupportedOperationException(
50 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
53 //check if checksum files exist
54 String path = repository.pathOfRemoteRepositoryMetadata( metadata );
55 File file = new File( repository.getBasedir(), path );
57 File md5File = new File( repository.getBasedir(), path + ".md5" );
58 if ( md5File.exists() )
62 if ( digester.verifyChecksum( file, FileUtils.fileRead( md5File ), Digester.MD5 ) )
64 reporter.addSuccess( metadata );
68 reporter.addFailure( metadata, "MD5 checksum does not match." );
71 catch ( NoSuchAlgorithmException e )
73 reporter.addFailure( metadata, "Unable to read MD5: " + e.getMessage() );
75 catch ( IOException e )
77 reporter.addFailure( metadata, "Unable to read MD5: " + e.getMessage() );
82 reporter.addFailure( metadata, "MD5 checksum file does not exist." );
85 File sha1File = new File( repository.getBasedir(), path + ".sha1" );
86 if ( sha1File.exists() )
90 if ( digester.verifyChecksum( file, FileUtils.fileRead( sha1File ), Digester.SHA1 ) )
92 reporter.addSuccess( metadata );
96 reporter.addFailure( metadata, "SHA-1 checksum does not match." );
99 catch ( NoSuchAlgorithmException e )
101 reporter.addFailure( metadata, "Unable to read SHA1: " + e.getMessage() );
103 catch ( IOException e )
105 reporter.addFailure( metadata, "Unable to read SHA1: " + e.getMessage() );
110 reporter.addFailure( metadata, "SHA-1 checksum file does not exist." );