001: package samples.encoding;
002:
003: import org.apache.axis.Constants;
004: import org.apache.axis.MessageContext;
005: import org.apache.axis.encoding.DeserializationContext;
006: import org.apache.axis.encoding.SerializationContext;
007: import org.apache.axis.encoding.SerializationContext;
008: import org.apache.axis.encoding.TypeMapping;
009: import org.apache.axis.encoding.TypeMappingRegistry;
010: import org.apache.axis.message.RPCElement;
011: import org.apache.axis.message.RPCParam;
012: import org.apache.axis.message.SOAPEnvelope;
013: import org.apache.axis.server.AxisServer;
014: import org.xml.sax.InputSource;
015:
016: import javax.xml.namespace.QName;
017: import java.io.FileReader;
018: import java.io.Reader;
019: import java.io.StringReader;
020: import java.io.StringWriter;
021: import java.io.Writer;
022:
023: /** Little serialization test with a struct.
024: */
025: public class TestSer {
026: public static final String myNS = "urn:myNS";
027:
028: public static void main(String args[]) {
029: MessageContext msgContext = new MessageContext(new AxisServer());
030: SOAPEnvelope msg = new SOAPEnvelope();
031: RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam",
032: "this is a string");
033: QName dataQName = new QName("typeNS", "Data");
034:
035: Data data = new Data();
036: Data data2 = new Data();
037: data.stringMember = "String member";
038: data.floatMember = new Float("1.23");
039: data.dataMember = data2;
040:
041: data2.stringMember = "another str member";
042: data2.floatMember = new Float("4.56");
043: data2.dataMember = null; // "data;" for loop-test of multi-refs
044:
045: RPCParam arg2 = new RPCParam("", "struct", data);
046: RPCElement body = new RPCElement("urn:myNamespace", "method1",
047: new Object[] { arg1, arg2 });
048: msg.addBodyElement(body);
049:
050: try {
051: Reader reader = null;
052:
053: if (args.length == 0) {
054: Writer stringWriter = new StringWriter();
055: SerializationContext context = new SerializationContext(
056: stringWriter, msgContext);
057:
058: TypeMappingRegistry reg = context
059: .getTypeMappingRegistry();
060: TypeMapping tm = (TypeMapping) reg
061: .getOrMakeTypeMapping(Constants.URI_SOAP11_ENC);
062: tm.register(Data.class, dataQName,
063: new DataSerFactory(), new DataDeserFactory());
064:
065: msg.output(context);
066:
067: String msgString = stringWriter.toString();
068: System.out.println("Serialized msg:");
069: System.out.println(msgString);
070:
071: System.out.println("-------");
072: System.out.println("Testing deserialization...");
073:
074: reader = new StringReader(msgString);
075: } else {
076: reader = new FileReader(args[0]);
077: }
078:
079: DeserializationContext dser = new DeserializationContext(
080: new InputSource(reader), msgContext,
081: org.apache.axis.Message.REQUEST);
082: dser.parse();
083: SOAPEnvelope env = dser.getEnvelope();
084:
085: RPCElement rpcElem = (RPCElement) env.getFirstBody();
086: RPCParam struct = rpcElem.getParam("struct");
087: if (struct == null)
088: throw new Exception("No <struct> param");
089:
090: if (!(struct.getObjectValue() instanceof Data)) {
091: System.out.println("Not a Data object! ");
092: System.out.println(struct.getObjectValue());
093: System.exit(1);
094: }
095:
096: Data val = (Data) struct.getObjectValue();
097: if (val == null)
098: throw new Exception("No value for struct param");
099:
100: System.out.println(val.toString());
101: } catch (Exception e) {
102: e.printStackTrace();
103: }
104: }
105: }
|