Skip to content

Commit ff8da2a

Browse files
committed
[feature] get schema endpoint and list all schema
1 parent d862d6f commit ff8da2a

4 files changed

Lines changed: 203 additions & 0 deletions

File tree

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@
9090
<version>2.0</version>
9191
<scope>runtime</scope>
9292
</dependency>
93+
94+
<!-- Jackson Databind for JSON parsing -->
95+
<dependency>
96+
<groupId>com.fasterxml.jackson.core</groupId>
97+
<artifactId>jackson-databind</artifactId>
98+
<version>2.15.3</version>
99+
</dependency>
93100
</dependencies>
94101

95102
<build>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.evolvedbinary.bblValidator.controller;
2+
3+
import com.evolvedbinary.bblValidator.dto.SchemaInfo;
4+
import com.evolvedbinary.bblValidator.service.SchemaService;
5+
import io.micronaut.http.MediaType;
6+
import io.micronaut.http.annotation.Controller;
7+
import io.micronaut.http.annotation.Get;
8+
import io.micronaut.http.annotation.Produces;
9+
10+
import java.util.List;
11+
12+
@Controller("/schema")
13+
public class SchemaController {
14+
15+
private final SchemaService schemaService;
16+
17+
public SchemaController(SchemaService schemaService) {
18+
this.schemaService = schemaService;
19+
}
20+
21+
@Get
22+
@Produces(MediaType.APPLICATION_JSON)
23+
public List<SchemaInfo> listSchemas() {
24+
return schemaService.listSchemas();
25+
}
26+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.evolvedbinary.bblValidator.dto;
2+
3+
import io.micronaut.serde.annotation.Serdeable;
4+
5+
@Serdeable
6+
public class SchemaInfo {
7+
8+
private String id;
9+
private String name;
10+
private String version;
11+
private String date;
12+
private String url;
13+
private String description;
14+
15+
public SchemaInfo() {
16+
}
17+
18+
public SchemaInfo(String id, String name, String version, String date, String url, String description) {
19+
this.id = id;
20+
this.name = name;
21+
this.version = version;
22+
this.date = date;
23+
this.url = url;
24+
this.description = description;
25+
}
26+
27+
public String getId() {
28+
return id;
29+
}
30+
31+
public void setId(String id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public String getVersion() {
44+
return version;
45+
}
46+
47+
public void setVersion(String version) {
48+
this.version = version;
49+
}
50+
51+
public String getDate() {
52+
return date;
53+
}
54+
55+
public void setDate(String date) {
56+
this.date = date;
57+
}
58+
59+
public String getUrl() {
60+
return url;
61+
}
62+
63+
public void setUrl(String url) {
64+
this.url = url;
65+
}
66+
67+
public String getDescription() {
68+
return description;
69+
}
70+
71+
public void setDescription(String description) {
72+
this.description = description;
73+
}
74+
}
75+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.evolvedbinary.bblValidator.service;
2+
3+
import com.evolvedbinary.bblValidator.dto.SchemaInfo;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import jakarta.annotation.PostConstruct;
6+
import jakarta.inject.Singleton;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.nio.charset.StandardCharsets;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.stream.Stream;
19+
20+
@Singleton
21+
public class SchemaService {
22+
23+
private static final Logger LOG = LoggerFactory.getLogger(SchemaService.class);
24+
private static final String SCHEMA_DIRECTORY = "schemas";
25+
26+
private final List<SchemaInfo> schemas = new ArrayList<>();
27+
private final ObjectMapper objectMapper = new ObjectMapper();
28+
29+
@PostConstruct
30+
public void loadSchemas() {
31+
try {
32+
// Load schemas from classpath
33+
ClassLoader classLoader = getClass().getClassLoader();
34+
35+
// Get all .json files from the schemas directory
36+
try (InputStream is = classLoader.getResourceAsStream(SCHEMA_DIRECTORY)) {
37+
if (is == null) {
38+
LOG.warn("Schemas directory not found in classpath");
39+
return;
40+
}
41+
}
42+
43+
// Scan for schema metadata files
44+
loadSchemasFromClasspath();
45+
46+
LOG.info("Loaded {} schemas from disk", schemas.size());
47+
} catch (Exception e) {
48+
LOG.error("Error loading schemas from disk", e);
49+
}
50+
}
51+
52+
private void loadSchemasFromClasspath() {
53+
try {
54+
// Get resource URL and list files
55+
ClassLoader classLoader = getClass().getClassLoader();
56+
var resource = classLoader.getResource(SCHEMA_DIRECTORY);
57+
58+
if (resource != null) {
59+
Path schemaPath = Paths.get(resource.toURI());
60+
61+
try (Stream<Path> paths = Files.walk(schemaPath, 1)) {
62+
paths.filter(path -> path.toString().endsWith(".json"))
63+
.forEach(this::loadSchemaMetadata);
64+
}
65+
}
66+
} catch (Exception e) {
67+
LOG.error("Error scanning schema directory", e);
68+
}
69+
}
70+
71+
private void loadSchemaMetadata(Path metadataPath) {
72+
try {
73+
String content = Files.readString(metadataPath, StandardCharsets.UTF_8);
74+
SchemaInfo schemaInfo = objectMapper.readValue(content, SchemaInfo.class);
75+
76+
// Load corresponding schema file
77+
String schemaFileName = metadataPath.getFileName().toString().replace(".json", ".csvs");
78+
Path schemaFilePath = metadataPath.getParent().resolve(schemaFileName);
79+
80+
if (Files.exists(schemaFilePath)) {
81+
schemas.add(schemaInfo);
82+
LOG.debug("Loaded schema: {}", schemaInfo.getId());
83+
} else {
84+
LOG.warn("Schema file not found for metadata: {}", schemaFileName);
85+
}
86+
} catch (IOException e) {
87+
LOG.error("Error loading schema metadata from: {}", metadataPath, e);
88+
}
89+
}
90+
91+
public List<SchemaInfo> listSchemas() {
92+
return new ArrayList<>(schemas);
93+
}
94+
}
95+

0 commit comments

Comments
 (0)