1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.swid.plugin;
28
29 import gov.nist.secauto.swid.builder.SWIDBuilder;
30 import gov.nist.secauto.swid.builder.ValidationException;
31 import gov.nist.secauto.swid.builder.output.CBOROutputHandler;
32 import gov.nist.secauto.swid.builder.output.OutputHandler;
33 import gov.nist.secauto.swid.builder.output.XMLOutputHandler;
34 import gov.nist.secauto.swid.plugin.entry.FileEntry;
35 import gov.nist.secauto.swid.plugin.entry.resource.ResourceFileEntryProcessor;
36 import gov.nist.secauto.swid.plugin.generate.MavenProjectSwidBuilderHelper;
37 import gov.nist.secauto.swid.plugin.model.Entity;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 import org.apache.maven.plugin.AbstractMojo;
56 import org.apache.maven.plugin.MojoExecutionException;
57 import org.apache.maven.plugins.annotations.LifecyclePhase;
58 import org.apache.maven.plugins.annotations.Mojo;
59 import org.apache.maven.plugins.annotations.Parameter;
60 import org.apache.maven.project.MavenProject;
61
62 import java.io.BufferedOutputStream;
63 import java.io.File;
64 import java.io.FileOutputStream;
65 import java.io.IOException;
66 import java.security.NoSuchAlgorithmException;
67 import java.util.Collections;
68 import java.util.List;
69
70
71
72
73 @Mojo(name = "generate", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
74 public class SwidGenerateMojo extends AbstractMojo {
75 private static final List<String> DEFAULT_INCLUDES = Collections.singletonList("**/**");
76
77
78
79
80 @Parameter(defaultValue = "${project}", readonly = true, required = true)
81 private MavenProject project;
82
83
84
85
86 @Parameter(defaultValue = "${project.build.directory}/classes/META-INF", property = "outputDir", required = true)
87 private String outputDirectory;
88
89
90
91
92 @Parameter(defaultValue = "XML", required = true)
93 private String tagFormat;
94
95
96
97
98 @Parameter(defaultValue = "SWIDTAG", required = true)
99 private String tagPath;
100
101
102
103
104 @Parameter(defaultValue = "swid-tag", required = true)
105 private String tagName;
106
107
108
109
110 @Parameter
111 private List<String> includes;
112
113
114
115
116 @Parameter
117 private List<String> excludes;
118
119
120
121
122 @Parameter
123 private List<Entity> entities;
124
125
126
127
128
129
130 public MavenProject getProject() {
131 return project;
132 }
133
134
135
136
137
138
139 public String[] getIncludes() {
140 List<String> retval;
141 if (includes != null && !includes.isEmpty()) {
142 retval = includes;
143 } else {
144 retval = DEFAULT_INCLUDES;
145 }
146 return retval.toArray(new String[retval.size()]);
147 }
148
149
150
151
152
153
154 public String[] getExcludes() {
155 List<String> retval;
156 if (excludes != null && !excludes.isEmpty()) {
157 retval = excludes;
158 } else {
159 retval = Collections.emptyList();
160 }
161 return retval.toArray(new String[retval.size()]);
162 }
163
164
165
166
167
168
169
170
171 public File getSwidTagFile() throws MojoExecutionException {
172
173
174 File retval = new File(outputDirectory);
175 if (!retval.exists() && !retval.mkdirs()) {
176 throw new MojoExecutionException(
177 "Unable to create the directory specified by outputDirectory configuration parameter: " + retval.getPath());
178 }
179
180
181 String tagPath = this.tagPath;
182 retval = new File(retval, tagPath);
183 if (!retval.exists() && !retval.mkdirs()) {
184 throw new MojoExecutionException(
185 "Unable to create the directory specified by tagPath configuration parameter: " + retval.getPath());
186 }
187
188
189 String tagName = this.tagName;
190 if (tagName == null) {
191 tagName = "swid-tag";
192 }
193
194 String tagFormat = getTagFormat();
195 switch (tagFormat) {
196 case "XML":
197 tagName = tagName + ".xml";
198 break;
199 case "CBOR":
200 tagName = tagName + ".cbor";
201 break;
202 default:
203 throw new UnsupportedOperationException("Invalid tag format '" + tagFormat + "'. Must be one of: XML or CBOR.");
204 }
205
206 retval = new File(retval, tagName);
207 return retval;
208 }
209
210
211
212
213
214
215 public String getTagFormat() {
216 String retval = tagFormat;
217 if (retval == null) {
218
219 retval = "XML";
220 }
221 return retval;
222 }
223
224 protected OutputHandler getOutputHander() {
225 String tagFormat = getTagFormat();
226
227 OutputHandler retval = null;
228 switch (tagFormat) {
229 case "XML":
230 retval = new XMLOutputHandler();
231 break;
232 case "CBOR":
233 retval = new CBOROutputHandler();
234 break;
235 default:
236 throw new UnsupportedOperationException(
237 "Invalid output handeler type '" + tagFormat + "'. Must be one of: XML or CBOR.");
238 }
239 return retval;
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253 @Override
254 public void execute() throws MojoExecutionException {
255
256 String swidPath = "META-INF/" + tagPath + "/" + tagName;
257
258
259 ResourceFileEntryProcessor processor
260 = new ResourceFileEntryProcessor(project.getBuild().getOutputDirectory(), getLog());
261 processor.setIncludes(getIncludes());
262 processor.setExcludes(getExcludes());
263
264 List<FileEntry> swidFiles;
265 try {
266
267 swidFiles = processor.process(this.project.getBuild().getResources());
268 } catch (IOException e) {
269 throw new MojoExecutionException("An error occured while processing build files", e);
270 }
271 for (FileEntry entry : swidFiles) {
272 getLog().debug("Found file: " + entry.getPath() + " as: " + entry.getOutputRelativePath());
273 }
274
275
276 SWIDBuilder builder;
277 try {
278 builder = MavenProjectSwidBuilderHelper.buildSwidTag(project, swidPath, this.entities, swidFiles);
279 } catch (NoSuchAlgorithmException e) {
280 throw new MojoExecutionException("A requested hash algorithm is not supported.", e);
281 } catch (IOException e) {
282 throw new MojoExecutionException("Unable to read file while building a SWID tag.", e);
283 }
284
285
286
287 File tagFile = getSwidTagFile();
288 OutputHandler outputHandler = getOutputHander();
289 try {
290 outputHandler.write(builder, new BufferedOutputStream(new FileOutputStream(tagFile)));
291 } catch (IOException e) {
292 throw new MojoExecutionException("Unable to write tag", e);
293 } catch (ValidationException e) {
294 throw new MojoExecutionException("The generated tag was found to be invalid", e);
295 }
296 }
297
298 }