• Aucun résultat trouvé

JAXWS et Netbeans

N/A
N/A
Protected

Academic year: 2022

Partager "JAXWS et Netbeans"

Copied!
43
0
0

Texte intégral

(1)

JAXWS et Netbeans

(2)

* Utilisation transparente (et facile)

* Création d’un projet Web (web application)

* Ajout de Services Web

* A partir de POJO

* A partir de WSDL

* Création de clients par génération de stubs (skelette)

Utilisation de JAXWS dans netbeans

(3)

* Exemple de création de service:

* Faire un projet Web Application et

Utilisation de JAXWS dans netbeans

(4)

* Exemple de création de service:

Utilisation de JAXWS dans netbeans

Cliquer sur non

(5)

* Exemple de service:

Utilisation de JAXWS dans netbeans

(6)

* Exemple de service:

Utilisation de JAXWS dans netbeans

(7)

* Consommation d’un service en utilisant un proxy qui est généré (par la cmd wsimport)

* Création d’un projet Java application

* New Web service Client => generation de stubs

* Dans le code, faire insertion de code, appel de service Web

Utilisation de JAXWS dans netbeans

(8)

Utilisation de JAXWS dans netbeans

Consommation d’un service Choix du service :

(9)

Utilisation de JAXWS dans netbeans

Consommation d’un service Insertion de code :

(10)

Utilisation de JAXWS dans netbeans

Consommation d’un service en mode asynchrone

Puis insertion de code comme precedemment

(11)

JAWS et Gest. Des Exceptions,

Stateful WS

(12)

1. Gestion des exceptions BIG PICTURE

Try{

..

}

Catch Exception e{

Throw new XXXXX

VOIR APRESXXX ()}

Erreurs SOAP (fault code Fault string

)

Try{

…}

Catch Exception

ou ..

}

=> =>

serveur Réponse soap client

Le message texte est encapsulé dans fault string et est récupérable chez le client

Resp: S. SALVA IUT licence pro

(13)

JAXWS et gest. Des exceptions

MODE FACILE

Utiliser Unmodeled Faults => Runtime exception

throw new RuntimeException("Please enter a name."); //Unmodeled fault

(14)

JAXWS et gest. Des exceptions

MODE PERSONALISE => Modeled Faults

https://docs.oracle.com/cd/E24329_01/web.1211/e24965/faults.htm#WSADV633 Exemple complet avec FaultString et Detail :

Exception CheckVerifyFault

(15)

JAXWS et gen. De SoapFault

@WebFault(name="CheckVerifyFault")

public class CheckVerifyFault extends Exception { private CheckFaultBean faultInfo;

public CheckVerifyFault(String message, CheckFaultBean faultInfo) { super(message);

this.faultInfo = faultInfo;

}

public CheckVerifyFault(String message, CheckFaultBean faultInfo, Throwable cause) {

super(message, cause);

this.faultInfo = faultInfo;

}

public CheckFaultBean getFaultInfo() { return faultInfo;

} }

(16)

JAXWS et gen. De SoapFault

class CheckFaultBean { private String message;

public CheckFaultBean() { }

public CheckFaultBean(String message) { this.message = message;

}

public String getMessage() { return message;

} }

(17)

JAXWS et gen. De SoapFault

Dans le code du service:

throw new CheckVerifyFault("Fault Detected", new CheckFaultBean("detail"));

Dans le WSDL généré :

<operation name="getbook"><input wsam:Action="http://ws/S1/getbookRequest"

message="tns:getbook"/><output wsam:Action="http://ws/S1/getbookResponse"

message="tns:getbookResponse"/>

<fault message="tns:CheckVerifyFault" name="CheckVerifyFault"

wsam:Action="http://ws/S1/getbook/Fault/CheckVerifyFault"/>

</operation>

(18)

JAXWS Statefull WS

2 possibilités :

• Par creation de session HTTP

• Passage de la session dans les messages,

• Plus légé au niveau du serveur

• Par l’annotation @stateful

• Création d’instance au niveau du serveur

(19)

JAXWS Statefull WS

Création de session HTTP

https://docs.oracle.com/cd/E14571_01/web.1111/e13734/stateful.htm#WSADV234 Besoin de modifier le code du serveur ET le code du client

(20)

JAXWS Statefull WS

https://docs.oracle.com/cd/E14571_01/web.1111/e13734/stateful.htm#WSADV234 Dans le code du service :

@WebService

public class ShoppingCart {

@Resource // Step 1

private WebServiceContext wsContext; // Step 2 public int addToCart(Item item) {

// Find the HttpSession

MessageContext mc = wsContext.getMessageContext(); // Step 3 HttpSession session =

((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).

getSession();

(21)

JAXWS Statefull WS

Suite du service (code exemple, depend de la logique à mettre en place)

if (session == null)

throw new WebServiceException("No HTTP Session found");

// Get the cart object from the HttpSession (or create a new one)

List<Item> cart = (List<Item>)session.getAttribute("myCart"); // Step 4 if (cart == null)

cart = new ArrayList<Item>();

// Add the item to the cart (note that Item is a class defined // in the WSDL)

cart.add(item);

// Save the updated cart in the HTTPSession (since we use the same // "myCart" name, the old cart object will be replaced)

session.setAttribute("myCart", cart);

// return the number of items in the stateful cart return cart.size();

} }

(22)

JAXWS Statefull WS

Utilisation de la session chez le client :

ShoppingCart proxy = new CartService().getCartPort();

((BindingProvider)proxy).getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PR OPERTY, true);

// Create a new Item object with a part number of '123456' and an item // count of 4.

Item item = new Item('123456', 4);

// After first call, we'll print '1' (the return value is the number of objects // in the Cart object)

System.out.println(proxy.addToCart(item));

// After the second call, we'll print '2', since we've added another // Item to the stateful, saved Cart object.

System.out.println(proxy.addToCart(item));

(23)

* Par annotation @stateful

* https://javaee.github.io/metro-jax-ws/doc/user-guide/release- documentation.html#users-guide-stateful-webservice

* Importer dans votre projet les lib jaxws et jaxb

* Ajouter le module adressing avec @adressing

* Module qui je cite « provides transport-neutral mechanisms to address Web services and messages. »

JAXWS Statefull WS

(24)

* Par annotation @stateful

* Exemple:

@Stateful

@WebService(serviceName = "Serv")

@Addressing public class Serv { private String chaine;

/**

* This is a sample web service operation

*/

@WebMethod(operationName = "addchaine")

public synchronized String addchaine(@WebParam(name = "name") String txt) {// ou public static chaine = chaine + txt;

return chaine ; }

}

JAXWS Statefull WS

(25)

PHP et Soap

(26)

Resp: S. SALVA IUT licence pro

Possibilité de créer des services web et des clients avec PHP5 Les web services:

1.Création (manuelle) du fichier WSDL 2.Création du service web en PHP5:

Possibilités:

fonctions natives frameworks:

nusoap par exemple (http://sourceforge.net/projects/nusoap/)

(génération du wsdl, mais parfois il faut quasiment le définir dans le code)

(27)

Resp: S. SALVA IUT licence pro

Code du web service (http://stackoverflow.com/questions/2573494/php5-and-soap- wsdl)

<?php

function getRot13($pInput) {

$rot = str_rot13($pInput);

return($rot);

}

function getMirror($pInput) {

$mirror = strrev($pInput);

return $mirror;

}

ini_set("soap.wsdl_cache_enabled", "0");

$server = new SoapServer('scramble.wsdl');

$server->addFunction("getRot13");

$server->addFunction("getMirror");

$server->handle();

(28)

<?php

// turn off the WSDL cache

ini_set("soap.wsdl_cache_enabled", "0");

$client = new SoapClient("http://localhost/test/scramble.wsdl");

$origtext = "mississipi";

print("The original text : $origtext\n");

$mirror = $client->getMirror($origtext);

print("The mirrored text : $mirror\n");

$scramble = $client->getRot13($mirror);

print("The scrambled text : $scramble\n");

Code client:

Si plusieurs paramètres: utiliser array

Resp: S. SALVA IUT licence pro

(29)

Resp: S. SALVA IUT licence pro

Code du client: méthode soapcall (méthode PHP) /* Initialize webservice with your WSDL */

$client = new SoapClient("http://localhost/Service1?wsdl");

/* Set your parameters for the request */

$params = array(

“isbn" => “1234”,

"description" => “Book",

"amount" => 500, );

/* Invoke webservice method with your parameters, in this case: Buy */

$response = $client->__soapCall(“Buy", array($params));

/* Print webservice response */

var_dump($response);

(30)

SOAP et Springboot

(31)

* Oui c’est possible

* Mais il faut écrire les schéma XML (ceux en debut du WSDL) à la main L

Soap et Springboot

(32)

* Oui c’est possible

* Approche Top-down (je passe)

* Approche Bottom-Up

* D’après la doc spring il faut écrire les schéma XML (ceux en debut du WSDL) à la main L

* https://spring.io/guides/gs/producing-web-service/

* Ici: pas besoin de schémas ? https://blog.sodifrance.fr/creer-des-services-web- soap-simplement-avec-jax-ws-et-spring-boot-part-i/

Soap et Springboot

(33)

Create an XML Schema to Define the Domain

<xs:element name="getCountryRequest">

<xs:complexType>

<xs:sequence>

<xs:element name="name"

type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element

name="getCountryResponse">

<xs:complexType>

<xs:sequence>

<xs:element name="country"

type="tns:country"/>

</xs:sequence>

</xs:complexType>

</xs:element>

Soap et Springboot

<xs:complexType name="country">

<xs:sequence>

<xs:element name="name"

type="xs:string"/>

<xs:element name="population"

type="xs:int"/>

<xs:element name="capital"

type="xs:string"/>

<xs:element name="currency"

type="tns:currency"/>

</xs:sequence>

</xs:complexType>

<xs:simpleType name="currency">

<xs:restriction base="xs:string">

<xs:enumeration value="GBP"/>

<xs:enumeration value="EUR"/>

<xs:enumeration value="PLN"/>

</xs:restriction>

</xs:simpleType>

</xs:schema>

(34)

* Create Country Service Endpoint (code du service)

@Endpoint public class CountryEndpoint {

private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web- service";

private CountryRepository countryRepository;

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")

@ResponsePayload

public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) { GetCountryResponse response = new GetCountryResponse();

response.setCountry());

return response; } }

Soap et Springboot

(35)

* Configuration

Des namespaces, des schemas, des noms d’opération dans

src/main/java/com/example/producingwebservice/WebServiceConfig.jav a

Soap et Springboot

(36)

* Make the Application Executable

@SpringBootApplication

public class ProducingWebServiceApplication { public static void main(String[] args) {

SpringApplication.run(ProducingWebServiceApplication.class, args); } }

Soap et Springboot

(37)

* Faire un client

* https://spring.io/guides/gs/consuming-web-service/

* Plus simple?

* Utilise wsimport pour générer les stubs dans target/generated-sources

* Faire une classe fille à WebServiceGatewaySupport pour préparer les appels

* Configurer la sérialisation / deserialisation

* Et appel

Soap et Springboot

(38)

* Faire un client

* https://spring.io/guides/gs/consuming-web-service/

@SpringBootApplication

public class ConsumingWebServiceApplication { public static void main(String[] args) {

SpringApplication.run(ConsumingWebServiceApplication.class, args); }

@Bean CommandLineRunner lookup(CountryClient quoteClient) { return args -> { String country = "Spain";

if (args.length > 0) { country = args[0]; }

GetCountryResponse response = quoteClient.getCountry(country);

System.err.println(response.getCountry().getCurrency());

};

} }

Soap et Springboot

(39)

2 mots sur la sécurité

(40)

Il existe plusieurs standards de sécurité :

•WS-Security: standard proposé par Microsoft pour la sécurisation des services interopérables. Plus large que SAML, il intègre le traitement de l'authentification, du chiffrement et de l'intégrité des données.

•XML encryption : cryptage de documents XML

•XML signature: signature digitale dans un document XML

•SAML-XML: (security assertions markup language) spécifie une procédure d’authentification

•XKMS: utilisation de clés publiques

Resp: S. SALVA IUT licence pro

(41)

Implémentations

• Des API payantes L, ou

http://ws.apache.org/wss4j/ en lien avec https://cxf.apache.org/docs/ws- security.html, spingboot compliant ?

• HTTPS

• Ex: https://www.codeflow.site/fr/article/webservices__jax-ws__application- authentication-with-jax-ws

Resp: S. SALVA IUT licence pro

(42)

Parefeu XML:

se déploie en aval du pare-feu traditionnel

conçu spécifiquement pour inspecter les flux XML et SOAP sur protocoles de base HTTP/HTTPS et éventuellement sur des protocoles tiers

sécurité sur:

•la validation des messages,

•l’« obfuscation » des exceptions,

•la détection des intrusions,

•le routage selon le type de contenu,

•la transition de protocoles

•la sécurité au niveau de la couche de message.

Resp: S. SALVA IUT licence pro

(43)

Manque de performance ?

Mais apporte sécurité, transactions, Uniformité

Mode SMTP pour envoi importants

A recommander pour transactions sensibles entre sites

Resp: S. SALVA IUT licence pro

Références

Documents relatifs

 Si on génère les classes Java (voir juste après) avec ce schéma et qu'on essaye de sérialiser un sport avec des disciplines, ça plante. com.sun.istack.internal.SAXException2:

• Les étudiants doivent suivre la spécialité, les conseils, le plan et les objectifs fixés par l’encadrant scientifique (en coordination avec l’encadrant de stage si il existe)

Cambridge 39 0 Massachusetts SUBJECT: BIWEEKLY REPORT 2 SEPTEMBER 2 p 1956 To: Frank Mo Verzuh. From: Scientific and Engineering Computation Group

TCP_PROTOKOLL BOOL TRUE -&gt; Use TCP protocol FALSE -&gt; Use UDP protocol Return value: Data type: Comment:. ERROR WORD = 0: no

Thus, game developers began to think not so much in terms of a specific game, but about generic game systems that played different games depending on the data files.. The more

Operating Systems: Concurrent and Distributed Software Design achieves this integration by setting up a common framework of modular structure (a simple object model is

The Review and Comment Tasks button appears in your browser window; from its pop-up menu, choose Invite Others to Review This Document to open the Start Browser-Based Review dialog

Each span in the physical graph (a) has an associated total capacity that is annotated on the figure and represents the number of basic transmission channels or transmission