Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace everit json schema validator with java-json-tools/json-schem-validator #169

Merged
merged 17 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion schema/common.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Common",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"entry": {
"title": "Entry",
Expand Down
2 changes: 1 addition & 1 deletion schema/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Config",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"required": [
"timestamp",
Expand Down
2 changes: 1 addition & 1 deletion schema/config_blobset.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Blobset Config",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"blobs": {
Expand Down
2 changes: 1 addition & 1 deletion schema/config_blobset_blob.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Blob Blobset Config",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"stage": {
Expand Down
2 changes: 1 addition & 1 deletion schema/config_gateway.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Gateway Config",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"required": [
"proxy_ids"
Expand Down
2 changes: 1 addition & 1 deletion schema/config_localnet.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Localnet Config",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"subsystem": {
Expand Down
2 changes: 1 addition & 1 deletion schema/config_pointset.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Pointset Config",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"state_etag": {
Expand Down
2 changes: 1 addition & 1 deletion schema/config_pointset_point.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Point Pointset Config",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"ref": {
Expand Down
2 changes: 1 addition & 1 deletion schema/config_system.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "System Config",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"min_loglevel": {
Expand Down
2 changes: 1 addition & 1 deletion schema/discover.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Discover",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"timestamp": {
Expand Down
2 changes: 1 addition & 1 deletion schema/event.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"title": "Event",
"description": "Container object for all event schemas, not directly used.",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"system": {
Expand Down
2 changes: 1 addition & 1 deletion schema/event_pointset.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Pointset Event",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"timestamp": {
Expand Down
2 changes: 1 addition & 1 deletion schema/event_pointset_point.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Point Pointset Event",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"present_value": {
Expand Down
2 changes: 1 addition & 1 deletion schema/event_system.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "System Event",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"timestamp": {
Expand Down
2 changes: 1 addition & 1 deletion schema/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Metadata",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"required": [
"timestamp",
Expand Down
2 changes: 1 addition & 1 deletion schema/metadata_cloud.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Cloud Metadata",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"auth_type": {
Expand Down
2 changes: 1 addition & 1 deletion schema/metadata_gateway.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Gateway Metadata",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"gateway_id": {
Expand Down
2 changes: 1 addition & 1 deletion schema/metadata_localnet.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Localnet Metadata",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"subsystem": {
Expand Down
2 changes: 1 addition & 1 deletion schema/metadata_pointset.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Pointset Metadata",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"points": {
Expand Down
2 changes: 1 addition & 1 deletion schema/metadata_pointset_point.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Point Pointset Metadata",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"units": {
Expand Down
2 changes: 1 addition & 1 deletion schema/metadata_system.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "System Metadata",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"location": {
Expand Down
2 changes: 1 addition & 1 deletion schema/properties.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Properties",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"required": [
"key_type",
Expand Down
2 changes: 1 addition & 1 deletion schema/state.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "State",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"required": [
"timestamp",
Expand Down
2 changes: 1 addition & 1 deletion schema/state_blobset.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Blobset State",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"state_etag": {
Expand Down
2 changes: 1 addition & 1 deletion schema/state_blobset_blob.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Blob Blobset State",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"stage": {
Expand Down
2 changes: 1 addition & 1 deletion schema/state_gateway.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Gateway State",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"required": [
"error_ids"
Expand Down
2 changes: 1 addition & 1 deletion schema/state_pointset.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Pointset State",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"state_etag": {
Expand Down
2 changes: 1 addition & 1 deletion schema/state_pointset_point.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Point Pointset State",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"value_state": {
Expand Down
2 changes: 1 addition & 1 deletion schema/state_system.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "System State",
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"make_model": {
Expand Down
2 changes: 1 addition & 1 deletion validator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
compile group: 'org.slf4j', name: 'slf4j-simple', version:'1.7.5'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.4'
compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.11.0'
compile group: 'com.github.everit-org.json-schema', name: 'org.everit.json.schema', version: '1.9.1'
compile group: 'com.github.java-json-tools', name: 'json-schema-validator', version: '2.2.14'
compile 'io.jsonwebtoken:jjwt:0.7.0'
compile 'javax.xml.bind:jaxb-api:2.3.1'
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package com.google.bos.iot.core.proxy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration;
import com.github.fge.jsonschema.core.load.download.URIDownloader;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaClient;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MessageValidator {

private static final Logger LOG = LoggerFactory.getLogger(MessageValidator.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

Map<String, Schema> schemaMap = Maps.newConcurrentMap();
Map<String, JsonSchema> schemaMap = Maps.newConcurrentMap();
private File schemaRoot;

public MessageValidator(String schemaRootPath) {
Expand All @@ -33,45 +37,50 @@ public MessageValidator(String schemaRootPath) {
}

public List<String> validateMessage(String subFolder, String data) {
Schema schema = schemaMap.computeIfAbsent(subFolder, this::getSchema);
JsonSchema schema = schemaMap.computeIfAbsent(subFolder, this::getSchema);
try {
schema.validate(new JSONObject(new JSONTokener(data)));
schema.validate(OBJECT_MAPPER.readTree(data));
return ImmutableList.of();
} catch (ValidationException e) {
return e.getAllMessages();
} catch (JsonProcessingException | ProcessingException e) {
return ImmutableList.of(e.getMessage());
}
}

private Schema getSchema(String subFolder) {
private JsonSchema getSchema(String subFolder) {
return getSchema(new File(schemaRoot, subFolder + ".json"));
}

private Schema getSchema(File schemaFile) {
private JsonSchema getSchema(File schemaFile) {
try (InputStream schemaStream = new FileInputStream(schemaFile)) {
JSONObject rawSchema = new JSONObject(new JSONTokener(schemaStream));
SchemaLoader loader = SchemaLoader.builder()
.schemaJson(rawSchema).httpClient(new RelativeClient()).build();
return loader.load().build();
JsonNode rawSchema = OBJECT_MAPPER.readTree(schemaStream);
return JsonSchemaFactory.newBuilder()
.setLoadingConfiguration(
LoadingConfiguration.newBuilder()
.addScheme("scheme", new RelativeDownloader())
.freeze())
.freeze()
.getJsonSchema(rawSchema);
} catch (Exception e) {
throw new RuntimeException("While loading schema " + schemaFile.getAbsolutePath(), e);
}
}

class RelativeClient implements SchemaClient {
class RelativeDownloader implements URIDownloader {
public static final String FILE_URL_PREFIX = "file:";

@Override
public InputStream get(String url) {
public InputStream fetch(URI source) {
String url = source.getPath();
try {
if (!url.startsWith(FILE_URL_PREFIX)) {
throw new IllegalStateException("Expected path to start with " + FILE_URL_PREFIX);
}
String new_url = FILE_URL_PREFIX + new File(schemaRoot, url.substring(FILE_URL_PREFIX.length()));
String new_url =
FILE_URL_PREFIX + new File(schemaRoot, url.substring(FILE_URL_PREFIX.length()));
return (InputStream) (new URL(new_url)).getContent();
} catch (Exception e) {
throw new RuntimeException("While loading URL " + url, e);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.google.api.services.cloudiot.v1.model.Device;
import com.google.bos.iot.core.proxy.MessageValidator.RelativeClient;
import com.google.cloud.ServiceOptions;
import com.google.daq.mqtt.util.CloudIotConfig;
import java.time.Duration;
Expand Down
Loading