1 16 package org.jboss.axis.message; 17 18 import java.io.IOException ; 19 import java.util.List ; 20 import java.util.Locale ; 21 22 import javax.xml.namespace.QName ; 23 import javax.xml.soap.DetailEntry ; 24 import javax.xml.soap.Name ; 25 import javax.xml.soap.SOAPElement ; 26 import javax.xml.soap.SOAPException ; 27 import javax.xml.soap.SOAPFault ; 28 29 import org.jboss.axis.AxisFault; 30 import org.jboss.axis.Constants; 31 import org.jboss.axis.description.FaultDesc; 32 import org.jboss.axis.description.OperationDesc; 33 import org.jboss.axis.encoding.DeserializationContext; 34 import org.jboss.axis.encoding.SerializationContext; 35 import org.jboss.axis.soap.SOAPConstants; 36 import org.jboss.axis.utils.Messages; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 import org.xml.sax.Attributes ; 40 import org.xml.sax.helpers.AttributesImpl ; 41 42 49 public class SOAPFaultImpl extends SOAPBodyElementAxisImpl implements SOAPFault 50 { 51 protected AxisFault fault; 52 protected String prefix; 53 private java.util.Locale locale; 54 55 public SOAPFaultImpl(String namespace, String localName, String prefix, 56 Attributes attrs, DeserializationContext context) 57 throws AxisFault 58 { 59 super(namespace, localName, prefix, attrs, context); 60 } 61 62 public SOAPFaultImpl(AxisFault fault) 63 { 64 this.fault = fault; 65 } 66 67 public void outputImpl(SerializationContext context) 68 throws IOException 69 { 70 SOAPConstants soapConstants = context.getMessageContext() == null ? 71 SOAPConstants.SOAP11_CONSTANTS : 72 context.getMessageContext().getSOAPConstants(); 73 74 namespaceURI = soapConstants.getEnvelopeURI(); 75 name = Constants.ELEM_FAULT; 76 77 context.registerPrefixForURI(prefix, soapConstants.getEnvelopeURI()); 78 context.startElement(new QName (this.getNamespaceURI(), 79 this.getName()), 80 attributes); 81 82 if (fault instanceof AxisFault) 84 { 85 AxisFault axisFault = fault; 86 if (axisFault.getFaultCode() != null) 87 { 88 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 91 { 92 String faultCode = context.qName2String(axisFault.getFaultCode()); 93 context.startElement(Constants.QNAME_FAULTCODE_SOAP12, null); 94 context.startElement(Constants.QNAME_FAULTVALUE_SOAP12, null); 95 context.writeSafeString(faultCode); 96 context.endElement(); 97 QName [] subcodes = axisFault.getFaultSubCodes(); 98 if (subcodes != null) 99 { 100 for (int i = 0; i < subcodes.length; i++) 101 { 102 faultCode = context.qName2String(subcodes[i]); 103 context.startElement(Constants.QNAME_FAULTSUBCODE_SOAP12, null); 104 context.startElement(Constants.QNAME_FAULTVALUE_SOAP12, null); 105 context.writeSafeString(faultCode); 106 context.endElement(); 107 } 108 109 for (int i = 0; i < subcodes.length; i++) 110 context.endElement(); 111 112 } 113 context.endElement(); 114 } 115 else 116 { 117 String faultCode = context.qName2String(axisFault.getFaultCode()); 118 context.startElement(Constants.QNAME_FAULTCODE, null); 119 context.writeSafeString(faultCode); 120 context.endElement(); 121 } 122 } 123 124 if (axisFault.getFaultString() != null) 125 { 126 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 127 { 128 context.startElement(Constants.QNAME_FAULTREASON_SOAP12, null); 129 AttributesImpl attrs = new AttributesImpl (); 130 attrs.addAttribute("http://www.w3.org/XML/1998/namespace", "lang", "xml:lang", "CDATA", "en"); 131 context.startElement(Constants.QNAME_TEXT_SOAP12, attrs); 132 } 133 else 134 context.startElement(Constants.QNAME_FAULTSTRING, null); 135 context.writeSafeString(axisFault.getFaultString()); 136 context.endElement(); 137 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 138 { 139 context.endElement(); 140 } 141 } 142 143 if (axisFault.getFaultActor() != null) 144 { 145 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 146 context.startElement(Constants.QNAME_FAULTROLE_SOAP12, null); 147 else 148 context.startElement(Constants.QNAME_FAULTACTOR, null); 149 150 context.writeSafeString(axisFault.getFaultActor()); 151 context.endElement(); 152 } 153 154 if (axisFault.getFaultNode() != null) 155 { 156 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 157 { 158 context.startElement(Constants.QNAME_FAULTNODE_SOAP12, null); 159 context.writeSafeString(axisFault.getFaultNode()); 160 context.endElement(); 161 } 162 } 163 164 QName qname = getFaultQName(fault.getClass(), context); 166 167 if (qname == null && fault.detail != null) 168 { 169 Class detailClass = fault.detail.getClass(); 170 qname = getFaultQName(detailClass, context); 171 } 172 173 if (qname == null) 174 { 175 qname = new QName ("", "faultData"); 177 } 178 179 Element [] faultDetails = axisFault.getFaultDetails(); 180 if (faultDetails != null) 181 { 182 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 183 context.startElement(Constants.QNAME_FAULTDETAIL_SOAP12, null); 184 else 185 context.startElement(Constants.QNAME_FAULTDETAILS, null); 186 187 axisFault.writeDetails(qname, context); 189 190 for (int i = 0; i < faultDetails.length; i++) 192 { 193 context.writeDOMElement(faultDetails[i]); 194 } 195 196 context.endElement(); 197 } 198 } 199 200 context.endElement(); 201 } 202 203 private QName getFaultQName(Class cls, SerializationContext context) 204 { 205 FaultDesc faultDesc = getFaultDesc(cls, context); 206 if (faultDesc != null) 207 { 208 return faultDesc.getQName(); 209 } 210 return null; 211 } 212 213 private QName getFaultXMLType(Class cls, SerializationContext context) 214 { 215 FaultDesc faultDesc = getFaultDesc(cls, context); 216 if (faultDesc != null) 217 { 218 return faultDesc.getXmlType(); 219 } 220 return null; 221 } 222 223 private FaultDesc getFaultDesc(Class cls, SerializationContext context) 224 { 225 FaultDesc faultDesc = null; 226 if (!cls.equals(AxisFault.class)) 227 { 228 if (context.getMessageContext() != null) 229 { 230 OperationDesc op = context.getMessageContext().getOperation(); 231 if (op != null) 232 { 233 faultDesc = op.getFaultByClass(cls); 234 } 235 } 236 } 237 return faultDesc; 238 } 239 240 public AxisFault getFault 241 () 242 { 243 return fault; 244 } 245 246 public void setFault 247 (AxisFault 248 fault) 249 { 250 this.fault = fault; 251 } 252 253 267 public void setFaultCode 268 (String 269 faultCode) throws SOAPException 270 { 271 fault.setFaultCodeAsString(faultCode); 272 } 273 274 280 public String getFaultCode 281 () 282 { 283 return fault.getFaultCode().getLocalPart(); 284 } 285 286 300 public void setFaultActor 301 (String 302 faultActor) throws SOAPException 303 { 304 fault.setFaultActor(faultActor); 305 } 306 307 315 public String getFaultActor 316 () 317 { 318 return fault.getFaultActor(); 319 } 320 321 332 public void setFaultString 333 (String 334 faultString) throws SOAPException 335 { 336 fault.setFaultString(faultString); 337 } 338 339 346 public String getFaultString 347 () 348 { 349 return fault.getFaultString(); 350 } 351 352 363 public javax.xml.soap.Detail getDetail 364 () 365 { 366 367 List children = this.getChildren(); 368 if (children == null || children.size() <= 0) 369 return null; 370 371 for (int i = 0; i < children.size(); i++) 373 { 374 Object obj = children.get(i); 375 if (obj instanceof javax.xml.soap.Detail ) 376 { 377 return (javax.xml.soap.Detail )obj; 378 } 379 } 380 return null; 381 } 382 383 397 public javax.xml.soap.Detail addDetail 398 () throws SOAPException 399 { 400 if (getDetail() != null) 401 { 402 throw new SOAPException (Messages.getMessage("valuePresent")); 403 } 404 DetailImpl detail = convertToDetail(fault); 405 addChildElement(detail); 406 return detail; 407 } 408 409 public void setFaultCode 410 (Name 411 faultCodeQName) throws SOAPException 412 { 413 String uri = faultCodeQName.getURI(); 414 String local = faultCodeQName.getLocalName(); 415 String prefix = faultCodeQName.getPrefix(); 416 417 this.prefix = prefix; 418 QName qname = new QName (uri, local); 419 fault.setFaultCode(qname); 420 } 421 422 public Name getFaultCodeAsName 423 () 424 { 425 QName qname = fault.getFaultCode(); 426 String uri = qname.getNamespaceURI(); 427 String local = qname.getLocalPart(); 428 return new NameImpl(local, prefix, uri); 429 } 430 431 public void setFaultString 432 (String 433 faultString, Locale 434 locale) throws SOAPException 435 { 436 fault.setFaultString(faultString); 437 this.locale = locale; 438 } 439 440 public Locale getFaultStringLocale 441 () 442 { 443 return locale; 444 } 445 446 453 private static DetailImpl convertToDetail 454 (AxisFault 455 fault) 456 throws SOAPException 457 { 458 DetailImpl detail = new DetailImpl(fault); 459 Element [] darray = fault.getFaultDetails(); 460 for (int i = 0; i < darray.length; i++) 461 { 462 Element element = darray[i]; 463 NameImpl name = new NameImpl(element.getLocalName(), element.getPrefix(), element.getNamespaceURI()); 464 DetailEntry detailEntry = detail.addDetailEntry(name); 465 copyChildren(detailEntry, element); 466 } 467 return detail; 468 } 469 470 477 private static void copyChildren 478 (SOAPElement 479 soapElement, Element 480 domElement) 481 throws SOAPException 482 { 483 org.w3c.dom.NodeList nl = domElement.getChildNodes(); 484 for (int j = 0; j < nl.getLength(); j++) 485 { 486 org.w3c.dom.Node childNode = nl.item(j); 487 if (childNode.getNodeType() == Node.TEXT_NODE) 488 { 489 soapElement.addTextNode(childNode.getNodeValue()); 490 break; } 492 if (childNode.getNodeType() == Node.ELEMENT_NODE) 493 { 494 String uri = childNode.getNamespaceURI(); 495 SOAPElement childSoapElement = null; 496 if (uri == null) 497 { 498 childSoapElement = soapElement.addChildElement(childNode.getLocalName 499 ()); 500 } 501 else 502 { 503 childSoapElement = soapElement.addChildElement(childNode.getLocalName(), 504 childNode.getPrefix(), uri); 505 } 506 copyChildren(childSoapElement, (Element )childNode); 507 } 508 } 509 } 510 } 511 | Popular Tags |