// --------------------------------------------------- // Exemple chapitre 11 // // Utilisation de l'API JAXRPC en mode DII // --------------------------------------------------- import java.io.*; import java.util.*; import javax.xml.rpc.Call; import javax.xml.rpc.Service; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceFactory; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import javax.xml.rpc.encoding.XMLType; import javax.xml.rpc.NamespaceConstants; import javax.xml.rpc.soap.SOAPFaultException; /** * Cette classe est un client SOAP qui va interroger un Web Service * permettant de connaitre le nombre d'habitants d'un pays. */ public class NombreHabitants { private static final String _endpoint = "http://cs.uga.edu:8080/axis/services/urn%3acountryInfoLookup"; private static final String _namespace = _endpoint; private static final String _service = "CountryInfoLookupService"; private static final String _port = "urn%3acountryInfoLookup"; private static final String _operation = "getPopulation"; /** * Point d'entree de l'application */ public static void main( String[] args ) { // 1. // Verification des arguments if ( args.length != 1 ) { System.out.println("Vous devez passer en argument le nom du pays (en anglais)."); System.exit(1); } String pays = args[0]; // 2. // Creation de l'objet symbolisant le service a appeler Service service = null; try { ServiceFactory factory = ServiceFactory.newInstance(); service = factory.createService( new QName( _service ) ); } catch ( ServiceException ex ) { ex.printStackTrace(); return; } // ----------------------------------------- // 3. // Prepare le message de requete SOAP // Creation de l'objet symbolisant la requete Call call = null; try { call = service.createCall( new QName( _port ) ); } catch ( ServiceException ex ) { ex.printStackTrace(); return; } // Indique le point d'acces call.setTargetEndpointAddress( _endpoint ); // Le nom de l'operation a invoquer call.setOperationName( new QName( _namespace, _operation ) ); // Le modele d'encodage call.setProperty( "javax.xml.rpc.encodingstyle.namespace.uri", NamespaceConstants.NSURI_SOAP_ENCODING ); // Definition du parametre de retour call.setReturnType( XMLType.XSD_STRING ); // Definition des parametres // Le paremetre "in0" de type "String" dans lequel nous indiquons // le pays recherche call.addParameter( "in0", XMLType.XSD_STRING, ParameterMode.IN ); String[] params = { pays }; // // ----------------------------------------- // 4. // Invocation de la requete String population = null; try { population = (String) call.invoke(params); } catch ( SOAPFaultException ex ) { System.out.println("Une exception s'est produite durant l'invocation :"); System.out.println("Code = " + ex.getFaultCode() + " : " + ex.getFaultString() ); return; } catch ( Exception ex ) { ex.printStackTrace(); return; } // ----------------------------------------- // 5. // Affiche la reponse if ( population != null ) System.out.println( population ); else System.out.println("Impossible de connaitre le nombre d'habitants"); } }