1 package org.apache.maven.archiva.reporting.processor;
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.Artifact;
20 import org.apache.maven.artifact.repository.ArtifactRepository;
21 import org.apache.maven.model.Model;
22 import org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor;
23 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
24 import org.codehaus.plexus.digest.Digester;
25 import org.codehaus.plexus.digest.DigesterException;
26 import org.codehaus.plexus.util.FileUtils;
29 import java.io.IOException;
32 * This class reports invalid and mismatched checksums of artifacts and metadata files.
33 * It validates MD5 and SHA-1 checksums.
35 * @plexus.component role="org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor" role-hint="checksum"
37 public class ChecksumArtifactReportProcessor
38 implements ArtifactReportProcessor
41 * @plexus.requirement role-hint="sha1"
43 private Digester sha1Digester;
46 * @plexus.requirement role-hint="md5"
48 private Digester md5Digester;
50 private static final String ROLE_HINT = "checksum";
52 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
54 ArtifactRepository repository = artifact.getRepository();
56 if ( !"file".equals( repository.getProtocol() ) )
58 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
59 throw new UnsupportedOperationException(
60 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
63 //check if checksum files exist
64 String path = repository.pathOf( artifact );
65 File file = new File( repository.getBasedir(), path );
67 // TODO: make md5 configurable
68 // verifyChecksum( repository, path + ".md5", file, md5Digester, reporter, artifact );
69 verifyChecksum( repository, path + ".sha1", file, sha1Digester, reporter, artifact );
72 private void verifyChecksum( ArtifactRepository repository, String path, File file, Digester digester,
73 ReportingDatabase reporter, Artifact artifact )
75 File checksumFile = new File( repository.getBasedir(), path );
76 if ( checksumFile.exists() )
80 digester.verify( file, FileUtils.fileRead( checksumFile ) );
82 catch ( DigesterException e )
84 addFailure( reporter, artifact, "checksum-wrong", e.getMessage() );
86 catch ( IOException e )
88 addFailure( reporter, artifact, "checksum-io-exception", "Read file error: " + e.getMessage() );
93 addFailure( reporter, artifact, "checksum-missing",
94 digester.getAlgorithm() + " checksum file does not exist." );
98 private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
100 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
101 reporter.addFailure( artifact, ROLE_HINT, problem, reason );