users@jax-rpc.java.net

Re: Serializers & Deserializers

From: Arun Gupta <arun.gupta_at_sun.com>
Date: Tue, 16 Apr 2002 15:42:41 -0700

Hi Michael,

I'm attaching files to demonstrate the usage of String[] and an array of
a complex type in a DII client. The files are:

- service endpoint interface
- service endpoint class
- DII client to invoke the service
- config.xml
- web.xml

I ran these file on JWSDP EA2 and the client printed all the results
successfully.

Thanks for your interest in JAX-RPC.

Thanks,
-Arun

YAWN,MICHAEL (HP-Cupertino,ex1) wrote:

>Hi,
>
>I'm having difficulty getting serializers and deserializers to work. I'm
>using the code provided
>by Arun last week as a guide, but also looking at the code generated by
>XRPCC to help
>resolve some questions.
>
>Task 1: Need to serialize a String[].
>- Arun's example code created a SimpleTypeSerializer, but XRPCC uses an
>AttachmentSerializer
> to handle the String. Is there a reason to prefer one over the other?
>- What is the rule for using ENCODE_TYPE vs DONT_ENCODE_TYPE in the
>serializer constructor
> calls? Arun's code uses DONT_ENCODE for the String, ENCODE for the array.
>The xrpcc-generated
> code has ENCODE_TYPE for both.
>- What is the first parameter to the SimpleTypeArraySerializer constructor?
>Arun's code passes a
> QNAME_TYPE_INT, but the xrpcc-generated code uses the QName for
>'ArrayOfstring'. The type
> of the sixth parameter seems similarly confused.
>- Why does XRPCC use the constant NS_SOAP_ENCODING, while Arun's code uses
> URI_ENCODING for the same purpose, and in my code neither will work -- I
>must use
> URI_NS_SOAP_ENCODING.
>- What is the rule for using SERIALIZE_AS_REF vs. DONT_SERIALIZE_AS_REF in
>the
> ReferenceableSerializerImpl constructor?
>- My code as written fails with 'Illegal MimeHeader name or value'. Does
>this provide any
> insight into where my problem is likely to be?
>
>Task 2: Need to serialize an array of a complex type
>- Is there an example available for serializing a complex type? I have a
>type which consists
> of an int[], a String, and a float. It's hard to follow exactly how this
>is being done in the
> xrpcc-generated code since it seems to be spread across several classes.
>
>Any available documentation on these constructor calls would go a long way
>toward
>helping me get these examples working. I can send (via list or separately)
>source for
>the client program if desired.
>
>Thanks,
>Mike
>

--
=============================================
There is only one me, I must live myself!
There is only one today, I must live itself!
=============================================
http://members.tripod.com/~apgupta/index.html
=============================================


package duke;

public class Complex {
        private String foo;
        private int bar;

        public Complex() { }

        public Complex(String foo, int bar) {
                this.foo = foo;
                this.bar = bar;
        }

        public void setFoo(String foo) { this.foo = foo; }
        public void setBar(int bar) { this.bar = bar; }

        public String getFoo() { return foo; }
        public int getBar() { return bar; }

        public String toString() {
                return foo + ", " + bar;
        }

        public boolean equals(Complex that) {
                if (that == null)
                        return false;

                return (this.foo.equals(that.foo) &&
                        (this.bar == that.bar));
        }
}



package duke;

import java.rmi.RemoteException;

import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.namespace.QName;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;
import javax.xml.rpc.encoding.SerializerFactory;
import javax.xml.rpc.encoding.DeserializerFactory;

import com.sun.xml.rpc.client.dii.CallPropertyConstants;

import com.sun.xml.rpc.encoding.CombinedSerializer;
import com.sun.xml.rpc.encoding.ObjectArraySerializer;
import com.sun.xml.rpc.encoding.SimpleTypeArraySerializer;
import com.sun.xml.rpc.encoding.SimpleTypeSerializer;
import com.sun.xml.rpc.encoding.simpletype.XSDStringEncoder;

import com.sun.xml.rpc.encoding.SerializerConstants;
import com.sun.xml.rpc.encoding.SingletonSerializerFactory;
import com.sun.xml.rpc.encoding.SingletonDeserializerFactory;

import com.sun.xml.rpc.encoding.soap.SOAPConstants;
import com.sun.xml.rpc.wsdl.document.schema.SchemaConstants;
import com.sun.xml.rpc.encoding.ReferenceableSerializerImpl;

public class DIIClient implements CallPropertyConstants, SerializerConstants {

        QName stringQname = null;
        QName stringArrayTypeQname = null;
        QName stringArrayElementQname = null;

        QName complexQname = null;
        QName complexArrayTypeQname = null;
        QName complexArrayElementQname = null;

        final String TYPE_NAMESPACE_VALUE = "http://duke.org/types";
        final String WSDL_NAMESPACE_VALUE = "http://duke.org/wsdl";

        ServiceFactory factory = null;

        public static void main(String[] args) {
                DIIClient client = new DIIClient();
                client.echoString();
                client.echoStringArray();
                client.echoComplex();
                client.echoComplexArray();
        }

        public DIIClient() {
                try {
                        stringArrayTypeQname = new QName(TYPE_NAMESPACE_VALUE, "ArrayOfstring");
                        stringArrayElementQname = new QName("", "string");

                        complexQname = new QName(TYPE_NAMESPACE_VALUE, "Complex");
                        complexArrayTypeQname = new QName(TYPE_NAMESPACE_VALUE, "ArrayOfComplex");
                        complexArrayElementQname = new QName("", "Complex");

                        factory = ServiceFactory.newInstance();
                } catch (Exception ex) {
                        ex.printStackTrace(System.out);
                }
        }

        void echoString() {
                String result = null;

                System.out.println("Echoing a String ...");
                try {
                        Call call = newStringCall();
                        call.setReturnType(SchemaConstants.QNAME_TYPE_STRING);
                        call.setOperationName(new QName(WSDL_NAMESPACE_VALUE, "echoString"));
                        call.addParameter("String_1",SchemaConstants.QNAME_TYPE_STRING,ParameterMode.PARAM_MODE_IN);
                        String[] params = { "Duke!" };
                        result = (String)call.invoke(params);
                        System.out.println(result);
                } catch (Exception ex) {
                        ex.printStackTrace(System.out);
                }
                System.out.println(" ... done.\n");
        }

        void echoStringArray() {
                String[] result = null;

                System.out.println("Echoing an array of String ...");
                try {
                        Call call = newStringCall();
                        call.setReturnType(stringArrayTypeQname);
                        call.setOperationName(new QName(WSDL_NAMESPACE_VALUE, "echoStringArray"));
                        result = (String[])call.invoke(null);
                        for (int i=0; i<result.length; i++)
                                System.out.println(result[i]);
                } catch (Exception ex) {
                        ex.printStackTrace(System.out);
                }
                System.out.println(" ... done.\n");
        }

        void echoComplex() {
                Complex result = null;

                System.out.println("Echoing a complex type ...");
                try {
                        Call call = newComplexCall();

                        call.setReturnType(complexQname);
                        call.setOperationName(new QName(WSDL_NAMESPACE_VALUE, "echoComplex"));
                        call.addParameter("Complex_1", complexQname, ParameterMode.PARAM_MODE_IN);
                        Complex[] params = { new Complex("Duke", 8) };
                        result = (Complex)call.invoke(params);
                        System.out.println(result);
                } catch (Exception ex) {
                        ex.printStackTrace(System.out);
                }
                System.out.println(" ... done.\n");
        }

        void echoComplexArray() {
                Complex[] result = null;

                System.out.println("Echoing an array of complex type ...");
                try {
                        Call call = newComplexCall();

                        call.setReturnType(complexArrayTypeQname);
                        call.setOperationName(new QName(WSDL_NAMESPACE_VALUE, "echoComplexArray"));
                        call.addParameter("arrayOfComplex_1",complexArrayTypeQname,ParameterMode.PARAM_MODE_IN);
                        Complex[] array = { new Complex("Duke", 7), new Complex("Java", 8) };
                        Object[] params = { array };
                        result = (Complex[])call.invoke(params);

                        for (int i=0; i<result.length; i++)
                                System.out.println(result[i]);
                } catch (Exception ex) {
                        ex.printStackTrace(System.out);
                }
                System.out.println(" ... done.\n");
        }

        Call newStringCall() throws Exception {
                CombinedSerializer stringSerializer = new SimpleTypeSerializer(SchemaConstants.QNAME_TYPE_STRING, DONT_ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING, XSDStringEncoder.getInstance());
                CombinedSerializer stringArraySerializer = new SimpleTypeArraySerializer(stringArrayTypeQname, ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING, null, SchemaConstants.QNAME_TYPE_STRING, String.class, 1, null, (SimpleTypeSerializer)stringSerializer);
                stringArraySerializer = new ReferenceableSerializerImpl(SERIALIZE_AS_REF, stringArraySerializer);

                SerializerFactory stringArraySerializerFactory = new SingletonSerializerFactory(stringArraySerializer);
                DeserializerFactory stringArrayDeserializerFactory = new SingletonDeserializerFactory(stringArraySerializer);

                Service service = factory.createService(new QName("Duke"));
                QName port = new QName(TYPE_NAMESPACE_VALUE, "DIIPort");

                TypeMappingRegistry registry = service.getTypeMappingRegistry();
                TypeMapping typeMapping = registry.getTypeMapping(SOAPConstants.URI_ENCODING);
                typeMapping.register(String[].class, stringArrayTypeQname, stringArraySerializerFactory, stringArrayDeserializerFactory);

                Call call = newCall(service, port);

                return call;
        }

        Call newComplexCall() throws Exception {
                CombinedSerializer complexSerializer = new Complex_SOAPSerializer(complexQname, ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING);
                complexSerializer = new ReferenceableSerializerImpl(SERIALIZE_AS_REF, complexSerializer);
                SerializerFactory complexSerializerFactory = new SingletonSerializerFactory(complexSerializer);
                DeserializerFactory complexDeserializerFactory = new SingletonDeserializerFactory(complexSerializer);

                CombinedSerializer complexArraySerializer = new ObjectArraySerializer(complexArrayTypeQname, ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING, complexArrayElementQname, complexQname, Complex.class, 1, null);
                complexArraySerializer = new ReferenceableSerializerImpl(SERIALIZE_AS_REF, complexArraySerializer);
                SingletonSerializerFactory complexArraySerializerFactory = new SingletonSerializerFactory(complexArraySerializer);
                SingletonDeserializerFactory complexArrayDeserializerFactory = new SingletonDeserializerFactory(complexArraySerializer);

                Service service = factory.createService(new QName("Duke"));
                QName port = new QName(TYPE_NAMESPACE_VALUE, "DIIPort");

                TypeMappingRegistry registry = service.getTypeMappingRegistry();
                TypeMapping typeMapping = registry.getTypeMapping(SOAPConstants.URI_ENCODING);
                typeMapping.register(Complex.class, complexQname, complexSerializerFactory, complexDeserializerFactory);
                typeMapping.register(Complex[].class, complexArrayTypeQname, complexArraySerializerFactory, complexArrayDeserializerFactory);

                Call call = newCall(service, port);

                return call;
        }

        Call newCall(Service service, QName port) throws Exception {
                Call call = service.createCall();
                call.setPortTypeName(port);
                call.setTargetEndpointAddress(System.getProperty("endpoint"));

                call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true));
                call.setProperty(Call.SOAPACTION_URI_PROPERTY, "");
                call.setProperty(ENCODING_STYLE_PROPERTY, SOAPConstants.URI_ENCODING);

                return call;
        }
}



package duke;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface DIIPort extends Remote {
        public String echoString(String s) throws RemoteException;
        public String[] echoStringArray() throws RemoteException;

        public Complex echoComplex(Complex complex) throws RemoteException;
        public Complex[] echoComplexArray(Complex[] complexArray) throws RemoteException;
}



package duke;

public class DIIService implements DIIPort {
        public String echoString(String string) {
                System.out.println("echoString(String)");
                return string;
        }

        public String[] echoStringArray() {
                System.out.println("echoStringArray()");
                String[] stringArray = { "Duke", "Rocks" };

                return stringArray;
        }

        public Complex echoComplex(Complex complex) {
                System.out.println("echoComplex(Complex)");
                return complex;
        }

        public Complex[] echoComplexArray(Complex[] complexArray) {
                System.out.println("echoComplexArray(Complex[])");
                return complexArray;
        }
}