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      private String nonexistentFileMessage;
49  
50      @Override
51      public void execute( EnforcerRuleHelper helper )
52          throws EnforcerRuleException
53      {
54          if ( this.file == null )
55          {
56              throw new EnforcerRuleException( "Input file unspecified" );
57          }
58  
59          if ( this.type == null )
60          {
61              throw new EnforcerRuleException( "Hash type unspecified" );
62          }
63  
64          if ( this.checksum == null )
65          {
66              throw new EnforcerRuleException( "Checksum unspecified" );
67          }
68  
69          if ( !this.file.exists() )
70          {
71              String message = nonexistentFileMessage;
72              if ( message == null )
73              {
74                  message = "File does not exist: " + this.file.getAbsolutePath();
75              }
76              throw new EnforcerRuleException( message );
77          }
78  
79          if ( this.file.isDirectory() )
80          {
81              throw new EnforcerRuleException( "Cannot calculate the checksum of directory: "
82                  + this.file.getAbsolutePath() );
83          }
84  
85          if ( !this.file.canRead() )
86          {
87              throw new EnforcerRuleException( "Cannot read file: " + this.file.getAbsolutePath() );
88          }
89  
90          String checksum = calculateChecksum();
91  
92          if ( !checksum.equalsIgnoreCase( this.checksum ) )
93          {
94              String exceptionMessage = getMessage();
95              if ( exceptionMessage == null )
96              {
97                  exceptionMessage = this.type + " hash of " + this.file + " was " + checksum
98                      + " but expected " + this.checksum;
99              }
100             throw new EnforcerRuleException( exceptionMessage );
101         }
102     }
103 
104     /**
105      * The file to check.
106      *
107      * @param file file
108      */
109     public void setFile( File file )
110     {
111         this.file = file;
112     }
113 
114     /**
115      * The expected checksum value.
116      *
117      * @param checksum checksum
118      */
119     public void setChecksum( String checksum )
120     {
121         this.checksum = checksum;
122     }
123 
124     /**
125      * The checksum algorithm to use. Possible values: "md5", "sha1", "sha256", "sha384", "sha512".
126      *
127      * @param type algorithm
128      */
129     public void setType( String type )
130     {
131         this.type = type;
132     }
133 
134     /**
135      * The friendly message to use when the file does not exist.
136      *
137      * @param nonexistentFileMessage message
138      */
139     public void setNonexistentFileMessage( String nonexistentFileMessage )
140     {
141         this.nonexistentFileMessage = nonexistentFileMessage;
142     }
143 
144     private String calculateChecksum()
145         throws EnforcerRuleException
146     {
147         InputStream inputStream = null;
148         try
149         {
150             inputStream = new FileInputStream( this.file );
151             String checksum;
152             if ( "md5".equals( this.type ) )
153             {
154                 checksum = DigestUtils.md5Hex( inputStream );
155             }
156             else if ( "sha1".equals( this.type ) )
157             {
158                 checksum = DigestUtils.shaHex( inputStream );
159             }
160             else if ( "sha256".equals( this.type ) )
161             {
162                 checksum = DigestUtils.sha256Hex( inputStream );
163             }
164             else if ( "sha384".equals( this.type ) )
165             {
166                 checksum = DigestUtils.sha384Hex( inputStream );
167             }
168             else if ( "sha512".equals( this.type ) )
169             {
170                 checksum = DigestUtils.sha512Hex( inputStream );
171             }
172             else
173             {
174                 throw new EnforcerRuleException( "Unsupported hash type: " + this.type );
175             }
176             return checksum;
177         }
178         catch ( IOException e )
179         {
180             throw new EnforcerRuleException( "Unable to calculate checksum", e );
181         }
182         finally
183         {
184             IOUtil.close( inputStream );
185         }
186     }
187 }