users@jax-rpc.java.net

JAXM Client for JAX-RPC Web Service

From: Ramasamy S/O Valliappan <rama_at_SIMTECH.A-STAR.EDU.SG>
Date: Fri, 18 Jul 2003 10:18:50 +0800

Hi Guys

The following is my JAX-RPC Hello Service WSDL

<?xml version="1.0" encoding="UTF-8" ?>
- <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://com.test/wsdl/MyHello" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="MyHello" targetNamespace="http://com.test/wsdl/MyHello">
<types />
- <message name="HelloIF_sayHello">
<part name="String_1" type="xsd:string" />
</message>
- <message name="HelloIF_sayHelloResponse">
<part name="result" type="xsd:string" />
</message>
- <portType name="HelloIF">
- <operation name="sayHello" parameterOrder="String_1">
<input message="tns:HelloIF_sayHello" />
<output message="tns:HelloIF_sayHelloResponse" />
</operation>
</portType>
- <binding name="HelloIFBinding" type="tns:HelloIF">
- <operation name="sayHello">
- <input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://com.test/wsdl/MyHello" />
</input>
- <output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="http://com.test/wsdl/MyHello" />
</output>
<soap:operation soapAction="" />
</operation>
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
</binding>
- <service name="MyHello">
- <port name="HelloIFPort" binding="tns:HelloIFBinding">
<soap:address xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" location="http://localhost:8080/hello-jaxrpc/hello" />
</port>
</service>
</definitions>

The following is my jaxm hello client for the above service (please correct the code if any errors)

package hello;

import java.net.*;
import java.io.*;
import java.util.*;

import javax.servlet.http.*;
import javax.servlet.*;

import javax.xml.soap.*;

import javax.activation.*;
import org.apache.commons.logging.*;

public class JaxmHelloClient {
    static Log logger = LogFactory.getFactory().getInstance("Samples/Hello");

    // Connection to send messages.
    static SOAPConnection con;
    static SOAPMessage msg;
    static SOAPMessage reply;
    
    public static void main(String[] args) {
      try {
          SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
          con = scf.createConnection();
        } catch(Exception e) {
          logger.error("Unable to open a SOAPConnection", e);
        }
        String retval ="<html><H4>";

        try {
            // Create a message factory.
            MessageFactory mf = MessageFactory.newInstance();

            // Create a message from the message factory.
            msg = mf.createMessage();

            // Message creation takes care of creating the SOAPPart - a
            // required part of the message as per the SOAP 1.1
            // specification.
            SOAPPart sp = msg.getSOAPPart();

            // Retrieve the envelope from the soap part to start building
            // the soap message.
            SOAPEnvelope envelope = sp.getEnvelope();

            // Create a soap header from the envelope.
            SOAPHeader hdr = envelope.getHeader();

            // Create a soap body from the envelope.
            SOAPBody bdy = envelope.getBody();

            // Add a soap body element to the soap body
            SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName("sayHello"));

            gltp.addChildElement(envelope.createName("String_1")).addTextNode("RAM");

            // Create an endpoint for the recipient of the message.
            URL urlEndpoint = new URL("http://localhost:8080/hello-jaxrpc/hello");

            System.err.println("Sending message to URL: "+urlEndpoint);
            System.err.println("Sent message is logged in \"sent.msg\"");

            retval += " Sent message (check \"sent.msg\") and ";

            FileOutputStream sentFile = new FileOutputStream("sent.msg");
            msg.writeTo(sentFile);
            sentFile.close();

            // Send the message to the provider using the connection.
            reply = con.call(msg, urlEndpoint);

            if (reply != null) {
                FileOutputStream replyFile = new FileOutputStream("reply.msg");
                reply.writeTo(replyFile);
                replyFile.close();
                System.err.println("Reply logged in \"reply.msg\"");
                retval += " received reply (check \"reply.msg\"). </H4></html>";

            } else {
                System.err.println("No reply");
                retval += " no reply was received. </H4></html>";
            }

        } catch(Throwable e) {
            e.printStackTrace();
            logger.error("Error in constructing or sending message "
            +e.getMessage());
            retval += " There was an error " +
            "in constructing or sending message. </H4></html>";
        }

        try {
            //resp.setContentType("text/xml");
            System.out.println(retval.getBytes());
            System.out.println("<br><b> SOAP Request Message : </b><br><br>".getBytes());
            msg.writeTo(System.out);
            
            System.out.println("<br><br><b>SOAP Response Message : </b><br><br>".getBytes());
            reply.writeTo(System.out);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error( "Error in outputting servlet response "
            + e.getMessage());
        }
    }
}

How can I write a JAXM-Client to invoke this web service or any other web service ? Is it possible for a JAXMClient to invoke a JAX-RPC Service ?


From
V. RAM
www.jssl.org
Singapore.