KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > soap > impl > llom > builder > StAXSOAPModelBuilder


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.soap.impl.llom.builder;
17
18 import org.apache.axis2.om.*;
19 import org.apache.axis2.om.impl.llom.builder.StAXBuilder;
20 import org.apache.axis2.om.impl.llom.exception.OMBuilderException;
21 import org.apache.axis2.soap.SOAPBody;
22 import org.apache.axis2.soap.SOAPEnvelope;
23 import org.apache.axis2.soap.SOAPFactory;
24 import org.apache.axis2.soap.SOAPHeader;
25 import org.apache.axis2.soap.impl.llom.SOAPConstants;
26 import org.apache.axis2.soap.impl.llom.SOAPEnvelopeImpl;
27 import org.apache.axis2.soap.impl.llom.SOAPProcessingException;
28 import org.apache.axis2.soap.impl.llom.soap11.SOAP11Constants;
29 import org.apache.axis2.soap.impl.llom.soap12.SOAP12Constants;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import javax.xml.stream.XMLStreamConstants;
34 import javax.xml.stream.XMLStreamReader;
35
36 /**
37  * Class StAXSOAPModelBuilder
38  */

39 public class StAXSOAPModelBuilder extends StAXBuilder {
40     /**
41      * Field envelope
42      */

43     private SOAPEnvelopeImpl envelope;
44     private OMNamespace envelopeNamespace;
45
46     private SOAPFactory soapFactory;
47
48     /**
49      * Field headerPresent
50      */

51     private boolean headerPresent = false;
52
53     /**
54      * Field bodyPresent
55      */

56     private boolean bodyPresent = false;
57
58     /**
59      * Field log
60      */

61     private Log log = LogFactory.getLog(getClass());
62
63     /**
64      * element level 1 = envelope level element level 2 = Header or Body level
65      * element level 3 = HeaderElement or BodyElement level
66      */

67     int elementLevel = 0;
68
69     private boolean processingFault = false;
70
71     //added
72
/*This is used to notice whether all mandatory fault elements are present
73     */

74     private boolean processingMandatoryFaultElements = false;
75
76     //added
77
/* This is used to indicate whether detail element is processing in soap 1.2 builderhelper
78     */

79     private boolean processingDetailElements = false;
80
81     private SOAPBuilderHelper builderHelper;
82
83     /**
84      * Constructor StAXSOAPModelBuilder
85      *
86      * @param parser
87      */

88     public StAXSOAPModelBuilder(XMLStreamReader parser) {
89         super(parser);
90         soapFactory = OMAbstractFactory.getDefaultSOAPFactory();
91         identifySOAPVersion();
92         parseHeaders();
93     }
94
95     public StAXSOAPModelBuilder(XMLStreamReader parser, SOAPFactory factory) {
96         super(parser);
97         soapFactory = factory;
98         identifySOAPVersion();
99
100         parseHeaders();
101     }
102
103     private void identifySOAPVersion() {
104         SOAPEnvelope soapEnvelope = getSOAPEnvelope();
105         if (soapEnvelope == null) {
106             throw new OMException("No SOAPHeader present !!");
107         }
108
109         envelopeNamespace = soapEnvelope.findNamespace(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI, "");
110         if (envelopeNamespace == null) {
111             envelopeNamespace = getSOAPEnvelope().findNamespace(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, "");
112             if (envelopeNamespace == null) {
113                 throw new OMException("Invalid SOAP message. Doesn't have proper namespace declaration !!");
114             } else {
115                 log.info("SOAP 1.1 message received ..");
116                 soapFactory = OMAbstractFactory.getSOAP11Factory();
117             }
118         } else {
119             log.info("SOAP 1.2 message received ..");
120             soapFactory = OMAbstractFactory.getSOAP12Factory();
121         }
122
123         omfactory = soapFactory;
124     }
125
126     private void parseHeaders() {
127         // by the time execution comes here the nullity of SOAPEnvelope has been cheched in the
128
// identifySOAPVersion() method. So not checking getSOAPEnvelope() == null here
129
SOAPHeader soapHeader = getSOAPEnvelope().getHeader();
130
131         if (soapHeader != null) {
132             while (!soapHeader.isComplete()) {
133                 next();
134             }
135         } else {
136             log.info("No SOAPHeaders present !!");
137         }
138     }
139
140
141     /**
142      * Method getSOAPEnvelope
143      *
144      * @return
145      * @throws OMException
146      */

147     public SOAPEnvelope getSOAPEnvelope() throws OMException {
148         while ((envelope == null) && !done) {
149             next();
150         }
151         return envelope;
152     }
153
154     /**
155      * Method createOMElement
156      *
157      * @return
158      * @throws OMException
159      */

160     protected OMNode createOMElement() throws OMException {
161         OMElement node;
162         String JavaDoc elementName = parser.getLocalName();
163         if (lastNode == null) {
164             node = constructNode(null, elementName, true);
165         } else if (lastNode.isComplete()) {
166             node = constructNode((OMElement)lastNode.getParent(), elementName, false);
167             lastNode.setNextSibling(node);
168             node.setPreviousSibling(lastNode);
169         } else {
170             OMElement e = (OMElement) lastNode;
171             node = constructNode((OMElement) lastNode, elementName, false);
172             e.setFirstChild(node);
173         }
174
175
176         log.info("Build the OMElelment " + node.getLocalName() + "By the StaxSOAPModelBuilder");
177         return node;
178     }
179
180     /**
181      * Method constructNode
182      *
183      * @param parent
184      * @param elementName
185      * @param isEnvelope
186      * @return
187      */

188     protected OMElement constructNode(OMElement parent, String JavaDoc elementName,
189                                       boolean isEnvelope) {
190         OMElement element = null;
191         if (parent == null) {
192             if (!elementName.equalsIgnoreCase(SOAPConstants.SOAPENVELOPE_LOCAL_NAME)) {
193                 throw new OMException("First Element must contain the local name, "
194                         + SOAPConstants.SOAPENVELOPE_LOCAL_NAME);
195             }
196             envelope =
197                     (SOAPEnvelopeImpl) soapFactory.createSOAPEnvelope(this);
198             element = envelope;
199             processNamespaceData(element, true);
200             // fill in the attributes
201
processAttributes(element);
202
203         } else if (elementLevel == 2) {
204
205             // this is either a header or a body
206
if (elementName.equals(SOAPConstants.HEADER_LOCAL_NAME)) {
207                 if (headerPresent) {
208                     throw new OMBuilderException("Multiple headers encountered!");
209                 }
210                 if (bodyPresent) {
211                     throw new OMBuilderException("Header Body wrong order!");
212                 }
213                 headerPresent = true;
214                 element =
215                         soapFactory.createSOAPHeader((SOAPEnvelope) parent,
216                                 this);
217
218                 // envelope.setHeader((SOAPHeader)element);
219
processNamespaceData(element, true);
220                 processAttributes(element);
221
222             } else if (elementName.equals(SOAPConstants.BODY_LOCAL_NAME)) {
223                 if (bodyPresent) {
224                     throw new OMBuilderException("Multiple body elements encountered");
225                 }
226                 bodyPresent = true;
227                 element =
228                         soapFactory.createSOAPBody((SOAPEnvelope) parent,
229                                 this);
230
231                 // envelope.setBody((SOAPBody)element);
232
processNamespaceData(element, true);
233                 processAttributes(element);
234
235             } else {
236                 throw new OMBuilderException(elementName
237                         + " is not supported here. Envelope can not have elements other than Header and Body.");
238             }
239         } else if ((elementLevel == 3)
240                 && parent.getLocalName().equalsIgnoreCase(SOAPConstants.HEADER_LOCAL_NAME)) {
241
242             // this is a headerblock
243
try {
244                 element = soapFactory.createSOAPHeaderBlock(elementName, null,
245                         (SOAPHeader) parent, this);
246             } catch (SOAPProcessingException e) {
247                 throw new OMBuilderException(e);
248             }
249             processNamespaceData(element, false);
250             processAttributes(element);
251
252         } else if ((elementLevel == 3) && parent.getLocalName().equalsIgnoreCase(SOAPConstants.BODY_LOCAL_NAME) && elementName.equalsIgnoreCase(SOAPConstants.BODY_FAULT_LOCAL_NAME)) {
253
254             // this is a headerblock
255
element = soapFactory.createSOAPFault((SOAPBody) parent, this);
256             processNamespaceData(element, false);
257             processAttributes(element);
258
259
260             processingFault = true;
261
262             //added
263
processingMandatoryFaultElements = true;
264             if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(envelopeNamespace.getName())) {
265                 builderHelper = new SOAP12BuilderHelper(this);
266             } else if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(envelopeNamespace.getName())) {
267                 builderHelper = new SOAP11BuilderHelper(this);
268             }
269
270         } else if (elementLevel > 3 && processingFault) {
271             element = builderHelper.handleEvent(parser, parent, elementLevel);
272         } else {
273
274             // this is neither of above. Just create an element
275
element = soapFactory.createOMElement(elementName, null,
276                     parent, this);
277             processNamespaceData(element, false);
278             processAttributes(element);
279
280         }
281         return element;
282     }
283
284     /**
285      * Method next
286      *
287      * @return
288      * @throws OMException
289      */

290     public int next() throws OMException {
291         try {
292             if (done) {
293                 throw new OMException();
294             }
295             int token = parser.next();
296             if (!cache) {
297                 return token;
298             }
299             switch (token) {
300                 case XMLStreamConstants.START_ELEMENT:
301                     elementLevel++;
302                     lastNode = createOMElement();
303                     break;
304                 case XMLStreamConstants.CHARACTERS:
305                     lastNode = createOMText();
306                     break;
307                 case XMLStreamConstants.END_ELEMENT:
308                     if (lastNode.isComplete()) {
309                         OMElement parent = (OMElement)lastNode.getParent();
310
311 // //added
312
// /*check whether all mandatory fault elements are present
313
// */
314
// if (parent.getLocalName().equals(SOAP12Constants.SOAPFAULT_LOCAL_NAME) && processingMandatoryFaultElements) {
315
// throw new OMBuilderException("Missing mandatory fault elements");
316
// }
317
// //added
318
// /*finish processing detail element in soap 1.2 builderhelper
319
// */
320
// if (parser.getLocalName().equals(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME)) {
321
// this.setProcessingDetailElements(false);
322
// }
323

324                         parent.setComplete(true);
325                         lastNode = parent;
326                     } else {
327                         OMElement e = (OMElement) lastNode;
328                         e.setComplete(true);
329                     }
330                     elementLevel--;
331                     break;
332                 case XMLStreamConstants.END_DOCUMENT:
333                     done = true;
334                     break;
335                 case XMLStreamConstants.SPACE:
336                     next();
337                     break;
338                 default :
339                     throw new OMException();
340             }
341             return token;
342         } catch (OMException e) {
343             throw e;
344         } catch (Exception JavaDoc e) {
345             throw new OMException(e);
346         }
347     }
348
349     /**
350      * Method getDocumentElement
351      *
352      * @return
353      */

354     public OMElement getDocumentElement() {
355         return getSOAPEnvelope();
356     }
357
358     /**
359      * Method processNamespaceData
360      *
361      * @param node
362      * @param isSOAPElement
363      */

364     protected void processNamespaceData(OMElement node, boolean isSOAPElement) {
365         int namespaceCount = parser.getNamespaceCount();
366         for (int i = 0; i < namespaceCount; i++) {
367             node.declareNamespace(parser.getNamespaceURI(i),
368                     parser.getNamespacePrefix(i));
369         }
370
371         // set the own namespace
372
String JavaDoc namespaceURI = parser.getNamespaceURI();
373         String JavaDoc prefix = parser.getPrefix();
374         OMNamespace namespace = null;
375         if (!"".equals(namespaceURI)) {
376             if (prefix == null) {
377                 // this means, this elements has a default namespace or it has inherited a default namespace from its parent
378
namespace = node.findNamespace(namespaceURI, "");
379                 if (namespace == null) {
380                     namespace = node.declareNamespace(namespaceURI, "");
381                 }
382             } else {
383                 namespace = node.findNamespace(namespaceURI, prefix);
384             }
385             node.setNamespace(namespace);
386         } else {
387
388         }
389
390
391
392         // TODO we got to have this to make sure OM reject mesagess that are not name space qualified
393
// But got to comment this to interop with Axis.1.x
394
// if (namespace == null) {
395
// throw new OMException("All elements must be namespace qualified!");
396
// }
397
if (isSOAPElement) {
398             if (node.getNamespace() != null && !node.getNamespace().getName().equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI) && !node.getNamespace().getName().equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
399                 throw new OMBuilderException("invalid SOAP namespace URI");
400             }
401         }
402
403     }
404
405     //added
406
/*these three methods to set and check detail element processing or mandatory fault element are present
407     */

408     public OMNamespace getEnvelopeNamespace() {
409         return envelopeNamespace;
410     }
411
412     public void setBooleanProcessingMandatoryFaultElements(boolean value) {
413         this.processingMandatoryFaultElements = value;
414     }
415
416     public boolean isProcessingDetailElements() {
417         return processingDetailElements;
418     }
419
420     public void setProcessingDetailElements(boolean value) {
421         processingDetailElements = value;
422     }
423
424 }
425
Popular Tags