1 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 39 public class StAXSOAPModelBuilder extends StAXBuilder { 40 43 private SOAPEnvelopeImpl envelope; 44 private OMNamespace envelopeNamespace; 45 46 private SOAPFactory soapFactory; 47 48 51 private boolean headerPresent = false; 52 53 56 private boolean bodyPresent = false; 57 58 61 private Log log = LogFactory.getLog(getClass()); 62 63 67 int elementLevel = 0; 68 69 private boolean processingFault = false; 70 71 74 private boolean processingMandatoryFaultElements = false; 75 76 79 private boolean processingDetailElements = false; 80 81 private SOAPBuilderHelper builderHelper; 82 83 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 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 147 public SOAPEnvelope getSOAPEnvelope() throws OMException { 148 while ((envelope == null) && !done) { 149 next(); 150 } 151 return envelope; 152 } 153 154 160 protected OMNode createOMElement() throws OMException { 161 OMElement node; 162 String 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 188 protected OMElement constructNode(OMElement parent, String 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 processAttributes(element); 202 203 } else if (elementLevel == 2) { 204 205 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 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 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 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 element = soapFactory.createSOAPFault((SOAPBody) parent, this); 256 processNamespaceData(element, false); 257 processAttributes(element); 258 259 260 processingFault = true; 261 262 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 element = soapFactory.createOMElement(elementName, null, 276 parent, this); 277 processNamespaceData(element, false); 278 processAttributes(element); 279 280 } 281 return element; 282 } 283 284 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 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 e) { 345 throw new OMException(e); 346 } 347 } 348 349 354 public OMElement getDocumentElement() { 355 return getSOAPEnvelope(); 356 } 357 358 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 String namespaceURI = parser.getNamespaceURI(); 373 String prefix = parser.getPrefix(); 374 OMNamespace namespace = null; 375 if (!"".equals(namespaceURI)) { 376 if (prefix == null) { 377 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 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 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 |