Skip to content
Permalink
Browse files
Updates to cache the Woodstox schema grammer
git-svn-id: https://svn.apache.org/repos/asf/cxf/trunk@1536861 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dkulp committed Oct 29, 2013
1 parent 79f8fca commit 4ef48b93801110085ca9a95b715a32618f477f4b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 45 deletions.
@@ -23,7 +23,6 @@
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.activation.DataSource;
@@ -227,17 +226,13 @@ private Element validate(XMLStreamReader input) throws XMLStreamException, IOExc

WoodstoxValidationImpl impl = new WoodstoxValidationImpl();
XMLStreamWriter nullWriter = null;
boolean canValidate = impl.canValidate();
if (canValidate) {
if (impl.canValidate()) {
nullWriter = StaxUtils.createXMLStreamWriter(new NUllOutputStream());
try {
impl.setupValidation(nullWriter, message.getExchange().getService().getServiceInfos().get(0));
} catch (Throwable t) {
LOG.log(Level.FINE, "Trouble setting up validation.", t);
canValidate = false;
}
impl.setupValidation(nullWriter, message.getExchange().getEndpoint(),
message.getExchange().getService().getServiceInfos().get(0));
}
if (canValidate) {
//check if the impl can still validate after the setup, possible issue loading schemas or similar
if (impl.canValidate()) {
//Can use the MSV libs and woodstox to handle the schema validation during
//parsing and processing. Much faster and single traversal
//filter xop node
@@ -21,6 +21,7 @@

import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.XMLConstants;
@@ -32,6 +33,7 @@

import org.apache.cxf.common.i18n.Message;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.service.model.SchemaInfo;
import org.apache.cxf.service.model.ServiceInfo;
@@ -49,6 +51,7 @@
*/
class Stax2ValidationUtils {
private static final Logger LOG = LogUtils.getL7dLogger(Stax2ValidationUtils.class);
private static final String KEY = XMLValidationSchema.class.getName();

public Stax2ValidationUtils() {
new W3CMultiSchemaFactory(); // will throw if wrong woodstox.
@@ -59,15 +62,20 @@ public Stax2ValidationUtils() {
*
* @throws XMLStreamException
*/
public void setupValidation(XMLStreamReader reader, ServiceInfo serviceInfo) throws XMLStreamException {
public boolean setupValidation(XMLStreamReader reader, Endpoint endpoint, ServiceInfo serviceInfo)
throws XMLStreamException {

// Gosh, this is bad, but I don't know a better solution, unless we're willing
// to require the stax2 API no matter what.
XMLStreamReader effectiveReader = reader;
if (effectiveReader instanceof DepthXMLStreamReader) {
effectiveReader = ((DepthXMLStreamReader)reader).getReader();
}
final XMLStreamReader2 reader2 = (XMLStreamReader2)effectiveReader;
XMLValidationSchema vs = getValidator(serviceInfo);
XMLValidationSchema vs = getValidator(endpoint, serviceInfo);
if (vs == null) {
return false;
}
reader2.setValidationProblemHandler(new ValidationProblemHandler() {

public void reportProblem(XMLValidationProblem problem) throws XMLValidationException {
@@ -76,18 +84,25 @@ public void reportProblem(XMLValidationProblem problem) throws XMLValidationExce
}
});
reader2.validateAgainst(vs);
return true;
}

public void setupValidation(XMLStreamWriter writer, ServiceInfo serviceInfo) throws XMLStreamException {
public boolean setupValidation(XMLStreamWriter writer, Endpoint endpoint, ServiceInfo serviceInfo)
throws XMLStreamException {

XMLStreamWriter2 writer2 = (XMLStreamWriter2)writer;
XMLValidationSchema vs = getValidator(serviceInfo);
XMLValidationSchema vs = getValidator(endpoint, serviceInfo);
if (vs == null) {
return false;
}
writer2.setValidationProblemHandler(new ValidationProblemHandler() {

public void reportProblem(XMLValidationProblem problem) throws XMLValidationException {
throw new Fault(problem.getMessage(), LOG);
}
});
writer2.validateAgainst(vs);
return true;
}

/**
@@ -97,32 +112,45 @@ public void reportProblem(XMLValidationProblem problem) throws XMLValidationExce
* @return
* @throws XMLStreamException
*/
private XMLValidationSchema getValidator(ServiceInfo serviceInfo) throws XMLStreamException {
Map<String, EmbeddedSchema> sources = new TreeMap<String, EmbeddedSchema>();

for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
XmlSchema sch = schemaInfo.getSchema();
String uri = sch.getTargetNamespace();
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri)) {
continue;
private XMLValidationSchema getValidator(Endpoint endpoint, ServiceInfo serviceInfo) throws XMLStreamException {
synchronized (endpoint) {
XMLValidationSchema ret = (XMLValidationSchema)endpoint.get(KEY);
if (ret == null) {
if (endpoint.containsKey(KEY)) {
return null;
}
Map<String, EmbeddedSchema> sources = new TreeMap<String, EmbeddedSchema>();

for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
XmlSchema sch = schemaInfo.getSchema();
String uri = sch.getTargetNamespace();
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri)) {
continue;
}
LOG.info(uri);

Element serialized = schemaInfo.getElement();
String schemaSystemId = sch.getSourceURI();
if (null == schemaSystemId) {
schemaSystemId = sch.getTargetNamespace();
}

EmbeddedSchema embeddedSchema = new EmbeddedSchema(schemaSystemId, serialized);
sources.put(sch.getTargetNamespace(), embeddedSchema);
}

W3CMultiSchemaFactory factory = new W3CMultiSchemaFactory();
// I don't think that we need the baseURI.
try {
ret = factory.loadSchemas(null, sources);
endpoint.put(KEY, ret);
} catch (XMLStreamException ex) {
LOG.log(Level.INFO, "Problem loading schemas. Falling back to slower method.", ret);
endpoint.put(KEY, null);
}
}
LOG.info(uri);

Element serialized = schemaInfo.getElement();
String schemaSystemId = sch.getSourceURI();
if (null == schemaSystemId) {
schemaSystemId = sch.getTargetNamespace();
}

EmbeddedSchema embeddedSchema = new EmbeddedSchema(schemaSystemId, serialized);
sources.put(sch.getTargetNamespace(), embeddedSchema);
return ret;
}

W3CMultiSchemaFactory factory = new W3CMultiSchemaFactory();
XMLValidationSchema vs;
// I don't think that we need the baseURI.
vs = factory.loadSchemas(null, sources);
return vs;
}

}
@@ -56,7 +56,8 @@ private void setSchemaInMessage(Message message, XMLStreamReader reader) throws
try {
WoodstoxValidationImpl mgr = new WoodstoxValidationImpl();
if (mgr.canValidate()) {
mgr.setupValidation(reader, message.getExchange().getService().getServiceInfos().get(0));
mgr.setupValidation(reader, message.getExchange().getEndpoint(),
message.getExchange().getService().getServiceInfos().get(0));
}
} catch (Throwable t) {
//likely no MSV or similar
@@ -56,7 +56,8 @@ private void setSchemaInMessage(Message message, XMLStreamWriter writer) throws
try {
WoodstoxValidationImpl mgr = new WoodstoxValidationImpl();
if (mgr.canValidate()) {
mgr.setupValidation(writer, message.getExchange().getService().getServiceInfos().get(0));
mgr.setupValidation(writer, message.getExchange().getEndpoint(),
message.getExchange().getService().getServiceInfos().get(0));
}
} catch (Throwable t) {
//likely no MSV or similar
@@ -27,6 +27,7 @@
import javax.xml.stream.XMLStreamWriter;

import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.service.model.ServiceInfo;

/**
@@ -54,15 +55,18 @@ public boolean canValidate() {
/** {@inheritDoc}
* @throws XMLStreamException */
public void setupValidation(XMLStreamReader reader,
Endpoint endpoint,
ServiceInfo serviceInfo) throws XMLStreamException {
if (utils != null && reader != null) {
utils.setupValidation(reader, serviceInfo);
if (utils != null && reader != null && !utils.setupValidation(reader, endpoint, serviceInfo)) {
utils = null;
}
}

public void setupValidation(XMLStreamWriter writer, ServiceInfo serviceInfo) throws XMLStreamException {
if (utils != null && writer != null) {
utils.setupValidation(writer, serviceInfo);
public void setupValidation(XMLStreamWriter writer,
Endpoint endpoint,
ServiceInfo serviceInfo) throws XMLStreamException {
if (utils != null && writer != null && !utils.setupValidation(writer, endpoint, serviceInfo)) {
utils = null;
}
}
}

0 comments on commit 4ef48b9

Please sign in to comment.