apache axis - khon kaen universitykrunapon/courses/178375/slides/apacheaxis.… · java web...
TRANSCRIPT
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 1
1
Apache AxisApache Axis
Dr. Kanda Dr. Kanda RunapongsaRunapongsaDepartment of Computer EngineeringDepartment of Computer Engineering
Khon Kaen UniversityKhon Kaen University
2
OverviewOverview
� What is Apache Axis�� What Apache Axis ProvidesWhat Apache Axis Provides�� Axis InstallationAxis Installation�� ..jwsjws ExtensionExtension�� Web Service Deployment Descriptors (WSDD)Web Service Deployment Descriptors (WSDD)�� Create Web Services by AxisCreate Web Services by Axis�� ExerciseExercise
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 2
3
What is Apache Axis?What is Apache Axis?
�� Apache Axis is an openApache Axis is an open--source implementation source implementation that provides a Javathat provides a Java--based SOAP based SOAP implementation for developing Web serviceimplementation for developing Web service
�� It facilitates a SOAP runtime environment and It facilitates a SOAP runtime environment and JavaJava--based API framework for implementing the based API framework for implementing the core components of Web services adopting core components of Web services adopting compliant standards and protocolscompliant standards and protocols
4
What Apache Axis ProvidesWhat Apache Axis Provides
�� An API library and runtime environment for An API library and runtime environment for developing SOAP RPC and SOAP messagingdeveloping SOAP RPC and SOAP messaging--based based applications and servicesapplications and services
�� A transportA transport--independent means for adopting a independent means for adopting a varieyvariey of of transport protocols (such as HTTP, SMTP, and FTP)transport protocols (such as HTTP, SMTP, and FTP)
�� Automatic serialization and Automatic serialization and deserializationdeserialization for Java for Java objects to and from XML in SOAP messagesobjects to and from XML in SOAP messages
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 3
5
What Apache Axis Provides What Apache Axis Provides (cont.)(cont.)
�� Support for exposing Support for exposing EJBsEJBs as Web services, as Web services, especially the methods of stateless session especially the methods of stateless session EJBsEJBs
�� Tools for creating WSDL from Java classes and Tools for creating WSDL from Java classes and vicevice--versaversa
�� Tools for deploying, monitoring, and testing the Tools for deploying, monitoring, and testing the Web servicesWeb services
6
Axis Installation for WindowsAxis Installation for Windows
� Java 2 SDK v1.4.2http:/ / java.sun.com/ j2se/ 1.4.2/ download.html
� Apache Tomcat v.5.0.28 http:/ / mirrors.qualsec.net/ apache/ jakarta/ tomcat-5/ v5.0.28/ bin/ jakarta-tomcat-5.0.28.exe
� Axis v1.1, http:/ / ws.apache.org/ axis/ dist/ 1_1/ axis-1_1.zip
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 4
7
Axis Installation (cont.)Axis Installation (cont.)
�� Unzip Unzip axis-1_1.zip archive to C: (this will create a directory C:\ axis-1_1)
�� Copy directory axis in C:Copy directory axis in C:\\ axisaxis--1_11_1\\ webapps to webapps to [tomcat directory][tomcat directory]\\ webappswebapps (so you have (so you have [tomcat directory][tomcat directory]\\ webappswebapps\\ axis)axis)
�� Start tomcat server and browse to Start tomcat server and browse to http:/ / localhost:8080/ axishttp:/ / localhost:8080/ axis. Click on the . Click on the ValidateValidate linklink to test that all libraries are to test that all libraries are accessible to Axis (both core and optional).accessible to Axis (both core and optional).
8
�� The optional libraries can put its into [tomcat The optional libraries can put its into [tomcat directory]directory]\\ webappswebapps\\ axisaxis\\ WEBWEB--INFINF\\ liblib
Axis Installation (cont.)Axis Installation (cont.)
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 5
9
Axis EnvironmentAxis Environment
�� Set AXIS_HOME=C:Set AXIS_HOME=C:\\ axisaxis--1_11_1�� Set AXIS_LIB=%AXIS_HOME%Set AXIS_LIB=%AXIS_HOME%\\ liblib�� Set Set
AXISCLASSPATH=AXISCLASSPATH= %%AXIS_LIBAXIS_LIB%%\\ axisaxis..jar;jar;%%AXIS_AXIS_LIBLIB%%\\ commonscommons--discoverydiscovery..jar;jar;%%AXIS_LIBAXIS_LIB%%\\ commonscommons--logginglogging..jar;jar;%%AXIS_LIBAXIS_LIB%%\\ jaxrpcjaxrpc..jar;jar;%%AXIS_LIBAXIS_LIB%%\\ sasaajaj..jar;jar;%%AXIS_LIBAXIS_LIB%%\\ log4jlog4j--1.2.81.2.8..jar;jar;%%AXIS_LIBAXIS_LIB%%\\ xmlxml--apisapis..jar;jar;%%AXIS_LIBAXIS_LIB%%\\ xercesImplxercesImpl..jarjar
10
..jwsjws ExtensionExtension
�� Stored in Stored in [tomcat directory][tomcat directory]\\ webappswebapps\\ axisaxis�� For example : For example : EchoHeaders.jws. EchoHeaders.jws.
�� Compile and executed by browse its URLCompile and executed by browse its URL�� For example browse to : For example browse to :
http:/ / localhost:8080/ axis/ EchoHeaders.jws?wsdlhttp:/ / localhost:8080/ axis/ EchoHeaders.jws?wsdl�� You will see in XMLYou will see in XML
�� The The jwsClassesjwsClasses stored in stored in [tomcat [tomcat directory]directory]\\ webappswebapps\\ axisaxis\\ WEBWEB--INFINF\\ jwsClassesjwsClasses
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 6
11
Web Service Deployment Descriptors Web Service Deployment Descriptors (WSDD) (WSDD)
�� Written by using XML.Written by using XML.�� can control which provider handlers are used to can control which provider handlers are used to
expose components as Web services expose components as Web services �� JavaJava--RPC ProviderRPC Provider�� JavaJava--MsgProviderMsgProvider�� EJBProviderEJBProvider
12
Axis deployment descriptors
� WSDD deployment descriptors� Extensive customization of server options
� Service style specification � RPC� document� wrapped� Allowed methods and required roles� Transport specification
� Handler chains for special processing� Logging or tracking usage� Authorization � authentication
� Custom serializers � deserializers� Transport specification
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 7
13
Create Web Services by AxisCreate Web Services by Axis
�� Create java file with .Create java file with .jwsjws extensionextension�� Store it in [tomcat directory]Store it in [tomcat directory]\\ webappswebapps\\ axisaxis�� Use Java2WSDL to generate .Use Java2WSDL to generate .jwsjws to WSDLto WSDL�� Use WSDL2Java to generate 4 Java files from Use WSDL2Java to generate 4 Java files from
WSDLWSDL�� Deploy Web Service on Apache AxisDeploy Web Service on Apache Axis�� Create client to call Web ServiceCreate client to call Web Service
14
ExampleExample
�� We will create the Web Service which has three We will create the Web Service which has three operation that deal with taxesoperation that deal with taxes
�� The first operation will figure out the tax percent if given theThe first operation will figure out the tax percent if given thesubtotal and the total money spent on a shopping purchase. subtotal and the total money spent on a shopping purchase. calcTaxRate(double subtotal, double total) calcTaxRate(double subtotal, double total)
�� The second operation will figure out the amount of tax paid The second operation will figure out the amount of tax paid when given the total and the tax percentage. when given the total and the tax percentage. calcSubTotal(doublecalcSubTotal(double total, double total, double taxpercenttaxpercent))
�� The third operation will figure out the total amount when The third operation will figure out the total amount when given the subtotal and the tax percent.given the subtotal and the tax percent.calcTotal(doublecalcTotal(double subtotal, double subtotal, double taxpercenttaxpercent))
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 8
15
Example (cont.)Example (cont.)�� Create filename Create filename ““TaxService.jwsTaxService.jws”” which has this contentwhich has this content
public class TaxService {public double calcTaxRate(double subtotal, double total) {
double rate = (total - subtotal)/ subtotal;return rate;
}public double calcSubTotal(double total, double taxpercent) {
double subtotal = total / (1 + taxpercent);return subtotal;
}public double calcTotal(double subtotal, double taxpercent) {
double total = subtotal * (1 + taxpercent);return total;
}}
16
Example (cont.)Example (cont.)
�� Store file Store file ““ TaxService.jwsTaxService.jws”” in [tomcat in [tomcat directory]directory]\\ webappswebapps\\ axisaxis\\ TaxService.jwsTaxService.jws
�� Start tomcat serverStart tomcat server�� Compile and execute it. Browse to Compile and execute it. Browse to
http:/ / localhost:8080/ axis/ TaxService.jwshttp:/ / localhost:8080/ axis/ TaxService.jws then then Click Click to see WSDLto see WSDL. . You should see a listing of XML You should see a listing of XML elements in your browser. elements in your browser.
�� If you see the WSDL file, then congratulations! You If you see the WSDL file, then congratulations! You have successfully created and deployed your first web have successfully created and deployed your first web service using Axis! service using Axis!
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 9
17
Example (cont.)Example (cont.)
18
Example (cont.)Example (cont.)
�� Writing the ClientWriting the Client�� Create a stub from the WSDL Create a stub from the WSDL �� Use a tool called WSDL2JavaUse a tool called WSDL2Java�� Follow these following commandFollow these following command
�� >> cdcd [tomcat directory][tomcat directory]\\ webappswebapps\\ axisaxis\\ WEBWEB--INFINF\\ liblib�� >java >java --cp axis.jar;commonscp axis.jar;commons--logging.jar;commonslogging.jar;commons--
discovery.jar;saaj.jar;wsdl4j.jar;jaxrpc.jar discovery.jar;saaj.jar;wsdl4j.jar;jaxrpc.jar org.apache.axis.wsdl.WSDL2Java org.apache.axis.wsdl.WSDL2Java http:/ / localhost:8080/ axis/ TaxService.jws?WSDLhttp:/ / localhost:8080/ axis/ TaxService.jws?WSDL
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 10
19
Example (cont.)Example (cont.)
�� Create directory Create directory texservicetexservice in C:in C:\\ axisaxis--1_11_1\\ samplessamples�� Folder called Folder called localhostlocalhost is created in the lib directory is created in the lib directory
of Axis of Axis �� Copy this directory and paste it in Copy this directory and paste it in taxservicetaxservice
directorydirectory�� Compile the client which following commandCompile the client which following command
�� C:C:\\ cdcd axisaxis--1_11_1\\ samplessamples\\ taxservicetaxservice�� taxservicetaxservice>> cdcd localhostlocalhost\\ axisaxis\\ TaxService_jwsTaxService_jws�� TaxService_jwsTaxService_jws>> javacjavac ––classpathclasspath
.;%AXISCLASSPATH% *.java.;%AXISCLASSPATH% *.java
20
Example (cont.)Example (cont.)�� In the C:In the C:\\ axisaxis--1_11_1\\ samplessamples\\ taxservice directory createtaxservice directory create the the
““ TaxClient.javaTaxClient.java”” which following contentwhich following content
import localhost.axis.TaxService_jws.TaxServiceServiceLocator;import localhost.axis.TaxService_jws.TaxService;import org.apache.axis.AxisFault;public class TaxClient {
public static void main(String[] args) {try {
/ / Make a serviceTaxServiceServiceLocator service = new
TaxServiceServiceLocator();TaxService port = service.getTaxService();/ / Make the actual calls to the three methodsdouble taxpercent = port.calcTaxRate(21.00, 23.10);double total = port.calcTotal(21.00, 0.10);double subtotal = port.calcSubTotal(23.10, 0.10);
Make the connection to our web service
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 11
21
Example (cont.)Example (cont.)/ / Output the results System.out.println("Subtotal: 21.00, Total: 23.10, Tax: " +
taxpercent);System.out.println("Subtotal: 21.00, Tax: 0.10, Total: " + total);System.out.println("Total: 23.10, Tax: 0.10, Subtotal: " + subtotal);
}catch (AxisFault af){
System.err.println("An Axis Fault occurred: " + af);}catch (Exception e){
System.err.println("Exception caught: " + e);}
}}
22
Example (cont.)Example (cont.)
�� Compile the client which the following commandCompile the client which the following command�� taxservicetaxservice>> javacjavac ––classpathclasspath .;%AXISCLASSPATH% .;%AXISCLASSPATH%
TaxClient.javaTaxClient.java
�� Execute the client which the Execute the client which the following commandfollowing command�� taxservicetaxservice>java >java ––cp cp .;%AXISCLASSPATH% .;%AXISCLASSPATH% TaxClientTaxClient
Subtotal: 21.00, Total: 23.10, Tax: 0.10000000000000006Subtotal: 21.00, Total: 23.10, Tax: 0.10000000000000006Subtotal: 21.00, Tax: 0.10, Total: 23.1Subtotal: 21.00, Tax: 0.10, Total: 23.1Total: 23.10, Tax: 0.10, Subtotal: 21.0Total: 23.10, Tax: 0.10, Subtotal: 21.0
�� You should see the above output!You should see the above output!
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 12
23
Example (cont.)Example (cont.)
�� Deploy the Web Service using WSDD.Deploy the Web Service using WSDD.�� Create the Create the ““deploy.wsdddeploy.wsdd”” which the following content and which the following content and
save it in C:save it in C:\\ axisaxis--1_11_1\\ samplessamples\\ taxservicetaxservice\\ deploy.wsdddeploy.wsdd
<deployment xmlns="http:/ / xml.apache.org/ axis/ wsdd/ " xmlns:java="http:/ / xml.apache.org/ axis/ wsdd/ providers/ java">
<service name="TaxService" provider="java:RPC"><parameter name="className" value="TaxService"/ ><parameter name="allowedMethods" value="*"/ >
</ service></ deployment>
24
Example (cont.)Example (cont.)
�� Lunch deploy.wsdd which the following commandLunch deploy.wsdd which the following command�� java java ––cp .;%AXISCLASSPATH% cp .;%AXISCLASSPATH%
org.apache.axis.client.AdminClientorg.apache.axis.client.AdminClient deploy.wsdd deploy.wsdd
�� If successful, it will output this information.If successful, it will output this information.Processing file deploy.wsddProcessing file deploy.wsdd<Admin>Done processing</ Admin><Admin>Done processing</ Admin>
�� You will see the deployed in You will see the deployed in http:/ / localhost:8080/ axis/ servlet/ AxisServlethttp:/ / localhost:8080/ axis/ servlet/ AxisServlet or or Click Click ViewView
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 13
25
Example (cont.)Example (cont.)
26
Example (cont.)Example (cont.)
�� UndeployUndeploy�� Create file Create file ““ undeploy.wsddundeploy.wsdd”” which the following content which the following content
and save it in C:and save it in C:\\ axisaxis--1_11_1\\ samplessamples\\ taxservicetaxservice\\ undeploy.wsddundeploy.wsdd
<undeployment xmlns="http:/ / xml.apache.org/ axis/ wsdd/ "><service name="TaxService"/ >
</ undeployment>
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 14
27
Example (cont.)Example (cont.)
�� Lunch Lunch undeploy.wsddundeploy.wsdd which the following which the following commandcommand
�� java java ––cp .;%AXISCLASSPATH% cp .;%AXISCLASSPATH% org.apache.axis.client.AdminClientorg.apache.axis.client.AdminClient undeploy.wsddundeploy.wsdd
�� If successful, it will output this information.If successful, it will output this information.Processing file deploy.wsddProcessing file deploy.wsdd<Admin>Done processing</ Admin><Admin>Done processing</ Admin>
28
1. Exercise 1. Exercise
�� For For EchoStringServiceEchoStringService file bellowfile bellowpublic class public class EchoStringServiceEchoStringService {{
public String public String echoString(StringechoString(String strstr) {) {return return strstr;;
}}}}
�� Use the Java2WSDL for generate WSDL fileUse the Java2WSDL for generate WSDL file�� Use the WSDL2Java for generate 4 Java filesUse the WSDL2Java for generate 4 Java files
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 15
29
1. Answer1. Answer
�� Java2WSDLJava2WSDL�� Browse URL : Browse URL :
http:/ / localhost:8080/ axis/ EchoStringService.jws?whttp:/ / localhost:8080/ axis/ EchoStringService.jws?wsdlsdl
�� WSDL2JavaWSDL2Java�� Use the following commandUse the following command
�� >> cdcd [tomcat directory][tomcat directory]\\ webappswebapps\\ axisaxis\\ WEBWEB--INFINF\\ liblib�� >java >java --cp axis.jar;commonscp axis.jar;commons--logging.jar;commonslogging.jar;commons--
discovery.jar;saaj.jar;wsdl4j.jar;jaxrpc.jar discovery.jar;saaj.jar;wsdl4j.jar;jaxrpc.jar org.apache.axis.wsdl.WSDL2Java org.apache.axis.wsdl.WSDL2Java http:/ / localhost:8080/ axis/ TaxService.jws?WSDLhttp:/ / localhost:8080/ axis/ TaxService.jws?WSDL
30
2. Exercise2. Exercise
�� Create a Create a CalculatorServiceCalculatorService has 2 operationhas 2 operation�� addServiceaddService�� subServicesubService
�� The services has return double typeThe services has return double type�� Deploy in on Apache AxisDeploy in on Apache Axis�� Create the client which call the operation aboveCreate the client which call the operation above
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 16
31
2. Answer2. Answer
�� CalculatorService.jwsCalculatorService.jwspublic class public class CalculatorServiceCalculatorService {{
public double public double addService(doubleaddService(double a1, double a2) {a1, double a2) {double sum = a1 + a2;double sum = a1 + a2;return sum;return sum;
}}public double public double subService(doublesubService(double s1, double s2) {s1, double s2) {
double sub = s1 double sub = s1 –– s2;s2;return sub;return sub;
}}}}
32
2. Answer (cont.)2. Answer (cont.)
�� Deploy Deploy CalculatorService.jwsCalculatorService.jws
<deployment xmlns="http:/ / xml.apache.org/ axis/ wsdd/ " xmlns:java="http:/ / xml.apache.org/ axis/ wsdd/ providers/ java">
<service name=“ CalculatorService" provider="java:RPC"><parameter name="className" value=" CalculatorService "/ ><parameter name="allowedMethods" value="*"/ >
</ service></ deployment>
Java Web Services, Software Park Thailand, 2004
Dr. Kanda Runapongsa, Khon Kaen University 17
33
ResourcesResources
�� Axis homepage:Axis homepage:�� http:/ / ws.apache.org/ axis/http:/ / ws.apache.org/ axis/
�� WebServices WebServices �� AxisAxis�� http:/ / ws.apache.org/ axis/ docs/ mshttp:/ / ws.apache.org/ axis/ docs/ ms--interop.htmlinterop.html
�� Apache Axis SOAP for JavaApache Axis SOAP for Java�� http:/ / www.sosnoski.com/ presents/ javahttp:/ / www.sosnoski.com/ presents/ java--
xml/ axis/ axisxml/ axis/ axis--javajava--soap.pdfsoap.pdf