This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 8f2c6de CAMEL-12165: camel-jaxb - Allow to configure severity level
on schema validation
8f2c6de is described below
commit 8f2c6dea474529d3d60ccddb7b4667e3bfe6443a
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Jan 19 13:51:47 2018 +0100
CAMEL-12165: camel-jaxb - Allow to configure severity level on schema
validation
---
.../camel/model/dataformat/JaxbDataFormat.java | 16 +++++++++
.../camel-jaxb/src/main/docs/jaxb-dataformat.adoc | 3 +-
.../camel/converter/jaxb/JaxbDataFormat.java | 38 ++++++++++++----------
.../springboot/JaxbDataFormatConfiguration.java | 16 +++++++++
4 files changed, 54 insertions(+), 19 deletions(-)
diff --git
a/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java
b/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java
index ed32ef7..5324072 100644
---
a/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java
+++
b/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java
@@ -43,6 +43,8 @@ public class JaxbDataFormat extends DataFormatDefinition {
private String contextPath;
@XmlAttribute
private String schema;
+ @XmlAttribute @Metadata(enums = "0,1,2", defaultValue = "0")
+ private Integer schemaSeverityLevel;
@XmlAttribute
private Boolean prettyPrint;
@XmlAttribute
@@ -106,6 +108,20 @@ public class JaxbDataFormat extends DataFormatDefinition {
this.schema = schema;
}
+ public Integer getSchemaSeverityLevel() {
+ return schemaSeverityLevel;
+ }
+
+ /**
+ * Sets the schema severity level to use when validating against a schema.
+ * This level determines the minimum severity error that triggers JAXB to
stop continue parsing.
+ * The default value of 0 (WARNING) means that any error (WARNING, ERROR
or FATAL ERROR) will trigger
+ * JAXB to stop. There are the following three levels: 0=WARNING, 1=ERROR,
2=FATAL ERROR.
+ */
+ public void setSchemaSeverityLevel(Integer schemaSeverityLevel) {
+ this.schemaSeverityLevel = schemaSeverityLevel;
+ }
+
public Boolean getPrettyPrint() {
return prettyPrint;
}
diff --git a/components/camel-jaxb/src/main/docs/jaxb-dataformat.adoc
b/components/camel-jaxb/src/main/docs/jaxb-dataformat.adoc
index aa0c254..82fcdd1 100644
--- a/components/camel-jaxb/src/main/docs/jaxb-dataformat.adoc
+++ b/components/camel-jaxb/src/main/docs/jaxb-dataformat.adoc
@@ -10,7 +10,7 @@ payload.
### Options
// dataformat options: START
-The JAXB dataformat supports 17 options which are listed below.
+The JAXB dataformat supports 18 options which are listed below.
@@ -19,6 +19,7 @@ The JAXB dataformat supports 17 options which are listed
below.
| Name | Default | Java Type | Description
| contextPath | | String | Package name where your JAXB classes are located.
| schema | | String | To validate against an existing schema. Your can use
the prefix classpath: file: or http: to specify how the resource should by
resolved. You can separate multiple schema files by using the '' character.
+| schemaSeverityLevel | 0 | Integer | Sets the schema severity level to use
when validating against a schema. This level determines the minimum severity
error that triggers JAXB to stop continue parsing. The default value of 0
(WARNING) means that any error (WARNING ERROR or FATAL ERROR) will trigger JAXB
to stop. There are the following three levels: 0=WARNING 1=ERROR 2=FATAL ERROR.
| prettyPrint | false | Boolean | To enable pretty printing output nicely
formatted. Is by default false.
| objectFactory | false | Boolean | Whether to allow using ObjectFactory
classes to create the POJO classes during marshalling. This only applies to
POJO classes that has not been annotated with JAXB and providing jaxb.index
descriptor files.
| ignoreJAXBElement | false | Boolean | Whether to ignore JAXBElement elements
- only needed to be set to false in very special use-cases.
diff --git
a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
index 0782e21..ea74d37 100644
---
a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
+++
b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
@@ -40,7 +40,6 @@ import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.namespace.QName;
-import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
@@ -54,7 +53,6 @@ import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
-import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.TypeConverter;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.spi.DataFormatName;
@@ -85,6 +83,7 @@ public class JaxbDataFormat extends ServiceSupport implements
DataFormat, DataFo
private JAXBIntrospector introspector;
private String contextPath;
private String schema;
+ private int schemaSeverityLevel; // 0 = warning, 1 = error, 2 = fatal
private String schemaLocation;
private String noNamespaceSchemaLocation;
@@ -124,7 +123,7 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
return "jaxb";
}
- public void marshal(Exchange exchange, Object graph, OutputStream stream)
throws IOException, SAXException {
+ public void marshal(Exchange exchange, Object graph, OutputStream stream)
throws IOException {
try {
// must create a new instance of marshaller as its not thread safe
Marshaller marshaller = createMarshaller();
@@ -169,7 +168,7 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
marshaller.setProperty(property.getKey(),
property.getValue());
}
}
- marshal(exchange, graph, stream, marshaller);
+ doMarshal(exchange, graph, stream, marshaller);
if (contentTypeHeader) {
if (exchange.hasOut()) {
@@ -183,8 +182,7 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
}
}
- void marshal(Exchange exchange, Object graph, OutputStream stream,
Marshaller marshaller)
- throws XMLStreamException, JAXBException,
NoTypeConversionAvailableException, IOException, InvalidPayloadException {
+ void doMarshal(Exchange exchange, Object graph, OutputStream stream,
Marshaller marshaller) throws Exception {
Object element = graph;
QName partNamespaceOnDataFormat = getPartNamespace();
@@ -264,7 +262,7 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
return needFiltering(exchange) || (xmlStreamWriterWrapper != null);
}
- public Object unmarshal(Exchange exchange, InputStream stream) throws
IOException, SAXException {
+ public Object unmarshal(Exchange exchange, InputStream stream) throws
IOException {
try {
Object answer;
@@ -274,7 +272,7 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
} else {
xmlReader = typeConverter.convertTo(XMLStreamReader.class,
stream);
}
- String partClassFromHeader =
(String)exchange.getIn().getHeader(JaxbConstants.JAXB_PART_CLASS);
+ String partClassFromHeader =
exchange.getIn().getHeader(JaxbConstants.JAXB_PART_CLASS, String.class);
if (partialClass != null || partClassFromHeader != null) {
// partial unmarshalling
if (partClassFromHeader != null) {
@@ -360,6 +358,14 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
this.schema = schema;
}
+ public int getSchemaSeverityLevel() {
+ return schemaSeverityLevel;
+ }
+
+ public void setSchemaSeverityLevel(int schemaSeverityLevel) {
+ this.schemaSeverityLevel = schemaSeverityLevel;
+ }
+
public boolean isPrettyPrint() {
return prettyPrint;
}
@@ -472,7 +478,6 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
this.jaxbProviderProperties = jaxbProviderProperties;
}
-
public boolean isContentTypeHeader() {
return contentTypeHeader;
}
@@ -537,16 +542,14 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
}
}
- protected Unmarshaller createUnmarshaller() throws JAXBException,
SAXException, FileNotFoundException,
- MalformedURLException {
+ protected Unmarshaller createUnmarshaller() throws JAXBException {
Unmarshaller unmarshaller = getContext().createUnmarshaller();
if (schema != null) {
unmarshaller.setSchema(cachedSchema);
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
- // stop unmarshalling if the event is an ERROR or FATAL
- // ERROR
- return event.getSeverity() == ValidationEvent.WARNING;
+ // continue if the severity is lower than the configured
level
+ return event.getSeverity() < getSchemaSeverityLevel();
}
});
}
@@ -554,15 +557,14 @@ public class JaxbDataFormat extends ServiceSupport
implements DataFormat, DataFo
return unmarshaller;
}
- protected Marshaller createMarshaller() throws JAXBException,
SAXException, FileNotFoundException,
- MalformedURLException {
+ protected Marshaller createMarshaller() throws JAXBException {
Marshaller marshaller = getContext().createMarshaller();
if (schema != null) {
marshaller.setSchema(cachedSchema);
marshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
- // stop marshalling if the event is an ERROR or FATAL ERROR
- return event.getSeverity() == ValidationEvent.WARNING;
+ // continue if the severity is lower than the configured
level
+ return event.getSeverity() < getSchemaSeverityLevel();
}
});
}
diff --git
a/platforms/spring-boot/components-starter/camel-jaxb-starter/src/main/java/org/apache/camel/converter/jaxb/springboot/JaxbDataFormatConfiguration.java
b/platforms/spring-boot/components-starter/camel-jaxb-starter/src/main/java/org/apache/camel/converter/jaxb/springboot/JaxbDataFormatConfiguration.java
index 7bd5336..2b06b48 100644
---
a/platforms/spring-boot/components-starter/camel-jaxb-starter/src/main/java/org/apache/camel/converter/jaxb/springboot/JaxbDataFormatConfiguration.java
+++
b/platforms/spring-boot/components-starter/camel-jaxb-starter/src/main/java/org/apache/camel/converter/jaxb/springboot/JaxbDataFormatConfiguration.java
@@ -43,6 +43,14 @@ public class JaxbDataFormatConfiguration
*/
private String schema;
/**
+ * Sets the schema severity level to use when validating against a schema.
+ * This level determines the minimum severity error that triggers JAXB to
+ * stop continue parsing. The default value of 0 (WARNING) means that any
+ * error (WARNING ERROR or FATAL ERROR) will trigger JAXB to stop. There
are
+ * the following three levels: 0=WARNING 1=ERROR 2=FATAL ERROR.
+ */
+ private Integer schemaSeverityLevel = 0;
+ /**
* To enable pretty printing output nicely formatted. Is by default false.
*/
private Boolean prettyPrint = false;
@@ -139,6 +147,14 @@ public class JaxbDataFormatConfiguration
this.schema = schema;
}
+ public Integer getSchemaSeverityLevel() {
+ return schemaSeverityLevel;
+ }
+
+ public void setSchemaSeverityLevel(Integer schemaSeverityLevel) {
+ this.schemaSeverityLevel = schemaSeverityLevel;
+ }
+
public Boolean getPrettyPrint() {
return prettyPrint;
}
--
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].