| 1 |
package samples.encoding; |
| 2 |
|
| 3 |
import org.apache.axis.Constants; |
| 4 |
import org.apache.axis.MessageContext; |
| 5 |
import org.apache.axis.encoding.DeserializationContext; |
| 6 |
import org.apache.axis.encoding.SerializationContext; |
| 7 |
import org.apache.axis.encoding.SerializationContext; |
| 8 |
import org.apache.axis.encoding.TypeMapping; |
| 9 |
import org.apache.axis.encoding.TypeMappingRegistry; |
| 10 |
import org.apache.axis.message.RPCElement; |
| 11 |
import org.apache.axis.message.RPCParam; |
| 12 |
import org.apache.axis.message.SOAPEnvelope; |
| 13 |
import org.apache.axis.server.AxisServer; |
| 14 |
import org.xml.sax.InputSource; |
| 15 |
|
| 16 |
import javax.xml.namespace.QName; |
| 17 |
import java.io.FileReader; |
| 18 |
import java.io.Reader; |
| 19 |
import java.io.StringReader; |
| 20 |
import java.io.StringWriter; |
| 21 |
import java.io.Writer; |
| 22 |
|
| 23 |
/** Little serialization test with a struct. |
| 24 |
*/ |
| 25 |
public class TestSer |
| 26 |
{ |
| 27 |
public static final String myNS = "urn:myNS"; |
| 28 |
|
| 29 |
public static void main(String args[]) { |
| 30 |
MessageContext msgContext = new MessageContext(new AxisServer()); |
| 31 |
SOAPEnvelope msg = new SOAPEnvelope(); |
| 32 |
RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string"); |
| 33 |
QName dataQName = new QName("typeNS", "Data"); |
| 34 |
|
| 35 |
Data data = new Data(); |
| 36 |
Data data2 = new Data(); |
| 37 |
data.stringMember = "String member"; |
| 38 |
data.floatMember = new Float("1.23"); |
| 39 |
data.dataMember = data2; |
| 40 |
|
| 41 |
data2.stringMember = "another str member"; |
| 42 |
data2.floatMember = new Float("4.56"); |
| 43 |
data2.dataMember = null; // "data;" for loop-test of multi-refs |
| 44 |
|
| 45 |
RPCParam arg2 = new RPCParam("", "struct", data); |
| 46 |
RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ arg1, arg2 }); |
| 47 |
msg.addBodyElement(body); |
| 48 |
|
| 49 |
try { |
| 50 |
Reader reader = null; |
| 51 |
|
| 52 |
if (args.length == 0) { |
| 53 |
Writer stringWriter = new StringWriter(); |
| 54 |
SerializationContext context = new SerializationContext(stringWriter, msgContext); |
| 55 |
|
| 56 |
TypeMappingRegistry reg = context.getTypeMappingRegistry(); |
| 57 |
TypeMapping tm = (TypeMapping) reg.getOrMakeTypeMapping(Constants.URI_SOAP11_ENC); |
| 58 |
tm.register(Data.class, dataQName, new DataSerFactory(), new DataDeserFactory()); |
| 59 |
|
| 60 |
msg.output(context); |
| 61 |
|
| 62 |
String msgString = stringWriter.toString(); |
| 63 |
System.out.println("Serialized msg:"); |
| 64 |
System.out.println(msgString); |
| 65 |
|
| 66 |
System.out.println("-------"); |
| 67 |
System.out.println("Testing deserialization..."); |
| 68 |
|
| 69 |
reader = new StringReader(msgString); |
| 70 |
} else { |
| 71 |
reader = new FileReader(args[0]); |
| 72 |
} |
| 73 |
|
| 74 |
DeserializationContext dser = new DeserializationContext( |
| 75 |
new InputSource(reader), msgContext, org.apache.axis.Message.REQUEST); |
| 76 |
dser.parse(); |
| 77 |
SOAPEnvelope env = dser.getEnvelope(); |
| 78 |
|
| 79 |
RPCElement rpcElem = (RPCElement)env.getFirstBody(); |
| 80 |
RPCParam struct = rpcElem.getParam("struct"); |
| 81 |
if (struct == null) |
| 82 |
throw new Exception("No <struct> param"); |
| 83 |
|
| 84 |
if (!(struct.getObjectValue() instanceof Data)) { |
| 85 |
System.out.println("Not a Data object! "); |
| 86 |
System.out.println(struct.getObjectValue()); |
| 87 |
System.exit(1); |
| 88 |
} |
| 89 |
|
| 90 |
Data val = (Data)struct.getObjectValue(); |
| 91 |
if (val == null) |
| 92 |
throw new Exception("No value for struct param"); |
| 93 |
|
| 94 |
System.out.println(val.toString()); |
| 95 |
} catch (Exception e) { |
| 96 |
e.printStackTrace(); |
| 97 |
} |
| 98 |
} |
| 99 |
} |