1 package org.apache.maven.archiva.reporting.processor;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.model.Model;
27 import org.codehaus.plexus.digest.Digester;
28 import org.codehaus.plexus.digest.DigesterException;
31 import java.io.IOException;
34 * This class reports invalid and mismatched checksums of artifacts and metadata files.
35 * It validates MD5 and SHA-1 checksums.
37 * @plexus.component role="org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor" role-hint="checksum"
39 public class ChecksumArtifactReportProcessor
40 implements ArtifactReportProcessor
43 * @plexus.requirement role-hint="sha1"
45 private Digester sha1Digester;
48 * @plexus.requirement role-hint="md5"
50 private Digester md5Digester;
52 private static final String ROLE_HINT = "checksum";
54 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
56 ArtifactRepository repository = artifact.getRepository();
58 if ( !"file".equals( repository.getProtocol() ) )
60 // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
61 throw new UnsupportedOperationException(
62 "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
65 //check if checksum files exist
66 String path = repository.pathOf( artifact );
67 File file = new File( repository.getBasedir(), path );
69 // TODO: make md5 configurable
70 // verifyChecksum( repository, path + ".md5", file, md5Digester, reporter, artifact );
71 verifyChecksum( repository, path + ".sha1", file, sha1Digester, reporter, artifact );
74 private void verifyChecksum( ArtifactRepository repository, String path, File file, Digester digester,
75 ReportingDatabase reporter, Artifact artifact )
77 File checksumFile = new File( repository.getBasedir(), path );
78 if ( checksumFile.exists() )
82 digester.verify( file, FileUtils.readFileToString( checksumFile, null ) );
84 catch ( DigesterException e )
86 addFailure( reporter, artifact, "checksum-wrong", e.getMessage() );
88 catch ( IOException e )
90 addFailure( reporter, artifact, "checksum-io-exception", "Read file error: " + e.getMessage() );
95 addFailure( reporter, artifact, "checksum-missing",
96 digester.getAlgorithm() + " checksum file does not exist." );
100 private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
102 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
103 reporter.addFailure( artifact, ROLE_HINT, problem, reason );