Web Service Axis : 

 

1   import org.apache.axis.client.Call;

2   import org.apache.axis.client.Service;

3   import javax.xml.namespace.QName;

4  

5   public class TestClient {

6     public static void main(String [] args) {

7       try {

8         String endpoint =

9             "http://ws.apache.org:5049/axis/services/echo";

10  

11        Service  service = new Service();

12        Call     call    = (Call) service.createCall();

13  

14        call.setTargetEndpointAddress( new java.net.URL(endpoint) );

15        call.setOperationName(new QName("http://soapinterop.org/", echoString"));

16  

17        String ret = (String) call.invoke( new Object[] { "Hello!" } );

18  

19        System.out.println("Sent 'Hello!', got '" + ret + "'");

20      } catch (Exception e) {

21        System.err.println(e.toString());

22      }

23    }

24  }

 

 

<?xml version="1.0" encoding="UTF-8"?> 

<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 

    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <SOAP-ENV:Body>

    <ns1:echoString xmlns:ns1="http://soapinterop.org/">

      <arg0 xsi:type="xsd:string">Hello!</arg0>

    </ns1:echoString>

  </SOAP-ENV:Body>

</SOAP-ENV:Envelope> 

 

 

Publishing web Applications : 

 

public class Calculator { 

  public int add(int i1, int i2) {

    return i1 + i2;

  }

 

  public int subtract(int i1, int i2) {

    return i1 - i2;

  }

}

 

% copy Calculator.java <your-webapp-root>/axis/Calculator.jws

Now for step 2... hm, wait a minute. You're done! You should now be able to access the service at the following URL (assuming your Axis web application is on port 8080): 

http://localhost:8080/axis/Calculator.jws

Axis automatically locates the file, compiles the class, and converts SOAP calls correctly into Java invocations of your service class. Try it out - there's a calculator client in samples/userguide/example2/CalcClient.java, which you can use like this: 

% java samples.userguide.example2.CalcClient -p8080 add 2 5 

Got result : 7 

% java samples.userguide.example2.CalcClient -p8080 subtract 10 9 

Got result : 1 

 

Web Service Deployment Descriptor (WSDD) 

 

<deployment xmlns="http://xml.apache.org/axis/wsdd/" 

    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

  <service name="MyService" provider="java:RPC">

    <parameter name="className" value="samples.userguide.example3.MyService"/>

    <parameter name="allowedMethods" value="*"/>

  </service>

</deployment> 

 

 

Service Styles - RPC, Document, Wrapped, and Message

 

?WSDL: Obtaining WSDL for deployed services

 

 

WSDL2Java: Building stubs, skeletons, and data types from WSDL

 

 

Java2WSDL: Building WSDL from Java

 

 

 

J2EE 5

 

 

 

A service endpoint interface or service endpoint implementation (SEI) is a Java interface or class, respectively, that declares the methods that a client can invoke on the service. An interface is not required when building a JAX-WS endpoint. The web service implementation class implicitly defines an SEI.

These are the basic steps for creating the web service and client: 

  1. Code the implementation class. 

  2. Compile the implementation class. 

  3. Use wsgen to generate the artifacts required to deploy the service.

  4. Package the files into a WAR file. 

  5. Deploy the WAR file. The web service artifacts (which are used to communicate with clients) are generated by the Application Server during deployment. 

  6. Code the client class. 

  7. Use wsimport to generate and compile the web service artifacts needed to connect to the service.

  8. Compile the client class. 

  9. Run the client. 

 

package helloservice.endpoint;

 

import javax.jws.WebMethod;

import javax.jws.WebService;

 

@WebService 

public class Hello {

    private String message = new String("Hello, ");

 

    public void Hello() {}

 

    @WebMethod

    public String sayHello(String name) {

        return message + name + ".";

    }

}

 

  wsgen -cp . helloservice.endpoint.Hello -wsdl -servicename http://localhost:8080/helloservice/hello

 

check URL: 

http://localhost:8080/helloservice/hello?WSDL

 

create port: 

  wsimport -d generated http://localhost:8080/helloservice/hello?WSDL

 

HelloClient is a stand-alone Java program that accesses the sayHello method of HelloService. It makes this call through a port, a local object that acts as a proxy for the remote service. The port is created at development time by the wsimport tool, which generates JAX-WS portable artifacts based on a WSDL file.

 

package simpleclient;

 

import javax.xml.ws.WebServiceRef;

import helloservice.endpoint.HelloService;

import helloservice.endpoint.Hello;

 

public class HelloClient {

    @WebServiceRef(wsdlLocation="http://localhost:8080/helloservice/hello?wsdl")

    static HelloService service;

 

    public static void main(String[] args) {

        try {

            HelloClient client = new HelloClient();

            client.doTest(args);

        } catch(Exception e) {

            e.printStackTrace();

        }

    }

 

    public void doTest(String[] args) {

        try {

            System.out.println("Retrieving the port from the following service: " + service);

            Hello port = service.getHelloPort();

            System.out.println("Invoking the sayHello operation on the port.");

 

            String name;

            if (args.length > 0) {

                name = args[0];

            } else {

                name = "No Name";

            }

 

            String response = port.sayHello(name);

            System.out.println(response);

        } catch(Exception e) {

            e.printStackTrace();

        }

    }

}

 

 

 

 

SAAJ

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SAAJ and DOM

The SAAJ APIs extend their counterparts in the org.w3c.dom package:

 

SAAJ Connection 

 

SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); 

SOAPConnection connection = factory.createConnection(); 

 

. . .// create a request message and give it content 

java.net.URL endpoint = new URL("http://fabulous.com/gizmo/order"); 

SOAPMessage response = connection.call(request, endpoint); 

 

Creating a Message

MessageFactory factory = MessageFactory.newInstance(); 

SOAPMessage message = factory.createMessage(); 

Accessing Elements of a Message

SOAPPart soapPart = message.getSOAPPart(); 

SOAPEnvelope envelope = soapPart.getEnvelope(); 

SOAPHeader header = envelope.getHeader(); 

SOAPBody body = envelope.getBody(); 

SOAPHeader header = message.getSOAPHeader(); 

SOAPBody body = message.getSOAPBody(); 

 

Adding Content to the Body

SOAPBody body = message.getSOAPBody(); 

QName bodyName = new QName("http://wombat.ztrade.com", "GetLastTradePrice", "m"); 

SOAPBodyElement bodyElement = body.addBodyElement(bodyName); 

QName name = new QName("symbol"); 

SOAPElement symbol = bodyElement.addChildElement(name); 

symbol.addTextNode("SUNW"); 

The content that you have just added to your SOAPBody object will look like the following when it is sent over the wire:

<SOAP-ENV:Envelope 

 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

  <SOAP-ENV:Body>

    <m:GetLastTradePrice xmlns:m="http://wombat.ztrade.com">

      <symbol>SUNW</symbol>

    </m:GetLastTradePrice>

  </SOAP-ENV:Body>

</SOAP-ENV:Envelope> 

 

 

SOAPBody body = message.getSOAPBody(); 

QName bodyName =  

    new QName("http://sonata.fruitsgalore.com", "PurchaseLineItems", "PO");

SOAPBodyElement purchaseLineItems = 

    body.addBodyElement(bodyName);

 

QName childName = new QName("Order"); 

SOAPElement order = purchaseLineItems.addChildElement(childName); 

 

childName = new QName("Product"); 

SOAPElement product = order.addChildElement(childName); 

product.addTextNode("Apple"); 

 

childName = new QName("Price"); 

SOAPElement price = order.addChildElement(childName); 

price.addTextNode("1.56"); 

 

childName = new QName("Order"); 

SOAPElement order2 = purchaseLineItems.addChildElement(childName); 

 

childName = new QName("Product"); 

SOAPElement product2 = order2.addChildElement(childName); 

product2.addTextNode("Peach"); 

 

childName = soapFactory.new QName("Price"); 

SOAPElement price2 = order2.addChildElement(childName); 

price2.addTextNode("1.48"); 

The SAAJ code in the preceding example produces the following XML in the SOAP body: 

<PO:PurchaseLineItems 

 xmlns:PO="http://sonata.fruitsgalore.com">

  <Order>

    <Product>Apple</Product>

    <Price>1.56</Price>

  </Order>

 

  <Order>

    <Product>Peach</Product>

    <Price>1.48</Price>

  </Order>

</PO:PurchaseLineItems> 

 

Sending a Message

SOAPConnectionFactory soapConnectionFactory = 

    SOAPConnectionFactory.newInstance();

SOAPConnection connection = soapConnectionFactory.createConnection(); 

java.net.URL endpoint = new URL("http://wombat.ztrade.com/quotes"); 

 

SOAPMessage response = connection.call(message, endpoint); 

connection.close(); 

Getting the Content of a Message

 

SOAPBody soapBody = response.getSOAPBody(); 

java.util.Iterator iterator = soapBody.getChildElements(bodyName); 

SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next(); 

String lastPrice = bodyElement.getValue(); 

System.out.print("The last price for SUNW is "); 

 

System.out.println(lastPrice); 

If more than one element had the name bodyName, you would have to use a while loop using the Iterator.hasNext method to make sure that you got all of them.

while (iterator.hasNext()) { 

    SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next();

    String lastPrice = bodyElement.getValue();

    System.out.print("The last price for SUNW is ");

    System.out.println(lastPrice);