Hello Axis Users,

I am posting this simple cookbook because it's the type of thing I think
lots of people need, and I haven't found a straitforward example that
describes explicitly.

This cookbook covers how to use a axis client and server with complex
serialized beans as a return type.

######################################################

1. Create your service.

This is just a simple stateful service (see previous post) that will
return a custom (complex) java type.

/*
 * StatefulService.java
 */
package com.noi.mailservlet.web.services;
import com.noi.mailservlet.web.bl.NameValue;

public class StatefulService
{
    private NameValue nv;
    
    
    public NameValue getNameValue(NameValue nv)
    {
        nv.setName("name1");
        nv.setValue("VALUE1");
          this.nv = nv;
        return this.nv;
    }
}

/*
 * NameValue.java
 */

package com.noi.mailservlet.web.bl;
import java.io.*;

public class NameValue implements Serializable {
    
    /** Holds value of property name. */
    private String name;
    
    /** Holds value of property value. */
    private String value;
    
    /** Creates a new instance of NameValue */
    public NameValue() {
    }
    
    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getValue() {
        return this.value;
    }
    
    public void setValue(String value) {
        this.value = value;
    }
    
}



######################################################

2. Define the deployment for the service, in this case I am using
BasicSessionHandler to provide a stateful service. Notice the "ns1"
namespace mapping in the deployment tag, this is required to ultimately
map the type to the client qname Later.

<?xml version="1.0" encoding="UTF-8"?>
<deployment
    xmlns="http://xml.apache.org/axis/wsdd/"; 
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"; 
    xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"; 
    xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance";
    xmlns:ns1="StatefulService"> 
    
    <service name="StatefulService" provider="java:RPC">
        <namespace>http://meis/mailservlet/</namespace> 
        <parameter name="scope" value="session"/>
        <requestFlow>
            <handler
type="java:org.apache.axis.handlers.SimpleSessionHandler"/> 
        </requestFlow>
        <responseFlow>
            <handler
type="java:org.apache.axis.handlers.SimpleSessionHandler"/> 
        </responseFlow>
        <parameter name="className"
value="com.noi.mailservlet.web.services.StatefulService"/>
        <parameter name="allowedMethods" value="*"/>
        <operation name="getNameValue" returnQName="returnqname"
returnType="ns1:NameValue" >
            <parameter name="nv" type="ns1:NameValue"/>
        </operation>
        <typeMapping  
 
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
 
languageSpecificType="java:com.noi.mailservlet.web.bl.NameValue" 
            qname="ns1:NameValue"
 
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" 
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
    </service>

</deployment>

This should generate the wsdl complex type:

<schema targetNamespace="http://bl.web.mailservlet.noi.com";
xmlns="http://www.w3.org/2001/XMLSchema";>
        <import namespace="http://schemas.xmlsoap.org/soap/encoding/"; />

        <complexType name="NameValue">
                <sequence>
                        <element name="name" nillable="true"
type="xsd:string" /> 
                        <element name="value" nillable="true"
type="xsd:string" /> 
                </sequence>
        </complexType>
</schema>


######################################################
3. invoke the "getNameValue" method via call in the client application

            
            Service  service = new Service();
            
            //configure the stateful client engine ommitted, in previous
post

                //get name value object;
                
            Call call    = (Call) service.createCall();
            call.setTargetEndpointAddress( new java.net.URL(endpointURL)
);
            call.setOperationName( new QName("StatefulService",
"getNameValue") );  
            NameValue nv = new NameValue();
            QName qn = new QName("StatefulService", "NameValue");
            call.registerTypeMapping(
                NameValue.class, 
                qn, 
                new
org.apache.axis.encoding.ser.BeanSerializerFactory(NameValue.class, qn),
                new
org.apache.axis.encoding.ser.BeanDeserializerFactory(NameValue.class,
qn)
                );
            
            call.addParameter("source", qn,
                          ParameterMode.IN); 
        
            call.setReturnType(qn);
            nv = (NameValue)call.invoke( new Object[] { (Object)nv } ); 
            
            System.out.println("name:"+nv.getName()+"
value:"+nv.getValue());

######################################################

That's it, I have whole java and wsdd files also, and can send them you
if you want them.


-----------------------------------------
Clay Graham
President
newObjectivity, Inc.
making the mobile-world-office
http://www.newobjectivity.com/



-----Original Message-----
From: Clay Graham [mailto:[EMAIL PROTECTED] 
Sent: Friday, April 04, 2003 1:18 PM
To: [EMAIL PROTECTED]
Subject: [newbie] Empty parameter client?


I have a service, its really just a simple bean pattern:

public class StatefulService
{
    private String value;
    
    public void setValue(String value)
    {
        this.value = value;
    }
    
    public String getValue()
    {
        return this.value;
    }
}

I am having a hard time figuring out what you would do from the client
side to use the "getValue" method of the service, it produces the
following wsdl operation:

<wsdl:operation name="getValue">
  <wsdlsoap:operation soapAction="" /> 
  <wsdl:input name="getValueRequest">
  <wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/";
namespace="http://meis/mailservlet/"; use="encoded" /> 
  </wsdl:input>
  <wsdl:output name="getValueResponse">
  <wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/";
namespace="http://meis/mailservlet/"; use="encoded" /> 
  </wsdl:output>
  </wsdl:operation>


I tried this:

            call.setOperationName( new QName("StatefulService",
"getValue") );
                
                //why do I have to do this?   
            call.addParameter( "testval", XMLType.XSD_ANY,
ParameterMode.IN);

            call.setReturnType(
org.apache.axis.encoding.XMLType.XSD_STRING);

                //why does invoke require an object array if there are
no parameters?
                //shouldn't there be an overloaded method with no
parameters?
            String ret = (String)call.invoke( new Object[] { null } );  
 

But it doesn't work, I can't find any services without parameters in the
examples, any ideas?



-----------------------------------------
Clay Graham
President
newObjectivity, Inc.
making the mobile-world-office
http://www.newobjectivity.com/



-----Original Message-----
From: Clay Graham [mailto:[EMAIL PROTECTED] 
Sent: Friday, April 04, 2003 10:13 AM
To: [EMAIL PROTECTED]
Subject: RE: Stateful web services.


This has been filed as bug# 18718

Hope it helps!

-----------------------------------------
Clay Graham
President
newObjectivity, Inc.
making the mobile-world-office
http://www.newobjectivity.com/



-----Original Message-----
From: Davanum Srinivas [mailto:[EMAIL PROTECTED] 
Sent: Thursday, April 03, 2003 5:44 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: RE: Stateful web services.


See directions for patches at
http://nagoya.apache.org/wiki/apachewiki.cgi?AxisProjectPages/SubmitPatc
hes

--- Clay Graham <[EMAIL PROTECTED]> wrote:
> Absolutely.
> 
> I am not real familiar with the development process of axis, but I am 
> more than willing to try!
> 
> What I will try to do is make a nice package based on the existing 
> docs, and then submit it (where?).
> 
> 
> 
> -----------------------------------------
> Clay Graham
> President
> newObjectivity, Inc.
> making the mobile-world-office
> http://www.newobjectivity.com/
> 
> 
> 
> -----Original Message-----
> From: Tom Jordahl [mailto:[EMAIL PROTECTED]
> Sent: Thursday, April 03, 2003 1:50 PM
> To: '[EMAIL PROTECTED]'
> Subject: RE: Stateful web services.
> 
> 
> 
> Clay,
> 
> This is great.  Do you think you could check out the current Axis HTML

> docs and see if you can put together a patch that would integrate this

> in to it?
> 
> Your code could just be new files in the samples directory.
> 
> If you could do that, then put the info in a Bugzilla report, that 
> would increase the chances that someone (me) would check it in to the 
> tree for others.
> 
> --
> Tom Jordahl
> Macromedia Server Development
> 
> -----Original Message-----
> From: Clay Graham [mailto:[EMAIL PROTECTED]
> Sent: Thursday, April 03, 2003 3:22 PM
> To: [EMAIL PROTECTED]
> Subject: RE: Stateful web services.
> 
> [SOLUTION]
> 
> I am including the full solution because I think this is the type of 
> thing everybody wants....
> 
> #####################################################################
> 
> 1. Create your service
> 
> /*
>  * NOIMailService.java
>  *
>  * Created on April 2, 2003, 5:19 PM
>  */
> 
> package com.noi.mailservlet.web.services;
> import javax.activation.*;
> import java.util.*;
> import javax.ejb.*;
> import javax.mail.*;
> import javax.mail.internet.*;
> import javax.mail.search.*;
> import java.io.*;
> 
> /**
>  *
>  * @author  clay
>  */
> public class NOIMailService {
>     
>     /** Holds value of property username. */
>     private String username;    
>     
>     /** Holds value of property password. */
>     private String password;
>     
>     /** Holds value of property hostname. */
>     private String hostname;
>     
>     private boolean connected;
>     
>     /** Holds value of property protocol. */
>     private String protocol;
>     
>     private static final String loginmbox = "INBOX";
>         
>     private Store store;
>     private Session session;
>     private URLName url;    
>     
>     /** Creates a new instance of NOIMailService */
>     public NOIMailService() {
>         this.connected = false;
>         this.protocol = "imap";
>     }
>     
>     
>     public boolean login(String protocol, String username, String 
> password, String hostname)
>     {     
>         
>         this.protocol = protocol;
>         this.username = username;
>         this.password = password;
>         this.hostname = hostname;
>         
>         try{
>             url = new URLName(
>                     this.protocol, 
>                     this.hostname, 
>                     -1, 
>                     this.loginmbox, 
>                     this.username, 
>                     this.password);
> 
>            Properties props = System.getProperties();
> 
>             if (hostname != null)
>                 props.put("mail.smtp.host", this.hostname);
>             else if (props.getProperty("mail.smtp.host") == null)
>                 props.put("mail.smtp.host", "localhost");
> 
>             this.session = Session.getDefaultInstance(props, null); 
>             this.session.setDebug(true);      
> 
>             PasswordAuthentication pw = new 
> PasswordAuthentication(url.getUsername(), this.url.getPassword());
>             this.session.setPasswordAuthentication(url, pw);
> 
>             this.store = this.session.getStore(url);       
>             this.store.connect();
>             this.connected = true;
>             return this.connected;
>         }
>         catch(Exception e)
>         {
>             this.connected = false;
>             return this.connected;
>         }
>         
>     }
>     
>     public boolean isConnected()
>     {
>         return this.connected;
>     }
>     
>     public boolean sendStatelessMessage(String protocol, String 
> username, String password, String hostname, String to, String cc, 
> String bcc, String subject, String body)
>     {
>         if(this.login(protocol, username, password, hostname))
>         {
>             try {
> 
>                 Message msg = new MimeMessage(this.session);
> 
>                 //to
>                 InternetAddress[] toAddrs = null;
>                 if ((to != null) && !to.equals("")) {
>                     toAddrs = InternetAddress.parse(to, false);
>                     msg.setRecipients(Message.RecipientType.TO,
> toAddrs);
>                 }
>                 else
>                     return false;
> 
> 
>                 //sent date
>                 msg.setSentDate(Calendar.getInstance().getTime());
> 
>                 //from
>                 String fromAddress = url.getUsername() +
> "@"+url.getHost();        
>                 msg.setFrom(new InternetAddress(fromAddress));
> 
>                 //cc
>                 InternetAddress[] ccAddrs = null;
>                 if ((cc != null) && !cc.equals("")) {
>                     ccAddrs = InternetAddress.parse(cc, false);
>                     msg.setRecipients(Message.RecipientType.CC,
> ccAddrs);           
>                 }
> 
>                 InternetAddress[] bccAddrs = null;
>                 if ((bcc != null) && !bcc.equals("")) {
>                     bccAddrs = InternetAddress.parse(bcc, false);
>                     msg.setRecipients(Message.RecipientType.BCC,
> toAddrs);
>                 }
> 
>                 //subject
> 
=== message truncated ===


=====
Davanum Srinivas - http://webservices.apache.org/~dims/

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com

Reply via email to