You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by sc...@apache.org on 2009/06/18 15:28:49 UTC
svn commit: r786055 - in /webservices/axis2/trunk/java/modules/jaxws:
src/org/apache/axis2/jaxws/spi/handler/BaseHandlerResolver.java
test/org/apache/axis2/jaxws/handler/HandlerChainProcessorTests.java
Author: scheu
Date: Thu Jun 18 13:28:49 2009
New Revision: 786055
URL: http://svn.apache.org/viewvc?rev=786055&view=rev
Log:
JIRA:AXIS2-4397
Contributor:Rich Scheuerle
Summary:
Corrected the regular expression processing for the handler's <jws:port-name-pattern> or
<jws:service-name-pattern> qnames. Also provided validation tests to ensure that future changes do not break this code.
Modified:
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/spi/handler/BaseHandlerResolver.java
webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/HandlerChainProcessorTests.java
Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/spi/handler/BaseHandlerResolver.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/spi/handler/BaseHandlerResolver.java?rev=786055&r1=786054&r2=786055&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/spi/handler/BaseHandlerResolver.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/spi/handler/BaseHandlerResolver.java Thu Jun 18 13:28:49 2009
@@ -116,42 +116,154 @@
}
}
- private static boolean doesPatternMatch(QName portInfoQName, QName pattern) {
- if (pattern == null)
- return true;
+ /**
+ * Returns true of the specified qName matches the pattern.
+ * Some customers may have become dependent on the older
+ * algorithm. So first the "official" algorithm is used
+ * and if that fails, the older algorithm is used.
+ * @param qName QName
+ * @param pattern QName
+ * @return true or false
+ */
+ public static boolean doesPatternMatch(QName qName, QName pattern) {
+
+ // Validate pattern only generates warnings if the pattern qname
+ // does not align with the specification.
validatePattern(pattern);
- // build up the strings according to the regular expression defined at http://java.sun.com/xml/ns/javaee/javaee_web_services_1_2.xsd
- // use the prefix, not the literal namespace
- String portInfoPrefix = portInfoQName.getNamespaceURI(); //Prefix();
- String portInfoLocalPart = portInfoQName.getLocalPart();
- String portInfoString = ((portInfoPrefix == null) || (portInfoPrefix.equals(""))) ? "" : portInfoPrefix + ":";
- portInfoString += portInfoLocalPart;
- String patternStringPrefix = pattern.getNamespaceURI(); //Prefix();
- String patternInfoLocalPart = pattern.getLocalPart();
- String patternString = ((patternStringPrefix == null) || (patternStringPrefix.equals(""))) ? "" : patternStringPrefix + ":";
- patternString += patternInfoLocalPart;
+ // Try the official pattern match algorithm
+ boolean match = doesPatternMatch_Official(qName, pattern);
- // now match the portInfoQName to the user pattern
- // But first, convert the user pattern to a regular expression. Remember, the only non-QName character allowed is "*", which
- // is a wildcard, with obvious restrictions on what characters can match (for example, a .java filename cannot contain perentheses).
- // We'll just use part of the above reg ex to form the appropriate restrictions on the user-specified "*" character:
- Pattern userp = Pattern.compile(patternString.replace("*", "(\\w|\\.|-|_)*"));
- Matcher userm = userp.matcher(portInfoString);
- boolean match = userm.matches();
- if (log.isDebugEnabled()) {
- if (!match) {
- log.debug("Pattern match failed: \"" + portInfoString + "\" does not match \"" + patternString + "\"");
- } else {
- log.debug("Pattern match succeeded: \"" + portInfoString + "\" matches \"" + patternString + "\"");
+ // Customers may be dependent on the old algorithm, so this is retained.
+ if (!match) {
+ if (log.isDebugEnabled()) {
+ log.debug("The offical matching algorithm failed. Re-attempting with the prior algorithm");
+ }
+ match = doesPatternMatch_Old(qName, pattern);
+ if (log.isDebugEnabled()) {
+ log.debug("The old matching algorithm returns " + match);
}
}
return match;
+ }
+
+ /**
+ * The old match algorithm combines the namespace and localpart into
+ * a single string to do the matching. Unfortunately this will cause
+ * the pure wildcard (*) pattern to fail. And in addition, it may cause
+ * other patterns to succeed. Prefer the doesPatternMatch_Official algorithm
+ * @param portInfoQName
+ * @param pattern
+ * @return
+ */
+ private static boolean doesPatternMatch_Old(QName portInfoQName, QName pattern) {
+ if (pattern == null)
+ return true;
+
+ // build up the strings according to the regular expression defined at http://java.sun.com/xml/ns/javaee/javaee_web_services_1_2.xsd
+ // use the prefix, not the literal namespace
+ String portInfoPrefix = portInfoQName.getNamespaceURI(); //Prefix();
+ String portInfoLocalPart = portInfoQName.getLocalPart();
+ String portInfoString = ((portInfoPrefix == null) || (portInfoPrefix.equals(""))) ? "" : portInfoPrefix + ":";
+ portInfoString += portInfoLocalPart;
+
+ String patternStringPrefix = pattern.getNamespaceURI(); //Prefix();
+ String patternInfoLocalPart = pattern.getLocalPart();
+ String patternString = ((patternStringPrefix == null) || (patternStringPrefix.equals(""))) ? "" : patternStringPrefix + ":";
+ patternString += patternInfoLocalPart;
+
+ // now match the portInfoQName to the user pattern
+ // But first, convert the user pattern to a regular expression. Remember, the only non-QName character allowed is "*", which
+ // is a wildcard, with obvious restrictions on what characters can match (for example, a .java filename cannot contain perentheses).
+ // We'll just use part of the above reg ex to form the appropriate restrictions on the user-specified "*" character:
+ Pattern userp = Pattern.compile(patternString.replace("*", "(\\w|\\.|-|_)*"));
+ Matcher userm = userp.matcher(portInfoString);
+ boolean match = userm.matches();
+ if (log.isDebugEnabled()) {
+ if (!match) {
+ log.debug("Pattern match failed: \"" + portInfoString + "\" does not match \"" + patternString + "\"");
+ } else {
+ log.debug("Pattern match succeeded: \"" + portInfoString + "\" matches \"" + patternString + "\"");
+ }
+ }
+ return match;
+
+ }
+
+ /**
+ * Determine if the indicated qname matches the pattern
+ * @param qName
+ * @param pattern
+ * @return
+ */
+ private static boolean doesPatternMatch_Official(QName qName, QName pattern) {
+ if (log.isDebugEnabled()) {
+ log.debug("entry pattern=" + pattern + " qname=" + qName);
+ }
+ if (pattern == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("Successful Match: Pattern is null");
+ }
+ return true;
+ }
+
+ // Do a pattern match on the local part of the qname.
+ String patternLocalPart = pattern.getLocalPart();
+ String localPart = qName.getLocalPart();
+
+ // Replace the wildcard (*) references in the QName with an appropriate regular expression.
+ String regEx = patternLocalPart.replace("*", "(\\w|\\.|-|_)*");
+ Pattern userp = Pattern.compile(regEx);
+ Matcher userm = userp.matcher(localPart);
+ boolean match = userm.matches();
+ if (!match) {
+ if (log.isDebugEnabled()) {
+ log.debug("No Match: The local name does not match the regex pattern: " + regEx);
+ }
+ return false;
+ }
+
+ // Now do the matching with the namespace.
+ // If the entire pattern is a wildcard (*), then all namespaces are acceptable.
+ // For example:
+ // <port-name-pattern>*</port-name-pattern>
+ //
+ // In such cases, the assumption is that the pattern namespace will be empty.
+ String patternNamespace = pattern.getNamespaceURI();
+ String namespace = qName.getNamespaceURI();
+ if (patternNamespace.length() == 0) { // By definition, a namespace will never be null.
+ if (log.isDebugEnabled()) {
+ log.debug("Successful Match: The local name matches and the pattern namespace is empty.");
+ }
+ return true;
+ }
+
+ // If a namespace is specified, it will be done via a prefix. For example:
+ // <port-name-pattern>p:MyPortName</port-name-pattern>
+ // Thus according to the current pattern structure there is no way to
+ // specify wildcards for the namespace portion of the match
+ if (patternNamespace.equals(namespace)) {
+ if (log.isDebugEnabled()) {
+ log.debug("Successful Match: The local names and namespaces match.");
+ }
+ return true;
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("No Match");
+ }
+ return false;
}
private static void validatePattern(QName pattern) {
+ if (pattern == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("The pattern qname is null. This is accepted and interpretted as a wildcard");
+ }
+ return;
+ }
String patternStringPrefix = pattern.getPrefix();
String patternInfoLocalPart = pattern.getLocalPart();
String patternString = ((patternStringPrefix == null) || (patternStringPrefix.equals(""))) ? "" : patternStringPrefix + ":";
Modified: webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/HandlerChainProcessorTests.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/HandlerChainProcessorTests.java?rev=786055&r1=786054&r2=786055&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/HandlerChainProcessorTests.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/HandlerChainProcessorTests.java Thu Jun 18 13:28:49 2009
@@ -25,6 +25,7 @@
import org.apache.axis2.jaxws.message.Protocol;
import org.apache.axis2.jaxws.registry.FactoryRegistry;
+import javax.xml.namespace.QName;
import javax.xml.ws.ProtocolException;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.LogicalHandler;
@@ -863,6 +864,66 @@
assertNotNull(factory);
assertNotNull(factory.createHandlerInvoker(new MessageContext()));
}
+
+ /**
+ * Test the pattern match algorithm in BaseHanderResolverImpl
+ */
+ public void testPattenMatch() {
+
+ QName qName = new QName("http://mysample", "MyName", "prefix");
+ QName pattern = new QName("http://mysample", "MyName");
+
+ // Test direct match
+ boolean match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ // Test partial localName match (post)
+ pattern = new QName("http://mysample", "My*");
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ // Test partial localName match (pre)
+ pattern = new QName("http://mysample", "*Name");
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ // Test partial localName match (inner)
+ pattern = new QName("http://mysample", "M*e");
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ // Test full wildcard on localName
+ pattern = new QName("http://mysample", "*");
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ // Test full wildcard
+ pattern = new QName("", "*");
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ // Negative Test (not supposed to match prefix)
+ pattern = new QName("prefix", "*");
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is not expected to match the pattern " + pattern, !match);
+
+
+ // Test partial localName match (pre) and no namespace
+ pattern = new QName("", "*Name");
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ // Test partial localName match (inner) and no namespace
+ pattern = new QName("", "M*e");
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ // Test null pattern...for legacy reasons this is accepted and interpretted as a wildcard
+ pattern = null;
+ match = HandlerResolverImpl.doesPatternMatch(qName, pattern);
+ assertTrue("The qName " + qName + " is expected to match the pattern " + pattern, match);
+
+ }
private class SOAPHandler1 implements SOAPHandler<SOAPMessageContext> {