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