【发布时间】:2021-05-09 00:03:50
【问题描述】:
我在 Mule 4 组件中发现了以下错误。如何向 Mulesoft 报告此问题?
Mule 4 XML 模块 1.2.3 引入了一个错误,导致模块中出现错误的 Mule 错误。
在验证无效的 XML 有效负载(非 xml 字符串、带有未闭合或未配对标签的“XML”等)1.2.2 版组件时会引发 mule 错误XML-MODULE:INVALID_INPUT_XML,但使用 1.2.3 版组件现在的错误是XML-MODULE:TRANSFORMATION。
问题似乎是该模块的版本 1.2.3 删除了对 XMLUtils.toDOMNode 的调用,该调用用于对消息进行初始验证,并在处理无效 XML 时抛出类 InvalidInputXmlException 异常。
XML 模块:1.2.2
public class SchemaValidatorOperation extends PooledTransformerOperation<SchemaValidatorOperation.SchemaKey, Validator> {
private LSResourceResolver resourceResolver = (LSResourceResolver)new MuleResourceResolver();
@Validator
@Execution(ExecutionType.CPU_INTENSIVE)
@Throws({SchemaValidatorErrorTypeProvider.class})
public void validateSchema(@Path(type = PathModel.Type.FILE, acceptedFileExtensions = {"xsd"}) String schemas, @Optional(defaultValue = "W3C") SchemaLanguage schemaLanguage, @Content(primary = true) InputStream content, @Config XmlModule config) {
Node node = XMLUtils.toDOMNode(content, this.documentBuilderFactory);
withTransformer(new SchemaKey(schemas, schemaLanguage.getLanguageUri(), this.expandEntities), validator -> {
validator.setResourceResolver(this.resourceResolver);
final List<SchemaViolation> errors = new LinkedList<>();
validator.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException exception) {}
public void error(SAXParseException exception) {
trackError(exception);
}
public void fatalError(SAXParseException exception) {
trackError(exception);
}
private void trackError(SAXParseException exception) {
errors.add(new SchemaViolation(exception.getLineNumber(), exception.getColumnNumber(), exception.getMessage()));
}
});
try {
validator.validate(new DOMSource(node));
} catch (SAXParseException e) {
throw new TransformationException("Failed to validate schema. " + e.getMessage(), e);
} catch (IOException e) {
throw new InvalidInputXmlException("Could not validate schema because the input was not valid XML. " + e.getMessage(), e);
}
if (!errors.isEmpty())
throw new SchemaValidationException("Input XML was not compliant with the schema. Check this error's Mule message for the list of problems (e.g: #[error.errorMessage.payload[0].description)", errors);
return null;
});
}
XML 模块:1.2.3
public class SchemaValidatorOperation extends PooledTransformerOperation<SchemaValidatorOperation.SchemaKey, Validator> {
private LSResourceResolver resourceResolver = (LSResourceResolver)new MuleResourceResolver();
@Validator
@Execution(ExecutionType.CPU_INTENSIVE)
@Throws({SchemaValidatorErrorTypeProvider.class})
public void validateSchema(@Path(type = PathModel.Type.FILE, acceptedFileExtensions = {"xsd"}) String schemas, @Optional(defaultValue = "W3C") SchemaLanguage schemaLanguage, @Content(primary = true) InputStream content, @Config XmlModule config) {
withTransformer(new SchemaKey(schemas, schemaLanguage.getLanguageUri(), this.expandEntities), validator -> {
validator.setResourceResolver(this.resourceResolver);
final List<SchemaViolation> errors = new LinkedList<>();
validator.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException exception) {}
public void error(SAXParseException exception) {
trackError(exception);
}
public void fatalError(SAXParseException exception) {
trackError(exception);
}
private void trackError(SAXParseException exception) {
errors.add(new SchemaViolation(exception.getLineNumber(), exception.getColumnNumber(), exception.getMessage()));
}
});
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setFeature("http://xml.org/sax/features/external-general-entities", this.expandEntities.isAcceptExternalEntities());
spf.setFeature("http://xml.org/sax/features/external-parameter-entities", this.expandEntities.isAcceptExternalEntities());
spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !this.expandEntities.isExpandInternalEntities());
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", this.expandEntities.isExpandInternalEntities());
validator.validate(new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(content)));
} catch (SAXParseException e) {
throw new TransformationException("Failed to validate schema. " + e.getMessage(), e);
} catch (IOException e) {
throw new InvalidInputXmlException("Could not validate schema because the input was not valid XML. " + e.getMessage(), e);
}
if (!errors.isEmpty())
throw new SchemaValidationException("Input XML was not compliant with the schema. Check this error's Mule message for the list of problems (e.g: #[error.errorMessage.payload[0].description)", errors);
return null;
});
}
并不是说XMLUtils.toDOMNode 是完美的,因为它捕获了任何异常,但至少在尝试验证不正确的 xml 时检测实例很有用。
XMLUtils.toDOMNode
public class XMLUtils {
public static Node toDOMNode(InputStream src, DocumentBuilderFactory factory) {
return toDOMNode(src, factory, null);
}
public static Node toDOMNode(InputStream src, DocumentBuilderFactory factory, EntityResolver entityResolver) {
try {
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
if (entityResolver != null)
documentBuilder.setEntityResolver(entityResolver);
return documentBuilder.parse(src);
} catch (Exception e) {
throw new InvalidInputXmlException("Cannot parse input XML because it is invalid.", e);
}
}
}
【问题讨论】:
-
到目前为止你尝试过什么?你被困在哪里了?