[jboss-svn-commits] JBL Code SVN: r6487 - labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Sep 30 19:08:27 EDT 2006
Author: mark.little at jboss.com
Date: 2006-09-30 19:08:25 -0400 (Sat, 30 Sep 2006)
New Revision: 6487
Added:
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/CallHelper.java
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/PortReferenceHelper.java
Log:
Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/CallHelper.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/CallHelper.java 2006-09-30 23:02:16 UTC (rev 6486)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/CallHelper.java 2006-09-30 23:08:25 UTC (rev 6487)
@@ -0,0 +1,224 @@
+package org.jboss.internal.soa.esb.addressing;
+
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author mark.little at jboss.com
+ */
+
+import org.jboss.internal.soa.esb.addressing.PortReferenceHelper;
+
+import org.jboss.soa.esb.addressing.Call;
+import org.jboss.soa.esb.addressing.XMLUtil;
+import org.jboss.soa.esb.addressing.EPR;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * A call represents an exchange pattern for this message. It is built up as the message
+ * flows through the ESB and identifies where the message should go, along with any
+ * routing information for faults, replies etc.
+ */
+
+import java.net.URI;
+import java.security.InvalidParameterException;
+
+public class CallHelper
+{
+
+ public static Element toXML (Call call, Document doc, Element header)
+ {
+ if (call == null)
+ throw new InvalidParameterException();
+
+ try
+ {
+ if (call.getTo() != null)
+ {
+ Element toElement = doc.createElementNS(XMLUtil.WSA_NAMESPACE_URI, XMLUtil.WSA_PREFIX+":"+XMLUtil.TO_TAG);
+ PortReferenceHelper.toXML(header, doc, toElement, call.getTo().getAddr(), true);
+ header.appendChild(toElement);
+ }
+
+ if (call.getFrom() != null)
+ {
+ Element fromElement = doc.createElementNS(XMLUtil.WSA_NAMESPACE_URI, XMLUtil.WSA_PREFIX+":"+XMLUtil.FROM_TAG);
+ PortReferenceHelper.toXML(header, doc, fromElement, call.getFrom().getAddr(), false);
+ header.appendChild(fromElement);
+ }
+
+ if (call.getReplyTo() != null)
+ {
+ Element replyToElement = doc.createElementNS(XMLUtil.WSA_NAMESPACE_URI, XMLUtil.WSA_PREFIX+":"+XMLUtil.REPLY_TO_TAG);
+ PortReferenceHelper.toXML(header, doc, replyToElement, call.getReplyTo().getAddr(), false);
+ header.appendChild(replyToElement);
+ }
+
+ if (call.getRelatesTo() != null)
+ {
+ Element relatesToElement = doc.createElementNS(XMLUtil.WSA_NAMESPACE_URI, XMLUtil.WSA_PREFIX+":"+XMLUtil.RELATES_TO_TAG);
+ relatesToElement.setNodeValue(call.getRelatesTo().toString());
+ header.appendChild(relatesToElement);
+ }
+
+ if (call.getFaultTo() != null)
+ {
+ Element faultToElement = doc.createElementNS(XMLUtil.WSA_NAMESPACE_URI, XMLUtil.WSA_PREFIX+":"+XMLUtil.FAULT_TO_TAG);
+ PortReferenceHelper.toXML(header, doc, faultToElement, call.getFaultTo().getAddr(), false);
+ header.appendChild(faultToElement);
+ }
+
+ if (call.getAction() != null)
+ {
+ Element actionElement = doc.createElementNS(XMLUtil.WSA_NAMESPACE_URI, XMLUtil.WSA_PREFIX+":"+XMLUtil.ACTION_TAG);
+ actionElement.setNodeValue(call.getAction().toString());
+ header.appendChild(actionElement);
+ }
+
+ if (call.getMessageID() != null)
+ {
+ Element messageIDElement = doc.createElementNS(XMLUtil.WSA_NAMESPACE_URI, XMLUtil.WSA_PREFIX+":"+XMLUtil.MESSAGE_IDENTIFIER_TAG);
+ messageIDElement.setNodeValue(call.getMessageID().toString());
+ header.appendChild(messageIDElement);
+ }
+
+ return header;
+ }
+ catch (Exception ex)
+ {
+ // TODO improve error handling.
+
+ ex.printStackTrace();
+
+ throw new InvalidParameterException(ex.toString());
+ }
+ }
+
+ public static Call fromXML (Element header)
+ {
+ Call call = null;
+
+ try
+ {
+ call = new Call();
+
+ NodeList nl = header.getChildNodes();
+
+ for (int i = 0; i < nl.getLength(); i++)
+ {
+ Node n = nl.item(i);
+
+ if (n.getNodeName().equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.TO_TAG))
+ {
+ try
+ {
+ call.setTo(new EPR(PortReferenceHelper.fromXML((Element) n, true)));
+ }
+ catch (Exception ex)
+ {
+
+ }
+ }
+
+ if (n.getNodeName().equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.FROM_TAG))
+ {
+ try
+ {
+ call.setFrom(new EPR(PortReferenceHelper.fromXML((Element) n, false)));
+ }
+ catch (Exception ex)
+ {
+
+ }
+ }
+
+ if (n.getNodeName().equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.REPLY_TO_TAG))
+ {
+ try
+ {
+ call.setReplyTo(new EPR(PortReferenceHelper.fromXML((Element) n, false)));
+ }
+ catch (Exception ex)
+ {
+
+ }
+ }
+
+ if (n.getNodeName().equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.RELATES_TO_TAG))
+ {
+ try
+ {
+ call.setRelatesTo(new URI(n.getNodeValue()));
+ }
+ catch (Exception ex)
+ {
+
+ }
+ }
+
+ if (n.getNodeName().equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.FAULT_TO_TAG))
+ {
+ try
+ {
+ call.setFaultTo(new EPR(PortReferenceHelper.fromXML((Element) n, false)));
+ }
+ catch (Exception ex)
+ {
+
+ }
+ }
+
+ if (n.getNodeName().equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.ACTION_TAG))
+ {
+ try
+ {
+ call.setAction(new URI(n.getNodeValue()));
+ }
+ catch (Exception ex)
+ {
+
+ }
+ }
+
+ if (n.getNodeName().equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.MESSAGE_IDENTIFIER_TAG))
+ {
+ try
+ {
+ call.setMessageID(new URI(n.getNodeValue()));
+ }
+ catch (Exception ex)
+ {
+
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ // TODO error checking!!
+
+ ex.printStackTrace();
+ }
+
+ return call;
+ }
+
+}
\ No newline at end of file
Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/PortReferenceHelper.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/PortReferenceHelper.java 2006-09-30 23:02:16 UTC (rev 6486)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/addressing/PortReferenceHelper.java 2006-09-30 23:08:25 UTC (rev 6487)
@@ -0,0 +1,372 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author mark.little at jboss.com
+ */
+
+/*
+ * Copyright (c) 2002, 2003, Arjuna Technologies Limited.
+ *
+ * PortReferenceHelper.java
+ */
+
+package org.jboss.internal.soa.esb.addressing;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+import org.jboss.soa.esb.addressing.PortReference;
+import org.jboss.soa.esb.addressing.XMLUtil;
+import org.jboss.soa.esb.MarshalException;
+
+/*
+ * This should pack the PortReference into an ERP.
+ *
+ * It would be better if PortReference had a packToXML and unpackFromXML.
+ *
+ * This needs rewriting after the interoperability workshop!
+ */
+
+public class PortReferenceHelper {
+ /**
+ * WS-Addressing is broken in that the To field is a URI, even if the From
+ * field was an EPR! You have to use the EPR bits separately. So, the
+ * toField is used to indicate whether we are packing this address as a To
+ * field (when we need to treat it differently) or as an EPR.
+ */
+
+ /** ************************************************** */
+ public static void toXML(org.w3c.dom.Element header,
+ org.w3c.dom.Document document,
+ org.w3c.dom.Element portReferenceElement,
+ PortReference portReference, boolean toField)
+ throws MarshalException {
+ try {
+ if (!toField) {
+ Element addressElement = document.createElementNS(
+ XMLUtil.WSA_NAMESPACE_URI, XMLUtil.ADDRESS_TAG);
+ addressElement.setPrefix(XMLUtil.WSA_PREFIX);
+ addressElement.appendChild(document
+ .createTextNode(portReference.getAddress()));
+ portReferenceElement.appendChild(addressElement);
+ } else {
+ portReferenceElement.appendChild(document
+ .createTextNode(portReference.getAddress()));
+ }
+
+ Iterator extensions = portReference.getExtensions();
+ Element referenceProperties = null;
+
+ while (extensions.hasNext()) {
+ PortReference.Extension extension = (PortReference.Extension) extensions
+ .next();
+
+ /*
+ * WS-Addressing is broken in that the To field is a URI, even
+ * if the From field was an EPR! You have to use the EPR bits
+ * separately. Doh!
+ */
+
+ if (!toField) {
+ if (referenceProperties == null) {
+ referenceProperties = document.createElementNS(
+ XMLUtil.WSA_NAMESPACE_URI,
+ XMLUtil.REFERENCE_PROPERTIES_TAG);
+ referenceProperties.setPrefix(XMLUtil.WSA_PREFIX);
+ portReferenceElement.appendChild(referenceProperties);
+ }
+ } else
+ referenceProperties = header;
+
+ extensionToXML(referenceProperties, document, extension);
+ }
+ } catch (Exception exception) {
+ exception.printStackTrace();
+ throw new MarshalException("Marshal failure: " + exception);
+ }
+ }
+
+ /** **************************** */
+ public static PortReference fromXML(org.w3c.dom.Element portReferenceElement, boolean toField)
+ throws MarshalException {
+ PortReference portReference = new PortReference();;
+
+ if (toField)
+ portReference.setAddress(portReferenceElement.getTextContent());
+
+ org.w3c.dom.NodeList elements = portReferenceElement.getChildNodes();
+
+ for (int i = 1; i < elements.getLength(); i++) {
+ final Object extensionObject = elements.item(i);
+
+ if (extensionObject instanceof Element) {
+ int parentNodeType = PortReference.Extension.NEITHER;
+ Element extensionElement = (Element) extensionObject;
+ NodeList children = extensionElement.getChildNodes();
+ String parentName = extensionElement.getNodeName();
+ boolean haveChildren = false;
+
+ if (parentName.equals(XMLUtil.REFERENCE_PROPERTIES_TAG))
+ parentNodeType = PortReference.Extension.REFERENCE_PROPERTIES;
+ else {
+ if (parentName.equals(XMLUtil.REFERENCE_PARAMETERS_TAG))
+ parentNodeType = PortReference.Extension.REFERENCE_PARAMETERS;
+ else
+ {
+ if (!toField && parentName.equals(XMLUtil.WSA_PREFIX+":"+XMLUtil.ADDRESS_TAG))
+ {
+ portReference.setAddress(extensionElement.getTextContent());
+ }
+ }
+ }
+
+ final int numChildren = children.getLength();
+ for (int count = 0; count < numChildren; count++) {
+ final Object childObject = children.item(count);
+
+ if (childObject instanceof Element) {
+ Element childElement = (Element) childObject;
+ portReference
+ .addExtension(childrenFromXML(childElement));
+ haveChildren = true;
+ }
+ }
+
+ if (!haveChildren) {
+ PortReference.Extension ext = new PortReference.Extension(
+ extensionElement.getNodeName(), null, null,
+ extensionElement.getNodeValue(), parentNodeType);
+
+ portReference.addExtension(ext);
+
+ final NamedNodeMap attrs = extensionElement.getAttributes();
+ final int numAttrs = attrs.getLength();
+ if (numAttrs > 0) {
+ final HashMap p = new HashMap();
+ for (int count = 0; count < numAttrs; count++) {
+ final Attr attr = (Attr) attrs.item(count);
+ if (!"http://www.w3.org/2000/xmlns/".equals(attr
+ .getNamespaceURI())) {
+ final ArjunaName name = generateName(attr);
+ p.put(name, attr.getValue());
+ }
+ }
+
+ ext.addAttributes(p);
+ }
+ }
+ }
+ }
+
+ return portReference;
+ }
+
+ private final static PortReference.Extension childrenFromXML(
+ Element childRoot) {
+ final NodeList children = childRoot.getChildNodes();
+ final int numChildNodes = children.getLength();
+
+ final PortReference.Extension extension;
+ if (numChildNodes > 0) {
+ Object childObject = children.item(0);
+ if (childObject instanceof Element) {
+ extension = new PortReference.Extension(childRoot
+ .getNodeName(), childRoot.getPrefix(), childRoot
+ .getNamespaceURI());
+ for (int count = 1; count < numChildNodes; count++) {
+ extension.addChild(childrenFromXML((Element) childObject));
+ childObject = children.item(count);
+ }
+ } else {
+ extension = new PortReference.Extension(childRoot
+ .getNodeName(), childRoot.getPrefix(), childRoot
+ .getNamespaceURI(), ((Node) childObject).getNodeValue());
+ }
+ } else {
+ extension = new PortReference.Extension(childRoot.getNodeName(),
+ childRoot.getPrefix(), childRoot.getNamespaceURI(),
+ childRoot.getNodeValue());
+ }
+
+ final NamedNodeMap attrs = childRoot.getAttributes();
+ final int numAttrs = attrs.getLength();
+ if (numAttrs > 0) {
+ final HashMap p = new HashMap();
+ for (int count = 0; count < numAttrs; count++) {
+ final Attr attr = (Attr) attrs.item(count);
+ if (!"http://www.w3.org/2000/xmlns/".equals(attr
+ .getNamespaceURI())) {
+ final ArjunaName name = generateName(attr);
+ p.put(name, attr.getValue());
+ }
+ }
+
+ extension.addAttributes(p);
+ }
+ return extension;
+ }
+
+ private final static Element extensionToXML(Element packInto,
+ org.w3c.dom.Document document, PortReference.Extension toPack)
+ throws MarshalException {
+ final String uri = toPack.getURI();
+ final Element element;
+ if (uri == null) {
+ element = document.createElement(toPack.getTag());
+ } else {
+ final String prefix = toPack.getPrefix();
+ element = document.createElementNS(uri, toPack.getTag());
+ element.setPrefix(prefix);
+ element.setAttributeNS(XMLUtil.XMLNS_URI, XMLUtil.XMLNS_PREFIX
+ + toPack.getPrefix(), uri);
+ }
+ packInto.appendChild(element);
+
+ if (toPack.getValue() != null) {
+ final Text text = document.createTextNode(toPack.getValue());
+ element.appendChild(text);
+ }
+
+ LinkedList extensions = toPack.getChildren();
+
+ if (extensions != null) {
+ for (int i = 0; i < extensions.size(); i++) {
+ PortReference.Extension ext = (PortReference.Extension) extensions
+ .get(i);
+
+ extensionToXML(element, document, ext);
+ }
+ }
+
+ HashMap attrs = toPack.getAttributes();
+
+ if (attrs != null) {
+ Iterator names = attrs.entrySet().iterator();
+
+ while (names.hasNext()) {
+ Map.Entry entry = (Map.Entry) names.next();
+ ArjunaName name = (ArjunaName) entry.getKey();
+ final String value = (String) entry.getValue();
+ final String attrURI = name.toString();
+ if (attrURI != null) {
+ element.setAttributeNS(attrURI, name.getQualifiedName(),
+ value);
+ } else {
+ element.setAttribute(name.getQualifiedName(), value);
+ }
+ }
+ }
+
+ return element;
+ }
+
+ /**
+ * Generate a name based on the attribute.
+ *
+ * @param attr
+ * The current attribute.
+ * @return The name
+ */
+ private static ArjunaName generateName(final Attr attr) {
+ final String localName = attr.getNodeName();
+ final String uri = attr.getNamespaceURI();
+ final String prefix = attr.getPrefix();
+
+ return new ArjunaName(uri, prefix, localName);
+ }
+
+ private static class ArjunaName
+ {
+ /**
+ * The uri.
+ */
+ private final String uri;
+
+ /**
+ * The prefix.
+ */
+ private final String prefix;
+
+ /**
+ * The local name.
+ */
+ private final String localName;
+
+ /**
+ * Construct the name.
+ *
+ * @param uri
+ * The uri.
+ * @param prefix
+ * The prefix.
+ * @param localName
+ * The local name.
+ */
+ public ArjunaName(final String uri, final String prefix,
+ final String localName) {
+ this.uri = uri;
+ this.prefix = prefix;
+ this.localName = localName;
+ }
+
+ /**
+ * Get the uri.
+ *
+ * @return the uri.
+ */
+ public String getURI() {
+ return uri;
+ }
+
+ /**
+ * Get the prefix.
+ *
+ * @return the prefix.
+ */
+ public String getPrefix() {
+ return prefix;
+ }
+
+ /**
+ * Get the local name.
+ *
+ * @return the local name.
+ */
+ public String getLocalName() {
+ return localName;
+ }
+
+ /**
+ * Get the qualified name.
+ *
+ * @return the qualified name.
+ */
+ public String getQualifiedName() {
+ return (prefix == null ? localName : prefix + ":" + localName);
+ }
+ }
+}
More information about the jboss-svn-commits
mailing list