-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apicurio Registry integration with Avro+Protobuf ser/des support
Signed-off-by: Michael Edgar <[email protected]>
- Loading branch information
Showing
19 changed files
with
1,455 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
api/src/main/java/com/github/streamshub/console/api/SchemasResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.github.streamshub.console.api; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Base64; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.BadRequestException; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.Response.Status; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.eclipse.microprofile.openapi.annotations.media.Content; | ||
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; | ||
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; | ||
import org.eclipse.microprofile.openapi.annotations.tags.Tag; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.streamshub.console.api.support.serdes.MultiformatSchemaParser; | ||
|
||
import io.apicurio.registry.resolver.DefaultSchemaResolver; | ||
import io.apicurio.registry.resolver.SchemaResolver; | ||
import io.apicurio.registry.rest.client.RegistryClient; | ||
import io.apicurio.registry.rest.client.RegistryClientFactory; | ||
import io.apicurio.registry.serde.strategy.ArtifactReference; | ||
|
||
@Path("/api/schemas/{schemaId}") | ||
@Tag(name = "Schema Registry Resources") | ||
public class SchemasResource { | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
SchemaResolver<Object, ?> schemaResolver; | ||
|
||
@PostConstruct | ||
void initialize() { | ||
String registryUrl = ConfigProvider.getConfig().getOptionalValue("console.registry.endpoint", String.class) | ||
// TODO: remove default | ||
.orElse("http://localhost:9080"); | ||
|
||
RegistryClient registryClient = RegistryClientFactory.create(registryUrl); | ||
schemaResolver = new DefaultSchemaResolver<>(); | ||
schemaResolver.setClient(registryClient); | ||
schemaResolver.configure(Collections.emptyMap(), new MultiformatSchemaParser<>(Collections.emptySet())); | ||
} | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@APIResponse(responseCode = "200", ref = "Configurations", content = @Content()) | ||
@APIResponse(responseCode = "404", ref = "NotFound") | ||
@APIResponse(responseCode = "500", ref = "ServerError") | ||
@APIResponse(responseCode = "504", ref = "ServerTimeout") | ||
public Response describeConfigs( | ||
@Parameter(description = "Schema identifier") | ||
@PathParam("schemaId") | ||
String schemaId) { | ||
|
||
|
||
|
||
InputStream in = new ByteArrayInputStream(Base64.getUrlDecoder().decode(schemaId)); | ||
JsonNode id; | ||
|
||
try { | ||
id = objectMapper.readTree(in); | ||
} catch (IOException e) { | ||
throw new BadRequestException("Schema id could not be parsed"); | ||
} | ||
|
||
var builder = ArtifactReference.builder(); | ||
|
||
if (id.has("globalId")) { | ||
builder.globalId(id.get("globalId").asLong()); | ||
} | ||
if (id.has("contentId")) { | ||
builder.contentId(id.get("contentId").asLong()); | ||
} | ||
if (id.has("groupId")) { | ||
builder.groupId(id.get("groupId").asText()); | ||
} | ||
if (id.has("artifactId")) { | ||
builder.artifactId(id.get("artifactId").asText()); | ||
} | ||
if (id.has("version")) { | ||
builder.version(id.get("version").asText()); | ||
} | ||
|
||
var schema = schemaResolver.resolveSchemaByArtifactReference(builder.build()); | ||
|
||
var response = Optional.ofNullable(schema) | ||
.map(s -> s.getParsedSchema()) | ||
.map(s -> s.getRawSchema()) | ||
.map(Response::ok) | ||
.orElseGet(() -> Response.status(Status.NOT_FOUND)); | ||
|
||
return response.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.