You are viewing a plain text version of this content. The canonical link for it is here.
Posted to muse-user@ws.apache.org by "Vinh Nguyen (vinguye2)" <vi...@cisco.com> on 2006/11/29 02:36:27 UTC

custom serialization sample code?

Does anyone have sample code that shows how to return a custom type from
a capability operation, and then how to retrieve that object from the
client side?
 
For example, I have a BoxType as the return value of a capability
method.  I have a custom BoxTypeSerializer and specified it in muse.xml.
In the server trace, I see the xml data being passed ok to the client.
But, in myclient code, I'm not sure what to write in order to retrieve
this BoxType object properly.
 
The apache-httpd sample only shows how to retrieve a custom type as a
resource property by calling WsResourceClient.getPropertyAsObject().
But, this can't be used when trying to get the custom type as a return
value of an operation.
 
My client proxy class implements AbstractResourceClient.  The only
methods available for calling resource operations are
invoke(String,Element) and invoke(ProxyHandler,Object[]).  The first
method returns an xml Element, which is not what I want because I'm
expecting a BoxType object.  The second method doesn't make sense on the
client side because the client would not know the reflection info to
pass in the call (which must be specified to ReflectionProxyHandler, the
only implementation of the ProxyHandler interface).  I think ideally,
this should all be hidden from the client, and the only thing the client
needs to know is the resource EPR, and the operation name and
parameters.
 
Here's my server code (using XmlBeans for serialization):
    public class BoxCapability extends AbstractCapability implements
IBoxCapability
    {
        public BoxType boxOperation(int width) throws Exception
        {
            BoxDocument doc = BoxDocument.Factory.newInstance();
            BoxType type = doc.addNewBox();
            type.setWidth(BigInteger.valueOf(width));
            type.setHeight(BigInteger.valueOf(width));
            return type;
        }
    }
 
Here's my incomplete client code:
    public class SimpleResourceClient extends AbstractResourceClient
    {
        public BoxType testBoxOperation(int width) throws SoapFault
        {
            Element body =
XmlUtils.createElement(IBoxCapability.OP_QNAME, new Integer(width));

            BoxType response = ...  // what do I write here???

            return response;
        }
    }
 
 

Re: custom serialization sample code?

Posted by Daniel Jemiolo <da...@us.ibm.com>.
The easiest way to do this (meaning, the way that involves modifying as 
little of the generated code as possible) would be to take the code for 
boxOperation() as generated and add to the end of it like so:

Element response = (Element)invoke(...);  // last line of generated code

Element boxXML = XmlUtils.getFirstElement(response);
Serializer boxSer = 
SerializerRegistry().getInstance().getSerializer(BoxType.class);

return (BoxType)boxSer.fromXML(boxXML);


Then, I'd add the BoxSerializer like so:

static
{
        SerializerRegistry reg = SerializerRegistry.getInstance();
        reg.registerSerializer(BoxType.class, new BoxSerializer());
}


The serializer lookup and call to fromXML() is normally done by the 
invoke() method (notice it returns an Object, not an Element). However, 
this only happens for types that it knows about during code generation. If 
you look at the private arrays that are generated in the client, you'll 
see a bunch of info that is used to make the right call and parsing 
decisions in invoke(). Rather than have you modify that stuff, though, I 
think you're better off trying the code above.


"Vinh Nguyen \(vinguye2\)" <vi...@cisco.com> wrote on 11/28/2006 
08:36:27 PM:

> Does anyone have sample code that shows how to return a custom type from
> a capability operation, and then how to retrieve that object from the
> client side?
> 
> For example, I have a BoxType as the return value of a capability
> method.  I have a custom BoxTypeSerializer and specified it in muse.xml.
> In the server trace, I see the xml data being passed ok to the client.
> But, in myclient code, I'm not sure what to write in order to retrieve
> this BoxType object properly.
> 
> The apache-httpd sample only shows how to retrieve a custom type as a
> resource property by calling WsResourceClient.getPropertyAsObject().
> But, this can't be used when trying to get the custom type as a return
> value of an operation.
> 
> My client proxy class implements AbstractResourceClient.  The only
> methods available for calling resource operations are
> invoke(String,Element) and invoke(ProxyHandler,Object[]).  The first
> method returns an xml Element, which is not what I want because I'm
> expecting a BoxType object.  The second method doesn't make sense on the
> client side because the client would not know the reflection info to
> pass in the call (which must be specified to ReflectionProxyHandler, the
> only implementation of the ProxyHandler interface).  I think ideally,
> this should all be hidden from the client, and the only thing the client
> needs to know is the resource EPR, and the operation name and
> parameters.
> 
> Here's my server code (using XmlBeans for serialization):
>     public class BoxCapability extends AbstractCapability implements
> IBoxCapability
>     {
>         public BoxType boxOperation(int width) throws Exception
>         {
>             BoxDocument doc = BoxDocument.Factory.newInstance();
>             BoxType type = doc.addNewBox();
>             type.setWidth(BigInteger.valueOf(width));
>             type.setHeight(BigInteger.valueOf(width));
>             return type;
>         }
>     }
> 
> Here's my incomplete client code:
>     public class SimpleResourceClient extends AbstractResourceClient
>     {
>         public BoxType testBoxOperation(int width) throws SoapFault
>         {
>             Element body =
> XmlUtils.createElement(IBoxCapability.OP_QNAME, new Integer(width));
> 
>             BoxType response = ...  // what do I write here???
> 
>             return response;
>         }
>     }
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: muse-user-unsubscribe@ws.apache.org
For additional commands, e-mail: muse-user-help@ws.apache.org