Précédent   Forum du club des développeurs et IT Pro > Java > Développement Web en Java > Web Services
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 21/11/2012, 23h37   #1
hajer88
Invité de passage
 
Femme
Étudiant
Inscription : janvier 2012
Messages : 9
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : Tunisie

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Matériel informatique

Informations forums :
Inscription : janvier 2012
Messages : 9
Points : 0
Points : 0
Par défaut From Element to SOAP Envelope

Bjr,
Mon soucis concerne la récupération du soap envelope d'un dom document. J'ai écrit la classe suivante:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
public static SOAPEnvelope getSOAPEnvelopeFromDOMDocument(Document doc, boolean useDoom) 
throws WSSecurityException { 
 
Element documentElement = doc.getDocumentElement(); 
if (documentElement instanceof SOAPEnvelope) { 
SOAPEnvelope env = (SOAPEnvelope)documentElement; 
// If the DOM tree already implements the Axiom API and the corresponding 
// Axiom implementation is also used as default implementation, then just return 
// the SOAPEnvelope directly. Note that this will never be the case for DOOM, 
// but may be the case for a non standard Axiom implementation. 
if (env.getOMFactory().getMetaFactory() == OMAbstractFactory.getMetaFactory()) { 
return env; 
} 
} 
 
if(useDoom) { 
try { 
//Get processed headers 
SOAPEnvelope env = (SOAPEnvelope)doc.getDocumentElement(); 
ArrayList processedHeaderQNames = new ArrayList(); 
SOAPHeader soapHeader = env.getHeader(); 
 
if(soapHeader != null) { 
Iterator headerBlocs = soapHeader.getChildElements(); 
while (headerBlocs.hasNext()) { 
 
OMElement element = (OMElement)headerBlocs.next(); 
SOAPHeaderBlock header = null; 
 
if (element instanceof SOAPHeaderBlock) { 
header = (SOAPHeaderBlock) element; 
 
// If a header block is not an instance of SOAPHeaderBlock, it means that 
// it is a header we have added in rampart eg. EncryptedHeader and should 
// be converted to SOAPHeaderBlock for processing 
} else { 
header = soapHeader.addHeaderBlock(element.getLocalName(), element.getNamespace()); 
Iterator attrIter = element.getAllAttributes(); 
while (attrIter.hasNext()) { 
OMAttribute attr = (OMAttribute)attrIter.next(); 
header.addAttribute(attr.getLocalName(), attr.getAttributeValue(), attr.getNamespace()); 
} 
Iterator nsIter = element.getAllDeclaredNamespaces(); 
while (nsIter.hasNext()) { 
OMNamespace ns = (OMNamespace) nsIter.next(); 
header.declareNamespace(ns); 
} 
// retrieve all child nodes (including any text nodes) 
// and re-attach to header block 
Iterator children = element.getChildren(); 
while (children.hasNext()) { 
OMNode child = (OMNode)children.next(); 
children.remove(); 
header.addChild(child); 
} 
 
headerBlocs.remove(); 
 
soapHeader.build(); 
 
header.setProcessed(); 
 
} 
 
if(header.isProcessed()) { 
processedHeaderQNames.add(element.getQName()); 
} 
} 
 
} 
XMLStreamReader reader = ((OMElement) doc.getDocumentElement()) 
.getXMLStreamReader(); 
SOAPModelBuilder stAXSOAPModelBuilder = OMXMLBuilderFactory.createStAXSOAPModelBuilder( 
reader); 
SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope(); 
 
//Set the processed flag of the processed headers 
SOAPHeader header = envelope.getHeader(); 
for (Iterator iter = processedHeaderQNames.iterator(); iter 
.hasNext();) { 
QName name = (QName) iter.next(); 
Iterator omKids = header.getChildrenWithName(name); 
if(omKids.hasNext()) { 
((SOAPHeaderBlock)omKids.next()).setProcessed(); 
} 
} 
 
envelope.build(); 
 
return envelope; 
 
} catch (FactoryConfigurationError e) { 
throw new WSSecurityException(e.getMessage()); 
} 
} else { 
try { 
ByteArrayOutputStream os = new ByteArrayOutputStream(); 
XMLUtils.outputDOM(doc.getDocumentElement(), os, true); 
ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray()); 
 
SOAPModelBuilder stAXSOAPModelBuilder = OMXMLBuilderFactory.createSOAPModelBuilder(bais, null); 
return stAXSOAPModelBuilder.getSOAPEnvelope(); 
} catch (Exception e) { 
throw new WSSecurityException(e.getMessage()); 
} 
} 
}
J'ai une erreur lors du cast qui me dit:
Code :
exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl cannot be cast to org.apache.axiom.soap.SOAPEnvelope

Merci
hajer88 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/11/2012, 00h48   #2
tchize_
Expert Confirmé Sénior
 
Avatar de tchize_
 
Homme
Responsable de service informatique
Inscription : avril 2007
Messages : 18 287
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 33
Localisation : Belgique

Informations professionnelles :
Activité : Responsable de service informatique
Secteur : Service public

Informations forums :
Inscription : avril 2007
Messages : 18 287
Points : 32 766
Points : 32 766
Envoyer un message via MSN à tchize_ Envoyer un message via Skype™ à tchize_
ligne 5, tu traite le cas où doc.getDocumentElement() est de type SOAPEnveloppe

ligne 16, tu est sorti du "if", tu n'es donc plus nécessairement dans ce cas là, et ton message d'erreur t'indique que tu n'est pas dans ce cas là, tu as juste un objet xml standard.

Tu ne peux pas assumer que doc.getDocumentElement() est de type SOAPEnveloppe, rien ne te le laisse supposer.
__________________
⥀⥁ Чиз faq java, cours java, javadoc. Pensez à et
Laisse entrer le jour après une nuit sombre. Si tu es toujours là, tu n'es pas faite pour mourir.
tchize_ est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/11/2012, 08h58   #3
hajer88
Invité de passage
 
Femme
Étudiant
Inscription : janvier 2012
Messages : 9
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : Tunisie

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Matériel informatique

Informations forums :
Inscription : janvier 2012
Messages : 9
Points : 0
Points : 0
Merci pour ta réponse, et il n'y a pas de possibilité pour modifier un DOM en SOAPEnvelope j'en ai besoin dans mon travail
hajer88 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/11/2012, 13h39   #4
tchize_
Expert Confirmé Sénior
 
Avatar de tchize_
 
Homme
Responsable de service informatique
Inscription : avril 2007
Messages : 18 287
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 33
Localisation : Belgique

Informations professionnelles :
Activité : Responsable de service informatique
Secteur : Service public

Informations forums :
Inscription : avril 2007
Messages : 18 287
Points : 32 766
Points : 32 766
Envoyer un message via MSN à tchize_ Envoyer un message via Skype™ à tchize_
Citation:
Envoyé par hajer88 Voir le message
Merci pour ta réponse, et il n'y a pas de possibilité pour modifier un DOM en SOAPEnvelope j'en ai besoin dans mon travail
Ben un DOM, c'est un DOM.

Maintenant, je ne connais pas axiom, mais en 2 minute de lecture de la javadoc, on trouve la solution

Code :
OMelement soapElement = OMXMLBuilderFactory.createOMBuilder(new DOMsource(elementRacineDOM)).getDocumentElement();
__________________
⥀⥁ Чиз faq java, cours java, javadoc. Pensez à et
Laisse entrer le jour après une nuit sombre. Si tu es toujours là, tu n'es pas faite pour mourir.
tchize_ est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 17h01.


 
 
 
 
Partenaires

Hébergement Web