Skip to content

Commit

Permalink
[SHIBUI-999]
Browse files Browse the repository at this point in the history
Added dependency on commons-io.
Moved validation of json schema to before the body is read/used.
Fixed a bug in relying party overrides creation from representation that
would cause a boolean value to be persisted instead of the persistValue
defined in the yaml.
  • Loading branch information
Bill Smith committed Nov 14, 2018
1 parent 9b51a67 commit 0514933
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ dependencies {

//JSON schema validator
compile 'org.sharegov:mjson:1.4.1'

//Apache commons-io
compile group: 'commons-io', name: 'commons-io', version: '2.6'
}

def generatedSrcDir = new File(buildDir, 'generated/src/main/java')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package edu.internet2.tier.shibboleth.admin.ui.jsonschema

import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.EntityDescriptorRepresentation
import groovy.json.JsonBuilder
import mjson.Json
import org.apache.commons.io.IOUtils
import org.apache.commons.io.input.CloseShieldInputStream
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.MethodParameter
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpInputMessage
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter

import javax.annotation.PostConstruct
Expand Down Expand Up @@ -38,15 +43,29 @@ class RelyingPartyOverridesJsonSchemaValidatingControllerAdvice extends RequestB
}

@Override
Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
def relyingPartyOverrides = EntityDescriptorRepresentation.cast(body).relyingPartyOverrides
def relyingPartyOverridesJson = Json.make([relyingPartyOverrides: relyingPartyOverrides])
HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType)
throws IOException {
def baos = new ByteArrayOutputStream()
IOUtils.copy(inputMessage.body, baos)
def bytes = baos.toByteArray()
def schema = Json.schema(this.jsonSchemaLocation.uri)
def validationResult = schema.validate(relyingPartyOverridesJson)
def stream = new ByteArrayInputStream(bytes)
def validationResult = schema.validate(Json.read(stream.getText()))
if (!validationResult.at('ok')) {
throw new JsonSchemaValidationFailedException(validationResult.at('errors').asList())
}
body
return new HttpInputMessage() {
@Override
InputStream getBody() throws IOException {
return new ByteArrayInputStream(bytes)
}

@Override
HttpHeaders getHeaders() {
return inputMessage.getHeaders()
}
}
}

@ExceptionHandler(JsonSchemaValidationFailedException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ public List<Attribute> getAttributeListFromRelyingPartyOverridesRepresentation(M
switch (ModelRepresentationConversions.AttributeTypes.valueOf(overrideProperty.getDisplayType().toUpperCase())) {
case BOOLEAN:
if (overrideProperty.getPersistType() != null &&
!overrideProperty.getPersistType().equalsIgnoreCase("boolean")) {
!overrideProperty.getPersistType().equalsIgnoreCase("boolean") &&
(Boolean) entry.getValue()) {
list.add(attributeUtility.createAttributeWithStringValues(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
(String) entry.getValue()));
overrideProperty.getPersistValue()));
} else {
list.add(attributeUtility.createAttributeWithBooleanValue(overrideProperty.getAttributeName(),
overrideProperty.getAttributeFriendlyName(),
Expand Down

0 comments on commit 0514933

Please sign in to comment.