handling web services with apache axis n.
Skip this Video
Loading SlideShow in 5 Seconds..
Handling Web Services with Apache AXIS PowerPoint Presentation
Download Presentation
Handling Web Services with Apache AXIS

Handling Web Services with Apache AXIS

114 Vues Download Presentation
Télécharger la présentation

Handling Web Services with Apache AXIS

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Handling Web Services with Apache AXIS

  2. Web-Service Review By Examples ...

  3. POST /wsprimegenerator.exe/soap/IPrimeGenerator HTTP/1.0 Content-Type: text/xml; charset=utf-8 Host: SOAPAction:"urn:UnitIPrimeGenerator-IPrimeGenerator#primegenerator" Content-Length: 527 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="" xmlns:xsd="" xmlns:xsi=""> <soapenv:Body> <ns1:primegenerator soapenv:encodingStyle="" xmlns:ns1="urn:UnitIPrimeGenerator-IPrimeGenerator"> <valstart xsi:type="xsd:string">12</valstart> <valend xsi:type="xsd:string">120</valend> </ns1:primegenerator> </soapenv:Body> </soapenv:Envelope> A request to

  4. HTTP/1.0 200 OK Date: Mon, 09 May 2005 20:58:35 GMT Content-Length: 619 Content-Type: text/xml Server: Microsoft-IIS/5.0 X-Powered-By: ASP.NET <?xml version="1.0" encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP- ENV="" xmlns:xsd="" xmlns:xsi="" xmlns:SOAP-ENC=""> <SOAP-ENV:Body> <NS1:primegeneratorResponse xmlns:NS1="urn:UnitIPrimeGenerator-IPrimeGenerator" SOAP-ENV:encodingStyle=""> <return xsi:type="xsd:string">13,17,19,23,29,31, 37, 41,43, 47, 53,59,61,67,71,73,79,83,89,97,101,103,107,109,113</return> </NS1:primegeneratorResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> The Response

  5. POST /soap HTTP/1.0 SOAPAction: "" Content-Length: 520 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="" xmlns:xsd="" xmlns:xsi=""> <soapenv:Body> <ns1:getRate soapenv:encodingStyle="" xmlns:ns1="urn:xmethods-CurrencyExchange"> <country1 xsi:type="xsd:string">Euro</country1> <country2 xsi:type="xsd:string">Israel</country2> </ns1:getRate> </soapenv:Body> </soapenv:Envelope> A request to

  6. HTTP/1.0 200 OK Date: Sat, 07 May 2005 23:26:21 GMT Content-Length: 492 Content-Type: text/xml <?xml version='1.0' encoding='UTF-8'?> <soap:Envelope xmlns:soap='' xmlns:xsi='' xmlns:xsd='' xmlns:soapenc='' soap:encodingStyle=''> <soap:Body> <n:getRateResponse xmlns:n='urn:xmethods-CurrencyExchange'> <Result xsi:type='xsd:float'>5.5825</Result> </n:getRateResponse> </soap:Body> </soap:Envelope> The Response

  7. A WSDL Example <?xml version="1.0"?> <definitionsname="CurrencyExchangeService" targetNamespace="" xmlns:tns="" xmlns:xsd="" xmlns:soap="" xmlns=""> <message name="getRateRequest"> <part name="country1" type="xsd:string"/> <part name="country2" type="xsd:string"/> </message> <message name="getRateResponse"> <part name="Result" type="xsd:float"/> </message> <portType name="CurrencyExchangePortType"> <operation name="getRate"> <input message="tns:getRateRequest" name="getRate"/> <output message="tns:getRateResponse" name="getRateResponse"/> </operation></portType>

  8. <bindingname="CurrencyExchangeBinding"type="tns:CurrencyExchangePortType"> <soap:binding style="rpc" transport=""/> <operation name="getRate"> <soap:operation soapAction=""/> <input name="getRate"> <soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange" encodingStyle=""/> </input> <output name="getRateResponse"> <soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange" encodingStyle=""/> </output> </operation> </binding> <service name="CurrencyExchangeService"> <documentation>Returns the exchange rate between the two currencies</documentation> <port name="CurrencyExchangePort" binding="tns:CurrencyExchangeBinding"> <soap:address location=""/> </port> </service> </definitions>

  9. WSDL Elements Service Port * Binding Input Output Operation Port Type *

  10. Apache EXtensible Interaction System(Axis)

  11. What is AXIS • Axis is essentially a SOAP engine – a framework for constructing SOAP processors • clients side • server side • Axis implements the interfaces of JAX-RPC (XML-based remote procedure calls in Java) • AXIS =Apache Extensible Interaction System

  12. Remote Method Invocation is not a New Idea • java.rmi has been in Java since Java’s early versions • In Java RMI, objects can invoke methods of objects that reside on a remote computer • (RMI = Remote Method Invocation) • So, what has been changed? • Using HTTP for communication • Using agreed protocols, Java can invoke methods that were not written in Java (e.g., .NET methods) and vice versa • A complex registry procedure has been required in RMI

  13. AXIS Includes • Axis package includes: • a simple stand-alone server • a server which plugs into servlet engines such as Tomcat • tools for Web service invocation • extensive support for the Web Service Description Language(WSDL) • emitter tooling that generates Java classes from WSDL • and more

  14. What We Would Like to Create • Client applications: applications that can call a remote Web service • Services: methods that can be called by remote applications • Service descriptions: WSDL files that describe our methods

  15. Client Applications

  16. Calling Web Services • By now, we already know how to invoke a remote Web service in Java: • Open a socket to the remote server • Through the socket, send a SOAP request wrapped by a HTTP request • Parse the response (e.g., using SAX/DOM) • However, this approach is cumbersome, and most of it can be automated

  17. Invoking Services with Apache • Axis includes comfortable tools for managing calls to Web services • The programmer configures the request for the Web service using a friendly API • with, or without considering the WSDL • According to the configuration, a method invocation is transformed into a SOAP request that is sent to the remote server

  18. Invoking Services with Axis • To invoke an operation of a Web service, do the following: • Construct a Service instance • Using the Service instance, generate a Call instance • Configure the Call instance • Invoke the call

  19. Example: Currency Exchange import org.apache.axis.client.*; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.encoding.XMLType; publicclass CurrencyExchange1 { publicstaticvoid main(String[] args) throwsException { System.setProperty("http.proxyHost",""); System.setProperty("http.proxyPort","8080"); String endpointUrl = ""; String nsuri = "urn:xmethods-CurrencyExchange"; Service service = new Service(); Call call = (Call) service.createCall();

  20. Example: Currency Exchange (cont) call.setTargetEndpointAddress(endpointUrl); call.setOperationName(new QName(nsuri,"getRate")); call.addParameter("country1", XMLType.SOAP_STRING, ParameterMode.IN); call.addParameter("country2", XMLType.SOAP_STRING, ParameterMode.IN); call.setReturnType(XMLType.SOAP_FLOAT); Object ret = call.invoke(newObject[] {"Euro","Israel"}); System.out.println(ret); } }

  21. Using the WSDL • Axis can read a given WSDL and configure the service as much as possible from that WSDL • Instead of using the default constructor, construct the service using the following constructor: Service(String wsdlLocation, QName serviceName) • Also, supply the port to get a prefilled Call object • In this approach, you usually need to know the following about the service: • the WSDL URL, the service name, the namespace uri, the operation name and port and the expected arguments

  22. publicclass CurrencyExchange2 { publicstaticvoid main(String[] args) throwsException { System.setProperty("http.proxyHost",""); System.setProperty("http.proxyPort","8080"); String wsdl = ""; String nsuri = ""; String sName = "CurrencyExchangeService", String oName = "getRate"; Service service = new Service(wsdl, new QName(nsuri,sName)); String port = service.getPorts().next().toString(); Call call = (Call)service.createCall (new QName(nsuri,port), new QName(nsuri,oName)); System.out.println(call.invoke(newObject[] {"UK","Israel"})); }

  23. The WSDL2Java Application • Axis provides a mechanism for communicating with a Web service using stubs • That is, generation of regular Java classes that have an interface similar to that of the Web service and implementation that wraps Web service management • Invoke class WSDL2Java in order to create the required Java classes

  24. An Example

  25. Using The Generated Classes import*; publicclass CurrencyExchange3 { publicstaticvoid main(String[] args) throwsException { CurrencyExchangeService ce = new CurrencyExchangeServiceLocator(); CurrencyExchangePortType cep = ce.getCurrencyExchangePort(); float rate = cep.getRate("USA","Israel"); System.out.println("Rate: " + rate); } }

  26. Generated Classes • WSDL2Java generates the following: • A service interface and a service implementation (locator) for each service • A stub class for each binding • An interface for each port type • This interface contains methods that correspond to the operations of the port type • A class for each complex type

  27. Server Applications

  28. AXIS Installation • Axis works inside a Servlet container (e.g., Tomcat) • You should add to your Tomcat libs some jar files: axis.jar, saaj.jar, wsdl4j.jar, … • You need to include several jar files in your CLASSPATH • This has already been done for you! • The needed CLASSPATH definitions where added to dbi.cshrc • The needed jar files are in $CATALINA_BASE/shared/lib/

  29. The directory that you need to copy Classes were added for you AXIS Installation (cont) • You need to copy the Axis application to your Tomcat's application directory: cp -r ~dbi/tomcat/axis/ $CATALINA_BASE/webapps $CATALINA_BASE webapps shared myapplication axis lib classes

  30. Creating a Web Service • Next, we will see how we can create and publish a Web service using the Axis plugin in Tomcat

  31. 1. Generate the Implementing Class package myws; publicclass Power { publicint power(int a, int n) { return (int)Math.pow(a,n); } } $CATALINA_BASE/webapps/axis/classes/myws/Power.class

  32. 2. Deploy the Service using Web Service Deployment Descriptor <deployment xmlns="" xmlns:java=""> <service name="mypower" provider="java:RPC"> <parameter name="className" value="myws.Power"/> <parameter name="scope" value="application"/> <parameter name="allowedMethods" value="*"/> </service> </deployment> services.wsdd java org.apache.axis.client.AdminClient -hlocalhost -p8090 services.wsdd

  33. That's it. Call the Service. import org.apache.axis.client.*; publicclass PowerClient { publicstaticvoid main(String[] argv)throwsException { String endpoint = "http://mangal:8090/axis/services/mypower"; Call call = (Call) new Service().createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName("power"); Object value = call.invoke(newObject[] {newInteger(2), newInteger(5)}); System.out.println(2+"^"+5 +"=" + value); } }

  34. How Does it Work? • The AXIS plugin is simply a Web application that resides in Tomcat (under webapps/) • The Servlet AxisServlet of this application is responsible for invoking services • All URLs of the form /services/* are mapped to the AxisServlet • Where is that written?

  35. How Does it Work? (cont) • The wsdd file defines mappings between a Web service elements to a Java class elements • I.e., names, methods, etc. • The class AdminClient sends a request to the application to register the service based on the wsdd content • When a SOAP request arrives, the AxisServlet object parses the request and invokes the corresponding method of the class associated with the service URL

  36. The Deployment Descriptor <deployment xmlns="" xmlns:java=""> <service name="mypower" provider="java:RPC"> <parameter name="className" value="myws.Power"/> <parameter name="scope" value="application"/> <parameter name="allowedMethods" value="*"/> </service> </deployment> services.wsdd

  37. The Scope of the Object • Request (the default): a new object is created for each request, the service instance is available for the duration of the request, regardless of forwarding • Session: a new object is created for each new session and the service instance is available for the entire session • Application: a singleton shared service instance is used to serve all invocations

  38. Undeploying a Service <undeployment xmlns=""> <service name="mypower"/> </undeployment> undeploy.wsdd java org.apache.axis.client.AdminClient -hlocalhost -p8090 undeploy.wsdd

  39. Implementing Classes • The class that implements the Web service must be accessible to the Axis Servlet • Hence, this class should reside in a package in one of the directories • $CATALINA_BASE/webapps/axis/WEB-INF/classes • $CATALINA_BASE/shared/classes (classes that are required for other applications besides Axis) • Of course, all helper classes should be accessible to the Axis application too

  40. The Service WSDL • Axis automatically provides a WSDL for each deployed service • To get the WSDL, use the service URL with the empty argument wsdl http://localhost:8090/axis/services/mypower?wsdl

  41. Power-Service WSDL <?xmlversion="1.0" encoding="UTF-8"?> <wsdl:definitionstargetNamespace="http://localhost/axis/services/mypower" xmlns="" xmlns:apachesoap="" xmlns:impl="http://localhost/axis/services/mypower" xmlns:intf="http://localhost/axis/services/mypower" xmlns:soapenc="" xmlns:wsdl="" xmlns:wsdlsoap="" xmlns:xsd=""> <wsdl:message name="powerResponse"> <wsdl:part name="powerReturn" type="xsd:int"/> </wsdl:message> <wsdl:message name="powerRequest"> <wsdl:part name="a" type="xsd:int"/> <wsdl:part name="n" type="xsd:int"/> </wsdl:message>

  42. <wsdl:portType name="Power"> <wsdl:operation name="power" parameterOrder="a n"> <wsdl:input message="impl:powerRequest" name="powerRequest"/> <wsdl:output message="impl:powerResponse" name="powerResponse"/> </wsdl:operation> </wsdl:portType>

  43. <wsdl:binding name="mypowerSoapBinding"type="impl:Power"> <wsdlsoap:binding style="rpc" transport=""/> <wsdl:operation name="power"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="powerRequest"> <wsdlsoap:body encodingStyle="" namespace="http://myws" use="encoded"/> </wsdl:input> <wsdl:output name="powerResponse"> <wsdlsoap:body encodingStyle="" namespace="http://localhost/axis/services/mypower" use="encoded"/> </wsdl:output> </wsdl:operation> </wsdl:binding>

  44. <wsdl:service name="PowerService"> <wsdl:port binding="impl:mypowerSoapBinding" name="mypower"> <wsdlsoap:address location="http://localhost/axis/services/mypower"/> </wsdl:port> </wsdl:service> </wsdl:definitions>

  45. jws Files • There is a fast and easy way of crating a service: • Create a Java class • Rename your class to end with jws: myClass.jws • Put the jws file directly under the directory $CATALINA_BASE/webapps/axis/ • That is all. Now you can call the service!

  46. Example: a Calculator Service publicclass SimpleCalculator { publicint add(int i1, int i2) { return i1 + i2; } publicint subtract(int i1, int i2) { return i1 - i2; } } $CATALINA_BASE/webapps/axis/SimpleCalculator.jws Service URL: http://server:port/axis/SimpleCalculator.jws

  47. Example: a Calculator Service publicclass CalculatorClient { publicstaticvoid main(String[] argv)throwsException { String endpoint = "http://localhost:80/axis/SimpleCalculator.jws"; Call call = (Call) new Service().createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName("add"); Object value = call.invoke(newObject[] {new Integer(4), newInteger(6)}); System.out.println(value); } }

  48. How does it Work? • On the first time the jws file is being called, it is compiled into a class • WEB-INF/jwsClasses/SimpleCalculator.class • Axis then considers the URL of the jws as one of a regular web service • Default configurations are assumed • Sounds like any other technology we know?

  49. When not to Use jws Files • When you do not have the Java source code • When you don’t want to expose your code • When you want to use custom type mappings • When you want to use other configuration options

  50. Axis Type Mappings