Web Services with JAX-RPC and Apache Axis

Shared by: r7Be86
Categories
Tags
-
Stats
views:
31
posted:
6/25/2012
language:
English
pages:
38
Document Sample
scope of work template
							Web Services with
JAX-RPC and Apache Axis
Sep. 18, 2002



Pankaj Kumar (pankaj_kumar@hp.com),
Software Architect,
Web Services Management Organization,
Hewlett Packard Co.
  Session Objectives


• Web Services in Perspective
• Learn about Web Services, wire-level interoperability
  standards and portable Java APIs.
• Get to know JAX-RPC and Apache Axis to consume and
  produce Web Services.




                                                     Monday, June 25,
                                                                2012
     Speaker Introduction
• More than 12 years of development and project
  management experience
• Worked on Web Services product development at HP
• Expert Group Member of JAX-RPC and JSR109
• Contributor to Apache Axis and Cocoon projects
• Author of open source project XPB4J (http://xpb4j.sf.net)
• Frequent speaker at developer conferences
• Authoring a book on J2EE and Web Services Security
• Architect with HP Web Services Mgmt. Operation
• Home Page: http://www.pankaj-k.net
                                                 Monday, June 25,
                                                            2012
    Outline of the Session

•   Introduction to Web Services
•   Web Services Standards
•   Java support for Web Services
•   Apache Axis
    –   Installing Apache Axis
    –   Invoking a service
    –   Deploying a service
    –   Handlers
    –   Serializers and Deserializers
• Where to find more information
                                        Monday, June 25,
                                                   2012
 Web Services Infrastructure




Language and platform independent infrastructure for
      loosely-coupled, inter-operable, app2app
          communication over the Internet.




                                             Monday, June 25,
                                                        2012
 Web Services (Contd.)

Language and platform independent =>
   separation of specification and implementation
Loosely coupled =>
   message based, synchronous and asynchronous
   interactions.
Over the Internet =>
   No centralized control, use of established
   protocols, security considerations.
Inter-operable =>
   Standards based.
                                           Monday, June 25,
                                                      2012
  Early (Internet) Technologies
• SMTP/MIME
   – e-mail is still the killer app
• FTP, NNTP
• HTTP/HTTPS, HTML
   – the protocol behind Internet’s popularity



 Most of these facilitated app to human interaction
             over the Internet/intranet

                                                 Monday, June 25,
                                                            2012
  Early (intranet) Technologies
• DCE from OSF -- RPC based, procedural
• ORB -- object oriented, mostly synchronous
   – CORBA, COM/DCOM from Microsoft, Java RMI/EJBs
• MOM -- message oriented, synchronous as well as
  asynchronous
   – JMS ( Java API standard )
   – Many proprietary implementations

Most of these facilitated app2app interaction within a
 trusted intranet and without much consideration to
  interoperability across different implementations.
                                                 Monday, June 25,
                                                            2012
  App2App Interaction -- the Web
  Services Way
• Transport protocol
   – HTTP/HTTPS
• Data Encoding
   – SOAP (Simple Object Access Protocol), XML Schema
• Interface Description
   – WSDL (Web Services Description Language)
• Service Description and Discovery
   – UDDI (Universal Description, Discovery and
     Integration)
• Security
   – WS-Security, XML-Signature, XML-Encryption, ...
                                                       Monday, June 25,
                                                                  2012
  The Web Services Way
Web Services standards (SOAP, WSDL, … ): based
   on widely accepted Internet friendly technologies
  (HTTP/HTTPS, XML, …), are mostly orthogonal to
   each other and enjoy broad support from vendors



Web Services: Network accessible programs, expose
  functionality by receiving/sending SOAP messages
   over HTTP/HTTPS, and describe this interface as
                   WSDL descriptions.

                                            Monday, June 25,
                                                       2012
Additional Web Services
Infrastructure Components
• Key Management (Security)
   – XKMS
• Web Services Management
   – OMI (Open Management Interface)
• ...



        Interesting thing to note is that these
          are Web Services in themselves
                                                  Monday, June 25,
                                                             2012
  SOAP In One Slide
                                   SOAP1.1 Message
• XML based protocol for
                                      Structure
  exchange of information                                   SOAP
   – Encoding rules for datatype                           Envelope
     instances
                                                             Header
   – Convention for representing                             Entries
     RPC invocations
• Designed for loosely-coupled                                [Header
  distributed computing                                       Element]
   – No remote references
                                                                Body
• Used with XML Schema                                         Element
• Transport independent
• SOAP with Attachments                                       [Fault
  allow arbitrary data to be                                Element]
  packaged.
                                                Monday, June 25,
                                                           2012
  WSDL in One Slide
                                 WSDL1.1 Document
                                    Structure
• A WSDL document describes                                  WSDL
   – What the service can do                               Document
   – Where it resides                                     [Types]
   – How to invoke it
                                                          {Messages}
• WSDL are like IDL but lot
  more flexible and extensible
                                                        {Port Types}
• Defines binding for SOAP1.1,
  HTTP GET/POST and MIME                                  {Bindings}
• WSDL descriptions can be
  made available from an                                  {Services}
  UDDI registry


                                               Monday, June 25,
                                                          2012
  Java APIs for Web Services

• SOAP messages as Java objects
   – SAAJ ( SOAP with Attachments API for Java)
• Programming Model
   – JAX-RPC ( JSR101), JSR109, EJB2.1
• Accessing WSDL descriptions
   – JWSDL (JSR110)
• Accessing Web Services Registries
   – JAXR (Java API for XML Registries)



                                                  Monday, June 25,
                                                             2012
SAAJ Object Model
SOAPPart         SOAPMessage              AttachmentPart



                     Node

                                                     SOAPFault
                 SOAPElement

                                                  SOAPFaultElement
                                                                         *
     SOAPBody                    SOAPHeader


 * SOAPBodyElement          SOAPHeaderElement *


                SOAPEnvelope
                                                      Monday, June 25,
                                                                 2012
    JAX-RPC

•   WSDL/XML to Java Mapping
•   Java to WSDL/XML Mapping
•   SOAP Message with Attachments
•   Client API
    – Classes generated from WSDL
    – Dynamic Proxy
    – DII call Interface
• SOAP Message Handler
• Extensible Type Mapping
                                    Monday, June 25,
                                               2012
 JAX-RPC Physical Architecture

                             WSDL description

Service Client                                                Service Endpoint

        Stub
                              WSDL<->Java Mapping     Container
                                                                  Dispatch



           JAX-RPC API                                   JAX-RPC API
       Client Side JAX-RPC                          Server Side JAX-RPC
         Runtime System                               Runtime System
                               Protocol (SOAP)
                                 Transport



                                                                      Monday, June 25,
                                                                                 2012
Interoperability and JAX-RPC

                  WSDL description
   Client                               Service
JAX-RPC impl.                         Vendor prod.
 <Vendor A>                           <Vendor C>




    Client                              Service
 Vendor prod.                        J2EE Container
 <Vendor B>     WSDL description      <Vendor D>



                                              Monday, June 25,
                                                         2012
  Apache Axis

• A SOAP Processing Engine
  –   JAX-RPC Client System
  –   JAX-RPC Server System ( Servlet based )
  –   SAAJ implementation
  –   Flexible and extensible architecture
  –   Tools, Examples, Documentation, …
  –   A great place to learn about Web Services !!
• Open-source, hosted by Apache Software
  Foundation
• Ready for use ( RC1 released on Sep. 6)

                                                     Monday, June 25,
                                                                2012
     Install & Deploy Apache Axis
• Make sure that you have
   – J2SE SDK 1.3 or 1.4: We will use 1.4                 Direcotry Structure:
   – A Servlet Container: We will use
     Tomcat4.0.1                                              axis-1_0

• Download xml-axis-rc1-bin.zip
  from http://xml.apache.org/axis            webapps        lib     docs         samples
• Unzip it and look at the dir. tree. Note
  that Axis runs as a Servlet.                axis

• Deploy Axis.                               WEB-INF
   – Copy webapps\axis tree to webapps
     directory of Tomcat.                            lib
   – Alternatively, modify server.xml of             classes
     Tomcat.
                                                     web.xml
• Run Tomcat: issue bin\startup from
  Tomcat home.                                       ……

                                                                   Monday, June 25,
                                                                              2012
    Test the Deployment
• Point your browser to http://localhost:8080/axis




                                                 Monday, June 25,
                                                            2012
 A Simple Example

•AddFunction: A simple Java
class with method to add two
integers. Notice the filename
extension – it is .jws ( for Java
                                        // File: AddFunction.jws
Web Service).                           public class AddFunction {
                                          int addInt(int a, int b){
•Deploy it. Just copy the                   return(a+b);
AddFunction.jws file to                   }
webapps/axis directory.                 }


•Examine its WSDL description.
Point your browser to               Note: All sources with instructions
http://localhost:8080/axis/AddFu    to run are available at my web-site:
nction.jws?wsdl                     http://www.pankaj-k.net
                                                         Monday, June 25,
                                                                    2012
  Writing the Client Program

• There are many ways to write a Client program
   – Using Dynamic Invocation Interface ( DII)
   – Using generated Stubs from Service WSDL description
   – Using Dynamic Proxy
• We will look at each of these


       Writing the client requires more work
              than writing the service


                                                   Monday, June 25,
                                                              2012
      AddFunctionClient – using DII
// File: lesson1\client\dii\AddFunctionClient.java, edited for presentation
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.namespace.QName;

public class AddFunctionClient {
  public static void main(String [] args) {
    try {
      String endpoint = "http://localhost:8080/axis/AddFunction.jws";
      Service service = new Service();
      Call   call = (Call) service.createCall();
      call.setOperationName(new QName(endpoint, "addInt"));
      call.setTargetEndpointAddress( new java.net.URL(endpoint) );
      Integer ret = (Integer)call.invoke(new Object[]{new Integer(5), new Integer(6)});
      System.out.println("addInt(5, 6) = " + ret);
    } catch (Exception e) {
           System.err.println("Execution failed. Exception: " + e);
    }
  }
}

                                                                        Monday, June 25,
                                                                                   2012
Compiling and Running the
DII Client




                            Monday, June 25,
                                       2012
  AddFunctionClient – using
  Dynamic Proxy
// File: lesson1\client\dproxy\AddFunctionClient.java, edited for presentation
import javax.xml.namespace.QName;
import javax.xml.rpc.*;
public class AddFunctionClient {
  public static void main(String [] args) {
     try {
       String wsdlUrl = "http://localhost:8080/axis/AddFunction.jws?wsdl";
       String nameSpaceUri = "http://localhost:8080/axis/AddFunction.jws";
       String serviceName = "AddFunctionService";
       String portName = "AddFunction";
       ServiceFactory serviceFactory = ServiceFactory.newInstance();
       Service afs = serviceFactory.createService(new java.net.URL(wsdlUrl),
                  new QName(nameSpaceUri, serviceName));
       AddFunctionServiceIntf afsIntf = (AddFunctionServiceIntf)afs.getPort(
                  new QName(nameSpaceUri, portName), AddFunctionServiceIntf.class);
       System.out.println("addInt(5, 3) = " + afsIntf.addInt(5, 3));
       } catch (Exception e) {
           System.err.println("Execution failed. Exception: " + e);
       }
   }
}                                                                        Monday, June 25,
                                                                                    2012
Compiling and Running the
Dynamic Proxy Client




                            Monday, June 25,
                                       2012
   AddFunctionClient – using
   Generated Stubs
Generate the stubs:
java org.apache.axis.wsdl.WSDL2Java \
  http://localhost:8080/axis/AddFunction.jws?wsdl

// File: lesson1\client\stub\AddFunctionClient.java, edited for
presentation
Import localhost.*;
public class AddFunctionClient{
  public static void main(String [] args) {
    try {
      AddFunctionService afs = new AddFunctionServiceLocator();
      AddFunction af = afs.getAddFunction();
      System.out.println("addInt(5, 3) = " + af.addInt(5, 3));
    } catch (Exception e) {
      System.err.println("Execution failed. Exception: " + e);
    }
  }
}


                                                                  Monday, June 25,
                                                                             2012
Generating Stubs, Compiling
and Running the Stub Client




                              Monday, June 25,
                                         2012
  Deployment Descriptors

• JWS deployment is simple, but has limitations:
   – You must have the source code
   – Can’t specify custom type mappings, handlers etc.
• WSDD (Web Services Deployment Descriptors)
  allow more flexible deployments
   –   Handlers in request or response path
   –   Custom type mappings
   –   Different transports – HTTP/S, TCP/IP, DIME
   –   Different Dispatchers – Java Class, EJB, Servlet
   –   …

                                                          Monday, June 25,
                                                                     2012
 Adding complexitiy to the Simple
 Example
•AddFunction1: A simple
                                    // File: Complex.java
Java class with method to add       public class Complex {
two Complex numbers.                  public Complex(){}
                                      public double getR(){ … }
Complex is user defined Java          public void setR(double r){ … }
class.                                …
                                      public Complex add(Complex c){ … }
•Deploy it.
    •Compile sources
    •Copy .class files.                  // File: AddFunction1.java
    •Write deployment descriptor         public class AddFunction1 {
    •Run AdminClient.                      public Complex addComplex
                                             (Complex a, Complex b){
•Examine its WSDL description.               return a.add(b);
Point your browser to                      }
                                         }
http://localhost:8080/axis/servic
es/AddFunction1Service?wsdl
                                                         Monday, June 25,
                                                                    2012
The Deployment Descriptor
// File: lesson2\service\deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <handler name="print" type="java:LogHandler"/>
   <service name="AddFunction1Service" provider="java:RPC">
   <requestFlow>
     <handler type="print"/>
   </requestFlow>
  <parameter name="className" value="AddFunction1"/>
  <parameter name="allowedMethods" value="*"/>
  <beanMapping qname="myNS:Complex" xmlns:myNS="urn:BeanService"
               languageSpecificType="java:Complex"/>
 </service>
</deployment>
                   Note:
                   (1) xmlns:java
                   (2) A handler in the request path
                   (3) Dispatch to RPC provider
                   (4) Bean type mapping                            Monday, June 25,
                                                                               2012
Deploying the Service




                        Monday, June 25,
                                   2012
   AddFunction1Client – using
   Generated Stubs
Generate the stubs:
java org.apache.axis.wsdlWSDL2Java \
  http://localhost:8080/axis/services/AddFunction1Service?wsdl

// File: lesson2\client\stub\AddFunction1Client.java, edited
import localhost.*;
import BeanService.*;
public class AddFunction1Client {
  public static void main(String [] args) throws Exception {
    Complex a = new Complex();
    Complex b = new Complex();
                                                                  Generated
    a.setR(10.0); a.setI(5.0);
                                                                    class
    b.setR(3.0); b.setI(2.0);
    AddFunction1Service afs = new AddFunction1ServiceLocator();
    AddFunction1 af = afs.getAddFunction1Service();
    Complex ret = af.addComplex(a, b);
    System.out.println("addComplex(a + b) = ("
             + ret.getR() + ", " + ret.getI() + ")");
  }
}
                                                                    Monday, June 25,
                                                                               2012
Running the Client




                     Monday, June 25,
                                2012
    Additional (Advanced!) Features

•   SOAP with Attachments
•   Custom type mappings (Pluggable Serializers)
•   One-way invocations
•   Document exchange
•   Dispatch to EJBs
•   HTTPS transport and mutual authentication
•   Username and password based authentication
•   …


                                              Monday, June 25,
                                                         2012
  Where to find more information?

• Apache Axis Home: http://xml.apache.org/axis
• Sun’s Web Services Developer Pack Home:
  http://java.sun.com/webservices/webservicespack.
  html
• W3C’s Web Services Activity Home Page:
  http://www.w3.org/2002/ws/
• My Home Page: http://www.pankaj-k.net




                                            Monday, June 25,
                                                       2012
Thank
 You
        Monday, June 25,
                   2012

						
Related docs
Other docs by r7Be86
Early Years of the Cold War
Views: 0  |  Downloads: 0
nat turner - PowerPoint
Views: 43  |  Downloads: 0
Global Trends in Truncation - PowerPoint
Views: 34  |  Downloads: 0
NPPO VHA -Cash Receipts Cycle
Views: 2  |  Downloads: 0
Jhony Peralta
Views: 0  |  Downloads: 0