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
23
24
25
26
27
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
39
40 public ContentHandle getContent() {
41 return content;
42 }
43
44
45
46
47 public String getType() {
48 return type;
49 }
50
51
52
53
54 public String getResourceName() {
55 return resourceName;
56 }
57
58
59
60
61 public Path getPath() {
62 return path;
63 }
64
65 public boolean matches(String resourceName, Path relativeToFile, Set<Path> schemaPaths) {
66
67 if (this.path == null) {
68 return this.resourceName.equals(resourceName);
69 }
70
71
72
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
84 Path resolvedPath = relativeToFile.getParent().resolve(resourceName);
85 boolean resolves = this.path.normalize().equals(resolvedPath.normalize());
86
87
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
97
98 public VersionMetaData getRegistration() {
99 return registration;
100 }
101
102
103
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 }