You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ff...@apache.org on 2009/04/13 05:40:16 UTC
svn commit: r764354 - in
/servicemix/components/shared-libraries/trunk/servicemix-common/src:
main/java/org/apache/servicemix/common/ResolvedEndpoint.java
test/java/org/apache/servicemix/common/ResolveEndpointTest.java
Author: ffang
Date: Mon Apr 13 03:40:15 2009
New Revision: 764354
URL: http://svn.apache.org/viewvc?rev=764354&view=rev
Log:
[SM-1835]whitespace interfering with ResolvedEndpoint.resolveEndpoint()
Added:
servicemix/components/shared-libraries/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/ResolveEndpointTest.java (with props)
Modified:
servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ResolvedEndpoint.java
Modified: servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ResolvedEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ResolvedEndpoint.java?rev=764354&r1=764353&r2=764354&view=diff
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ResolvedEndpoint.java (original)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/main/java/org/apache/servicemix/common/ResolvedEndpoint.java Mon Apr 13 03:40:15 2009
@@ -73,33 +73,43 @@
}
public static ServiceEndpoint resolveEndpoint(DocumentFragment epr, QName elementName, QName serviceName, String uriPrefix) {
- if (epr.getChildNodes().getLength() == 1) {
- Node child = epr.getFirstChild();
- if (child instanceof Element) {
- Element elem = (Element) child;
- String nsUri = elem.getNamespaceURI();
- String name = elem.getLocalName();
- // Check simple endpoints
- if (elementName.getNamespaceURI().equals(nsUri) && elementName.getLocalPart().equals(name)) {
- return new ResolvedEndpoint(epr, DOMUtil.getElementText(elem), serviceName);
- // Check WSA endpoints
- }
- else {
- NodeList nl = elem.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
- if (nl.getLength() == 1) {
- Element address = (Element) nl.item(0);
- String uri = DOMUtil.getElementText(address);
- if (uri != null && uriPrefix != null) {
- uri = uri.trim();
- if (uri.startsWith(uriPrefix)) {
- return new ResolvedEndpoint(epr, uri, serviceName);
+
+ int elementCount = 0;
+ ServiceEndpoint resolvedEndpoint = null;
+ for(Node child = epr.getFirstChild(); child != null; child = child.getNextSibling()) {
+ if (child instanceof Element) {
+ elementCount++;
+ if (elementCount==1)
+ {
+ Element elem = (Element) child;
+ String nsUri = elem.getNamespaceURI();
+ String name = elem.getLocalName();
+ // Check simple endpoints
+ if (elementName.getNamespaceURI().equals(nsUri) && elementName.getLocalPart().equals(name)) {
+ resolvedEndpoint = new ResolvedEndpoint(epr, DOMUtil.getElementText(elem), serviceName);
+ // Check WSA endpoints
+ }
+ else {
+ NodeList nl = elem.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
+ if (nl.getLength() == 1) {
+ Element address = (Element) nl.item(0);
+ String uri = DOMUtil.getElementText(address);
+ if (uri != null && uriPrefix != null) {
+ uri = uri.trim();
+ if (uri.startsWith(uriPrefix)) {
+ resolvedEndpoint = new ResolvedEndpoint(epr, uri, serviceName);
+ }
}
}
}
- }
- }
- }
- return null;
+
+ }
+ else
+ // stop searching if an element was already found
+ return null;
+ }
+ }
+ return resolvedEndpoint;
}
}
Added: servicemix/components/shared-libraries/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/ResolveEndpointTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/shared-libraries/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/ResolveEndpointTest.java?rev=764354&view=auto
==============================================================================
--- servicemix/components/shared-libraries/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/ResolveEndpointTest.java (added)
+++ servicemix/components/shared-libraries/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/ResolveEndpointTest.java Mon Apr 13 03:40:15 2009
@@ -0,0 +1,54 @@
+package org.apache.servicemix.common;
+
+import java.io.StringReader;
+
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import junit.framework.TestCase;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import org.xml.sax.InputSource;
+
+public class ResolveEndpointTest extends TestCase {
+
+ public void testResolveEndpoint_noWhitespace() throws Exception {
+ DocumentFragment fragment = createFragment();
+ ServiceEndpoint endpoint = ResolvedEndpoint.resolveEndpoint(fragment, new QName("urn:test", "elementName"), new QName("urn:test", "serviceName"), "http:");
+ assertNotNull("failed to parse wsa endpoint", endpoint);
+ }
+
+ public void testResolveEndpoint_hasWhitespace() throws Exception {
+ DocumentFragment fragment = createFragment();
+ // add some whitespace
+ Node node = fragment.getOwnerDocument().createTextNode("\n");
+ fragment.insertBefore(node, fragment.getFirstChild());
+ ServiceEndpoint endpoint = ResolvedEndpoint.resolveEndpoint(fragment, new QName("urn:test", "elementName"), new QName("urn:test", "serviceName"), "http:");
+ assertNotNull("failed to resolve an endpoint with whitespace", endpoint);
+ }
+
+ public void testResolveEndpoint_multipleElements() throws Exception {
+ DocumentFragment fragment = createFragment();
+ // add multiple elements
+ Node clone = fragment.getFirstChild().cloneNode(true);
+ fragment.appendChild(clone);
+ ServiceEndpoint endpoint = ResolvedEndpoint.resolveEndpoint(fragment, new QName("urn:test", "elementName"), new QName("urn:test", "serviceName"), "http:");
+ assertNull("should have rejected fragment since it had multiple elements", endpoint);
+ }
+
+ private DocumentFragment createFragment() throws Exception {
+ String xml = "<wsa:EndpointReference xmlns:wsa='http://www.w3.org/2005/08/addressing'>" +
+ "<wsa:Address>http://example.org/service/MyService</wsa:Address>" +
+ "</wsa:EndpointReference>";
+
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(true);
+ Document epr = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
+ DocumentFragment fragment = epr.createDocumentFragment();
+ fragment.appendChild(epr.getFirstChild());
+ return fragment;
+ }
+}
Propchange: servicemix/components/shared-libraries/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/ResolveEndpointTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: servicemix/components/shared-libraries/trunk/servicemix-common/src/test/java/org/apache/servicemix/common/ResolveEndpointTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date