User Tools

Site Tools


sysrep:tp2

TP2 : Web Services

On suppose le serveur Apache Tomcat correctement installé (cf. TP1).

Documentation

Installation de Axis

Télécharger et décompresser Axis 1.4 (http://ws.apache.org/axis). On nomme ce répetoire <axis>.

wget http://apache.crihan.fr/dist/ws/axis/1_4/axis-bin-1_4.tar.gz
wget http://apache.mirrors.multidist.eu/axis/axis/java/1.4/axis-bin-1_4.tar.gz

Configurer votre environnement Bash.

export AXIS_HOME=<axis>

Il faut ensuite copier le répertoire <axis>/webapps/axis dans le répertoire “<tomcat>/webapps/”. Relancer votre serveur Web, puis vérifier que votre installation est correcte à l'URL : http://localhost:8080/axis

Déploiement avec JWS

Déployez avec JWS les services webs suivants.

HelloWorld.java
public class HelloWorld
{
    public String test(String data)
    {
	return "Hello World! You sent the string '" + data + "'.";
    }
}
Calculator.java
public class Calculator {
  public int add(int i1, int i2) {
    return i1 + i2; 
  }
 
  public int subtract(int i1, int i2) {
    return i1 - i2;
  }
}
  • Consultez le guide utilisateur de Axis pour mettre en place ce service et tester les méthodes à partir de votre navigateur web.
  • Affichez la description WSDL du service.

Client Statique JAX-RPC

Récupérez la description WSDL du service HelloWorld, puis générez les stubs avec la commande :

java  -cp ${AXIS_HOME}/lib/\* org.apache.axis.wsdl.WSDL2Java HelloWorld.wsdl

Compilez les fichiers stubs.

Ecrire un client Java statique. Pour ce faire, il faut utiliser la classe “HelloWorldServiceLocator” pour instancier le stub. Il se manipule ensuite comme un objet de type HelloWorld.

HelloWorldStaticClient.java
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import localhost.axis.HelloWorld_jws.*;
 
public class HelloWorldStaticClient
{
    public static void main(String[ ] args) 
	throws ServiceException, RemoteException
    {	
	HelloWorldService locator = new HelloWorldServiceLocator();
	HelloWorld stub = locator.getHelloWorld();
	String returnValue = stub.test("toto");
	System.out.println(returnValue);
    }
}

Pour compiler et exécuter :

  $ javac -cp ${AXIS_HOME}/lib/\* HeloWorldStaticClient.java localhost/axis/HelloWorld_jws/*.java
  $ java -cp ${AXIS_HOME}/lib/\*:. HeloWorldStaticClient

Utilisation d'un Web Service sur Internet

Client Dynamique JAX-RPC

Consultez le fichier “HelloWorldClient.java”.

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
{
 
    private static final String ENDPOINT = "http://localhost:1234/axis/HelloWorld.jws";
    private static final String NAMESPACE = "http://soapinterop.org/";
    private static final String OPERATION = "test";
 
    public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException
    {			
	Service service = new Service();	
	Call call = (Call)service.createCall();
	call.setTargetEndpointAddress(new URL(ENDPOINT));
	call.setOperationName(new QName(NAMESPACE, OPERATION));	
	String returnValue = (String)call.invoke(new Object[]{"toto"});       
	System.out.println(returnValue);
    }
}

Vérifiez que le ENDPOINT est configuré correctement pour votre serveur.

Compilez et testez ce client.

$ export CLASSPATH="${AXIS_HOME}/lib/*:" 
$ javac HelloWorldClient.java
$ java HelloWorldClient

En utilisant tcpmon, intercepté les messages SOAP.

java org.apache.axis.utils.tcpmon [listenPort targetHost targetPort]

Déploiement avec WSDD

On souhaite maintenant déployer le Service Web avec WSDD. Consulter le guide utilisateur. Dans ce cas, on part de l'interface HelloWorld.java, fournie ci-dessous.

HelloWorld.java
  public interface HelloWorld {
    public java.lang.String test(java.lang.String data);
  }

Compiler cette interface avec l'option debug (-g), puis générer le WSDL associé à votre service à l'aide de la commande “java org.apache.axis.wsdl.Java2WSDL <…>”. N'oubliez pas l'option '-l' qui indique la localisation future de votre service (endpoint), c'est-à-dire “http://localhost:8080/axis/services/HelloWorld”.

$ javac -g HelloWorld.java
$ java org.apache.axis.wsdl.Java2WSDL HelloWorld -l "http://localhost:8080/axis/services/HelloWorld" -n MyServices  

Vous pouvez maintenant générer les fichiers WSDD et les classes serveurs avec la commande “java org.apache.axis.wsdl.WSDL2Java <…> <wsdl-file>”. Je vous laisse regarder les options utiles.

$ java org.apache.axis.wsdl.WSDL2Java HelloWorld.wsdl -s

Implanter ce service en complétant le fichier généré “HelloWorldSoapBindingImpl.java” dans le répertoire MyServices/.

$ javac MyServices/*.java

Copier les classes de votre service dans <tomcat>/webapps/axis/WEB-INF/classes/ :

$ cp -rf MyService <tomcat>/webapps/axis/WEB-INF/classes/

Déployer ce service avec la commande :

$ java org.apache.axis.client.AdminClient MyServices/deploy.wsdd
sysrep/tp2.txt · Last modified: 2024/03/18 15:06 by 127.0.0.1