Une solution est d`utiliser l`en-tete d`une requête SOAP. Pour cela on

Transcription

Une solution est d`utiliser l`en-tete d`une requête SOAP. Pour cela on
SoapHeader : s'authentifier proprement a un WebService SOAP
Une s olutio n e st d'utilis er l'en - tete d'un e requ ête SOAP. Pour cela o n va t o u t
d ' a b o r d créer u n n o uvea u ty pe q ui h é rite d e Soa pHea der , ce ty pe co n tie n d ra les
info r m a tio n s d e co n nexion :
public class CredentialHeader : SoapHeader
{
public CredentialHeader()
: base()
{ }
public CredentialHeader(int Pid)
: base()
{
this._Pid = Pid;
}
private int _Pid;
public int Pid
{
get { return _Pid; }
set { _Pid = value; }
}
}
Ens uite n o u s rajo u t o n s l'att rib u te SoapHeader Attribute s u r la WebMet hod q ui
p r e n d e n a rg u m e n t le n o m d' u n e variable p u blic d u ty pe q u e l'on vien t d e créer.
Ainsi lor s q u' u n client e nverra u n e re q uê t e SOAP co n te n a n t les infor m a tio n s d e
co n n exio n d a n s l'e n - tê te, n o t re variable A u t he ntication s era a u t o m a tiq ue m e n t
re n seig né, n o u s p o uvo n s alors te s te r si le pi d p o u r le s uivit d e n o t re correlatio n.
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
public CredentialHeader Authentication;
[WebMethod]
[SoapHeader("Authentication")]
public Product GetProduct(int productID)
{
if (Authentication.Pid == 5)
return new Product(productID, "title", "description", 100);
else
throw new System.Security.Authentication.InvalidCredentialException();
}
}
Not re re q u ê te SOAP d evra d o nc co n t e nir les infor m a tio n s d e co n nexio n s d a n s le
h e a d er. Cela se t r a d uit p a r u n e re q uê te d e ce ty pe :
<?xml version="1.0" encoding="utf­8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema­instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<CredentialHeader xmlns="http://tem puri.org/">
<Pid>int</Pid>
</CredentialHeader>
</soap:Header>
<soap:Body>
<GetProduct xmlns="http://tempuri.org/">
<productID>int</productID>
</GetProduct>
</soap:Body>
</soap:Envelope>
Pou r e nvoyer les p a r a m é t re s d a n s le h ea d e r d' u n re q u e te SOAP n o u s référe nço n s
clas siq u e m e n t n o t re p a r a m é t re e n faisa n t u n "A d d Web Refere nce" d a n s Visual
Stu dio. Puis lors q ue n o u s ins t a ncio n s n o t re clas se p r oxy, n o u s avo n s la p o s sibilité
d e r e n seig ner u n e p r o p riété CredentialHeaderValue d e ty pe CredentialHeader :
WS.WebService ws = new WS.WebService();
ws.CredentialHeaderValue = new WS.CredentialHeader();
ws.CredentialHeaderValue.Pid = 5;
Console.WriteLine(ws.GetProduct(10).Price);
SOAP with Attachments API for Java SOAP API fo r Java (SAAJ) is u s e d m ai nly for t he SOAP m e s s aging t h a t goes o n
b e hin d t h e sce ne s in JAX - RPC a n d JAXR im ple m e n t a tio n s. Seco n d a rily, it is a n API
t h a t d evelo pe r s ca n u s e w he n t h ey cho o se t o write SOAP m e s s aging a p plicatio n s
directly r a t h er t h a n u s e JAX - RPC. The SAAJ API allows you t o d o XML m e s s aging
fro m t h e Java pla tfor m: By si m ply m a ki ng m e t h o d calls u si ng t he SAAJ API, yo u
ca n rea d a n d write SOAP - ba s e d XML m e s s age s, a n d you ca n o p tio n ally s e n d a n d
receive s u c h m e s s ages over t he Inter ne t (so m e im ple m e n t a tio n s m ay n o t s u p p o r t
s e n di ng a n d receiving). This cha p t e r will h el p yo u lear n h ow t o u s e t h e SAAJ API.
The SAAJ API co nfo r m s t o t h e Sim ple Object Acces s Protocol (SOAP) 1.1
s p ecificatio n a n d t he SOAP wit h Attac h m e n t s s p ecificatio n. The SAAJ 1.2
s p ecificatio n d efine s t he javax.xml.soap p ackage, w hich co n t ai n s t he API for
crea ti ng a n d p o p ulating a SOAP m e s s age. This p ackage h a s all t h e API n eces s a ry
fo r s e n di ng re q ue s t - re s p o n se m e s s age s. (Req ues t - re s p o n s e m e s s ages are
ex plaine d in SOAPCon nectio n Object s .)
Le proc e s s u s fonctionn e en 5 étp e s:
1.
2.
3.
4.
5.
creatio n d' u n co n nectio n SOAP
Creatio n d' u n m e s s age SOAP
le r e m plir
l'e nvoyer
Recevoir la re p o n se
I / Conn ection
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConnection;
public class SOAPTip {
public static void main(String args[]) {
try {
//First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
//Close the connection connection.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
II / creation du m e s sag e
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBody;
public class SOAPTip {
public static void main(String args[]) {
try {
//First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
//Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
//Create objects for the message parts SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//Close the connection connection.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
III / Remplis sag e d u m e s sag e
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
public class SOAPTip {
public static void main(String args[]) {
try {
...
//Create objects for the message parts SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//Populate the body
//Create the main element and namespace
SOAPElement bodyElement = body.addChildElement(envelope.createName("getPrice" , "ns1", "urn:xmethods­BNPriceCheck"));
//Add content
bodyElement.addChildElement("isbn").addTextNode("0672324229");
//Save the message
message.saveChanges();
//Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
//Close the connection connection.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
IV /En v oi s du m e s sag e
public class SOAPTip {
public static void main(String args[]) {
...
//Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply //Set the destination
String destination = "http://services.xmethods.net:80/soap/servlet/rpcrouter";
//Send the message
SOAPMessage reply = connection.call(message, destination);
//Close the connection connection.close(); ...
}
}
V / Receptio n d e la repon s e
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
public class SOAPTip {
public static void main(String args[]) {
try {
...
//Send the message
SOAPMessage reply = connection.call(message, destination);
//Check the output
System.out.println("\nRESPONSE:\n");
//Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
//Close the connection connection.close();
... }
}

Documents pareils