Thursday, 23 August 2007

Axis1 simple example

Apache Axis is an implementation of the SOAP ("Simple Object Access Protocol")
submission to W3C.

SOAP (Simple Object Access Protocol)
WSDL (Web Service Description Langauge)
WSDD (Web Service Deployment Descriptor)

1. Apache Tomcat 4.1 Over , Full Version
Check http://localhost:8080
If you need any library , please copy it to c:\tomcat\common\lib\
* set up enviroment value
set CATALINA_HOME=c:\tomcat

2. JDK : over JDK 1.4

* Set up classpath
set CLASSPATH=.;c:\jdk15\lib\tools.jar;c:\tomcat\common\lib\servlet-api.jar;
Add libraries (ex JDBC ...)

set JAVA_HOME=c:\jdk15
set PATH=c:\jdk15\bin;

3. ANT (option) : ant

http://ant.apache.org (down) ant 1.6.2
* set up classpath
set ANT_HOME=c:\ant
set PATH=c:\ant\bin;
copy C:\tomcat\server\lib\catalina-ant.jar files into c:\ant\lib\
if you need any library such as junit or xml libraries, please copy it into c:\ant\lib\

4. AXIS (Version 1.1) : http://ws.apache.org/axis/ , http://ws.apache.org/axis/releases.html

set AXIS_HOME=c:\axis
set AXIS_LIB=%AXIS_HOME%\lib
set AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery.jar;
%AXIS_LIB%\commons-logging.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;
%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar;
%AXIS_LIB%\wsdl4j.jar
set CLASSPATH=%CLASSPATH%;%AXISCLASSPATH%
etc :
you can download xml-apis.jar and xercesImpl.jar from http://xml.apache.org/

5. connect between axis and tomcat
copy axis directory from c:\axis\webapps\axis\ to c:\tomcat\webapps\axis

6. Test

Start Tomcat
http://localhost:8080/axis
* Test libraries
http://localhost:8080/axis/happyaxis.jsp
From this page, you can see missing jar files. Please download from web and copy it into
c:\tomcat\webapps\axis\lib\

Check http://localhost:8080/axis/happyaxis.jsp page again

7. SOAP test

http://localhost:8080/axis/services/Version?method=getVersion


- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-
-
Apache Axis version: 1.1 Built on Jun 13, 2003 (09:19:43 EDT)




8. AdminClient test

Move C:\axis\samples\stock
Open dos command and type below line
java org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy.wsdd

comfirm deploy, if you have any error , it is from classpath problem. so please check classpath again.

check deploy is ok or not
http://localhost:8080/axis/servlet/AxisServlet
you will see below page
urn:xmltoday-delayed-quotes (wsdl)
test
getQuote

9. Client test
Open dos command
Move C:\axis\
type below line
java samples.stock.GetQuote -lhttp://localhost:8080/axis/servlet/AxisServlet -uuser1 -wpass1 XXX
or
java -cp %AXISCLASSPATH% samples.stock.GetQuote -lhttp://localhost:8080/axis/servlet/AxisServlet -uuser1 -wpass1 XXX
or
Go to C:\axis\samples\stock
Open GetQuote.java and delete "package samples.stock " line
javac GetQuote.java

goto C:\axis\
java GetQuote -lhttp://localhost:8080/axis/servlet/AxisServlet -uuser1 -wpass1 XXX

You will see XXX: 5525

10. order for programm

a. implement "Interface" and Class for Server side
b. Create WSDL from implemented class
c. create deploy.wsdd file from WSDL file
d. deploy webservice from deploy.wsdd file and update server-config.wsdd
e. create client-side class

11. program flow





12. example

java Interface, Implement
go to c:\test\
create 2 files

//begin - HelloIF.java
package webservice.hello;
public interface HelloIF extends java.rmi.Remote {
public String hello(java.lang.String name) throws java.rmi.RemoteException;
}
//end - HelloIF.java

//begin - HelloImpl.java
package webservice.hello;
public class HelloImpl implements HelloIF {
public HelloImpl() {
}
public String hello(String name) {
System.out.println(name);
return "hi " + name;
}
}
//end - HelloImpl.java


compile

javac -d . HelloImpl.java

If there is error.
c:\test>javac -d . HelloIF.java
c:\test>javac -d . HelloImpl.java

HelloImpl.class and HelloIF.class under c:\test\webservice/hello
(If you can not compile , please use eclipse. because we need only 2 class files with folder)

copy (webservice)folder into C:\Tomcat\webapps\axis\WEB-INF\classes

* Create WSDL file

c:\test>java org.apache.axis.wsdl.Java2WSDL -o c:\test\webservice\hello\Hello.wsdl
-l http://localhost:8080/axis/services/Hello -n http://hello.webservice -pwebservice.hello
http://hello.webservice webservice.hello.HelloIF

Java2WSDL parameter (http://ws.apache.org/axis/java/reference.html)

-o file path and wsdl locationi to create
-l URL for connecting from URL Client
-n Namespace : WSDL target NameSpace
-p package Name space and mapping with namespace Target inerface name


[check Hello.wsdl file from c:\test\webservice\hello]

.......


* Create deploy.wsdd file and create Client-side java class

c:\test>java org.apache.axis.wsdl.WSDL2Java -o c:\test\webservice\hello\
-d Application -s c:\test\webservice\hello\Hello.wsdl

webservice/hello directory is created under c:\test\webservice\hello
deploy.wsdd - the file to deploy in web server
HelloIF.java
HelloIFService.java
HelloIFServiceLocator.java
HelloSoapBindingImpl.java
HelloSoapBindingStub.java
undeploy.wsdd - the file to undeploy in web server

Parameter
-o directory path for wsdd file and class files
-d install area of scope - Application, Request , Session
-s create wsdd file
Target wsdl file path


[deploy.wsdd - update]

wsdd file is created from wsdl file so it does not set up what is real implement class.
So you should update that part
from webservice.hello.HelloSoapBindingImpl to webservice.hello.HelloImpl

deployment
xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
...
......
...
parameter name="className" value="webservice.hello.HelloImpl"/>
...
......
...
parameter name="allowedMethods" value="hello"/>
parameter name="scope" value="Application"/>
/service>
/deployment>


*deploy service
Go to C:\test\webservice\hello\webservice\hello
java org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy.wsdd

[checking deply]
http://localhost:8080/axis/servlet/AxisServlet
'Hello' Serviec (display)


* undeploy service

java org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService undeploy.wsdd

[Checking undeploy service]
http://localhost:8080/axis/servlet/AxisServlet
'Hello' service (disappear)

*deploy service
Go to C:\test\webservice\hello\webservice\hello
java org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy.wsdd

[checking deply]
http://localhost:8080/axis/servlet/AxisServlet
'Hello' Serviec (display)

* confirm WSDL from web
http://localhost:8080/axis/services/Hello?wsdl

14. implement client

create client side to access

crate below file and copy to c:\tomcat\webapps\axis\


[HelloClient.jsp]

%@ page contentType="text/html; charset=utf-8" language="java"
import="java.net.MalformedURLException,
java.net.URL,
java.rmi.RemoteException,
javax.xml.namespace.QName,
javax.xml.rpc.ServiceException,
org.apache.axis.client.Call,
org.apache.axis.client.Service" %>
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html>
head>
title>test web service : Hello World!/title>
/head>
body>
%
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new URL("http://localhost:8080/axis/services/Hello?wsdl"));
call.setOperationName(new QName("http://soapinterop.org/", "hello"));
String returnValue = (String)call.invoke(new Object[]{"! your name"});
out.println(returnValue);
%>
/body>
/html>





http://localhost:8080/axis/HelloClient.jsp

hi ! your name - (if you can see this message, you are success until here.)


14. second example

There are faster way
after you crate java file and change jws files(change file extenstion from java to jws)

please refer to c:\tomcat\webapps\axis\WEB-INF\web.xml

Please create below file under c:\tomcat\webapps\axis\
[ HelloWorldService.jws]

public class HelloWorldService
{
public String HelloWorld(String data)
{
return "Hello World! You sent the string '" + data + "'.";
}
}

* Access to below address
http://localhost:8080/axis/HelloWorldService.jws
you will see
========================
There is a Web Service here
Click to see the WSDL
=======================
It address there are html page install,
try call HelloWorld method

http://localhost:8080/axis/HelloWorldService.jws?method=HelloWorld&data=Hi+my+name+is+rrr

you received the XML as a result for calling method


* now implement client with java

create below java file in anywhere
[ HelloWorldClient.java]

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class HelloWorldClient
{
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException
{
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new URL("http://debianbox:8080/axis/HelloWorldService.jws"));
call.setOperationName(new QName("http://soapinterop.org/", "HelloWorld"));
String returnValue = (String)call.invoke(new Object[]{"My name is rrr."});
System.out.println(returnValue);
}
}

compile and run
You will see
Hello World ! you send the string My name is rrr.
from dos command

1 comment:

Anonymous said...

Genial brief and this enter helped me alot in my college assignement. Say thank you you for your information.