Default handler for XmlSlurper (#879)

* Default handler for XmlSlurper
This commit is contained in:
konstantinshevchuk
2019-02-13 13:02:05 +01:00
committed by Olga Maciaszek-Sharma
parent d27020bfdf
commit 59954d1da0
3 changed files with 31 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import groovy.transform.CompileDynamic
import groovy.xml.XmlUtil
import org.springframework.cloud.contract.spec.Contract
import repackaged.nl.flotsam.xeger.Xeger
import org.springframework.cloud.contract.verifier.util.ContentUtils
import java.nio.charset.StandardCharsets
@@ -33,6 +34,7 @@ import static org.apache.commons.text.StringEscapeUtils.escapeJava
* Converts WireMock stubs into the DSL format
*
* @since 1.0.0
* @author Konstantin Shevchuk
*/
@CompileDynamic
class WireMockToDslConverter {
@@ -123,7 +125,7 @@ class WireMockToDslConverter {
return wrapWithMultilineGString(JsonOutput.prettyPrint(responseBody))
} catch (Exception jsonException) {
try {
def xml = new XmlSlurper().parseText(responseBody)
def xml = ContentUtils.getXmlSlurperWithDefaultErrorHandler().parseText(responseBody)
return wrapWithMultilineGString(XmlUtil.serialize(responseBody))
} catch (Exception xmlException) {
return wrapWithMultilineGString(responseBody)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.cloud.contract.spec.internal.Headers
import org.springframework.cloud.contract.spec.internal.MatchingStrategy
import org.springframework.cloud.contract.spec.internal.NamedProperty
import org.springframework.cloud.contract.spec.internal.OptionalProperty
import org.xml.sax.helpers.DefaultHandler
import java.util.regex.Matcher
import java.util.regex.Pattern
@@ -43,6 +44,7 @@ import static org.apache.commons.text.StringEscapeUtils.unescapeXml
* A utility class that can operate on a message body basing on the provided Content Type.
*
* @since 1.0.0
* @author Konstantin Shevchuk
*/
@TypeChecked
@Commons
@@ -110,7 +112,7 @@ class ContentUtils {
return ContentType.JSON
} catch(JsonException e) {
try {
new XmlSlurper().parseText(extractValueForXML(bodyAsValue, GET_STUB_SIDE).toString())
getXmlSlurperWithDefaultErrorHandler().parseText(extractValueForXML(bodyAsValue, GET_STUB_SIDE).toString())
return ContentType.XML
} catch (Exception exception) {
extractValueForGString(bodyAsValue, GET_STUB_SIDE)
@@ -125,7 +127,7 @@ class ContentUtils {
return ContentType.JSON
} catch(JsonException e) {
try {
new XmlSlurper().parseText(bodyAsValue)
getXmlSlurperWithDefaultErrorHandler().parseText(bodyAsValue)
return ContentType.XML
} catch (Exception exception) {
return ContentType.UNKNOWN
@@ -189,7 +191,7 @@ class ContentUtils {
bodyAsValue.strings.clone() as String[]
)
// try to convert it to XML
new XmlSlurper().parseText(impl.toString())
getXmlSlurperWithDefaultErrorHandler().parseText(impl.toString())
return impl
}
@@ -385,7 +387,7 @@ class ContentUtils {
gstring.strings.clone() as String[]
)
try {
new XmlSlurper().parseText(stringWithoutValues.toString())
getXmlSlurperWithDefaultErrorHandler().parseText(stringWithoutValues.toString())
return true
} catch (Exception e) {
// Not XML
@@ -447,4 +449,15 @@ class ContentUtils {
return quote + escapeJava(property.value.serverValue.toString()) + quote + ".getBytes()"
}
/**
* Creates new XmlSlurper with default error handler
*
* @return XmlSlurper with default error handler
*/
static XmlSlurper getXmlSlurperWithDefaultErrorHandler() {
XmlSlurper xmlSlurper = new XmlSlurper()
xmlSlurper.setErrorHandler(new DefaultHandler())
return xmlSlurper
}
}

View File

@@ -3,9 +3,11 @@ package org.springframework.cloud.contract.verifier.util
import org.springframework.cloud.contract.spec.internal.DslProperty
import org.springframework.cloud.contract.verifier.util.ContentUtils
import spock.lang.Specification
import org.xml.sax.helpers.DefaultHandler
/**
* @author Marcin Grzejszczak
* @author Konstantin Shevchuk
*/
class ContentUtilsSpec extends Specification {
@@ -22,4 +24,11 @@ class ContentUtilsSpec extends Specification {
expect:
"test" == ContentUtils.GET_TEST_SIDE(dslProperty)
}
def "should return XmlSlurper with default error handler"() {
given:
XmlSlurper xmlSlurper = ContentUtils.getXmlSlurperWithDefaultErrorHandler()
expect:
xmlSlurper.getErrorHandler() instanceof DefaultHandler
}
}