Author: pzf
Date: Fri Aug  3 05:17:08 2007
New Revision: 562425

URL: http://svn.apache.org/viewvc?view=rev&rev=562425
Log:
helper classes

Added:
    
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/PayloadHelper.java
    
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMap.java
    
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMapImpl.java

Added: 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/PayloadHelper.java
URL: 
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/PayloadHelper.java?view=auto&rev=562425
==============================================================================
--- 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/PayloadHelper.java
 (added)
+++ 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/PayloadHelper.java
 Fri Aug  3 05:17:08 2007
@@ -0,0 +1,262 @@
+package org.apache.synapse.util;
+
+import java.util.Iterator;
+
+import javax.activation.DataHandler;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.soap.SOAP11Version;
+import org.apache.axiom.soap.SOAPBody;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPVersion;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.util.SimpleMap;
+
+public class PayloadHelper {
+       public final static String AXIOMPAYLOADNS = 
"http://ws.apache.org/commons/ns/payload";;
+
+       public final static QName BINARYELT = new QName(AXIOMPAYLOADNS, 
"binary",
+                       "ax");
+
+       public final static QName TEXTELT = new QName(AXIOMPAYLOADNS, "text", 
"ax");
+
+       public final static QName MAPELT = new QName(AXIOMPAYLOADNS, "map", 
"ax");
+
+       public final static int XMLPAYLOADTYPE = 0, BINARYPAYLOADTYPE = 1,
+                       TEXTPAYLOADTYPE = 2, MAPPAYLOADTYPE = 3;
+
+       public static final Log log = LogFactory.getLog(PayloadHelper.class);
+
+       // gets a indication of the payload type. Default is XML
+       // You cannot set the payload type. Instead, it is set automatically 
when
+       // the payload is set
+       public static int getPayloadType(SOAPEnvelope envelope) {
+               OMElement el = getXMLPayload(envelope);
+               if (el.getQName().equals(BINARYELT))
+                       return BINARYPAYLOADTYPE;
+               else if (el.getQName().equals(TEXTELT))
+                       return TEXTPAYLOADTYPE;
+               else if (el.getQName().equals(MAPELT))
+                       return MAPPAYLOADTYPE;
+               else
+                       return XMLPAYLOADTYPE; // default XML
+       }
+
+       public static int getPayloadType(MessageContext mc) {
+               if (mc.getEnvelope() == null)
+                       return 0;
+               return getPayloadType(mc.getEnvelope());
+       }
+
+       // XML Payload is carried as the first (and only) child of the body
+       public static OMElement getXMLPayload(SOAPEnvelope envelope) {
+               SOAPBody body = envelope.getBody();
+               if (body == null) {
+                       log.error("No body found");
+                       return null;
+               }
+               OMElement bodyEl = body.getFirstElement();
+               if (bodyEl == null) {
+                       log.error("No body child found");
+                       return null;
+               }
+               return bodyEl;
+       }
+
+       public static void setXMLPayload(SOAPEnvelope envelope, OMElement 
element) {
+               SOAPBody body = envelope.getBody();
+               if (body == null) {
+
+                       SOAPVersion version = envelope.getVersion();
+                       if (version.getEnvelopeURI().equals(
+                                       
SOAP11Version.SOAP_ENVELOPE_NAMESPACE_URI)) {
+                               body = 
OMAbstractFactory.getSOAP11Factory().createSOAPBody();
+                       } else {
+                               body = 
OMAbstractFactory.getSOAP12Factory().createSOAPBody();
+                       }
+                       if (envelope.getHeader() != null) {
+                               envelope.getHeader().insertSiblingAfter(body);
+                       } else {
+                               envelope.addChild(body);
+                       }
+               } else {
+                       for (Iterator it = body.getChildren(); it.hasNext();) {
+                               OMNode node = (OMNode) it.next();
+                               node.discard();
+                       }
+               }
+               body.addChild(element);
+       }
+
+       public static void setXMLPayload(MessageContext mc, OMElement element) {
+               if (mc.getEnvelope() == null) {
+                       try {
+                               
mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
+                                               .createSOAPEnvelope());
+                       } catch (Exception e) {
+                               throw new SynapseException(e);
+                       }
+               }
+               setXMLPayload(mc.getEnvelope(), element);
+       }
+
+       // Binary Payload is carried in a wrapper element with QName BINARYELT
+       public static DataHandler getBinaryPayload(SOAPEnvelope envelope) {
+               OMElement el = getXMLPayload(envelope);
+               if (el == null)
+                       return null;
+               if (!el.getQName().equals(BINARYELT)) {
+                       log.error("Wrong QName" + el.getQName());
+                       return null;
+               }
+               OMNode textNode = el.getFirstOMChild();
+               if (textNode.getType() != OMNode.TEXT_NODE) {
+                       log.error("Text Node not found");
+                       return null;
+               }
+               OMText text = (OMText) textNode;
+               DataHandler dh = null;
+               try {
+                       dh = (DataHandler) text.getDataHandler();
+               } catch (ClassCastException ce) {
+                       log.error("cannot get DataHandler" + ce.getMessage());
+                       return null;
+               }
+               return dh;
+
+       }
+
+       public static DataHandler getBinaryPayload(MessageContext mc) {
+               if (mc.getEnvelope() == null) {
+                       log.error("null envelope");
+                       return null;
+               }
+               return getBinaryPayload(mc.getEnvelope());
+       }
+
+       public static void setBinaryPayload(SOAPEnvelope envelope, DataHandler 
dh) {
+               OMFactory fac = envelope.getOMFactory();
+               OMElement binaryElt = envelope.getOMFactory()
+                               .createOMElement(BINARYELT);
+               OMText text = fac.createOMText(dh, true);
+               binaryElt.addChild(text);
+               setXMLPayload(envelope, binaryElt);
+       }
+
+       public static void setBinaryPayload(MessageContext mc, DataHandler dh) {
+               if (mc.getEnvelope() == null) {
+                       try {
+                               
mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
+                                               .createSOAPEnvelope());
+                       } catch (Exception e) {
+                               throw new SynapseException(e);
+                       }
+               }
+               setBinaryPayload(mc.getEnvelope(), dh);
+
+       }
+
+       // Text payload is carried in a wrapper element with QName TEXTELT
+       public static String getTextPayload(SOAPEnvelope envelope) {
+               OMElement el = getXMLPayload(envelope);
+               if (el == null)
+                       return null;
+               if (!el.getQName().equals(TEXTELT)) {
+                       log.error("Wrong QName" + el.getQName());
+                       return null;
+               }
+               OMNode textNode = el.getFirstOMChild();
+               if (textNode.getType() != OMNode.TEXT_NODE) {
+                       log.error("Text Node not found");
+                       return null;
+               }
+               OMText text = (OMText) textNode;
+               return text.getText();
+       }
+
+       public static String getTextPayload(MessageContext mc) {
+               if (mc.getEnvelope() == null) {
+                       log.error("null envelope");
+                       return null;
+               }
+               return getTextPayload(mc.getEnvelope());
+       }
+
+       public static void setTextPayload(SOAPEnvelope envelope, String text) {
+               OMFactory fac = envelope.getOMFactory();
+               OMElement textElt = 
envelope.getOMFactory().createOMElement(TEXTELT);
+               OMText textNode = fac.createOMText(text);
+               textElt.addChild(textNode);
+               setXMLPayload(envelope, textElt);
+       }
+
+       public static void setTextPayload(MessageContext mc, String text) {
+               if (mc.getEnvelope() == null) {
+                       try {
+                               
mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
+                                               .createSOAPEnvelope());
+                       } catch (Exception e) {
+                               throw new SynapseException(e);
+                       }
+               }
+               setTextPayload(mc.getEnvelope(), text);
+       }
+
+       // Map payload must be a Map of String->int, boolean, float, double, 
char,
+       // short, byte, byte[], long, String
+       public static SimpleMap getMapPayload(SOAPEnvelope envelope) {
+               OMElement el = getXMLPayload(envelope);
+               if (el == null)
+                       return null;
+               if (!el.getQName().equals(MAPELT)) {
+                       log.error("Wrong QName" + el.getQName());
+                       return null;
+               }
+               SimpleMap map = new SimpleMapImpl(el);
+               return map;
+       }
+
+       public static SimpleMap getMapPayload(MessageContext mc) {
+               if (mc.getEnvelope() == null) {
+                       log.error("null envelope");
+                       return null;
+               }
+               return getMapPayload(mc.getEnvelope());
+       }
+
+       public static void setMapPayload(SOAPEnvelope envelope, SimpleMap map) {
+
+               if (map instanceof SimpleMapImpl) {
+                       SimpleMapImpl impl = (SimpleMapImpl) map;
+                       OMElement mapElt = 
impl.getOMElement(envelope.getOMFactory());
+                       if (mapElt == null) {
+                               log.debug("null map element returned");
+                               return;
+                       }
+                       setXMLPayload(envelope, mapElt);
+               } else {
+                       throw new SynapseException("cannot handle any other 
instance of SimpleMap at this point TODO");
+               }
+       }
+
+       public static void setMapPayload(MessageContext mc, SimpleMap map) {
+               if (mc.getEnvelope() == null) {
+                       try {
+                               
mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
+                                               .createSOAPEnvelope());
+                       } catch (Exception e) {
+                               throw new SynapseException(e);
+                       }
+               }
+               setMapPayload(mc.getEnvelope(), map);
+       }
+}

Added: 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMap.java
URL: 
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMap.java?view=auto&rev=562425
==============================================================================
--- 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMap.java
 (added)
+++ 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMap.java
 Fri Aug  3 05:17:08 2007
@@ -0,0 +1,42 @@
+package org.apache.synapse.util;
+
+import java.util.Map;
+/*
+ * This interface is a helper for dealing with a restriction on Map messages 
+ * The key MUST be a String
+ * and the value MUST be one of
+ * boolean, char, String, int, double, float, long, short, byte, byte[] 
(Consider adding DataHandler in future)
+ * or the equivalent Object
+ * The simple type putters are equivalent to using an Object
+ * so 
+ * put("paul", new Integer(38));
+ * getInt("paul"); returns 38
+ */
+
+public interface SimpleMap extends Map {
+       
+       public Object get(String name);
+       public void put(String name, Object value);
+       public boolean getBoolean(String name);
+       public void putBoolean(String name, boolean b);
+       public String getString(String value);
+       public void putString(String name, String value);
+       public char getChar(String name);
+       public void putChar(String name, char c);
+       public int getInt(String name);
+       public void putInt(String name, int i);
+       public short getShort(String name);
+       public void putShort(String name, short s);
+       public float getFloat(String name);
+       public void putFloat(String name, float fl);
+       public double getDouble(String name);
+       public void putDouble(String name, double d);
+       public long getLong(String name);
+       public void putLong(String name, long l);
+       public byte getByte(String name);
+       public void putByte(String name, byte b);
+       public byte[] getBytes(String name);
+       public void putBytes(String name, byte[] bytes);
+       
+
+}

Added: 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMapImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMapImpl.java?view=auto&rev=562425
==============================================================================
--- 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMapImpl.java
 (added)
+++ 
webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/SimpleMapImpl.java
 Fri Aug  3 05:17:08 2007
@@ -0,0 +1,322 @@
+package org.apache.synapse.util;
+
+import java.io.ByteArrayOutputStream;
+
+import java.util.HashMap;
+import java.util.Iterator;
+
+import javax.activation.DataHandler;
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.attachments.ByteArrayDataSource;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMText;
+
+
+public class SimpleMapImpl extends HashMap implements SimpleMap {
+       private static final OMNamespace attrNS = 
OMAbstractFactory.getOMFactory().createOMNamespace("", "");
+       private static final String TYPE = "type";
+
+       private static final String NAME = "name";
+
+       private static final String ENTRY = "entry";
+
+       private static final String SHORT = "short";
+
+       private static final String LONG = "long";
+
+       private static final String DOUBLE = "double";
+       private static final String INTEGER = "int";
+       private static final String FLOAT = "float";
+
+       private static final String BYTEARRAY = "byte[]";
+
+       private static final String BYTE = "byte";
+
+       private static final String STRING = "string";
+
+       private static final String BOOLEAN = "boolean";
+
+       private static final String CHAR = "char";
+
+       private static final long serialVersionUID = 1L;
+
+       public SimpleMapImpl() {
+               super();
+       }
+
+       public Object get(String name) {
+               return this.get((Object) name);
+       }
+
+       public boolean getBoolean(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof Boolean) {
+                       return ((Boolean) o).booleanValue();
+               } else {
+                       throw new RuntimeException("getBoolean(" + name + "): "
+                                       + o.getClass().getName() + " is not an 
instance of Boolean");
+               }
+       }
+
+       public byte getByte(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof Byte) {
+                       return ((Byte) o).byteValue();
+               } else {
+                       throw new RuntimeException("getByte(" + name + "): "
+                                       + o.getClass().getName() + " is not an 
instance of Byte");
+               }
+       }
+
+       public byte[] getBytes(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof byte[]) {
+                       return (byte[]) o;
+               } else {
+                       throw new RuntimeException("getByteArray(" + name + "): 
"
+                                       + o.getClass().getName() + " is not an 
instance of byte[]");
+               }
+       }
+
+       public char getChar(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof Character) {
+                       return ((Character) o).charValue();
+               } else {
+                       throw new RuntimeException("getChar(" + name + "): "
+                                       + o.getClass().getName()
+                                       + " is not an instance of Character");
+               }
+       }
+
+       public double getDouble(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof Double) {
+                       return ((Double) o).doubleValue();
+               } else {
+                       throw new RuntimeException("getDouble(" + name + "): "
+                                       + o.getClass().getName() + " is not an 
instance of Double");
+               }
+       }
+
+       public float getFloat(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof Float) {
+                       return ((Float) o).floatValue();
+               } else {
+                       throw new RuntimeException("getFloat(" + name + "): "
+                                       + o.getClass().getName() + " is not an 
instance of Float");
+               }
+       }
+
+       public int getInt(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof Integer) {
+                       return ((Integer) o).intValue();
+               } else {
+                       throw new RuntimeException("getInt(" + name + "): "
+                                       + o.getClass().getName() + " is not an 
instance of Integer");
+               }
+       }
+
+       public long getLong(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof Long) {
+                       return ((Long) o).longValue();
+               } else {
+                       throw new RuntimeException("getLong(" + name + "): "
+                                       + o.getClass().getName() + " is not an 
instance of Long");
+               }
+       }
+
+       public short getShort(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof Short) {
+                       return ((Short) o).shortValue();
+               } else {
+                       throw new RuntimeException("getShort(" + name + "): "
+                                       + o.getClass().getName() + " is not an 
instance of Short");
+               }
+       }
+
+       public String getString(String name) {
+               Object o = this.get((Object) name);
+               if (o instanceof String) {
+                       return ((String) o);
+               } else {
+                       throw new RuntimeException("getString(" + name + "): "
+                                       + o.getClass().getName() + " is not an 
instance of String");
+               }
+       }
+
+       public void put(String name, Object value) {
+               this.put((Object) name, value);
+       }
+
+       public void putBoolean(String name, boolean b) {
+               this.put((Object) name, new Boolean(b));
+
+       }
+
+       public void putByte(String name, byte b) {
+               this.put((Object) name, new Byte(b));
+
+       }
+
+       public void putBytes(String name, byte[] bytes) {
+               this.put((Object) name, bytes);
+
+       }
+
+       public void putChar(String name, char c) {
+               this.put((Object) name, new Character(c));
+
+       }
+
+       public void putDouble(String name, double d) {
+               this.put((Object) name, new Double(d));
+
+       }
+
+       public void putFloat(String name, float fl) {
+               this.put((Object) name, new Float(fl));
+
+       }
+
+       public void putInt(String name, int i) {
+               this.put((Object) name, new Integer(i));
+
+       }
+
+       public void putLong(String name, long l) {
+               this.put((Object) name, new Long(l));
+
+       }
+
+       public void putShort(String name, short s) {
+               this.put((Object) name, new Short(s));
+
+       }
+
+       public void putString(String name, String value) {
+               this.put((Object) name, value);
+
+       }
+
+       public OMElement getOMElement() {
+               return getOMElement(OMAbstractFactory.getOMFactory());
+       }
+
+       public OMElement getOMElement(OMFactory fac) {
+               OMElement mapElement = 
fac.createOMElement(PayloadHelper.MAPELT);
+               
+               for (Iterator it = this.keySet().iterator(); it.hasNext();) {
+                       OMElement entry = fac.createOMElement(new QName(
+                                       PayloadHelper.AXIOMPAYLOADNS, ENTRY), 
mapElement);
+                       
+                       
+                       Object key = it.next();
+                       System.out.println("key "+key);
+                       if (key instanceof String) {
+                               Object o = this.get(key);
+                               System.out.println("key "+key);
+                               entry.addAttribute(NAME, (String) key, attrNS);
+                               if (o instanceof Character) {
+                                       entry.addAttribute(TYPE, CHAR, attrNS);
+                                       entry.setText(o.toString());
+                               } else if (o instanceof Boolean) {
+                                       entry.addAttribute(TYPE, BOOLEAN, 
attrNS);
+                                       entry.setText(o.toString());
+                               } else if (o instanceof String) {
+                                       entry.addAttribute(TYPE, STRING, 
attrNS);
+                                       entry.setText(o.toString());
+                               } else if (o instanceof Byte) {
+                                       entry.addAttribute(TYPE, BYTE, attrNS);
+                                       entry.setText(((Byte) o).toString());
+                               } else if (o instanceof byte[]) {
+                                       entry.addAttribute(TYPE, BYTEARRAY, 
attrNS);
+                                       OMText text = fac.createOMText(new 
DataHandler(
+                                                       new 
ByteArrayDataSource((byte[]) o)), true);
+                                       entry.addChild(text);
+                               } else if (o instanceof Float) {
+                                       entry.addAttribute(TYPE, FLOAT, attrNS);
+                                       entry.setText(o.toString());
+                               } else if (o instanceof Double) {
+                                       entry.addAttribute(TYPE, DOUBLE, 
attrNS);
+                                       entry.setText(o.toString());
+                               } else if (o instanceof Long) {
+                                       entry.addAttribute(TYPE, LONG, attrNS);
+                                       entry.setText(o.toString());
+                               } else if (o instanceof Short) {
+                                       entry.addAttribute(TYPE, SHORT, attrNS);
+                                       entry.setText(o.toString());
+                               } else if (o instanceof Integer) {
+                                       entry.addAttribute(TYPE, INTEGER, 
attrNS);
+                                       entry.setText(o.toString());
+                               }
+
+                       } else {
+                               // shouldn't be any non-string keys. Ignore!
+                       }
+               }
+
+               return mapElement;
+       }
+
+       // create an instance from an OMElement (if its the right shape!!!)
+       public SimpleMapImpl(OMElement el) {
+               super();
+               if (el.getQName().equals(PayloadHelper.MAPELT)) {
+                       for (Iterator it = el.getChildElements(); it.hasNext(); 
) {
+                               OMElement child = (OMElement)it.next();
+                               if (child.getLocalName().equals(ENTRY)) {
+                                       String name = 
child.getAttributeValue(new QName("",NAME));
+                                       String type = 
child.getAttributeValue(new QName("", TYPE));
+                                       try {
+                                       if (type==null || name == null) {
+                                               //bad!
+                                               continue;
+                                       }
+                                       OMNode data = child.getFirstOMChild();
+                                       if (data.getType()!=OMNode.TEXT_NODE) {
+                                               continue; // BAD!
+                                       }
+                                       OMText text = (OMText)data;
+                                        if (type.equals(INTEGER)) {
+                                               this.put((Object)name, new 
Integer(text.getText()));
+                                       } else if (type.equals(CHAR)) {
+                                               this.put((Object)name, new 
Character((text.getText().charAt(0))));
+                                       } else if (type.equals(DOUBLE)) {
+                                               this.put((Object)name, new 
Double(text.getText()));
+                                       } else if (type.equals(FLOAT)) {
+                                               this.put((Object)name, new 
Float(text.getText()));
+                                       } else if (type.equals(BYTE)) {
+                                               this.put((Object)name, new 
Byte(text.getText().getBytes()[0]));
+                                       } else if (type.equals(SHORT)) {
+                                               this.put((Object)name, new 
Short(text.getText()));
+                                       } else if (type.equals(LONG)) {
+                                               this.put((Object)name, new 
Long(text.getText()));
+                                       } else if (type.equals(STRING)) {
+                                               this.put((Object)name, 
text.getText());
+                                       } else if (type.equals(BYTEARRAY)) {
+                                               DataHandler dh = 
(DataHandler)text.getDataHandler();
+                                               ByteArrayOutputStream baos = 
new ByteArrayOutputStream();
+                                               dh.writeTo(baos);
+                                               this.put((Object)name, 
baos.toByteArray());
+                                       }
+                                       } catch (Exception e) {
+                                               e.printStackTrace();
+                                               // ignore errors
+                                       }
+                                        
+                               }
+                       }
+               }
+       }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to