From a257f896394cb307d81707601dbaa53a1dee1657 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Mon, 15 Oct 2018 11:40:48 -0400 Subject: [PATCH] JSON schema validation wip --- backend/build.gradle | 3 + ...tadataSourcesUiDefinitionController.groovy | 21 +------ ...hemaValidationComponentsConfiguration.java | 18 ++++++ .../JsonSchemaValidationFailedException.java | 20 +++++++ ...dataSourcesJsonSchemaResourceLocation.java | 58 +++++++++++++++++++ 5 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/configuration/JsonSchemaValidationComponentsConfiguration.java create mode 100644 backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/jsonschema/JsonSchemaValidationFailedException.java create mode 100644 backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/jsonschema/MetadataSourcesJsonSchemaResourceLocation.java diff --git a/backend/build.gradle b/backend/build.gradle index 1ecb7fea8..f03f9f9ed 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -136,6 +136,9 @@ dependencies { //JSON schema generator testCompile 'com.kjetland:mbknor-jackson-jsonschema_2.12:1.0.29' testCompile 'javax.validation:validation-api:2.0.1.Final' + + //JSON schema validator + compile 'org.sharegov:mjson:1.4.1' } def generatedSrcDir = new File(buildDir, 'generated/src/main/java') diff --git a/backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionController.groovy b/backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionController.groovy index 3600667ba..9cf305d6a 100644 --- a/backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionController.groovy +++ b/backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/controller/MetadataSourcesUiDefinitionController.groovy @@ -2,15 +2,12 @@ package edu.internet2.tier.shibboleth.admin.ui.controller import com.fasterxml.jackson.databind.ObjectMapper import edu.internet2.tier.shibboleth.admin.ui.configuration.CustomPropertiesConfiguration +import edu.internet2.tier.shibboleth.admin.ui.jsonschema.MetadataSourcesJsonSchemaResourceLocation import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.context.properties.ConfigurationProperties -import org.springframework.core.io.ResourceLoader import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController -import javax.annotation.PostConstruct - import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR /** @@ -20,17 +17,10 @@ import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR * @author Dmitriy Kopylenko */ @RestController('/api/ui/MetadataSources') -@ConfigurationProperties('shibui') class MetadataSourcesUiDefinitionController { - //Configured via @ConfigurationProperties with 'shibui.metadata-sources-ui-schema-location' property and default - //value set here if that property is not explicitly set in application.properties - String metadataSourcesUiSchemaLocation = 'classpath:metadata-sources-ui-schema.json' - - URL jsonSchemaUrl - @Autowired - ResourceLoader resourceLoader + MetadataSourcesJsonSchemaResourceLocation jsonSchemaLocation @Autowired ObjectMapper jacksonObjectMapper @@ -41,7 +31,7 @@ class MetadataSourcesUiDefinitionController { @GetMapping ResponseEntity getUiDefinitionJsonSchema() { try { - def parsedJson = jacksonObjectMapper.readValue(this.jsonSchemaUrl, Map) + def parsedJson = jacksonObjectMapper.readValue(this.jsonSchemaLocation.url, Map) def widget = parsedJson["properties"]["attributeRelease"]["widget"] def data = [] customPropertiesConfiguration.getAttributes().each { @@ -59,9 +49,4 @@ class MetadataSourcesUiDefinitionController { sourceUiSchemaDefinitionFile: this.jsonSchemaUrl]) } } - - @PostConstruct - def init() { - jsonSchemaUrl = this.resourceLoader.getResource(this.metadataSourcesUiSchemaLocation).getURL() - } } diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/configuration/JsonSchemaValidationComponentsConfiguration.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/configuration/JsonSchemaValidationComponentsConfiguration.java new file mode 100644 index 000000000..11eab2f06 --- /dev/null +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/configuration/JsonSchemaValidationComponentsConfiguration.java @@ -0,0 +1,18 @@ +package edu.internet2.tier.shibboleth.admin.ui.configuration; + +import edu.internet2.tier.shibboleth.admin.ui.jsonschema.MetadataSourcesJsonSchemaResourceLocation; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ResourceLoader; + +/** + * @author Dmitriy Kopylenko + */ +@Configuration +public class JsonSchemaValidationComponentsConfiguration { + + @Bean + public MetadataSourcesJsonSchemaResourceLocation metadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader) { + return new MetadataSourcesJsonSchemaResourceLocation(resourceLoader); + } +} diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/jsonschema/JsonSchemaValidationFailedException.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/jsonschema/JsonSchemaValidationFailedException.java new file mode 100644 index 000000000..3f8463d66 --- /dev/null +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/jsonschema/JsonSchemaValidationFailedException.java @@ -0,0 +1,20 @@ +package edu.internet2.tier.shibboleth.admin.ui.jsonschema; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +/** + * Indicates JSON schema validation failure. Encapsulates a list of error messages produced by JSON schema validator + * component. + * + * @author Dmitriy Kopylenko + */ +@RequiredArgsConstructor +@Getter +public class JsonSchemaValidationFailedException extends RuntimeException { + + private final List errors; + +} diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/jsonschema/MetadataSourcesJsonSchemaResourceLocation.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/jsonschema/MetadataSourcesJsonSchemaResourceLocation.java new file mode 100644 index 000000000..5e3fab651 --- /dev/null +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/jsonschema/MetadataSourcesJsonSchemaResourceLocation.java @@ -0,0 +1,58 @@ +package edu.internet2.tier.shibboleth.admin.ui.jsonschema; + +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.core.io.ResourceLoader; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +/** + * Encapsulates metadata sources JSON schema location. + * + * @author Dmitriy Kopylenko + */ +@ConfigurationProperties("shibui") +public class MetadataSourcesJsonSchemaResourceLocation { + + //Configured via @ConfigurationProperties with 'shibui.metadata-sources-ui-schema-location' property and default + //value set here if that property is not explicitly set in application.properties + private String metadataSourcesUiSchemaLocation = "classpath:metadata-sources-ui-schema.json"; + + private URL jsonSchemaUrl; + + ResourceLoader resourceLoader; + + public MetadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + + public URL getUrl() { + return this.jsonSchemaUrl; + } + + public URI getUri() { + try { + return this.jsonSchemaUrl.toURI(); + } + catch (URISyntaxException ex) { + throw new RuntimeException(ex); + } + } + + @PostConstruct + public void init() { + try { + this.jsonSchemaUrl = this.resourceLoader.getResource(this.metadataSourcesUiSchemaLocation).getURL(); + } + catch (IOException ex) { + throw new BeanCreationException(ex.getMessage(), ex); + } + } +}