View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import org.apache.commons.codec.digest.DigestUtils;
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
30  import org.codehaus.plexus.util.IOUtil;
31  
32  /**
33   * Rule to validate a file to match the specified checksum.
34   *
35   * @author Edward Samson
36   * @author Lyubomyr Shaydariv
37   */
38  public class RequireFileChecksum
39      extends AbstractNonCacheableEnforcerRule
40  {
41  
42      private File file;
43  
44      private String checksum;
45  
46      private String type;
47  
48      @Override
49      public void execute( EnforcerRuleHelper helper )
50          throws EnforcerRuleException
51      {
52          if ( this.file == null )
53          {
54              throw new EnforcerRuleException( "Input file unspecified" );
55          }
56  
57          if ( this.type == null )
58          {
59              throw new EnforcerRuleException( "Hash type unspecified" );
60          }
61  
62          if ( this.checksum == null )
63          {
64              throw new EnforcerRuleException( "Checksum unspecified" );
65          }
66  
67          InputStream inputStream = null;
68          try
69          {
70              if ( this.file.isDirectory() || !this.file.canRead() )
71              {
72                  throw new EnforcerRuleException( "Cannot read file: " + this.file.getAbsolutePath() );
73              }
74  
75              inputStream = new FileInputStream( this.file );
76              String checksum;
77              if ( "md5".equals( this.type ) )
78              {
79                  checksum = DigestUtils.md5Hex( inputStream );
80              }
81              else if ( "sha1".equals( this.type ) )
82              {
83                  checksum = DigestUtils.shaHex( inputStream );
84              }
85              else if ( "sha256".equals( this.type ) )
86              {
87                  checksum = DigestUtils.sha256Hex( inputStream );
88              }
89              else if ( "sha384".equals( this.type ) )
90              {
91                  checksum = DigestUtils.sha384Hex( inputStream );
92              }
93              else if ( "sha512".equals( this.type ) )
94              {
95                  checksum = DigestUtils.sha512Hex( inputStream );
96              }
97              else
98              {
99                  throw new EnforcerRuleException( "Unsupported hash type: " + this.type );
100             }
101             if ( !checksum.equalsIgnoreCase( this.checksum ) )
102             {
103                 throw new EnforcerRuleException( this.type + " hash of " + this.file + " was " + checksum
104                     + " but expected " + this.checksum );
105             }
106         }
107         catch ( IOException e )
108         {
109             throw new EnforcerRuleException( "Unable to calculate checksum", e );
110         }
111         finally
112         {
113             IOUtil.close( inputStream );
114         }
115     }
116 
117     /**
118      * The file to check.
119      *
120      * @param file file
121      */
122     public void setFile( File file )
123     {
124         this.file = file;
125     }
126 
127     /**
128      * The expected checksum value.
129      *
130      * @param checksum checksum
131      */
132     public void setChecksum( String checksum )
133     {
134         this.checksum = checksum;
135     }
136 
137     /**
138      * The checksum algorithm to use. Possible values: "md5", "sha1", "sha256", "sha384", "sha512".
139      *
140      * @param type algorithm
141      */
142     public void setType( String type )
143     {
144         this.type = type;
145     }
146 
147 }