[jboss-svn-commits] JBL Code SVN: r6489 - labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Sep 30 19:09:08 EDT 2006


Author: mark.little at jboss.com
Date: 2006-09-30 19:09:02 -0400 (Sat, 30 Sep 2006)
New Revision: 6489

Added:
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/AttachmentImpl.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/BodyImpl.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/ContextImpl.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/FaultImpl.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/HeaderImpl.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/MessageImpl.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/PropertiesImpl.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/XMLMessagePlugin.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/XMLUtil.java
Log:


Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/AttachmentImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/AttachmentImpl.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/AttachmentImpl.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,270 @@
+/*
+ * 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
+ */
+
+package org.jboss.internal.soa.esb.message.format.xml;
+
+import org.jboss.internal.soa.esb.thirdparty.Base64;
+import org.jboss.soa.esb.message.Attachment;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Map;
+import java.io.Serializable;
+
+/**
+ * Messages may contain attachments that do not appear in the main payload body.
+ * For example, binary document formats, zip files etc.
+ * 
+ * @author Mark Little
+ */
+
+public class AttachmentImpl implements Attachment, java.io.Serializable
+{
+	public static final String ATTACHMENT_TAG = "Attachment";
+	public static final String NAME_ATTR = "name";
+    public static final String NAMED_TAG = "Named";
+    public static final String UNNAMED_TAG = "UnNamed";
+
+	private static final long serialVersionUID = 0x0;
+
+	public Object get(String name)
+	{
+		return _table.get(name);
+	}
+
+	public Object put(String name, Object value)
+	{
+		if (value instanceof Serializable)
+			return _table.put(name, (Serializable) value);
+		throw new IllegalArgumentException("value must be Serializable");
+	}
+
+	public Object remove(String name)
+	{
+		return _table.remove(name);
+	}
+
+	public String[] getNames()
+	{
+		return _table.keySet().toArray(new String[_table.size()]);
+	}
+
+	public Object itemAt(int index) throws IndexOutOfBoundsException
+	{
+		return _list.get(index);
+	}
+
+	public Object removeItemAt(int index) throws IndexOutOfBoundsException
+	{
+		return _list.remove(index);
+	}
+
+	public Object replaceItemAt(int index, Object value)
+			throws IndexOutOfBoundsException
+	{
+		if (value instanceof Serializable)
+			return _list.set(index, (Serializable) value);
+		throw new IllegalArgumentException("value must be Serializable");
+	}
+
+	public void addItem(Object value)
+	{
+		if (value instanceof Serializable)
+			_list.add((Serializable) value);
+		else
+			throw new IllegalArgumentException("value must be Serializable");
+	}
+
+	public void addItemAt(int index, Object value)
+			throws IndexOutOfBoundsException
+	{
+		if (value instanceof Serializable)
+			_list.add(index, (Serializable) value);
+		else
+			throw new IllegalArgumentException("value must be Serializable");
+	}
+
+	public int getNamedCount()
+	{
+		return _table.size();
+	}
+
+	public int getUnnamedCount()
+	{
+		return _list.size();
+	}
+
+	public String toString()
+	{
+		return new StringBuilder().append("Attachment - Named:").append(
+				_table.toString()).append(" Unnamed:").append(_list.toString())
+				.toString();
+	}
+
+	/**
+	 * toXML(elem) - Will build a child element with appropriate values and
+	 * append it to arg0
+	 * 
+	 * @param elem
+	 *            Element - where to add 'this' as a child node
+	 * @return Element - 'this' as the added Element, or <null> if no
+	 *         properties in table and nothing was appended to arg0
+	 * @see XMLUtil.ATTACHMENT_TAG
+	 */
+	public Element toXML(Element elem)
+	{
+		if (_table.size() < 1 && _list.size() < 1)
+			return null;
+
+		Document doc = elem.getOwnerDocument();
+		Element thisElement = doc.createElement(ATTACHMENT_TAG);
+
+		listToXml(doc, thisElement);
+		tableToXml(doc, thisElement);
+
+		elem.appendChild(thisElement);
+		return thisElement;
+	}
+
+	private void tableToXml(Document doc, Element elem)
+	{
+		for (Map.Entry<String, Serializable> oCurr : _table.entrySet())
+		{
+			Element named = doc.createElement(NAMED_TAG);
+			named.setAttribute(NAME_ATTR, oCurr.getKey());
+			named.appendChild(doc.createCDATASection(Base64.encodeObject(oCurr
+					.getValue())));
+			elem.appendChild(named);
+		}
+	}
+
+	private void listToXml(Document doc, Element elem)
+	{
+		for (Serializable oCurr : _list)
+		{
+			Element anonymous = doc.createElement(UNNAMED_TAG);
+			anonymous.appendChild(doc.createCDATASection(Base64
+					.encodeObject(oCurr)));
+			elem.appendChild(anonymous);
+		}
+	}
+
+	/**
+	 * fromXml(elem) - Populate properties found in appropriate child element
+	 * 
+	 * @see XMLUtil.ATTACHMENT_TAG
+	 * @param elem -
+	 *            Element where to look for child nodes
+	 */
+	public void fromXML(Element elem)
+	{
+		_table.clear();
+		_list.clear();
+
+		NodeList NL = elem.getElementsByTagName(ATTACHMENT_TAG);
+		for (int i1 = 0; i1 < NL.getLength(); i1++)
+		{
+			Node oCurr = NL.item(i1);
+			if ((oCurr instanceof Element))
+			{
+				listFromXml((Element) oCurr);
+				tableFromXml((Element) oCurr);
+			}
+		}
+	}
+
+	private void listFromXml(Element elem)
+	{
+		NodeList anonymous = elem.getElementsByTagName(UNNAMED_TAG);
+		for (int i1 = 0; i1 < anonymous.getLength(); i1++)
+		{
+			Node oCurr = anonymous.item(i1);
+			if (oCurr instanceof Element)
+			{
+				CDATASection cdata = (CDATASection) oCurr.getFirstChild();
+				Object value = Base64.decodeToObject(cdata.getWholeText());
+				_list.add((Serializable) value);
+			}
+		}
+	}
+
+	private void tableFromXml(Element elem)
+	{
+		NodeList named = elem.getElementsByTagName(NAMED_TAG);
+		for (int i1 = 0; i1 < named.getLength(); i1++)
+		{
+			Node oCurr = named.item(i1);
+			if (oCurr instanceof Element)
+			{
+				CDATASection cdata = (CDATASection) oCurr.getFirstChild();
+				String name = ((Element) oCurr).getAttribute(NAME_ATTR);
+				Object value = Base64.decodeToObject(cdata.getWholeText());
+				_table.put(name, (Serializable) value);
+			}
+		}
+	}
+
+	@Override
+	public boolean equals(Object arg)
+	{
+		if (! (arg instanceof Attachment))
+			return false;
+		Attachment other = (Attachment)arg;
+
+		if (other.getNamedCount() != _table.size())
+			return false;
+		for(Map.Entry<String,Serializable>oCurr : _table.entrySet())
+		{	
+			Object val = other.get(oCurr.getKey());
+			if (null==oCurr.getValue())
+				if (null==val)
+					continue;
+				else
+					return false;
+			if (!oCurr.getValue().equals(val))
+				return false;
+		}
+
+		if (other.getUnnamedCount() != _list.size())
+			return false;
+		int index = 0;
+		for (Serializable oCurr : _list)
+		{
+			Object val = other.itemAt(index++);
+			if (null==oCurr)
+				if (null==val)
+					continue;
+				else
+					return false;
+			if (!oCurr.equals(val))
+				return false;
+		}
+		return true;		
+	}
+
+	ArrayList<Serializable> _list = new ArrayList<Serializable>();
+	Hashtable<String, Serializable> _table = new Hashtable<String, Serializable>();
+}

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/BodyImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/BodyImpl.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/BodyImpl.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,216 @@
+package org.jboss.internal.soa.esb.message.format.xml;
+
+import java.io.Serializable;
+import java.lang.IllegalArgumentException;
+
+import org.jboss.soa.esb.message.Body;
+
+import org.jboss.internal.soa.esb.thirdparty.Base64;
+
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+/*
+ * 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
+ */
+
+/*
+ * A Body implementation that serializes to XML.
+ */
+
+public class BodyImpl implements Body
+{
+    public static final String BODY_TAG = "Body";
+	public static final String BYTES_TAG = "Bytes";
+	
+	public BodyImpl ()
+	{
+		_content = null;
+		_objects = new Hashtable<String, Object>();
+	}
+	
+	public void add (String name, Object value)
+	{
+		if ((name == null) || (value == null))
+			throw new IllegalArgumentException();
+		
+		if (value instanceof Serializable)
+		{
+			synchronized (_objects)
+			{
+				_objects.put(name, value);
+			}
+		}
+		else
+			throw new IllegalArgumentException("Object must be Serializable in this release.");
+	}
+	
+	public Object get (String name)
+	{
+		synchronized (_objects)
+		{
+			return _objects.get(name);
+		}
+	}
+	
+	public Object remove (String name)
+	{
+		synchronized (_objects)
+		{
+			return _objects.remove(name);
+		}
+	}
+	
+	public Element toXML (Element envelope)
+	{
+		Document doc = envelope.getOwnerDocument();
+		Element bodyElement = doc.createElement(BODY_TAG);
+		
+		envelope.appendChild(bodyElement);
+		
+		if (_content != null)
+		{
+			Element byteElement = doc.createElement(BYTES_TAG);
+			
+			byteElement.appendChild(doc.createCDATASection(Base64.encodeBytes(_content)));
+			
+			bodyElement.appendChild(byteElement);
+		}
+		
+		/*
+		 * This would normally be handled by an external adapter.
+		 */
+		
+		Enumeration<String> keys = _objects.keys();
+		
+		while (keys.hasMoreElements())
+		{
+			String key = keys.nextElement();
+			Object value = _objects.get(key);
+			
+			Element objElement = doc.createElement(key);
+			
+			// we already checked values are Serializable when they were added.
+			
+			objElement.appendChild(doc.createCDATASection(Base64.encodeObject((Serializable) value)));
+			
+			bodyElement.appendChild(objElement);
+		}
+		
+		
+		return bodyElement;
+	}
+	
+	public void fromXML (Element envelope)
+	{
+		NodeList nl = envelope.getChildNodes();
+		
+		for (int i = 0; i < nl.getLength(); i++)
+		{
+			/*
+			 * TODO
+			 * 
+			 * In the past, bugs in certain Dom implementations mean that getElementsByName
+			 * did not always work. Still the case? Plus this way is quicker.
+			 */
+			
+			if (nl.item(i).getNodeName().equals(BODY_TAG))
+			{
+				NodeList children = nl.item(i).getChildNodes();
+				
+				for (int j = 0; j < children.getLength(); j++)
+				{
+					Element child = (Element) children.item(j);
+					
+					// treat bytes specially.
+					CDATASection cdata = (CDATASection) child.getFirstChild();
+				
+					if (child.getNodeName().equals(BYTES_TAG))
+					{
+						_content = Base64.decode(cdata.getWholeText());
+					}
+					else
+					{
+						Object value = Base64.decodeToObject(cdata.getWholeText());
+						
+						_objects.put(child.getNodeName(), value);
+					}
+				}
+			}
+		}
+	}
+	
+	public void setContents (byte[] content)
+	{
+		_content = content;
+	}
+
+	public byte[] getContents ()
+	{
+		return _content;
+	}
+	
+	public void replace (Body b)
+	{
+		if (b == null)
+			throw new IllegalArgumentException();
+		
+		setContents(b.getContents());
+		
+		_objects = ((BodyImpl) b)._objects;
+	}
+	
+	public void merge (Body b)
+	{
+		if (b == null)
+			throw new IllegalArgumentException();
+		
+		byte[] toAdd = b.getContents();
+		
+		if ((toAdd != null) && (toAdd.length > 0))
+		{
+			if ((_content == null) || (_content.length == 0))
+			{
+				_content = toAdd;
+			}
+			else
+			{	
+				int newSize = _content.length + toAdd.length;
+				byte[] buffer = new byte[newSize];
+				
+				System.arraycopy(_content, 0, buffer, 0, _content.length);
+				System.arraycopy(toAdd, 0, buffer, _content.length, toAdd.length);
+				
+				_content = buffer;
+			}
+		}
+		
+		_objects.putAll(((BodyImpl) b)._objects);
+	}
+	
+	private byte[] _content;
+	private Hashtable<String, Object> _objects;
+	
+}
\ No newline at end of file

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/ContextImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/ContextImpl.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/ContextImpl.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,46 @@
+package org.jboss.internal.soa.esb.message.format.xml;
+
+import org.jboss.soa.esb.message.Context;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/*
+ * 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
+ */
+
+public class ContextImpl implements Context
+{
+    public static final String CONTEXT_TAG = "Context";
+    
+	public Element toXML (Element envelope)
+	{
+		Document doc = envelope.getOwnerDocument();
+		Element contextElement = doc.createElement(CONTEXT_TAG);
+		
+		envelope.appendChild(contextElement);
+		
+		return contextElement;
+	}
+	
+	public void fromXML (Element envelope)
+	{
+	}
+}
\ No newline at end of file

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/FaultImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/FaultImpl.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/FaultImpl.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,139 @@
+package org.jboss.internal.soa.esb.message.format.xml;
+
+import java.net.URI;
+
+import org.jboss.soa.esb.message.Fault;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+/*
+ * 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
+ */
+
+public class FaultImpl implements Fault
+{
+	public static final String FAULT_TAG = "Fault";
+
+	public static final String CODE_TAG = "Code";
+
+	public static final String REASON_TAG = "Reason";
+
+	public URI getCode()
+	{
+		return _code;
+	}
+
+	public void setCode(URI code)
+	{
+		_code = code;
+	}
+
+	public String getReason()
+	{
+		return _reason;
+	}
+
+	public void setReason(String reason)
+	{
+		_reason = reason;
+	}
+
+	public Element toXML(Element envelope)
+	{
+		Document doc = envelope.getOwnerDocument();
+		Element faultElement = doc.createElement(FAULT_TAG);
+
+		envelope.appendChild(faultElement);
+
+		if (_code != null)
+		{
+			//Attr codeElement = doc.createAttribute(CODE_TAG);
+
+			Element codeElement = doc.createElement(CODE_TAG);
+			Text content = doc.createTextNode(_code.toString());
+			
+			codeElement.appendChild(content);
+			
+			faultElement.appendChild(codeElement);
+			
+			//faultElement.setAttributeNode(codeElement);
+		}
+
+		if (_reason != null)
+		{
+			//Attr reasonElement = doc.createAttribute(REASON_TAG);
+
+			Element reasonElement = doc.createElement(REASON_TAG);
+			Text content = doc.createTextNode(_reason);
+			
+			reasonElement.appendChild(content);
+
+			faultElement.appendChild(reasonElement);
+			
+			//faultElement.setAttributeNode(reasonElement);
+		}
+
+		return faultElement;
+	}
+
+	public void fromXML (Element envelope)
+	{
+		NodeList nl = envelope.getChildNodes();
+
+		_code = null;
+		_reason = null;
+		
+		for (int i = 0; i < nl.getLength(); i++)
+		{
+			if (nl.item(i).getNodeName().equals(FAULT_TAG))
+			{
+				NodeList children = nl.item(i).getChildNodes();
+
+				for (int j = 0; j < children.getLength(); j++)
+				{
+					Element child = (Element) children.item(j);
+
+					if (child.getNodeName().equals(CODE_TAG))
+					{
+						try
+						{
+							_code = new URI(child.getFirstChild().getNodeValue());
+						}
+						catch (Exception ex)
+						{
+							ex.printStackTrace();
+						}
+					}
+
+					if (child.getNodeName().equals(REASON_TAG))
+						_reason = child.getFirstChild().getNodeValue();
+				}
+			}
+		}
+	}
+
+	private URI _code = null;
+
+	private String _reason = null;
+
+}
\ No newline at end of file

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/HeaderImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/HeaderImpl.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/HeaderImpl.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,104 @@
+package org.jboss.internal.soa.esb.message.format.xml;
+
+/*
+ * 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 java.lang.IllegalArgumentException;
+
+import org.jboss.internal.soa.esb.addressing.CallHelper;
+import org.jboss.soa.esb.message.Header;
+import org.jboss.soa.esb.addressing.Call;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * The message header. Contains such things as routing information.
+ */
+
+public class HeaderImpl implements Header
+{
+    public static final String HEADER_TAG = "Header";
+    
+	public HeaderImpl ()
+	{
+		_call = null;
+	}
+	
+	// TODO add other setters/getters for artibitrary attributes
+	
+	public Call getCall ()
+	{
+		return _call;
+	}
+	
+	public void setCall (Call call)
+	{
+		if (call == null)
+			throw new IllegalArgumentException();
+		
+		_call = call;
+	}
+	
+	public Element toXML (Element envelope)
+	{
+		Document doc = envelope.getOwnerDocument();
+		
+		// TODO remove MAGIC strings!!
+		
+		Element headerElement = doc.createElement(HEADER_TAG);
+		
+		envelope.appendChild(headerElement);
+		
+		if (_call != null)
+			return CallHelper.toXML(_call, doc, headerElement);
+		else
+			return headerElement;
+	}
+	
+	public void fromXML (Element envelope)
+	{
+		_call = new Call();
+		
+		NodeList nl = envelope.getChildNodes();
+		Element headerElement = null;
+		
+		for (int i = 0; i < nl.getLength(); i++)
+		{
+			Node n = nl.item(i);
+			
+			if (n.getNodeName().equals(HEADER_TAG))
+			{
+				headerElement = (Element) n;
+				break;
+			}
+		}
+		
+		// TODO error handling!!
+		
+		if (headerElement != null)
+			_call = CallHelper.fromXML(headerElement);
+	}
+	
+	private Call _call;
+}
\ No newline at end of file

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/MessageImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/MessageImpl.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/MessageImpl.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,198 @@
+/*
+ * 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
+ */
+package org.jboss.internal.soa.esb.message.format.xml;
+
+import org.jboss.soa.esb.message.Attachment;
+import org.jboss.soa.esb.message.Body;
+import org.jboss.soa.esb.message.Context;
+import org.jboss.soa.esb.message.Fault;
+import org.jboss.soa.esb.message.Header;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageType;
+import org.jboss.soa.esb.message.Properties;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import java.net.URI;
+
+
+/**
+ * This is the basic internal core message abstraction. A message consists of the following
+ * components:
+ * 
+ * Header: the header information contains information such as the destination EPR, the
+ * sender EPR, where the reply goes etc, i.e., general message-level functional information.
+ * Context: additional information to contextualise the message; for example, transaction or
+ * security data, the identity of the ultimate receiver, or HTTP-cookie like information.
+ * Body: the actual payload of the message.
+ * Fault: any fault information associated with the message.
+ * Attachment: any attachments associated with the message.
+ * Properties: any properties associated with the message.
+ * 
+ * Each message, once created, has a corresponding element for these 5 components. That element
+ * may be empty (<b>NOT NULL</b>). The object representing the element can then be used to act
+ * on the corresponding data item in the message.
+ * 
+ * @author Mark Little
+ *
+ */
+
+public class MessageImpl implements Message   // also implement XmlSerializable ?
+{
+    public static final String ENVELOPE_TAG = "Envelope";
+    
+	/**
+	 * @return get the header component of the message.
+	 */
+	
+	public Header getHeader ()
+	{
+		return _theHeader;
+	}
+	
+	/**
+	 * @return get the context component of the message.
+	 */
+	
+	public Context getContext ()
+	{
+		return _theContext;
+	}
+	
+	/**
+	 * @return get the body component of the message.
+	 */
+	
+	public Body getBody ()
+	{
+		return _theBody;
+	}
+	
+	/**
+	 * @return get any faults associated with the message. These should not
+	 * be application level faults, but comms level.
+	 */
+	
+	public Fault getFault ()
+	{
+		return _theFault;
+	}
+	
+	/**
+	 * @return get any message attachments.
+	 */
+	
+	public Attachment getAttachment ()
+	{
+		return _theAttachment;
+	}
+	
+	/**
+	 * @return the type of this message format.
+	 */
+	
+	public URI getType ()
+	{
+		return MessageType.JBOSS_XML;
+	}
+	/**
+	 * getProperties()
+	 * @return org.jboss.soa.esb.message.Properties - any message properties.
+	 */
+	public Properties getProperties() 
+	{
+		return _theProperties;
+	}
+	
+	// to/from XML here, rather than on individual elements
+	
+	public Document toXML (Document doc)
+	{
+		try
+		{		
+			// TODO remove magic strings!
+			
+			Element envelope = doc.createElement(ENVELOPE_TAG);
+
+			doc.appendChild(envelope);
+			
+			_theHeader.toXML(envelope);
+			_theContext.toXML(envelope);
+			_theBody.toXML(envelope);
+			_theFault.toXML(envelope);
+			_theAttachment.toXML(envelope);
+			_theProperties.toXML(envelope);
+			
+			return doc;
+		}
+		catch (Exception ex)
+		{
+			// TODO error handling
+			
+			ex.printStackTrace();
+			
+			return null;
+		}
+	}
+	
+	public void fromXML (Document doc)
+	{
+		try
+		{
+			NodeList nl = doc.getChildNodes();
+			Element envelope = null;
+			
+			for (int i = 0; i < nl.getLength(); i++)
+			{
+				Node n = nl.item(i);
+				
+				if (n.getNodeName().equals(ENVELOPE_TAG))
+				{
+					envelope = (Element) n;
+					break;
+				}
+			}
+
+			_theHeader.fromXML(envelope);
+			_theContext.fromXML(envelope);
+			_theBody.fromXML(envelope);
+			_theFault.fromXML(envelope);
+			_theAttachment.fromXML(envelope);
+			_theProperties.fromXML(envelope);
+		}
+		catch (Exception ex)
+		{
+			ex.printStackTrace();
+		}
+	}
+	
+	// TODO add equality operator(s)
+	
+	private HeaderImpl _theHeader = new HeaderImpl();
+	private ContextImpl _theContext = new ContextImpl();
+	private BodyImpl _theBody = new BodyImpl();
+	private FaultImpl _theFault = new FaultImpl();
+	private AttachmentImpl _theAttachment = new AttachmentImpl();
+	private PropertiesImpl _theProperties = new PropertiesImpl();
+}

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/PropertiesImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/PropertiesImpl.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/PropertiesImpl.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,161 @@
+/*
+ * 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 schifest at heuristica.com.ar
+ */
+package org.jboss.internal.soa.esb.message.format.xml;
+
+import org.jboss.soa.esb.message.Properties;
+import org.w3c.dom.*;
+import org.jboss.internal.soa.esb.thirdparty.Base64;
+
+import java.util.Map;
+import java.util.Hashtable;
+import java.io.Serializable;
+
+public class PropertiesImpl implements Properties
+{
+	public static final String PROPERTIES_TAG = "Properties";
+
+	public Object getProperty(String name)
+	{
+		return _table.get(name);
+	}
+
+	public Object getProperty(String name, Object defaultVal)
+	{
+		Object oRet = getProperty(name);
+		return (null == oRet) ? defaultVal : oRet;
+	}
+
+	public Object setProperty(String name, Object value)
+	{
+		if (value instanceof Serializable)
+			return _table.put(name, (Serializable) value);
+		else
+			throw new IllegalArgumentException("value must be XmlSerializable");
+	}
+
+	public Object remove(String name)
+	{
+		return _table.remove(name);
+	}
+
+	public int size() {return _table.size(); } 
+
+	public String[] getNames()
+	{
+		return _table.keySet().toArray(new String[_table.size()]);
+	}
+
+	/**
+	 * toXML(elem) - Will build a child element with appropriate values and
+	 * append it to arg0
+	 * 
+	 * @param elem
+	 *            Element - where to add 'this' as a child node
+	 * @return Element - 'this' as the added Element, or &lt;null&gt; if no
+	 *         properties in table and nothing was appended to arg0
+	 */
+	public Element toXML(Element elem)
+	{
+		if (_table.size() < 1)
+			return null;
+
+		Document doc = elem.getOwnerDocument();
+		Element thisElement = doc.createElement(PROPERTIES_TAG);
+
+		boolean bAdd = false;
+		for (Map.Entry<String, Serializable> oCurr : _table.entrySet())
+		{
+			Element oProp = doc.createElement(oCurr.getKey());
+			oProp.appendChild(doc.createCDATASection(Base64.encodeObject(oCurr
+					.getValue())));
+			thisElement.appendChild(oProp);
+			bAdd = true;
+		}
+		if (bAdd)
+		{
+			elem.appendChild(thisElement);
+			return thisElement;
+		}
+		else
+			return null;
+	}
+
+	/**
+	 * fromXml(elem) - Populate properties found in appropriate child element
+	 * 
+	 * @see XMLUtil.PROPERTIES_TAG
+	 * @param elem -
+	 *            Element where to look for child nodes
+	 */
+	public void fromXML(Element elem)
+	{
+		_table.clear();
+		
+		NodeList NL = elem.getElementsByTagName(PROPERTIES_TAG);
+		for (int i1 = 0; i1 < NL.getLength(); i1++)
+		{
+			Node oCurr = NL.item(i1);
+			if (!(oCurr instanceof Element))
+				continue;
+			NodeList props = oCurr.getChildNodes();
+			for (int i2 = 0; i2 < props.getLength(); i2++)
+			{
+				Node oProp = props.item(i2);
+				if (oProp instanceof Element)
+				{
+					CDATASection cdata = (CDATASection) oProp.getFirstChild();
+					Object value = Base64.decodeToObject(cdata.getWholeText());
+					_table.put(oProp.getNodeName(), (Serializable) value);
+				}
+			}
+		}
+	}
+
+	public String toString()
+	{
+		return _table.toString();
+	}
+
+	@Override
+	public boolean equals(Object arg)
+	{
+		if (! (arg instanceof Properties))
+			return false;
+		Properties other = (Properties)arg;
+		if (other.size() != _table.size())
+			return false;
+		for(Map.Entry<String,Serializable>oCurr : _table.entrySet())
+		{	
+			Object val = other.getProperty(oCurr.getKey());
+			if (null==oCurr.getValue())
+				if (null==val)
+					continue;
+				else
+					return false;
+			if (!oCurr.getValue().equals(val))
+				return false;
+		}
+		return true;
+	}
+
+	Hashtable<String, Serializable> _table = new Hashtable<String, Serializable>();
+
+}

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/XMLMessagePlugin.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/XMLMessagePlugin.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/XMLMessagePlugin.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,49 @@
+package org.jboss.internal.soa.esb.message.format.xml;
+
+import org.jboss.soa.esb.message.Message;
+
+import org.jboss.soa.esb.message.format.MessagePlugin;
+import org.jboss.soa.esb.message.format.MessageType;
+
+import java.net.URI;
+
+/*
+ * 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
+ */
+
+/**
+ * Used to plug in new message formats dynamically.
+ *  
+ * @author Mark Little
+ *
+ */
+
+public class XMLMessagePlugin implements MessagePlugin
+{
+	public Message getMessage ()
+	{
+		return new MessageImpl();
+	}
+	
+	public URI getType ()
+	{
+		return MessageType.JBOSS_XML;
+	}
+}

Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/XMLUtil.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/XMLUtil.java	2006-09-30 23:08:45 UTC (rev 6488)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/message/format/xml/XMLUtil.java	2006-09-30 23:09:02 UTC (rev 6489)
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ *
+ * XMLUtil.java
+ */
+
+package org.jboss.internal.soa.esb.message.format.xml;
+
+public class XMLUtil
+{
+    public static final String ESB_PREFIX = "jbesb";
+    public static final String ESB_NAMESPACE_URI = "http://www.jboss.org/ws/2006/09/esb";
+}




More information about the jboss-svn-commits mailing list