Implementing Web Services Using JAX-RPC

by Amol Raje


What is a Web Serivce?
Piece of business logic located somewhere on the internet that is accessible through standard-based internet protocols such as HTTP or SMTP.

Why Web Services ?
  • Web service is based on standardized XML.
  • Language neutral way of accessing data.
  • Supported by most major technology firms.
  • Increase the possibilities of software integration and interoperability.
Characteristics of a Web Service
  • XML-based
  • Loosely coupled
  • Course-grained
  • Synchronous/Asynchronous
  • Supports Remote Procedure Calls
  • Supports document exchange
What's at the core ?
  • Simple Object Access Protocol (SOAP)
  • Web Service Description Language (WSDL)
  • Universal Description, Discovery, and Integration (UDDI)
What is SOAP ? How does it fit into all this ?
  • Lightweight protocol for exchange of information in a decentralized, decentralized environment.
  • Provides a standard packaging structure for transporting XML documents over internet through HTTP, SMTP etc.
  • Provides encoding and binding standards for encoding non-XML RPC invocations in XML.
  • Can be bound to lower-level protocol like HTTP or SMTP.
  • Simple a simple structure for doing RPC document exchange.
What is WSDL ?
  • XML format for describing web services.
  • Describes a web service as a collection of ports and operations.
  • Standardizes a web service’s representation of its invocation details.
  • Allows disparate clients to understand how to interact with a web service.
What is UDDI ?
  • Provides a worldwide registry of web services for advertisement, discovery and integration purpose.
  • Directs systems looking for web services to documentation that provides them.
  • Provides white pages, yellow pages and green pages.
How they interact


How do developer write Web Services ?
Java provides you with a Java API for Web Services.
  • Allows developers to write web applications entirely in JAVA programming language.
  • Portability of data, portability of code and ease of use.
  • Reap maximum benefits of XML with little or no direct use of XML.
Jaxed !
1. Document Oriented
  • Java API for XML Processing (JAXP)
  • Java Architecture for XML binding (JAXB)
2. Procedure Oriented
  • Java API for XML Messaging (JAXM)
  • Java API for XML Registries (JAXR)
  • JAVA API for XML-based RPC (JAX-RPC)
JAXP
  • Makes it easy to process XML data with applications written in Java.
  • Leverages parser standards SAX and DOM.
  • Control over presentation of data through XSLT.
  • Provides namespace support to work with DTD.
JAXB
  • Provides an API and tool that allow two way mapping between XML documents and java objects.
  • It includes a compiler that maps a schema to a set of java classes.
  • This allows programmers to create java object representation of the XML data.
JAXM
  • Provides a standard way to send XML documents over the internet from the java platform.
  • Messaging capability for sending and receiving SOAP
  • SOAP packaging provides API for constructing and deconstructing SOAP and MIME envelopes
JAXR
  • Provides a standard way to send XML documents over the internet from the java platform.
  • Messaging capability for sending and receiving SOAP
  • SOAP packaging provides API for constructing and deconstructing SOAP and MIME envelopes
The Web Services Model


JAX RPC
  • Java API for developing and using web services.
  • Support for XML based RPC in the Java Platform
  • Interoperability between applications possible through JAX-RPC’s support for SOAP and WSDL.
  • The RPC is represented by an XML based protocol such as SOAP.
  • Complexities of the implementation of communication is hidden from both the web service and its client.
The Work Flow


At the server:
  • Compile the service’s interface and the implementation class.
  • Run the mapping tool which generates the WSDL and other lower-level classes called stubs and ties.
  • Package the application in a WAR file for distribution and installation.
  • Deploy the WAR which should have the deployment descriptor.
Invocation Types
  • The Static Stub
  • Dynamic Proxy Creation
  • Dynamic Invocation Interface
Static Stub :
- The mapping tool reads the configuration XML to get the location of WSDL and creates the stubs.
- Because the stubs are created before the client program runs it is called as the static stub model
e.g.
try{
Stub stub = createProxy();
HelloIF hello = (HelloIF)stub; System.out.println(hello.sayHello("Duke!"));
}
catch (Exception ex) {
ex.printStackTrace();
}

private static Stub createProxy() {
return (Stub)(new MyHello_Impl().getHelloIFPort());
}

Creating dynamic proxy :
- Needs the interface definition class. - Uses the ServiceFactory class to create instance of Service. - The getPort() method dynamically returns a proxy for the object being operated on.

Packages to be used:
java.net
javax.xml.rpc
javax.xml.namespace

Classes to be used:
java.net.URL
javax.xml.rpc.namespace.Qname: represents the value of a Qualified name. Qualified name contains namespaceURI and a localPart
javax.xml.rpc.Service
javax.xml.rpc.ServiceFactory

E.g.
String UrlString = "http://localhost:8080/ProxyHelloWorld.wsdl";
String nameSpaceUri = "http://proxy.org/wsdl";
String serviceName = "HelloWorld";
String portName = "HelloIFPort";
URL helloWsdlUrl = new URL(UrlString);
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service helloService = serviceFactory.createService(helloWsdlUrl, new QName(nameSpaceUri, serviceName));
HelloIF myProxy = (HelloIF) helloService.getPort( new QName(nameSpaceUri, portName), proxy.HelloIF.class);
System.out.println(myProxy.sayHello("Buzz"));

Dynamic Invocation Interface
- A client can call a remote procedure even if the signature of the remote procedure or the name of the service is unknown until runtime. - DII is based on the Call object which can be got by the Service.createCall() method. - Use javax.xml.rpc, javax.xml.namespace packages - Use java.xml.rpc.Call, javax.xml.rpc.Service, javax.xml.rpc.ParameterMode, javax.xml.rpc.ServiceFactory, javax.xml.namespace.Qname classes

E.g.
Call call = service.createCall(port);
call.setTargetEndpointAddress(endpoint);
call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true));
QName QNAME_TYPE_STRING = new QName(NS_XSD, "string");
call.setReturnType(QNAME_TYPE_STRING);
call.setOperationName( new QName(BODY_NAMESPACE_VALUE "sayHello"));
call.addParameter("String_1", QNAME_TYPE_STRING, ParameterMode.IN);
String[] params = { "Duke!" };
String result = (String)call.invoke(params);
System.out.println(result);



If you have any comments or questions, you can reach Amol at amol.raje@itcinfotech.com.