From 59954d1da0ab7eafd7e3fee8adefbf010419c402 Mon Sep 17 00:00:00 2001 From: konstantinshevchuk <43748896+konstantinshevchuk@users.noreply.github.com> Date: Wed, 13 Feb 2019 13:02:05 +0100 Subject: [PATCH] Default handler for XmlSlurper (#879) * Default handler for XmlSlurper --- .../wiremock/WireMockToDslConverter.groovy | 6 +++-- .../verifier/util/ContentUtils.groovy | 23 +++++++++++++++---- .../verifier/util/ContentUtilsSpec.groovy | 9 ++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/spring-cloud-contract-tools/spring-cloud-contract-converters/src/main/groovy/org/springframework/cloud/contract/verifier/wiremock/WireMockToDslConverter.groovy b/spring-cloud-contract-tools/spring-cloud-contract-converters/src/main/groovy/org/springframework/cloud/contract/verifier/wiremock/WireMockToDslConverter.groovy index 736fec8587..d283eab32c 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-converters/src/main/groovy/org/springframework/cloud/contract/verifier/wiremock/WireMockToDslConverter.groovy +++ b/spring-cloud-contract-tools/spring-cloud-contract-converters/src/main/groovy/org/springframework/cloud/contract/verifier/wiremock/WireMockToDslConverter.groovy @@ -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) diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContentUtils.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContentUtils.groovy index e0a5a7fb8f..4139fa0614 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContentUtils.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/util/ContentUtils.groovy @@ -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 + } + } diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/util/ContentUtilsSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/util/ContentUtilsSpec.groovy index 98c2229de7..90fb741344 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/util/ContentUtilsSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/util/ContentUtilsSpec.groovy @@ -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 + } }