Skip to content

Commit

Permalink
Refactor schema location
Browse files Browse the repository at this point in the history
  • Loading branch information
dima767 committed Oct 16, 2018
1 parent 074e2ba commit 7e3f1ec
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package edu.internet2.tier.shibboleth.admin.ui.controller

import com.fasterxml.jackson.databind.ObjectMapper
import edu.internet2.tier.shibboleth.admin.ui.configuration.CustomAttributesConfiguration

import edu.internet2.tier.shibboleth.admin.ui.jsonschema.MetadataSourcesJsonSchemaResourceLocation
import org.springframework.beans.factory.BeanInitializationException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.ConfigurationProperties
Expand All @@ -23,17 +23,10 @@ import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
* @author Bill Smith (wsmith@unicon.net)
*/
@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
Expand All @@ -44,7 +37,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 = []
customAttributesConfiguration.getAttributes().each {
Expand All @@ -59,24 +52,7 @@ class MetadataSourcesUiDefinitionController {
catch (Exception e) {
return ResponseEntity.status(INTERNAL_SERVER_ERROR)
.body([jsonParseError : e.getMessage(),
sourceUiSchemaDefinitionFile: this.jsonSchemaUrl])
}
}

@PostConstruct
def init() {
jsonSchemaUrl = this.resourceLoader.getResource(this.metadataSourcesUiSchemaLocation).getURL()
//Detect malformed JSON schema early, during application start up and fail fast with useful exception message
try {
this.jacksonObjectMapper.readValue(this.jsonSchemaUrl, Map)
}
catch (Exception e) {
def msg = """
An error is detected during JSON parsing => [${e.message}]
**********************************************************
Offending resource => [${this.jsonSchemaUrl}]
"""
throw new BeanInitializationException(msg.toString(), e)
sourceUiSchemaDefinitionFile: this.jsonSchemaLocation.url])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.internet2.tier.shibboleth.admin.ui.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
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, ObjectMapper jacksonMapper) {
return new MetadataSourcesJsonSchemaResourceLocation(resourceLoader, jacksonMapper);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package edu.internet2.tier.shibboleth.admin.ui.jsonschema;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanInitializationException;
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;
import java.util.Map;

/**
* 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;

private ResourceLoader resourceLoader;

private ObjectMapper jacksonMapper;



public MetadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader, ObjectMapper jacksonMapper) {
this.resourceLoader = resourceLoader;
this.jacksonMapper = jacksonMapper;
}

public void setMetadataSourcesUiSchemaLocation(String metadataSourcesUiSchemaLocation) {
this.metadataSourcesUiSchemaLocation = metadataSourcesUiSchemaLocation;
}

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();
//Detect malformed JSON schema early, during application start up and fail fast with useful exception message
this.jacksonMapper.readValue(this.jsonSchemaUrl, Map.class);
}
catch (Exception ex) {
StringBuilder msg =
new StringBuilder(String.format("An error is detected during JSON parsing => [%s]", ex.getMessage()));
msg.append(String.format("Offending resource => [%s]", this.metadataSourcesUiSchemaLocation));

throw new BeanInitializationException(msg.toString(), ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.internet2.tier.shibboleth.admin.ui.controller

import edu.internet2.tier.shibboleth.admin.ui.jsonschema.MetadataSourcesJsonSchemaResourceLocation
import org.springframework.beans.factory.BeanInitializationException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
Expand All @@ -20,6 +21,9 @@ class MetadataSourcesUiDefinitionControllerIntegrationTests extends Specificatio
@Autowired
MetadataSourcesUiDefinitionController controllerUnderTest

@Autowired
MetadataSourcesJsonSchemaResourceLocation schemaLocation

static RESOURCE_URI = '/api/ui/MetadataSources'

def "GET Metadata Sources UI definition schema"() {
Expand Down Expand Up @@ -54,9 +58,9 @@ class MetadataSourcesUiDefinitionControllerIntegrationTests extends Specificatio
}

private configureMalformedJsonInput(boolean simulateApplicationStartup) {
controllerUnderTest.metadataSourcesUiSchemaLocation = 'classpath:metadata-sources-ui-schema_MALFORMED.json'
schemaLocation.metadataSourcesUiSchemaLocation = 'classpath:metadata-sources-ui-schema_MALFORMED.json'
try {
controllerUnderTest.init()
schemaLocation.init()
}
catch (Exception e) {
if (simulateApplicationStartup) {
Expand Down

0 comments on commit 7e3f1ec

Please sign in to comment.