1 16 package org.apache.axis.message; 17 18 import org.apache.axis.AxisFault; 19 import org.apache.axis.Constants; 20 import org.apache.axis.description.FaultDesc; 21 import org.apache.axis.description.OperationDesc; 22 import org.apache.axis.encoding.DeserializationContext; 23 import org.apache.axis.encoding.SerializationContext; 24 import org.apache.axis.soap.SOAPConstants; 25 import org.apache.axis.utils.Messages; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.Node ; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.helpers.AttributesImpl ; 30 31 import javax.xml.namespace.QName ; 32 import javax.xml.soap.DetailEntry ; 33 import javax.xml.soap.Name ; 34 import javax.xml.soap.SOAPElement ; 35 import javax.xml.soap.SOAPException ; 36 import java.util.List ; 37 import java.util.Locale ; 38 import java.util.Iterator ; 39 40 46 public class SOAPFault extends SOAPBodyElement implements javax.xml.soap.SOAPFault 47 { 48 protected AxisFault fault; 49 protected String prefix; 50 private java.util.Locale locale; 51 protected Detail detail = null; 52 53 public SOAPFault(String namespace, String localName, String prefix, 54 Attributes attrs, DeserializationContext context) 55 throws AxisFault 56 { 57 super(namespace, localName, prefix, attrs, context); 58 } 59 60 public SOAPFault(AxisFault fault) 61 { 62 this.fault = fault; 63 } 64 65 public void outputImpl(SerializationContext context) 66 throws Exception 67 { 68 SOAPConstants soapConstants = context.getMessageContext() == null ? 69 SOAPConstants.SOAP11_CONSTANTS : 70 context.getMessageContext().getSOAPConstants(); 71 72 namespaceURI = soapConstants.getEnvelopeURI(); 73 name = Constants.ELEM_FAULT; 74 75 context.registerPrefixForURI(prefix, soapConstants.getEnvelopeURI()); 76 context.startElement(new QName (this.getNamespaceURI(), 77 this.getName()), 78 attributes); 79 80 if (fault instanceof AxisFault) { 82 AxisFault axisFault = fault; 83 if (axisFault.getFaultCode() != null) { 84 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { 87 String faultCode = context.qName2String(axisFault.getFaultCode()); 88 context.startElement(Constants.QNAME_FAULTCODE_SOAP12, null); 89 context.startElement(Constants.QNAME_FAULTVALUE_SOAP12, null); 90 context.writeSafeString(faultCode); 91 context.endElement(); 92 QName [] subcodes = axisFault.getFaultSubCodes(); 93 if (subcodes != null) { 94 for (int i = 0; i < subcodes.length; i++) { 95 faultCode = context.qName2String(subcodes[i]); 96 context.startElement(Constants.QNAME_FAULTSUBCODE_SOAP12, null); 97 context.startElement(Constants.QNAME_FAULTVALUE_SOAP12, null); 98 context.writeSafeString(faultCode); 99 context.endElement(); 100 } 101 102 for (int i = 0; i < subcodes.length; i++) 103 context.endElement(); 104 105 } 106 context.endElement(); 107 } else { 108 String faultCode = context.qName2String(axisFault.getFaultCode()); 109 context.startElement(Constants.QNAME_FAULTCODE, null); 110 context.writeSafeString(faultCode); 111 context.endElement(); 112 } 113 } 114 115 if (axisFault.getFaultString() != null) { 116 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { 117 context.startElement(Constants.QNAME_FAULTREASON_SOAP12, null); 118 AttributesImpl attrs = new AttributesImpl (); 119 attrs.addAttribute("http://www.w3.org/XML/1998/namespace", "lang", "xml:lang", "CDATA", "en"); 120 context.startElement(Constants.QNAME_TEXT_SOAP12, attrs); 121 } else 122 context.startElement(Constants.QNAME_FAULTSTRING, null); 123 context.writeSafeString(axisFault.getFaultString()); 124 context.endElement(); 125 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { 126 context.endElement(); 127 } 128 } 129 130 if (axisFault.getFaultActor() != null) { 131 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 132 context.startElement(Constants.QNAME_FAULTROLE_SOAP12, null); 133 else 134 context.startElement(Constants.QNAME_FAULTACTOR, null); 135 136 context.writeSafeString(axisFault.getFaultActor()); 137 context.endElement(); 138 } 139 140 if (axisFault.getFaultNode() != null) { 141 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { 142 context.startElement(Constants.QNAME_FAULTNODE_SOAP12, null); 143 context.writeSafeString(axisFault.getFaultNode()); 144 context.endElement(); 145 } 146 } 147 148 QName qname = getFaultQName(fault.getClass(), context); 150 if (qname == null && fault.detail != null) { 151 qname = getFaultQName(fault.detail.getClass(), context); 152 } 153 if (qname == null) { 154 qname = new QName ("", "faultData"); 156 } 157 Element [] faultDetails = axisFault.getFaultDetails(); 158 if (faultDetails != null) { 159 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 160 context.startElement(Constants.QNAME_FAULTDETAIL_SOAP12, null); 161 else 162 context.startElement(Constants.QNAME_FAULTDETAILS, null); 163 164 axisFault.writeDetails(qname, context); 166 for (int i = 0; i < faultDetails.length; i++) { 168 context.writeDOMElement(faultDetails[i]); 169 } 170 171 if (detail!= null) { 172 for (Iterator it = detail.getChildren().iterator(); it.hasNext();) { 173 ((NodeImpl)it.next()).output(context); 174 } 175 } 176 177 context.endElement(); 178 } 179 } 180 181 context.endElement(); 182 } 183 184 private QName getFaultQName(Class cls, SerializationContext context) { 185 QName qname = null; 186 if (! cls.equals(AxisFault.class)) { 187 FaultDesc faultDesc = null; 188 if (context.getMessageContext() != null) { 189 OperationDesc op = context.getMessageContext().getOperation(); 190 if (op != null) { 191 faultDesc = op.getFaultByClass(cls); 192 } 193 } 194 195 if (faultDesc != null) { 196 qname = faultDesc.getQName(); 197 } 198 } 199 return qname; 200 } 201 202 public AxisFault getFault() 203 { 204 return fault; 205 } 206 207 public void setFault(AxisFault fault) 208 { 209 this.fault = fault; 210 } 211 212 225 public void setFaultCode(String faultCode) throws SOAPException { 226 fault.setFaultCodeAsString(faultCode); 227 } 228 229 234 public String getFaultCode() { 235 return fault.getFaultCode().getLocalPart(); 236 } 237 238 251 public void setFaultActor(String faultActor) throws SOAPException { 252 fault.setFaultActor(faultActor); 253 } 254 255 262 public String getFaultActor() { 263 return fault.getFaultActor(); 264 } 265 266 277 public void setFaultString(String faultString) throws SOAPException { 278 fault.setFaultString(faultString); 279 } 280 281 287 public String getFaultString() { 288 return fault.getFaultString(); 289 } 290 291 301 public javax.xml.soap.Detail getDetail() { 302 List children = this.getChildren(); 303 if(children==null || children.size()<=0) 304 return null; 305 306 for (int i=0; i < children.size(); i++) { 308 Object obj = children.get(i); 309 if (obj instanceof javax.xml.soap.Detail ) { 310 return (javax.xml.soap.Detail ) obj; 311 } 312 } 313 return null; 314 } 315 316 329 public javax.xml.soap.Detail addDetail() throws SOAPException { 330 if(getDetail() != null){ 331 throw new SOAPException (Messages.getMessage("valuePresent")); 332 } 333 Detail detail = convertToDetail(fault); 334 addChildElement(detail); 335 return detail; 336 } 337 338 public void setFaultCode(Name faultCodeQName) throws SOAPException { 339 String uri = faultCodeQName.getURI(); 340 String local = faultCodeQName.getLocalName(); 341 String prefix = faultCodeQName.getPrefix(); 342 343 this.prefix = prefix; 344 QName qname = new QName (uri,local); 345 fault.setFaultCode(qname); 346 } 347 348 public Name getFaultCodeAsName() { 349 QName qname = fault.getFaultCode(); 350 String uri = qname.getNamespaceURI(); 351 String local = qname.getLocalPart(); 352 return new PrefixedQName(uri, local, prefix); 353 } 354 355 public void setFaultString(String faultString, Locale locale) throws SOAPException { 356 fault.setFaultString(faultString); 357 this.locale = locale; 358 } 359 360 public Locale getFaultStringLocale() { 361 return locale; 362 } 363 364 371 private Detail convertToDetail(AxisFault fault) 372 throws SOAPException 373 { 374 detail = new Detail(); 375 Element [] darray = fault.getFaultDetails(); 376 fault.setFaultDetail(new Element []{}); 377 for (int i = 0; i < darray.length; i++) 378 { 379 Element detailtEntryElem = darray[i]; 380 DetailEntry detailEntry = detail.addDetailEntry( 381 new PrefixedQName(detailtEntryElem.getNamespaceURI(), 382 detailtEntryElem.getLocalName(), detailtEntryElem.getPrefix())); 383 copyChildren(detailEntry, detailtEntryElem); 384 } 385 return detail; 386 } 387 388 395 private static void copyChildren(SOAPElement soapElement, Element domElement) 396 throws SOAPException 397 { 398 org.w3c.dom.NodeList nl = domElement.getChildNodes(); 399 for (int j = 0; j < nl.getLength(); j++) 400 { 401 org.w3c.dom.Node childNode = nl.item(j); 402 if (childNode.getNodeType() == Node.TEXT_NODE) 403 { 404 soapElement.addTextNode(childNode.getNodeValue()); 405 break; } 407 if (childNode.getNodeType() == Node.ELEMENT_NODE) 408 { 409 String uri = childNode.getNamespaceURI(); 410 SOAPElement childSoapElement = null; 411 if (uri == null) 412 { 413 childSoapElement = soapElement.addChildElement(childNode.getLocalName 414 ()); 415 } 416 else 417 { 418 childSoapElement = soapElement.addChildElement( 419 childNode.getLocalName(), 420 childNode.getPrefix(), uri); 421 } 422 copyChildren(childSoapElement, (Element ) childNode); 423 } 424 } 425 } 426 } 427 | Popular Tags |