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