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
21
22
23
24
25
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
37
38 public ContentHandle getContent() {
39 return content;
40 }
41
42
43
44
45 public String getType() {
46 return type;
47 }
48
49
50
51
52 public String getResourceName() {
53 return resourceName;
54 }
55
56
57
58
59 public Path getPath() {
60 return path;
61 }
62
63 public boolean matches(String resourceName, Path relativeToFile, Set<Path> schemaPaths) {
64
65 if (this.path == null) {
66 return this.resourceName.equals(resourceName);
67 }
68
69
70
71 if (ArtifactType.AVRO.equals(this.type)) {
72 if (this.resourceName.equals(resourceName)) {
73 return true;
74 }
75 }
76
77
78 Path resolvedPath = relativeToFile.getParent().resolve(resourceName);
79 boolean resolves = this.path.normalize().equals(resolvedPath.normalize());
80
81
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
91
92 public VersionMetaData getRegistration() {
93 return registration;
94 }
95
96
97
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 }