1 package io.apicurio.registry.maven;
2
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.google.protobuf.Descriptors.FileDescriptor;
6 import io.apicurio.registry.content.ContentHandle;
7 import io.apicurio.registry.content.TypedContent;
8 import io.apicurio.registry.content.refs.ExternalReference;
9 import io.apicurio.registry.content.refs.ReferenceFinder;
10 import io.apicurio.registry.maven.refs.IndexedResource;
11 import io.apicurio.registry.maven.refs.ReferenceIndex;
12 import io.apicurio.registry.rest.client.RegistryClient;
13 import io.apicurio.registry.rest.client.models.ArtifactReference;
14 import io.apicurio.registry.rest.client.models.CreateArtifact;
15 import io.apicurio.registry.rest.client.models.CreateVersion;
16 import io.apicurio.registry.rest.client.models.IfArtifactExists;
17 import io.apicurio.registry.rest.client.models.ProblemDetails;
18 import io.apicurio.registry.rest.client.models.RuleViolationProblemDetails;
19 import io.apicurio.registry.rest.client.models.VersionContent;
20 import io.apicurio.registry.rest.client.models.VersionMetaData;
21 import io.apicurio.registry.types.ArtifactType;
22 import io.apicurio.registry.types.ContentTypes;
23 import io.apicurio.registry.types.provider.ArtifactTypeUtilProvider;
24 import io.apicurio.registry.types.provider.DefaultArtifactTypeUtilProviderImpl;
25 import io.vertx.core.Vertx;
26 import org.apache.avro.Schema;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.charset.StandardCharsets;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.List;
45 import java.util.Set;
46 import java.util.Stack;
47 import java.util.concurrent.ExecutionException;
48 import java.util.stream.Collectors;
49
50
51
52
53 @Mojo(name = "register")
54 public class RegisterRegistryMojo extends AbstractRegistryMojo {
55
56
57
58
59 @Parameter(required = false)
60 List<ExistingReference> existingReferences;
61
62
63
64
65 @Parameter(required = true)
66 List<RegisterArtifact> artifacts;
67
68
69
70
71 @Parameter(property = "skipRegister", defaultValue = "false")
72 boolean skip;
73
74
75
76
77
78 @Parameter(property = "dryRun", defaultValue = "false")
79 boolean dryRun;
80
81 DefaultArtifactTypeUtilProviderImpl utilProviderFactory = new DefaultArtifactTypeUtilProviderImpl();
82
83
84
85
86 protected boolean validate() throws MojoExecutionException {
87 if (skip) {
88 getLog().info("register is skipped.");
89 return false;
90 }
91
92 if (artifacts == null || artifacts.isEmpty()) {
93 getLog().warn("No artifacts are configured for registration.");
94 return false;
95 }
96
97 if (existingReferences == null) {
98 existingReferences = new ArrayList<>();
99 }
100
101 int idx = 0;
102 int errorCount = 0;
103 for (RegisterArtifact artifact : artifacts) {
104 if (artifact.getGroupId() == null) {
105 getLog().error(String.format(
106 "GroupId is required when registering an artifact. Missing from artifacts[%d].",
107 idx));
108 errorCount++;
109 }
110 if (artifact.getArtifactId() == null) {
111 getLog().error(String.format(
112 "ArtifactId is required when registering an artifact. Missing from artifacts[%s].",
113 idx));
114 errorCount++;
115 }
116 if (artifact.getFile() == null) {
117 getLog().error(String.format(
118 "File is required when registering an artifact. Missing from artifacts[%s].", idx));
119 errorCount++;
120 } else if (!artifact.getFile().exists()) {
121 getLog().error(
122 String.format("Artifact file to register is configured but file does not exist: %s",
123 artifact.getFile().getPath()));
124 errorCount++;
125 }
126
127 idx++;
128 }
129
130 if (errorCount > 0) {
131 throw new MojoExecutionException(
132 "Invalid configuration of the Register Artifact(s) mojo. See the output log for details.");
133 }
134 return true;
135 }
136
137 @Override
138 protected void executeInternal() throws MojoExecutionException {
139 int errorCount = 0;
140 if (validate()) {
141 Vertx vertx = createVertx();
142 RegistryClient registryClient = createClient(vertx);
143
144 for (RegisterArtifact artifact : artifacts) {
145 String groupId = artifact.getGroupId();
146 String artifactId = artifact.getArtifactId();
147 try {
148 if (artifact.getAutoRefs() != null && artifact.getAutoRefs()) {
149
150
151 ReferenceIndex index = createIndex(artifact.getFile());
152 addExistingReferencesToIndex(registryClient, index, existingReferences);
153 addExistingReferencesToIndex(registryClient, index, artifact.getExistingReferences());
154 Stack<RegisterArtifact> registrationStack = new Stack<>();
155
156 registerWithAutoRefs(registryClient, artifact, index, registrationStack);
157 } else if (artifact.getAnalyzeDirectory() != null && artifact.getAnalyzeDirectory()) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 registerDirectory(registryClient, artifact);
174 } else {
175
176 List<ArtifactReference> references = new ArrayList<>();
177
178 if (hasReferences(artifact)) {
179 references = processArtifactReferences(registryClient, artifact.getReferences());
180 }
181 registerArtifact(registryClient, artifact, references);
182 }
183 } catch (Exception e) {
184 errorCount++;
185 getLog().error(String.format("Exception while registering artifact [%s] / [%s]", groupId,
186 artifactId), e);
187 }
188
189 }
190
191 if (errorCount > 0) {
192 throw new MojoExecutionException("Errors while registering artifacts ...");
193 }
194 }
195 }
196
197 private VersionMetaData registerWithAutoRefs(RegistryClient registryClient, RegisterArtifact artifact,
198 ReferenceIndex index, Stack<RegisterArtifact> registrationStack) throws IOException,
199 ExecutionException, InterruptedException, MojoExecutionException, MojoFailureException {
200 if (loopDetected(artifact, registrationStack)) {
201 throw new RuntimeException(
202 "Artifact reference loop detected (not supported): " + printLoop(registrationStack));
203 }
204 registrationStack.push(artifact);
205
206
207 ContentHandle artifactContent = readContent(artifact.getFile());
208 String artifactContentType = getContentTypeByExtension(artifact.getFile().getName());
209 TypedContent typedArtifactContent = TypedContent.create(artifactContent, artifactContentType);
210
211
212 ArtifactTypeUtilProvider provider = this.utilProviderFactory
213 .getArtifactTypeProvider(artifact.getArtifactType());
214 ReferenceFinder referenceFinder = provider.getReferenceFinder();
215 Set<ExternalReference> externalReferences = referenceFinder
216 .findExternalReferences(typedArtifactContent);
217
218
219 List<ArtifactReference> registeredReferences = new ArrayList<>(externalReferences.size());
220 for (ExternalReference externalRef : externalReferences) {
221 IndexedResource iresource = index.lookup(externalRef.getResource(),
222 Paths.get(artifact.getFile().toURI()));
223
224
225 if (iresource == null) {
226 throw new RuntimeException("Reference could not be resolved. From: "
227 + artifact.getFile().getName() + " To: " + externalRef.getFullReference());
228 }
229
230
231 if (!iresource.isRegistered()) {
232
233 String artifactId = externalRef.getResource();
234 File localFile = getLocalFile(iresource.getPath());
235 RegisterArtifact refArtifact = buildFromRoot(artifact, artifactId);
236 refArtifact.setArtifactType(iresource.getType());
237 refArtifact.setVersion(null);
238 refArtifact.setFile(localFile);
239 refArtifact.setContentType(getContentTypeByExtension(localFile.getName()));
240 try {
241 var car = registerWithAutoRefs(registryClient, refArtifact, index, registrationStack);
242 iresource.setRegistration(car);
243 } catch (IOException | ExecutionException | InterruptedException e) {
244 throw new RuntimeException(e);
245 }
246 }
247
248 var reference = new ArtifactReference();
249 reference.setName(externalRef.getFullReference());
250 reference.setVersion(iresource.getRegistration().getVersion());
251 reference.setGroupId(iresource.getRegistration().getGroupId());
252 reference.setArtifactId(iresource.getRegistration().getArtifactId());
253 registeredReferences.add(reference);
254 }
255 registeredReferences.sort((ref1, ref2) -> ref1.getName().compareTo(ref2.getName()));
256
257 registrationStack.pop();
258 return registerArtifact(registryClient, artifact, registeredReferences);
259 }
260
261 private void registerDirectory(RegistryClient registryClient, RegisterArtifact artifact)
262 throws IOException, ExecutionException, InterruptedException, MojoExecutionException,
263 MojoFailureException {
264 switch (artifact.getArtifactType()) {
265 case ArtifactType.AVRO:
266 final AvroDirectoryParser avroDirectoryParser = new AvroDirectoryParser(registryClient);
267 final ParsedDirectoryWrapper<Schema> schema = avroDirectoryParser.parse(artifact.getFile());
268 registerArtifact(registryClient, artifact, avroDirectoryParser
269 .handleSchemaReferences(artifact, schema.getSchema(), schema.getSchemaContents()));
270 break;
271 case ArtifactType.PROTOBUF:
272 final ProtobufDirectoryParser protobufDirectoryParser = new ProtobufDirectoryParser(
273 registryClient);
274 final ParsedDirectoryWrapper<FileDescriptor> protoSchema = protobufDirectoryParser
275 .parse(artifact.getFile());
276 registerArtifact(registryClient, artifact, protobufDirectoryParser.handleSchemaReferences(
277 artifact, protoSchema.getSchema(), protoSchema.getSchemaContents()));
278 break;
279 case ArtifactType.JSON:
280 final JsonSchemaDirectoryParser jsonSchemaDirectoryParser = new JsonSchemaDirectoryParser(
281 registryClient);
282 final ParsedDirectoryWrapper<org.everit.json.schema.Schema> jsonSchema = jsonSchemaDirectoryParser
283 .parse(artifact.getFile());
284 registerArtifact(registryClient, artifact, jsonSchemaDirectoryParser.handleSchemaReferences(
285 artifact, jsonSchema.getSchema(), jsonSchema.getSchemaContents()));
286 break;
287 default:
288 throw new IllegalArgumentException(
289 String.format("Artifact type not recognized for analyzing a directory structure %s",
290 artifact.getArtifactType()));
291 }
292 }
293
294 private VersionMetaData registerArtifact(RegistryClient registryClient, RegisterArtifact artifact,
295 List<ArtifactReference> references) throws FileNotFoundException, ExecutionException,
296 InterruptedException, MojoExecutionException, MojoFailureException {
297 if (artifact.getFile() != null) {
298 return registerArtifact(registryClient, artifact, new FileInputStream(artifact.getFile()),
299 references);
300 } else {
301 return getArtifactVersionMetadata(registryClient, artifact);
302 }
303 }
304
305 private VersionMetaData getArtifactVersionMetadata(RegistryClient registryClient,
306 RegisterArtifact artifact) {
307 String groupId = artifact.getGroupId();
308 String artifactId = artifact.getArtifactId();
309 String version = artifact.getVersion();
310
311 VersionMetaData amd = registryClient.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId)
312 .versions().byVersionExpression(version).get();
313 getLog().info(String.format("Successfully processed artifact [%s] / [%s]. GlobalId is [%d]", groupId,
314 artifactId, amd.getGlobalId()));
315
316 return amd;
317 }
318
319 private VersionMetaData registerArtifact(RegistryClient registryClient, RegisterArtifact artifact,
320 InputStream artifactContent, List<ArtifactReference> references)
321 throws ExecutionException, InterruptedException, MojoFailureException, MojoExecutionException {
322 String groupId = artifact.getGroupId();
323 String artifactId = artifact.getArtifactId();
324 String version = artifact.getVersion();
325 String type = artifact.getArtifactType();
326 Boolean canonicalize = artifact.getCanonicalize();
327 String ct = artifact.getContentType() == null ? ContentTypes.APPLICATION_JSON
328 : artifact.getContentType();
329 String data = null;
330 try {
331 if (artifact.getMinify() != null && artifact.getMinify()) {
332 ObjectMapper objectMapper = new ObjectMapper();
333 JsonNode jsonNode = objectMapper.readValue(artifactContent, JsonNode.class);
334 data = jsonNode.toString();
335 } else {
336 data = new String(artifactContent.readAllBytes(), StandardCharsets.UTF_8);
337 }
338 } catch (IOException e) {
339 throw new RuntimeException(e);
340 }
341
342 CreateArtifact createArtifact = new CreateArtifact();
343 createArtifact.setArtifactId(artifactId);
344 createArtifact.setArtifactType(type);
345
346 CreateVersion createVersion = new CreateVersion();
347 createVersion.setVersion(version);
348 createArtifact.setFirstVersion(createVersion);
349
350 VersionContent content = new VersionContent();
351 content.setContent(data);
352 content.setContentType(ct);
353 content.setReferences(references.stream().map(r -> {
354 ArtifactReference ref = new ArtifactReference();
355 ref.setArtifactId(r.getArtifactId());
356 ref.setGroupId(r.getGroupId());
357 ref.setVersion(r.getVersion());
358 ref.setName(r.getName());
359 return ref;
360 }).collect(Collectors.toList()));
361 createVersion.setContent(content);
362
363 try {
364 var vmd = registryClient.groups().byGroupId(groupId).artifacts().post(createArtifact, config -> {
365 if (artifact.getIfExists() != null) {
366 config.queryParameters.ifExists = IfArtifactExists
367 .forValue(artifact.getIfExists().value());
368 if (dryRun) {
369 config.queryParameters.dryRun = true;
370 }
371 }
372 config.queryParameters.canonical = canonicalize;
373 });
374
375 getLog().info(String.format("Successfully registered artifact [%s] / [%s]. GlobalId is [%d]",
376 groupId, artifactId, vmd.getVersion().getGlobalId()));
377
378 return vmd.getVersion();
379 } catch (RuleViolationProblemDetails | ProblemDetails e) {
380 logAndThrow(e);
381 return null;
382 }
383 }
384
385 private static boolean hasReferences(RegisterArtifact artifact) {
386 return artifact.getReferences() != null && !artifact.getReferences().isEmpty();
387 }
388
389 private List<ArtifactReference> processArtifactReferences(RegistryClient registryClient,
390 List<RegisterArtifactReference> referencedArtifacts) throws FileNotFoundException,
391 ExecutionException, InterruptedException, MojoExecutionException, MojoFailureException {
392 List<ArtifactReference> references = new ArrayList<>();
393 for (RegisterArtifactReference artifact : referencedArtifacts) {
394 List<ArtifactReference> nestedReferences = new ArrayList<>();
395
396
397 if (hasReferences(artifact)) {
398 nestedReferences = processArtifactReferences(registryClient, artifact.getReferences());
399 }
400 final VersionMetaData artifactMetaData = registerArtifact(registryClient, artifact,
401 nestedReferences);
402 references.add(buildReferenceFromMetadata(artifactMetaData, artifact.getName()));
403 }
404 return references;
405 }
406
407 public void setArtifacts(List<RegisterArtifact> artifacts) {
408 this.artifacts = artifacts;
409 }
410
411 public void setSkip(boolean skip) {
412 this.skip = skip;
413 }
414
415 private static ArtifactReference buildReferenceFromMetadata(VersionMetaData metaData,
416 String referenceName) {
417 ArtifactReference reference = new ArtifactReference();
418 reference.setName(referenceName);
419 reference.setArtifactId(metaData.getArtifactId());
420 reference.setGroupId(metaData.getGroupId());
421 reference.setVersion(metaData.getVersion());
422 return reference;
423 }
424
425
426
427
428
429
430 private static ReferenceIndex createIndex(File file) {
431 ReferenceIndex index = new ReferenceIndex(file.getParentFile().toPath());
432 Collection<File> allFiles = FileUtils.listFiles(file.getParentFile(), null, true);
433 allFiles.stream().filter(f -> f.isFile()).forEach(f -> {
434 index.index(f.toPath(), readContent(f));
435 });
436 return index;
437 }
438
439 private void addExistingReferencesToIndex(RegistryClient registryClient, ReferenceIndex index,
440 List<ExistingReference> existingReferences) throws ExecutionException, InterruptedException {
441 if (existingReferences != null && !existingReferences.isEmpty()) {
442 for (ExistingReference ref : existingReferences) {
443 VersionMetaData vmd;
444 if (ref.getVersion() == null || "LATEST".equalsIgnoreCase(ref.getVersion())) {
445 vmd = registryClient.groups().byGroupId(ref.getGroupId()).artifacts()
446 .byArtifactId(ref.getArtifactId()).versions().byVersionExpression("branch=latest")
447 .get();
448 } else {
449 vmd = new VersionMetaData();
450 vmd.setGroupId(ref.getGroupId());
451 vmd.setArtifactId(ref.getArtifactId());
452 vmd.setVersion(ref.getVersion());
453 }
454 index.index(ref.getResourceName(), vmd);
455 }
456 }
457 }
458
459 protected static ContentHandle readContent(File file) {
460 try {
461 return ContentHandle.create(Files.readAllBytes(file.toPath()));
462 } catch (IOException e) {
463 throw new RuntimeException("Failed to read schema file: " + file, e);
464 }
465 }
466
467 protected static RegisterArtifact buildFromRoot(RegisterArtifact rootArtifact, String artifactId) {
468 RegisterArtifact nestedSchema = new RegisterArtifact();
469 nestedSchema.setCanonicalize(rootArtifact.getCanonicalize());
470 nestedSchema.setArtifactId(artifactId);
471 nestedSchema.setGroupId(rootArtifact.getGroupId());
472 nestedSchema.setContentType(rootArtifact.getContentType());
473 nestedSchema.setArtifactType(rootArtifact.getArtifactType());
474 nestedSchema.setMinify(rootArtifact.getMinify());
475 nestedSchema.setContentType(rootArtifact.getContentType());
476 nestedSchema.setIfExists(rootArtifact.getIfExists());
477 nestedSchema.setAutoRefs(rootArtifact.getAutoRefs());
478 return nestedSchema;
479 }
480
481 private static File getLocalFile(Path path) {
482 return path.toFile();
483 }
484
485
486
487
488
489
490
491 private static boolean loopDetected(RegisterArtifact artifact,
492 Stack<RegisterArtifact> registrationStack) {
493 for (RegisterArtifact stackArtifact : registrationStack) {
494 if (artifact.getFile().equals(stackArtifact.getFile())) {
495 return true;
496 }
497 }
498 return false;
499 }
500
501 private static String printLoop(Stack<RegisterArtifact> registrationStack) {
502 return registrationStack.stream().map(artifact -> artifact.getFile().getName())
503 .collect(Collectors.joining(" -> "));
504 }
505
506 }