View Javadoc
1   package io.apicurio.registry.maven.refs;
2   
3   import io.apicurio.registry.content.ContentHandle;
4   import io.apicurio.registry.rest.client.models.VersionMetaData;
5   import io.apicurio.registry.types.ArtifactType;
6   
7   import java.nio.file.Path;
8   import java.util.Set;
9   
10  public class IndexedResource {
11  
12      private final Path path;
13      private final String type;
14      private final String resourceName;
15      private final ContentHandle content;
16      private VersionMetaData registration;
17  
18      /**
19       * Constructor.
20       * 
21       * @param path
22       * @param type
23       * @param resourceName
24       * @param content
25       */
26      public IndexedResource(Path path, String type, String resourceName, ContentHandle content) {
27          super();
28          this.path = path;
29          this.content = content;
30          this.type = type;
31          this.resourceName = resourceName;
32      }
33  
34      /**
35       * @return the content
36       */
37      public ContentHandle getContent() {
38          return content;
39      }
40  
41      /**
42       * @return the type
43       */
44      public String getType() {
45          return type;
46      }
47  
48      /**
49       * @return the resourceName
50       */
51      public String getResourceName() {
52          return resourceName;
53      }
54  
55      /**
56       * @return the path
57       */
58      public Path getPath() {
59          return path;
60      }
61  
62      public boolean matches(String resourceName, Path relativeToFile, Set<Path> schemaPaths) {
63          // If this is a pre-registered reference, the match has to happen by resource name.
64          if (this.path == null) {
65              return this.resourceName.equals(resourceName);
66          }
67  
68          // For Avro files the match can happen either via path (e.g. when referencing an Avro
69          // file from an AsyncAPI file) or via resource name (e.g. from Avro to Avro).
70          if (ArtifactType.AVRO.equals(this.type)) {
71              if (this.resourceName.equals(resourceName)) {
72                  return true;
73              }
74          }
75  
76          // The resource name will otherwise be a relative path to the resource.
77          Path resolvedPath = relativeToFile.getParent().resolve(resourceName);
78          boolean resolves = this.path.normalize().equals(resolvedPath.normalize());
79  
80          // Protobuf can resolve relative to the "schema paths" (aka --proto-paths in protoc).
81          if (!resolves && ArtifactType.PROTOBUF.equals(this.type)) {
82              resolves = schemaPaths.parallelStream()
83                      .anyMatch(path -> this.path.normalize().equals(path.resolve(resourceName).normalize()));
84          }
85          return resolves;
86      }
87  
88      /**
89       * @return the registration
90       */
91      public VersionMetaData getRegistration() {
92          return registration;
93      }
94  
95      /**
96       * @param registration the registration to set
97       */
98      public void setRegistration(VersionMetaData registration) {
99          this.registration = registration;
100     }
101 
102     public boolean isRegistered() {
103         return this.registration != null;
104     }
105 }