1 / 78

XML and Web Technologies

Spring 2010. mark.baker@computer.org. Outline. Parts of a WS Solution.Java APIs,Axis SOAP Engine.Axis Components.WSDD Administration.RPC Communications.Axis Set up.Happy AxisExamples/Demos:HelloServer,SimpleCalc,Summary.. Spring 2010. mark.baker@computer.org. Parts of a Web Services Solu

kim
Télécharger la présentation

XML and Web Technologies

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Spring 2010 mark.baker@computer.org XML and Web Technologies Prof Mark Baker ACET, University of Reading Tel: +44 118 378 8615 E-mail: Mark.Baker@computer.org Web: http://acet.rdg.ac.uk/~mab

    2. Spring 2010 mark.baker@computer.org Outline Parts of a WS Solution. Java APIs, Axis SOAP Engine. Axis Components. WSDD Administration. RPC Communications. Axis Set up. Happy Axis Examples/Demos: HelloServer, SimpleCalc, Summary.

    3. Spring 2010 mark.baker@computer.org Parts of a Web Services Solution There are three major parts to a WS solution. A programming model: How you write clients to access Web services, How you write service implementations, How you handle other parts of the SOAP spec (headers, attachments, etc.). A deployment model: Deployment descriptors to map service implementations to SOAP messages, Type mapping for parameters (at least in RPC). An Engine: Code to receive SOAP messages and invoke service implementations, Code to map Java types to XML.

    4. Spring 2010 mark.baker@computer.org 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).

    5. Spring 2010 mark.baker@computer.org The parts of a Web Services Solution In Apache Axis the preferred programming model is JAX-RPC. The deployment model is the Axis deployment model. Apache Axis is the underlying SOAP engine.

    6. Spring 2010 mark.baker@computer.org Apache Axis SOAP Engine Architecture A SOAP engine provides: Classes to create and parse SOAP messages, Server-side infrastructure for deploying, managing and running SOAP enabled services, Client-side API for invoking SOAP services.

    7. Spring 2010 mark.baker@computer.org What does a SOAP Engine do? Receive a message from a transport. Check the SOAP semantics. Process the SOAP headers. Deserialize the message. Route the message to the service. Serialize the response, Process the response SOAP headers. Send the response back over the transport.

    8. Spring 2010 mark.baker@computer.org What is Axis? Apache eXtensible Interaction System. Axis is a SOAP message processing engine. Primarily for SOAP messaging between service requester and service provider Axis is SOAP v3.0 of the Apache Software Foundation: Re-architecture of SOAP Processing, Goals of the architecture: Performance: By use of SAX, instead of DOM. Modularity, Pluggability, SOAP 1.2 compliance and interoperability.

    9. Spring 2010 mark.baker@computer.org Axis Components Axis Engine: The main entry point into the SOAP processor. Handlers: Building blocks inside Axis that link Axis to existing back ends. Chain: An ordered collection of Handlers. Transports: Mechanisms by which SOAP messages flow into and out of Axis. Deployment/Configuration: How Web Services are made available through Axis Serializers/DeSerializers: Code that will convert native data types to XML and back.

    10. Spring 2010 mark.baker@computer.org Axis Components

    11. Spring 2010 mark.baker@computer.org JAX-RPC JAX-RPC is the Java standard for a Web Services programming model: APIs for XML based RPC, Base-level protocol bindings: SOAP/HTTP, Not limited to HTTP; allows for other protocol bindings, but are not specified, Final release June 2002, JAX-RPC is designed for portability of code across multiple vendors: Any client or service implementation should be portable across any vendor that supports JAX-RPC. Apache Axis supports the JAX-RPC Programming model: Should be what all new WS in Java are developed to.

    12. Spring 2010 mark.baker@computer.org An Architectural View JAX-RPC and JSR-109 fit together to form a standard approach for Web Services implementation, access and deployment in Java Based on a few concepts: A Service Implementation (either a Java Bean or Stateless Session Bean) implements the methods of a WSDL-Described interface, A Service Endpoint Interface provides a Java "view" of the WSDL-described interface, Clients interact with objects implementing this interface, A Service mediates access to the Ports (acts as a factory for ports). SAX is the Simple API for XML JSR == Java Specification Requests SAX is the Simple API for XML JSR == Java Specification Requests

    13. Spring 2010 mark.baker@computer.org JAX RPC Features Mappings for WSDL and XML: Standard Java type <-> XML type mapping, Standard Java type <-> WSDL type mapping, Standard SOAP binding mapping. SOAP manipulation library: JAX-RPC is built on the SAAJ (Soap Attachments API for Java): SAAJ was previously part of JAX-M, Provides a pattern for sending and receiving SOAP messages, with or without attachments, synchronous and asynchronous. Servlet container Service Endpoint: Standard way to specify servlets that receive SOAP messages. Basic authentication and security. Handlers.

    14. Spring 2010 mark.baker@computer.org 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. Release 1.2 in September 2004.

    15. Spring 2010 mark.baker@computer.org Sub-systems The Axis source code is not as cleanly separated into sub-systems like this. Some sub-systems are spread over several packages and some packages overlap more than one sub-system.

    16. Spring 2010 mark.baker@computer.org Administration Sub-system The administrative sub-system provides a way of configuring Axis engines. The configuration information an engine needs is a collection of factories for runtime artefacts such as: Chains, SOAPServices, A set of global configuration options for the engine. The Message Flow sub-system's EngineConfiguration interface is implemented by the Administration sub-system.

    17. Spring 2010 mark.baker@computer.org WSDD-Based Administration WSDD is an XML grammar for deployment descriptors which are used to statically configure Axis engines – look at this later. Each Handler needs configuration in terms of: The concrete class name of a factory for the Handler, A set of options for the handler, A lifecycle scope value which determines the scope of sharing of instances of the Handler. The structure of the WSDD grammar is mirrored by a class hierarchy of factories for runtime artefacts.

    18. Spring 2010 mark.baker@computer.org Message Model Sub-system SOAP Message Model: The XML syntax of a SOAP message is fairly simple. A SOAP message consists of an envelope containing: An optional header containing zero or more header entries (sometimes ambiguously referred to as headers), A body containing zero or more body entries, and Zero or more additional, non-standard elements. The only body entry defined by SOAP is a SOAP fault which is used for reporting errors.

    19. Spring 2010 mark.baker@computer.org WSDL Tools Sub-system The WSDL Tools sub-system contains WSDL2Java and Java2WSDL. The Axis runtime does not depend on these tools – they are just there to make life easier for the user. WSDL2Java: This tool takes a description of a Web Service written in WSDL and emits Java artefacts used to access the web service: There are three layers inside the tool: Framework: SymbolTable, Emitter, WriterFactory WSDL2Java plugin to the framework: WSDL2Java (the main), JavaWriterFactory, and all the WSDL-relative writers: JavaPortTypeWriter, JavaBindingWriter, etc. The actual WSDL2Java emitters, one for each file generated: JavaInterfaceWriter, JavaStubWriter, etc.

    20. Spring 2010 mark.baker@computer.org Examples Writing SOAP with Java: Java Clients using SOAP: HelloServer, Calculator.

    21. Spring 2010 mark.baker@computer.org RPC Style Communications You now know all about SOAP! SOAP is not limited to a particular style of distributed computing: Looked at SOAP over HTTP, but there are other possible bindings, Bindings to other transport protocols need not be limited to request/response style of communications, We will spend most of the time on RPC. Four items needed for RPC: Target object (address), Method name, Method parameters, SOAP header data.

    22. Spring 2010 mark.baker@computer.org RPC Style Communications Need for target object: Protocol, Server address, Resource address, All above are in a URI.

    23. Spring 2010 mark.baker@computer.org RPC Items Method Name: A service might have multiple methods, For example, a calculator service might have an "add" and a "multiply" method, The method name is used as the element name which is the immediate child of the Body element in the SOAP message. The method name element usually (by convention) is given the namespace equal to the target (service) object's address: <SOAP-ENV:Envelope xmlns:SOAP-ENV="..." SOAP-ENV:encodingStyle="..."> <SOAP-ENV:Body> <color:GetFavoriteColor xmlns:color="http://acet.rdg.ac.uk/mab/color"> ... </color:GetFavoriteColor> </SOAP-ENV:Body> </SOAP-ENV:Envelope

    24. Spring 2010 mark.baker@computer.org RPC Items Method Parameters: Each parameter is an element that is an immediate child of the method-name element, All parameters are "in" parameters: There are no "in/out" parameters, The "out" parameters are all in the response message. Like the URL encoded parameters on the GET method in HTTP, SOAP messages are not coerced into passing the correct parameters: It is up to the service to determine if the necessary parameters are passed, When the service does not have the requisite parameters, it returns a fault.

    25. Spring 2010 mark.baker@computer.org RPC Items Method Response By convention, given request method name with "Response" appended: computeSizeRequest computeSizeResponse favoriteColorRequestMsg favoriteColorResponseMsg

    26. Spring 2010 mark.baker@computer.org What is Axis? Recall, that Axis is essentially a SOAP engine - a framework for constructing SOAP processors such as clients, servers, gateways, etc. The current version of Axis is written in Java, but a C++ implementation of the client side of Axis is being developed. But Axis is not just a SOAP engine, it also includes: A simple stand-alone server, A server which plugs into servlet engines such as Tomcat, Extensive support for the Web Service Description Language (WSDL), Emitter tooling that generates Java classes from WSDL. Some sample programs, and A tool for monitoring TCP/IP packets.

    27. Spring 2010 mark.baker@computer.org Axis Setup Copy the webapps/axis directories from the Axis distribution to the <Tomcat_Home>/webapps directory: This will result in <Tomcat_Home>/webapps/axis. You do not need to name this "axis“: Whatever you name it will be the basis of the URL for all your Web Services. Copy .jws files in <Tomcat_Home>/webapps/axis.

    28. Spring 2010 mark.baker@computer.org Axis Setup If your Web service is packaged in a .jar file, put the .jar file in: <Tomcat_Home>/webapps/axis/WEB-INF/lib. On your home machine, you can start the tomcat server, and then go to the main axis page: The default port is 8080. The main Axis page on my machine is: http://localhost:8080/axis

    29. Spring 2010 mark.baker@computer.org Axis set-up - more Ensure you have a Web server installed, I use Tomcat. Second, download Axis's latest release from http://xml.apache.org/axis/. Third, copy the Axis folder from axis-1_2\webapps into <Tomcat_Home>\webapps. Finally, set up a context in the server.xml file in the directory <Web_Home>\conf by adding this line: <Context path="/axis" docBase="axis" debug="0" reloadable="true"/> - depends on what version of Tomcat! That is it - Axis is now installed and ready to use. Check - http://localhost:8080/admin/Check - http://localhost:8080/admin/

    30. Spring 2010 mark.baker@computer.org Axis Main Page

    31. Spring 2010 mark.baker@computer.org Happy Axis - Validate

    32. Spring 2010 mark.baker@computer.org An Axis View List of Deployed Services

    33. Spring 2010 mark.baker@computer.org Axis Version Service WSDL

    34. Spring 2010 mark.baker@computer.org Axis – Call local endpoint

    35. Spring 2010 mark.baker@computer.org Create the Web service The Web service that we will create here is the "Hello World" of Web Services. As you can see below, it is a class that has one method, sayHelloTo(), which takes a name as its only parameter and returns a customised hello message. Note that the class is not littered with any Axis-specific code.

    36. Spring 2010 mark.baker@computer.org Deploy the Web Service the easy way To deploy this Hello World Web Service, just copy and paste the HelloServer.java file into the <Tomcat_Home>\webapps\axis\ directory and change its extension from .java to .jws. That is all there is to it; the Web Service is deployed and ready for use. And you did not even have to compile the Java file! When a client invokes a service deployed in this manner, Axis will automatically locate the file, compile the class, and convert SOAP calls correctly into your service class's Java invocations. As you will see, the service name corresponds to the name of the .jws file.

    37. Spring 2010 mark.baker@computer.org Deploy the Web service the easy way The magic that allows the Web server to invoke the Axis engine (i.e., servlet) when a .jws file is requested is located in the <Tomcat_Home>\webapps\axis\WEB-INF directory. This file is appropriate section is shown below: Based on the above configuration information, whenever Tomcat - or any J2EE-compliant Web server - encounters a .jws file in the Axis context (set up above during installation), it will invoke the AxisServlet. The AxisServlet then performs its magic as described above.

    38. Spring 2010 mark.baker@computer.org Deploy the Web service the easy way

    39. Spring 2010 mark.baker@computer.org Axis Example

    40. Spring 2010 mark.baker@computer.org Create Client1 Now let us create a simple client that we will use to test the service. The code to do that is shown here:

    41. Spring 2010 mark.baker@computer.org Create the client1 In the simplest case (as this one), you need to create only an instance of the class org.apache.axis.client.Service. The endpoint here is http://localhost:8080/axis/HelloServer.jws, since: The Web server is on the same machine as the client; The Tomcat by default listens for HTTP requests on port 8080; The service is deployed as HelloServer.jws. Finally, you invoke any method on the service by simply calling the invoke() method on the call instance. At the very least, invoke() requires both the method's name on the service to invoke, and an Object array of parameters.

    42. Spring 2010 mark.baker@computer.org Test the service To test the service, begin by compiling the client using the following command sequence: Note that axis.jar must be included in the CLASSPATH to successfully compile the client application: Add <AXIS_HOME>\lib\axis.jar to your CLASSPATH. > javac Client1.java Start your Web Server, if it is not already started. To start Tomcat, run the startup.bat batch file located in <Web_Home>\bin\. Next, run the client application: > java Client1 Mark Axis Admin tool The Apache Admin tool is a utility class that generates WAR level global configuration files based on deployment information specific to some services. The input to this utility is a XML file (typically named deploy.wsdd) containing deployment information about one or more services. The Apache Admin utility adds some global definitions that are necessary and writes an output file. Use this tool as follows. NOTE: You must set your CLASSPATH to include all jar files in the <install-dir>\lib\axis directory, before you run the following command. <java org.apache.axis.utils.Admin server|client deployment-file> This tool generates server-config.wsdd or client-config.wsdd based on what option you choose. Check out… http://article.gmane.org/gmane.text.xml.axis.devel/7138/match=13903Axis Admin tool The Apache Admin tool is a utility class that generates WAR level global configuration files based on deployment information specific to some services. The input to this utility is a XML file (typically named deploy.wsdd) containing deployment information about one or more services. The Apache Admin utility adds some global definitions that are necessary and writes an output file. Use this tool as follows. NOTE: You must set your CLASSPATH to include all jar files in the <install-dir>\lib\axis directory, before you run the following command. <java org.apache.axis.utils.Admin server|client deployment-file> This tool generates server-config.wsdd or client-config.wsdd based on what option you choose. Check out… http://article.gmane.org/gmane.text.xml.axis.devel/7138/match=13903

    43. Spring 2010 mark.baker@computer.org Test the service Axis Admin tool The Apache Admin tool is a utility class that generates WAR level global configuration files based on deployment information specific to some services. The input to this utility is a XML file (typically named deploy.wsdd) containing deployment information about one or more services. The Apache Admin utility adds some global definitions that are necessary and writes an output file. Use this tool as follows. NOTE: You must set your CLASSPATH to include all jar files in the <install-dir>\lib\axis directory, before you run the following command. <java org.apache.axis.utils.Admin server|client deployment-file> This tool generates server-config.wsdd or client-config.wsdd based on what option you choose. Check out… http://article.gmane.org/gmane.text.xml.axis.devel/7138/match=13903Axis Admin tool The Apache Admin tool is a utility class that generates WAR level global configuration files based on deployment information specific to some services. The input to this utility is a XML file (typically named deploy.wsdd) containing deployment information about one or more services. The Apache Admin utility adds some global definitions that are necessary and writes an output file. Use this tool as follows. NOTE: You must set your CLASSPATH to include all jar files in the <install-dir>\lib\axis directory, before you run the following command. <java org.apache.axis.utils.Admin server|client deployment-file> This tool generates server-config.wsdd or client-config.wsdd based on what option you choose. Check out… http://article.gmane.org/gmane.text.xml.axis.devel/7138/match=13903

    44. Spring 2010 mark.baker@computer.org Deploying the Web service the flexible way Deploying the Web Service as a .jws file works for simple services such as the one here. However, this approach fails to meet most commercial Web Services' flexibility requirements, such as the ability to specify custom type mappings or create handler chains. Instead, most deployments will probably use deployment files such as the one shown next for our Web Service:

    45. Spring 2010 mark.baker@computer.org Deploying the Web service the flexible way <deployment xmlns="http://xml.apache.org/axis/wsdd/"      xmlns:java=http://xml.apache.org/axis/wsdd/providers/java <service name="HelloServer" provider="java:RPC"> <parameter name="className" value="HelloServer"/> <parameter name="methodName" value="sayHelloTo"/> </service> </deployment>

    46. Spring 2010 mark.baker@computer.org Deploying the Web service the flexible way The configuration above is self-explanatory. I have basically created a new service that uses the class HelloServer and exposes the sayHelloTo() method. If I want to expose more methods, I would replace the single sayHelloTo() method name with a space-delimited list of method names.

    47. Spring 2010 mark.baker@computer.org Deploying the Web service the flexible way To deploy the service with a configuration file, follow these steps: Make sure that Tomcat is started. Compile HelloServer.java and copy the resulting HelloServer.class into the Axis directory created in <Web_HOME>\webapps\axis\WEB-INF\classes\ directory. Now execute the following commands: > java org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService client.wsdd Here, client.wsdd is a file that contains the configuration XML shown. java org.apache.axis.client.AdminClient deploy_client.xml -lhttp://dsg.port.ac.uk:8080/axis/services/AdminSevicejava org.apache.axis.client.AdminClient deploy_client.xml -lhttp://dsg.port.ac.uk:8080/axis/services/AdminSevice

    48. Spring 2010 mark.baker@computer.org Deploying the Web service the flexible way java org.apache.axis.client.AdminClient deploy_client.xml -lhttp://dsg.port.ac.uk:8080/axis/services/AdminSevicejava org.apache.axis.client.AdminClient deploy_client.xml -lhttp://dsg.port.ac.uk:8080/axis/services/AdminSevice

    49. Spring 2010 mark.baker@computer.org Deploying the Web service the flexible way If you want to test the newly deployed Web service, just change the following line in the Client: String endpoint = "http://localhost:8080/axis/HelloServer.jws"; to: String endpoint = "http://localhost:8080/axis/servlet/AxisServlet"; Also replace this line: String ret = (String) call.invoke("","sayHelloTo",new Object [] {args[0]}); with: String ret = (String) call.invoke("HelloServer","sayHelloTo", new Object[]{args[0]}); This time, instead of the endpoint being a .jws file, it is the AxisServlet. The servlet locates and instantiates an instance of the class corresponding to the service name and converts the SOAP messages into Java calls. The servlet then invokes the Java calls on the instantiated service class.

    50. Spring 2010 mark.baker@computer.org Check the Service is Deployed

    51. Spring 2010 mark.baker@computer.org Wait, where is the SOAP? The Axis API does such a good job of hiding the details in example above, you are probably wondering: where is the SOAP? Actually, on the wire, XML-encoded SOAP messages are being exchanged. If you were to look at it, the request would look something like:

    52. Spring 2010 mark.baker@computer.org Wait, where is the SOAP?

    53. Spring 2010 mark.baker@computer.org SOAPMonitor

    54. Spring 2010 mark.baker@computer.org SOAP Architecture

    55. Spring 2010 mark.baker@computer.org SOAPMonitor Architecture

    56. Spring 2010 mark.baker@computer.org The SOAPMonitor Enabling SOAPMonitor in Axis 1.x – disabled by default. All the code for this is present in the release, but the applet itself was not compiled and the service is not enabled in the distribution. Here are the steps to enable the SOAPMonitor service for Axis running under Tomcat: Go to the Tomcat webapps/axis directory, and compile the applet with: javac -classpath WEB-INF/lib/axis.jar SOAPMonitorApplet.java Now deploy the service by running the admin client with deploy-monitor.wsdd. Point a browser at http://localhost:8080/axis/SOAPMonitor to bring up the viewer applet.

    57. Spring 2010 mark.baker@computer.org SOAP Monitor

    58. Spring 2010 mark.baker@computer.org SOAPMonitorService Deployment Descriptor (deploy-monitor.wsdd) <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler"> <parameter name="wsdlURL" value="/axis/SOAPMonitorService-impl.wsdl"/> <parameter name="namespace" value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/> <parameter name="serviceName" value="SOAPMonitorService"/> <parameter name="portName" value="Demo"/> </handler> <service name="SOAPMonitorService" provider="java:RPC"> <parameter name="allowedMethods" value="publishMessage"/> <parameter name="className" value="org.apache.axis.monitor.SOAPMonitorService"/> <parameter name="scope" value="Application"/> </service> </deployment>

    59. Spring 2010 mark.baker@computer.org Enabling the SOAPMonitor To actually make use of it you need to set up request and response flows for some other service that go through the SOAPMonitor handler. You can do this by modifying the client.wsdd file for the service you want to monitor, adding requestFlow and responseFlow definitions immediately following the start tag for the <service> element, as below: Request and Response Flow for Monitored Service ... <requestFlow> <handler type="soapmonitor"/> </requestFlow> <responseFlow> <handler type="soapmonitor"/> </responseFlow>...

    60. Spring 2010 mark.baker@computer.org Modified Client WSDD <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="HelloServer" provider="java:RPC"> <requestFlow> <handler type="soapmonitor"/> </requestFlow> <responseFlow> <handler type="soapmonitor"/> </responseFlow> <parameter name="className" value="HelloServer"/> <parameter name="methodName" value="sayHelloTo"/> </service> </deployment>

    61. Spring 2010 mark.baker@computer.org SOAPMonitor in action

    62. Spring 2010 mark.baker@computer.org Calculator Demo

    63. Spring 2010 mark.baker@computer.org Calculator Example Here’s the Calculator service: public class Calculator { public int add(int i1, int i2) { return i1 + i2; } public int subtract(int i1, int i2) { return i1 - i2; } }

    64. Spring 2010 mark.baker@computer.org Calculator Demo - CalcClient public class CalcClient{ public static void main(String [] args) throws Exception { Options options = new Options(args); String endpoint = "http://localhost:" + options.getPort() + "/axis/Calculator.jws"; args = options.getRemainingArgs(); if (args == null || args.length != 3) { System.err.println("Usage: CalcClient <add|subtract> arg1 arg2"); return; } String method = args[0]; if (!(method.equals("add") || method.equals("subtract"))) { System.err.println("Usage: CalcClient <add|subtract> arg1 arg2"); return; } Integer i1 = new Integer(args[1]); Integer i2 = new Integer(args[2]); Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress( new java.net.URL(endpoint) ); call.setOperationName( method ); call.addParameter( "op1", XMLType.XSD_INT, ParameterMode.IN ); call.addParameter( "op2", XMLType.XSD_INT, ParameterMode.IN ); call.setReturnType( XMLType.XSD_INT ); Integer ret = (Integer) call.invoke( new Object [] { i1, i2 }); System.out.println("Got result : " + ret); } }

    65. Spring 2010 mark.baker@computer.org Call org.apache.axis.client.Call implements javax.xml.rpc.Call interface. The Call interface provides a way to invoke a service endpoint. You normally handle the actual object through the javax.xml.rpc.Call interface. The javax.xml.rpc.Service interface acts as a factory for the creation of Call instances.

    66. Spring 2010 mark.baker@computer.org Service org.apache.axis.client.Service class implements the javax.xml.rpc.Service interface - the Service object acts as a factory to create Call objects. Factory objects are used to create an implementation of an interface: Interfaces are used to allow different implementations of an operation be used in the same manner! For example, you can have several Animal objects, all of which implement the speak() method. Service objects create the Call object with the createCall() method.

    67. Spring 2010 mark.baker@computer.org Creating and Using the Call object We can now create the Call object: Service service = new Service(); Call call = (Call) service.createCall(); We still have not told it all: The endpoint (service address/URL), The method name, The parameters. To pass the endpoint, simply make a URL object (we have done this before) and pass it to the Call object: String endpoint="http://localhost:8080/axis/Calculator.jws"; URL url = new URL(endpoint); call.setTargetEndpointAddress(endpoint);

    68. Spring 2010 mark.baker@computer.org Method (Operation) Name The method name (or operation name) is just a little harder: Endpoint address goes in HTTP headers and in Socket endpoint parameter, Method name goes in the XML, In XML, we could have a namespace qualifier. javax.xml.namespace.QName class represents a qualified name: Contains a namespace URI, Contains a local name, Like other tools dealing with qualified name, has methods to get the namespace and the local name: getLocalPart() getNamespaceUri() toString()

    69. Spring 2010 mark.baker@computer.org Method (Operation) Name Two constructors: One takes just a local name (uses default namespace or no namespace), The other takes a namespace URI in one parameter and the local name in another parameter. call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName( method ); call.addParameter("op1",XMLType.XSD_INT,ParameterMode.IN); call.addParameter("op2",XMLType.XSD_INT,ParameterMode.IN); call.setReturnType( XMLType.XSD_INT );

    70. Spring 2010 mark.baker@computer.org Parameters Now just need to set the parameters. The parameters can be set in the actual invoke() method. Recall that the Axis sample service called Calculator takes two parameters to the "add" method, both are integers. You might think we can simply pass these in to the invoke() method like this: Integer intResult = (Integer) call.invoke(42, 13); But, the invoke() method has to have some specific prototype (signature).

    71. Spring 2010 mark.baker@computer.org Parameters The only way to make a method take some unknown types and number of parameters is to pass them in some type of container! The invoke() method passes them in an array of Objects int's are not Objects, so must put them in the Integer wrapper Integer intResult = (Integer) call.invoke(new Object[] {new Integer(42), new Integer(13)}); Or like: Integer ret = (Integer) call.invoke(new Object [] { i1, i2}); Can initialise an array with a list of individual objects

    72. Spring 2010 mark.baker@computer.org Response Like the parameters, the only way to make a method (specifically the invoke() method) returns a value of some unknown type is to return it as an Object. Since the actual type is specified in the XML of the SOAP message, Axis actually knows the return type: Thus, Axis really makes an object of the correct type (or a wrapper type for primitive types) and returns that Still must return it as the base Object class Thus, you must cast it to the type that you know it really is. Integer ret = (Integer) call.invoke(new Object[] {i1, i2});

    73. Spring 2010 mark.baker@computer.org SOAP Axis Calculator Demo

    74. Spring 2010 mark.baker@computer.org Calculator Demo: Simple Deployment. Copy Calculator.java to application server webapps/axis/Calculator.jws. Compile CalcClient.java Execute CalcClient: java CalcClient add 2 4

    75. Spring 2010 mark.baker@computer.org Calculator Demo (I) Compile Calculator.java and copy to the application server webapps/axis/WEB-INF/classes/Calculator.class. Write deployment descriptor: Deploy service:

    76. Spring 2010 mark.baker@computer.org Calculator Demo (II) Change CalcClient.java String endpoint = "http://localhost:" + options.getPort() + "/axis/servlet/AxisServlet"; Integer ret = (Integer) call.invoke("Calculator", method, new Object [] { i1, i2 });

    77. Spring 2010 mark.baker@computer.org Summary Discussed Axis and how to use it. Looked at deploying services under Axis. Gone through a variety of examples to start gaining some familiarity with Axis and it associated services.

    78. Spring 2010 mark.baker@computer.org Links More about making your own chains: http://www.webservices.org/index.php/article/articleview/415/1/12 http://edocs.bea.com/wls/docs70/webserv/design.html#1053805 Deploying a service using WSDD http://www.onjava.com/pub/a/onjava/2002/06/05/axis.html?page=2

More Related