You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@synapse.apache.org by as...@apache.org on 2006/07/04 14:29:19 UTC
svn commit: r418995 - in /incubator/synapse/trunk/java: ./
modules/core/src/org/apache/synapse/config/xml/
modules/core/src/org/apache/synapse/mediators/builtin/
modules/extensions/src/org/apache/synapse/config/xml/
modules/extensions/src/org/apache/sy...
Author: asankha
Date: Tue Jul 4 05:29:18 2006
New Revision: 418995
URL: http://svn.apache.org/viewvc?rev=418995&view=rev
Log:
commit patches by Hadrian
1) Fix spelling error in the Log mediator
2) Set -Djavax.xml.validation.SchemaFactory for unit tests
3) Enhance validate mediator to be able to use any of the Xerces features (with some minor modifications)
Modified:
incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/config/xml/LogMediatorFactory.java
incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/mediators/builtin/LogMediator.java
incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/config/xml/ValidateMediatorFactory.java
incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/mediators/ValidateMediator.java
incubator/synapse/trunk/java/modules/extensions/test/org/apache/synapse/ValidateMediatorTest.java
incubator/synapse/trunk/java/project.properties
Modified: incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/config/xml/LogMediatorFactory.java
URL: http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/config/xml/LogMediatorFactory.java?rev=418995&r1=418994&r2=418995&view=diff
==============================================================================
--- incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/config/xml/LogMediatorFactory.java (original)
+++ incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/config/xml/LogMediatorFactory.java Tue Jul 4 05:29:18 2006
@@ -74,10 +74,10 @@
}
}
- // check if a custom seperator has been supplied, if so use it
- OMAttribute seperator = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "seperator"));
- if (seperator != null) {
- logMediator.setSeperator(seperator.getAttributeValue());
+ // check if a custom separator has been supplied, if so use it
+ OMAttribute separator = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "separator"));
+ if (separator != null) {
+ logMediator.setSeparator(separator.getAttributeValue());
}
logMediator.addAllProperties(MediatorPropertyFactory.getMediatorProperties(elem));
Modified: incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/mediators/builtin/LogMediator.java
URL: http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/mediators/builtin/LogMediator.java?rev=418995&r1=418994&r2=418995&view=diff
==============================================================================
--- incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/mediators/builtin/LogMediator.java (original)
+++ incubator/synapse/trunk/java/modules/core/src/org/apache/synapse/mediators/builtin/LogMediator.java Tue Jul 4 05:29:18 2006
@@ -33,7 +33,7 @@
* which attributes would be logged, and is configurable. Additionally custom
* properties may be defined to the logger, where literal values or expressions
* could be specified for logging. The custom properties are printed into the log
- * using the defined seperator (\n, "," etc)
+ * using the defined separator (\n, "," etc)
*/
public class LogMediator extends AbstractMediator {
@@ -139,11 +139,11 @@
this.logLevel = logLevel;
}
- public String getSeperator() {
+ public String getSeparator() {
return SEP;
}
- public void setSeperator(String SEP) {
+ public void setSeparator(String SEP) {
this.SEP = SEP;
}
Modified: incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/config/xml/ValidateMediatorFactory.java
URL: http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/config/xml/ValidateMediatorFactory.java?rev=418995&r1=418994&r2=418995&view=diff
==============================================================================
--- incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/config/xml/ValidateMediatorFactory.java (original)
+++ incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/config/xml/ValidateMediatorFactory.java Tue Jul 4 05:29:18 2006
@@ -26,27 +26,33 @@
import org.apache.synapse.mediators.ValidateMediator;
import org.jaxen.JaxenException;
+import java.util.Iterator;
import javax.xml.namespace.QName;
/**
* Creates a validation mediator from the XML configuration
* <p/>
* <validate schema="url" [source="xpath"]>
- * <on-fail>
- * mediator+
- * </on-fail>
+ * <property name="<validation-feature-id>" value="true|false"/> *
+ * <on-fail>
+ * mediator+
+ * </on-fail>
* </validate>
*/
public class ValidateMediatorFactory extends AbstractListMediatorFactory {
private static final Log log = LogFactory.getLog(TransformMediatorFactory.class);
+
private static final QName VALIDATE_Q = new QName(Constants.SYNAPSE_NAMESPACE, "validate");
+ private static final QName ON_FAIL_Q = new QName(Constants.SYNAPSE_NAMESPACE, "on-fail");
+ private static final QName SCHEMA_Q = new QName(Constants.NULL_NAMESPACE, "schema");
+ private static final QName SOURCE_Q = new QName(Constants.NULL_NAMESPACE, "source");
public Mediator createMediator(OMElement elem) {
ValidateMediator validateMediator = new ValidateMediator();
- OMAttribute attSchema = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "schema"));
- OMAttribute attSource = elem.getAttribute(new QName(Constants.NULL_NAMESPACE, "source"));
+ OMAttribute attSchema = elem.getAttribute(SCHEMA_Q);
+ OMAttribute attSource = elem.getAttribute(SOURCE_Q);
if (attSchema != null) {
validateMediator.setSchemaUrl(attSchema.getAttributeValue());
@@ -61,7 +67,6 @@
AXIOMXPath xp = new AXIOMXPath(attSource.getAttributeValue());
validateMediator.setSource(xp);
Util.addNameSpaces(xp, elem, log);
-
} catch (JaxenException e) {
String msg = "Invalid XPath expression specified for attribute 'source'";
log.error(msg);
@@ -69,16 +74,21 @@
}
}
- OMElement onFail = elem.getFirstElement();
- if (new QName(Constants.SYNAPSE_NAMESPACE, "on-fail").equals(onFail.getQName()) &&
- onFail.getChildElements().hasNext()) {
- super.addChildren(onFail, validateMediator);
+ OMElement onFail = null;
+ Iterator iter = elem.getChildrenWithName(ON_FAIL_Q);
+ if (iter.hasNext()) {
+ onFail = (OMElement)iter.next();
+ }
+ if (onFail != null && onFail.getChildElements().hasNext()) {
+ super.addChildren(onFail, validateMediator);
} else {
- String msg = "A non-empty on-fail element is required for the validate mediator";
+ String msg = "A non-empty <on-fail> child element is required for the <validate> mediator";
log.error(msg);
throw new SynapseException(msg);
}
+
+ validateMediator.addAllProperties(MediatorPropertyFactory.getMediatorProperties(elem));
return validateMediator;
}
Modified: incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/mediators/ValidateMediator.java
URL: http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/mediators/ValidateMediator.java?rev=418995&r1=418994&r2=418995&view=diff
==============================================================================
--- incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/mediators/ValidateMediator.java (original)
+++ incubator/synapse/trunk/java/modules/extensions/src/org/apache/synapse/mediators/ValidateMediator.java Tue Jul 4 05:29:18 2006
@@ -44,7 +44,10 @@
import javax.xml.validation.Validator;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
import java.util.StringTokenizer;
/**
@@ -54,28 +57,27 @@
private static final Log log = LogFactory.getLog(ValidateMediator.class);
- /** A space or comma delimitered list of schemas to validate the source element against */
- private String schemaUrl = null;
/**
- * An XPath expression to be evaluated against the message to find the element to be validated.
- * If this is not specified, the validation will occur against the first child element of the SOAP body
+ * Default validation schema language (http://www.w3.org/2001/XMLSchema) and validator feature ids.
*/
- private AXIOMXPath source = null;
+ private static final String DEFAULT_SCHEMA_LANGUAGE = "http://www.w3.org/2001/XMLSchema";
/**
- * Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking).
+ * A space or comma delimitered list of schemas to validate the source element against
*/
- private static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
+ private String schemaUrl = null;
/**
- * Honour all schema locations feature id (http://apache.org/xml/features/honour-all-schemaLocations).
+ * An XPath expression to be evaluated against the message to find the element to be validated.
+ * If this is not specified, the validation will occur against the first child element of the SOAP body
*/
- private static final String HONOUR_ALL_SCHEMA_LOCATIONS_ID = "http://apache.org/xml/features/honour-all-schemaLocations";
+ private AXIOMXPath source = null;
/**
- * Default schema language (http://www.w3.org/2001/XMLSchema).
+ * A Map containing properties for the validate mediator - such as
+ * features to be passed to the actual validator (Xerces)
*/
- private static final String DEFAULT_SCHEMA_LANGUAGE = "http://www.w3.org/2001/XMLSchema";
+ private Map properties = new HashMap();
public String getSchemaUrl() {
@@ -164,8 +166,7 @@
// Create SchemaFactory and configure
SchemaFactory factory = SchemaFactory.newInstance(DEFAULT_SCHEMA_LANGUAGE);
factory.setErrorHandler(handler);
- factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
- factory.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, true);
+ setXmlFeatures(factory);
// Build Schema from schemaUrl
Schema schema = null;
@@ -188,11 +189,10 @@
schema = factory.newSchema();
}
- // Setup validator and input source.
+ // Setup validator and input source
+ // Features set for the SchemaFactory get propagated to Schema and Validator (JAXP 1.4).
Validator validator = schema.newValidator();
validator.setErrorHandler(handler);
- validator.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
- validator.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, true);
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXSource source = new SAXSource(reader, new InputSource(baisFromSource));
@@ -214,6 +214,63 @@
}
return true;
+ }
+
+ /**
+ * Get a mediator property. The common use case is a feature for the
+ * underlying Xerces validator
+ * @param key property key / feature name
+ * @return property string value (usually true|false)
+ */
+ public Object getProperty(String key) {
+ return properties.get(key);
+ }
+
+ /**
+ * Set a property for this mediator
+ * @param key the property key / feature name
+ * @param value property string value (usually true|false)
+ * @see #getProperty(String)
+ */
+ public void setProperty(String key, Object value) {
+ properties.put(key, value);
+ }
+
+ /**
+ * Add a list of 'MediatorProperty'ies to this mediator
+ * @param list a List of MediatorProperty objects
+ */
+ public void addAllProperties(List list) {
+ Iterator iter = list.iterator();
+ while (iter.hasNext()) {
+ Object o = iter.next();
+ if (o instanceof MediatorProperty) {
+ MediatorProperty prop = (MediatorProperty) o;
+ setProperty(prop.getName(), prop.getValue());
+ } else {
+ handleException("Attempt to set invalid property type. " +
+ "Expected MediatorProperty type got " + o.getClass().getName());
+ }
+ }
+ }
+
+ private void handleException(String msg) {
+ log.error(msg);
+ throw new SynapseException(msg);
+ }
+
+ /**
+ * Set the properties set on this mediator to the underlying Xerces
+ * @param factory Schema factory
+ * @throws SAXException on error
+ */
+ private void setXmlFeatures(SchemaFactory factory) throws SAXException {
+ Iterator iter = properties.entrySet().iterator();
+ while (iter.hasNext()) {
+ Map.Entry entry = (Map.Entry)iter.next();
+ String value = (String)entry.getValue();
+ factory.setFeature((String)entry.getKey(), value != null && "true".equals(value));
+ }
}
/**
Modified: incubator/synapse/trunk/java/modules/extensions/test/org/apache/synapse/ValidateMediatorTest.java
URL: http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/test/org/apache/synapse/ValidateMediatorTest.java?rev=418995&r1=418994&r2=418995&view=diff
==============================================================================
--- incubator/synapse/trunk/java/modules/extensions/test/org/apache/synapse/ValidateMediatorTest.java (original)
+++ incubator/synapse/trunk/java/modules/extensions/test/org/apache/synapse/ValidateMediatorTest.java Tue Jul 4 05:29:18 2006
@@ -15,16 +15,27 @@
*/
package org.apache.synapse;
+import java.io.ByteArrayInputStream;
+import javax.xml.stream.XMLStreamException;
import junit.framework.TestCase;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.xpath.AXIOMXPath;
import org.apache.synapse.MessageContext;
import org.apache.synapse.TestMediateHandler;
import org.apache.synapse.TestMediator;
import org.apache.synapse.mediators.TestUtils;
import org.apache.synapse.mediators.ValidateMediator;
+import org.apache.synapse.config.xml.ValidateMediatorFactory;
public class ValidateMediatorTest extends TestCase {
+ private static final String SCHEMA_FULL_CHECKING_FEATURE_ID =
+ "http://apache.org/xml/features/validation/schema-full-checking";
+
+ private static final String HONOUR_ALL_SCHEMA_LOCATIONS_FEATURE_ID =
+ "http://apache.org/xml/features/honour-all-schemaLocations";
+
private static final String VALID_ENVELOPE_TWO_SCHEMAS =
"<Outer xmlns=\"http://www.apache-synapse.org/test2\">" +
"<m0:CheckPriceRequest xmlns:m0=\"http://www.apache-synapse.org/test\">\n" +
@@ -65,6 +76,30 @@
"<Codes>String</Codes>\n" +
"</CheckPriceRequest>\n";
+ private static final String DEFAULT_FEATURES_MEDIATOR_CONFIG =
+ "<validate xmlns=\"http://ws.apache.org/ns/synapse\" " +
+ " schema=\"file:synapse_repository/conf/sample/validate.xsd\">" +
+ " <on-fail>" +
+ " <makefault>" +
+ " <code value=\"tns:Receiver\" xmlns:tns=\"http://www.w3.org/2003/05/soap-envelope\"/>" +
+ " <reason value=\"Invalid request\"/>" +
+ " </makefault>" +
+ " </on-fail>" +
+ "</validate>";
+
+ private static final String CUSTOM_FEATURES_MEDIATOR_CONFIG =
+ "<validate xmlns=\"http://ws.apache.org/ns/synapse\" " +
+ " schema=\"file:synapse_repository/conf/sample/validate.xsd\">" +
+ " <property name=\"" + SCHEMA_FULL_CHECKING_FEATURE_ID + "\" value=\"false\"/>" +
+ " <property name=\"" + HONOUR_ALL_SCHEMA_LOCATIONS_FEATURE_ID + "\" value=\"true\"/>" +
+ " <on-fail>" +
+ " <makefault>" +
+ " <code value=\"tns:Receiver\" xmlns:tns=\"http://www.w3.org/2003/05/soap-envelope\"/>" +
+ " <reason value=\"Invalid request\"/>" +
+ " </makefault>" +
+ " </on-fail>" +
+ "</validate>";
+
private boolean onFailInvoked = false;
private TestMediator testMediator = null;
@@ -82,7 +117,7 @@
this.onFailInvoked = onFailInvoked;
}
- public void testValidateMedaitorValidCase() throws Exception {
+ public void testValidateMediatorValidCase() throws Exception {
setOnFailInvoked(false);
// create a validate mediator
@@ -100,10 +135,10 @@
// test validate mediator, with static enveope
validate.mediate(TestUtils.getTestContext(VALID_ENVELOPE));
- assertTrue(!onFailInvoked);
+ assertFalse(onFailInvoked);
}
- public void testValidateMedaitorValidCaseTwoSchemas() throws Exception {
+ public void testValidateMediatorValidCaseTwoSchemas() throws Exception {
setOnFailInvoked(false);
// create a validate mediator
@@ -121,10 +156,10 @@
// test validate mediator, with static enveope
validate.mediate(TestUtils.getTestContext(VALID_ENVELOPE_TWO_SCHEMAS));
- assertTrue(!onFailInvoked);
+ assertFalse(onFailInvoked);
}
- public void testValidateMedaitorInvalidCaseTwoSchemas() throws Exception {
+ public void testValidateMediatorInvalidCaseTwoSchemas() throws Exception {
setOnFailInvoked(false);
// create a validate mediator
@@ -145,7 +180,7 @@
assertTrue(onFailInvoked);
}
- public void testValidateMedaitorInvalidCase() throws Exception {
+ public void testValidateMediatorInvalidCase() throws Exception {
setOnFailInvoked(false);
// create a validate mediator
@@ -166,7 +201,7 @@
assertTrue(onFailInvoked);
}
- public void testValidateMedaitorValidCaseNoNS() throws Exception {
+ public void testValidateMediatorValidCaseNoNS() throws Exception {
setOnFailInvoked(false);
// create a validate mediator
@@ -184,10 +219,10 @@
// test validate mediator, with static enveope
validate.mediate(TestUtils.getTestContext(VALID_ENVELOPE_NO_NS));
- assertTrue(!onFailInvoked);
+ assertFalse(onFailInvoked);
}
- public void testValidateMedaitorInvalidCaseNoNS() throws Exception {
+ public void testValidateMediatorInvalidCaseNoNS() throws Exception {
setOnFailInvoked(false);
// create a validate mediator
@@ -206,5 +241,61 @@
validate.mediate(TestUtils.getTestContext(IN_VALID_ENVELOPE_NO_NS));
assertTrue(onFailInvoked);
+ }
+
+ public void testValidateMediatorDefaultFeatures() throws Exception {
+
+ ValidateMediatorFactory mf = new ValidateMediatorFactory();
+ ValidateMediator validate = (ValidateMediator)mf.createMediator(
+ createOMElement(DEFAULT_FEATURES_MEDIATOR_CONFIG));
+
+ assertNull(validate.getProperty(SCHEMA_FULL_CHECKING_FEATURE_ID));
+ assertNull(validate.getProperty(HONOUR_ALL_SCHEMA_LOCATIONS_FEATURE_ID));
+
+ makeValidInvocation(validate);
+ }
+
+ public void testValidateMediatorCustomFeatures() throws Exception
+ {
+ ValidateMediatorFactory mf = new ValidateMediatorFactory();
+ ValidateMediator validate = (ValidateMediator)mf.createMediator(
+ createOMElement(CUSTOM_FEATURES_MEDIATOR_CONFIG));
+
+ assertNotNull(validate.getProperty(SCHEMA_FULL_CHECKING_FEATURE_ID));
+ assertFalse("true".equals((String)validate.getProperty(SCHEMA_FULL_CHECKING_FEATURE_ID)));
+ assertNotNull(validate.getProperty(HONOUR_ALL_SCHEMA_LOCATIONS_FEATURE_ID));
+ assertTrue("true".equals((String)validate.getProperty(HONOUR_ALL_SCHEMA_LOCATIONS_FEATURE_ID)));
+
+ makeValidInvocation(validate);
+ }
+
+ private void makeValidInvocation(ValidateMediator validate) throws Exception {
+
+ setOnFailInvoked(false);
+
+ // set the schema url, source xpath and any name spaces
+ validate.setSchemaUrl("../core/test-resources/misc/validate.xsd");
+ AXIOMXPath source = new AXIOMXPath("//m0:CheckPriceRequest");
+ source.addNamespace("m0", "http://www.apache-synapse.org/test");
+ validate.setSource(source);
+
+ // set dummy mediator to be called on fail
+ validate.removeChild(0);
+ validate.addChild(testMediator);
+
+ // test validate mediator, with static enveope
+ validate.mediate(TestUtils.getTestContext(VALID_ENVELOPE));
+
+ assertFalse(onFailInvoked);
+ }
+
+ private static OMElement createOMElement(String xml) {
+ try {
+ StAXOMBuilder builder = new StAXOMBuilder(new ByteArrayInputStream(xml.getBytes()));
+ OMElement omElement = builder.getDocumentElement();
+ return omElement;
+ } catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
}
}
Modified: incubator/synapse/trunk/java/project.properties
URL: http://svn.apache.org/viewvc/incubator/synapse/trunk/java/project.properties?rev=418995&r1=418994&r2=418995&view=diff
==============================================================================
--- incubator/synapse/trunk/java/project.properties (original)
+++ incubator/synapse/trunk/java/project.properties Tue Jul 4 05:29:18 2006
@@ -25,7 +25,8 @@
# JUnit
# -------------------------------------------------------------------
# explicit setting of Sax parser as below is a hack to avoid Junit from loading its own parsers ignoring maven.test.excludeXmlApis
-maven.junit.jvmargs=-Djava.awt.headless=true -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser
+# explicit setting of the SchemaFactory class as the default one in Java 5 does not support schema-full-checking and honour-all-schemaLocations features
+maven.junit.jvmargs=-Djava.awt.headless=true -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser -Djavax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema=org.apache.xerces.jaxp.validation.XMLSchemaFactory
maven.junit.fork=yes
maven.test.excludeXmlApis=yes
#maven.junit.dir=${basedir}/modules/core
---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org