1 57 58 package org.apache.soap; 59 60 import java.io.*; 61 import java.util.*; 62 import org.w3c.dom.*; 63 import org.apache.soap.util.*; 64 import org.apache.soap.util.xml.*; 65 import org.apache.soap.encoding.*; 66 import org.apache.soap.rpc.Parameter; 67 import org.apache.soap.rpc.RPCConstants; 68 import org.apache.soap.rpc.SOAPContext; 69 70 79 public class Fault 80 { 81 private String faultCode = null; 82 private String faultString = null; 83 private String faultActorURI = null; 84 private Vector detailEntries = null; 85 private Vector faultEntries = null; 86 private AttributeHandler attrHandler = new AttributeHandler(); 87 88 public Fault() {} 89 90 public Fault(SOAPException _soapException) 91 { 92 faultCode = _soapException.getFaultCode(); 93 faultString = _soapException.getMessage(); 94 } 95 96 public void setAttribute(QName attrQName, String value) 97 { 98 attrHandler.setAttribute(attrQName, value); 99 } 100 101 public String getAttribute(QName attrQName) 102 { 103 return attrHandler.getAttribute(attrQName); 104 } 105 106 public void removeAttribute(QName attrQName) 107 { 108 attrHandler.removeAttribute(attrQName); 109 } 110 111 public void declareNamespace(String nsPrefix, String namespaceURI) 112 { 113 attrHandler.declareNamespace(nsPrefix, namespaceURI); 114 } 115 116 public void setFaultCode(String faultCode) 117 { 118 this.faultCode = faultCode; 119 } 120 121 public String getFaultCode() 122 { 123 return faultCode; 124 } 125 126 public void setFaultString(String faultString) 127 { 128 this.faultString = faultString; 129 } 130 131 public String getFaultString() 132 { 133 return faultString; 134 } 135 136 public void setFaultActorURI(String faultActorURI) 137 { 138 this.faultActorURI = faultActorURI; 139 } 140 141 public String getFaultActorURI() 142 { 143 return faultActorURI; 144 } 145 146 public void setDetailEntries(Vector detailEntries) 147 { 148 this.detailEntries = detailEntries; 149 } 150 151 public Vector getDetailEntries() 152 { 153 return detailEntries; 154 } 155 156 public void setFaultEntries(Vector faultEntries) 157 { 158 this.faultEntries = faultEntries; 159 } 160 161 public Vector getFaultEntries() 162 { 163 return faultEntries; 164 } 165 166 public void marshall(String inScopeEncStyle, Writer sink, NSStack nsStack, 167 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 168 throws IllegalArgumentException , IOException 169 { 170 171 attrHandler.populateNSStack(nsStack); 172 173 String faultCode = getFaultCode(); 174 String faultString = getFaultString(); 175 String faultActorURI = getFaultActorURI(); 176 Vector detailEntries = getDetailEntries(); 177 Vector faultEntries = getFaultEntries(); 178 179 String soapEnvNSPrefix = attrHandler.getUniquePrefixFromURI( 181 Constants.NS_URI_SOAP_ENV, Constants.NS_PRE_SOAP_ENV, nsStack); 182 183 sink.write('<' + soapEnvNSPrefix + ':' + Constants.ELEM_FAULT); 186 187 attrHandler.marshall(sink, ctx); 189 190 sink.write('>' + StringUtils.lineSeparator + 191 '<' + Constants.ELEM_FAULT_CODE + '>' + faultCode + 192 "</" + Constants.ELEM_FAULT_CODE + '>' + 193 StringUtils.lineSeparator + 194 '<' + Constants.ELEM_FAULT_STRING + '>' + faultString + 195 "</" + Constants.ELEM_FAULT_STRING + '>' + 196 StringUtils.lineSeparator); 197 198 if (faultActorURI != null) 200 { 201 sink.write('<' + Constants.ELEM_FAULT_ACTOR + '>' + faultActorURI + 202 "</" + Constants.ELEM_FAULT_ACTOR + '>' + 203 StringUtils.lineSeparator); 204 } 205 206 if (detailEntries != null) 209 { 210 sink.write('<' + Constants.ELEM_DETAIL + '>' + 211 StringUtils.lineSeparator); 212 213 for (Enumeration e = detailEntries.elements(); e.hasMoreElements();) 215 { 216 Object detailEntry = e.nextElement(); 217 218 if (detailEntry instanceof Element) 220 { 221 Element detailEntryEl = (Element)detailEntry; 222 223 Utils.marshallNode(detailEntryEl, sink); 224 sink.write(StringUtils.lineSeparator); 225 } 226 230 else if (detailEntry instanceof Parameter) 231 { 232 try 233 { 234 Parameter detailEntryParameter = (Parameter)detailEntry; 235 Serializer s = xjmr.querySerializer(Parameter.class, inScopeEncStyle); 236 237 if (s != null) 238 { 239 s.marshall(null, 240 Parameter.class, 241 detailEntryParameter, 242 Constants.ELEM_FAULT_DETAIL_ENTRY, 243 sink, 244 nsStack, 245 xjmr, 246 ctx); 247 sink.write(StringUtils.lineSeparator); 248 } 249 else 250 { 251 throw new IllegalArgumentException ("Could not find Parameter " + 252 "serializer."); 253 } 254 } 255 catch (IllegalArgumentException iae) 256 { 257 } 258 } 259 } 260 261 sink.write("</" + Constants.ELEM_DETAIL + '>' + 262 StringUtils.lineSeparator); 263 } 264 265 if (faultEntries != null) 268 { 269 for (Enumeration e = faultEntries.elements(); e.hasMoreElements();) 270 { 271 Element faultEntryEl = (Element)e.nextElement(); 272 273 Utils.marshallNode(faultEntryEl, sink); 274 275 sink.write(StringUtils.lineSeparator); 276 } 277 } 278 279 sink.write("</" + soapEnvNSPrefix + ':' + Constants.ELEM_FAULT + '>' + 280 StringUtils.lineSeparator); 281 282 nsStack.popScope(); 283 } 284 285 public static Fault unmarshall(String inScopeEncStyle, Node src, 286 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 287 throws IllegalArgumentException 288 { 289 Element root = (Element)src; 290 Fault fault = new Fault(); 291 292 293 if (Constants.Q_ELEM_FAULT.matches(root)) 294 { 295 Element faultCodeEl = null; 296 Element faultStringEl = null; 297 Element faultActorEl = null; 298 Element detailEl = null; 299 Vector faultEntries = new Vector(); 300 Element tempEl = DOMUtils.getFirstChildElement(root); 301 302 fault.attrHandler = AttributeHandler.unmarshall(root, ctx); 304 305 while (tempEl != null) 307 { 308 String namespaceURI = tempEl.getNamespaceURI(); 309 String localPart = tempEl.getLocalName(); 310 311 if (localPart == null) 312 { 313 localPart = tempEl.getTagName(); 314 } 315 316 if (namespaceURI == null 318 || namespaceURI.equals(Constants.NS_URI_SOAP_ENV)) 319 { 320 if (localPart.equals(Constants.ELEM_FAULT_CODE)) 321 { 322 faultCodeEl = tempEl; 323 } 324 else if (localPart.equals(Constants.ELEM_FAULT_STRING)) 325 { 326 faultStringEl = tempEl; 327 } 328 else if (localPart.equals(Constants.ELEM_FAULT_ACTOR)) 329 { 330 faultActorEl = tempEl; 331 } 332 else if (localPart.equals(Constants.ELEM_DETAIL)) 333 { 334 detailEl = tempEl; 335 } 336 else 337 { 338 faultEntries.addElement(tempEl); 340 } 341 } 342 else 343 { 344 faultEntries.addElement(tempEl); 346 } 347 348 tempEl = DOMUtils.getNextSiblingElement(tempEl); 349 } 350 351 if (faultCodeEl != null) 353 { 354 String faultCode = DOMUtils.getChildCharacterData(faultCodeEl); 355 356 fault.setFaultCode(faultCode); 357 } 358 else 359 { 360 throw new IllegalArgumentException ("A '" + Constants.Q_ELEM_FAULT + 361 "' element must contain a: '" + 362 Constants.ELEM_FAULT_CODE + 363 "' element."); 364 } 365 366 if (faultStringEl != null) 368 { 369 String faultString = DOMUtils.getChildCharacterData(faultStringEl); 370 371 fault.setFaultString(faultString); 372 } 373 else 374 { 375 throw new IllegalArgumentException ("A '" + Constants.Q_ELEM_FAULT + 376 "' element must contain a: '" + 377 Constants.ELEM_FAULT_STRING + 378 "' element."); 379 } 380 381 if (faultActorEl != null) 383 { 384 String faultActorURI = DOMUtils.getChildCharacterData(faultActorEl); 385 386 fault.setFaultActorURI(faultActorURI); 387 } 388 389 if (detailEl != null) 391 { 392 Vector detailEntries = new Vector(); 393 394 for (Element el = DOMUtils.getFirstChildElement(detailEl); 395 el != null; 396 el = DOMUtils.getNextSiblingElement(el)) 397 { 398 try 400 { 401 String declEncStyle = 402 DOMUtils.getAttributeNS(el, 403 Constants.NS_URI_SOAP_ENV, 404 Constants.ATTR_ENCODING_STYLE); 405 String actualEncStyle = declEncStyle != null 406 ? declEncStyle 407 : inScopeEncStyle; 408 Bean paramBean = xjmr.unmarshall(declEncStyle, 409 RPCConstants.Q_ELEM_PARAMETER, 410 el, 411 ctx); 412 Parameter param = (Parameter)paramBean.value; 413 414 detailEntries.addElement(param); 415 } 416 catch (Exception e) 417 { 418 detailEntries.addElement(el); 419 } 420 } 421 422 fault.setDetailEntries(detailEntries); 423 } 424 425 if (faultEntries.size() > 0) 428 { 429 fault.setFaultEntries(faultEntries); 430 } 431 } 432 else 433 { 434 throw new IllegalArgumentException ("Root element of a SOAP Fault " + 435 "must be: '" + 436 Constants.Q_ELEM_FAULT + "'."); 437 } 438 439 return fault; 440 } 441 442 public String toString() 443 { 444 StringWriter sw = new StringWriter(); 445 PrintWriter pw = new PrintWriter(sw); 446 447 pw.print("[Attributes=" + attrHandler + "] " + 448 "[faultCode=" + faultCode + "] " + 449 "[faultString=" + faultString + "] " + 450 "[faultActorURI=" + faultActorURI + "] " + 451 "[DetailEntries="); 452 453 if (detailEntries != null) 454 { 455 pw.println(); 456 457 for (int i = 0; i < detailEntries.size(); i++) 458 { 459 Object detailEl = detailEntries.elementAt(i); 460 461 if (detailEl instanceof Parameter) 462 { 463 Parameter param = (Parameter)detailEl; 464 465 pw.println("[(" + i + ")=" + param +"]"); 466 } 467 else 468 { 469 pw.println("[(" + i + ")=" + 470 DOM2Writer.nodeToString((Element)detailEl) + "]"); 471 } 472 } 473 } 474 475 pw.print("] [FaultEntries="); 476 477 if (faultEntries != null) 478 { 479 pw.println(); 480 481 for (int i = 0; i < faultEntries.size(); i++) 482 { 483 pw.println("[(" + i + ")=" + 484 DOM2Writer.nodeToString((Element)faultEntries.elementAt(i)) + 485 "]"); 486 } 487 } 488 489 pw.print("]"); 490 491 return sw.toString(); 492 } 493 } 494 | Popular Tags |