1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.enforcer.rules.checksum;
20
21 import javax.inject.Named;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.file.Files;
27
28 import org.apache.commons.codec.digest.DigestUtils;
29 import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
30 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
31 import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
32
33
34
35
36
37
38
39
40 @Named("requireFileChecksum")
41 public class RequireFileChecksum extends AbstractStandardEnforcerRule {
42
43 private File file;
44
45 private String checksum;
46
47 private String type;
48
49 private String nonexistentFileMessage;
50
51 @Override
52 public void execute() throws EnforcerRuleException {
53 if (this.file == null) {
54 throw new EnforcerRuleError("Input file unspecified");
55 }
56
57 if (this.type == null) {
58 throw new EnforcerRuleError("Hash type unspecified");
59 }
60
61 if (this.checksum == null) {
62 throw new EnforcerRuleError("Checksum unspecified");
63 }
64
65 if (!this.file.exists()) {
66 String message = nonexistentFileMessage;
67 if (message == null) {
68 message = "File does not exist: " + this.file.getAbsolutePath();
69 }
70 throw new EnforcerRuleException(message);
71 }
72
73 if (this.file.isDirectory()) {
74 throw new EnforcerRuleError("Cannot calculate the checksum of directory: " + this.file.getAbsolutePath());
75 }
76
77 if (!this.file.canRead()) {
78 throw new EnforcerRuleError("Cannot read file: " + this.file.getAbsolutePath());
79 }
80
81 String checksum = calculateChecksum();
82
83 if (!checksum.equalsIgnoreCase(this.checksum)) {
84 String exceptionMessage = getMessage();
85 if (exceptionMessage == null) {
86 exceptionMessage =
87 this.type + " hash of " + this.file + " was " + checksum + " but expected " + this.checksum;
88 }
89 throw new EnforcerRuleException(exceptionMessage);
90 }
91 }
92
93
94
95
96
97
98 public void setFile(File file) {
99 this.file = file;
100 }
101
102 public File getFile() {
103 return file;
104 }
105
106
107
108
109
110
111 public void setChecksum(String checksum) {
112 this.checksum = checksum;
113 }
114
115 public String getChecksum() {
116 return checksum;
117 }
118
119
120
121
122
123
124 public void setType(String type) {
125 this.type = type;
126 }
127
128 public String getType() {
129 return type;
130 }
131
132
133
134
135
136
137 public void setNonexistentFileMessage(String nonexistentFileMessage) {
138 this.nonexistentFileMessage = nonexistentFileMessage;
139 }
140
141 public String getNonexistentFileMessage() {
142 return nonexistentFileMessage;
143 }
144
145 protected String calculateChecksum() throws EnforcerRuleException {
146 try (InputStream inputStream = Files.newInputStream(this.file.toPath())) {
147 return calculateChecksum(inputStream);
148 } catch (IOException e) {
149 throw new EnforcerRuleError("Unable to calculate checksum", e);
150 }
151 }
152
153 protected String calculateChecksum(InputStream inputStream) throws IOException, EnforcerRuleException {
154 String result;
155 if ("md5".equals(this.type)) {
156 result = DigestUtils.md5Hex(inputStream);
157 } else if ("sha1".equals(this.type)) {
158 result = DigestUtils.sha1Hex(inputStream);
159 } else if ("sha256".equals(this.type)) {
160 result = DigestUtils.sha256Hex(inputStream);
161 } else if ("sha384".equals(this.type)) {
162 result = DigestUtils.sha384Hex(inputStream);
163 } else if ("sha512".equals(this.type)) {
164 result = DigestUtils.sha512Hex(inputStream);
165 } else {
166 throw new EnforcerRuleError("Unsupported hash type: " + this.type);
167 }
168 return result;
169 }
170
171 @Override
172 public String toString() {
173 return String.format(
174 "RequireFileChecksum[message=%s, file=%s, checksum=%s, type=%s, nonexistentFileMessage=%s]",
175 getMessage(), file, checksum, type, nonexistentFileMessage);
176 }
177 }