1 16 package org.apache.axis2.soap.impl.llom; 17 18 import org.apache.axis2.om.*; 19 import org.apache.axis2.om.impl.llom.OMElementImpl; 20 import org.apache.axis2.soap.*; 21 import org.apache.axis2.soap.impl.llom.soap12.SOAP12Constants; 22 23 import javax.xml.namespace.QName ; 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import java.util.Iterator ; 27 28 31 public abstract class SOAPFaultImpl extends SOAPElement 32 implements SOAPFault, OMConstants { 33 34 protected Exception e; 35 36 42 public SOAPFaultImpl(SOAPBody parent, Exception e) throws SOAPProcessingException { 43 super(parent, SOAPConstants.SOAPFAULT_LOCAL_NAME, true); 44 setException(e); 45 } 46 47 public void setException(Exception e) { 48 this.e = e; 49 putExceptionToSOAPFault(e); 50 } 51 52 public SOAPFaultImpl(SOAPBody parent) throws SOAPProcessingException { 53 super(parent, SOAPConstants.SOAPFAULT_LOCAL_NAME, true); 54 } 55 56 63 public SOAPFaultImpl(SOAPBody parent, OMXMLParserWrapper builder) { 64 super(parent, SOAPConstants.SOAPFAULT_LOCAL_NAME, builder); 65 } 66 67 68 protected abstract SOAPFaultDetail getNewSOAPFaultDetail(SOAPFault fault) throws SOAPProcessingException; 69 70 72 public void setCode(SOAPFaultCode soapFaultCode) throws SOAPProcessingException { 73 setNewElement(getCode(), soapFaultCode); 74 } 75 76 public SOAPFaultCode getCode() { 77 return (SOAPFaultCode) this.getChildWithName(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME); 78 } 79 80 public void setReason(SOAPFaultReason reason) throws SOAPProcessingException { 81 setNewElement(getReason(), reason); 82 } 83 84 public SOAPFaultReason getReason() { 85 return (SOAPFaultReason) this.getChildWithName(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME); 86 } 87 88 public void setNode(SOAPFaultNode node) throws SOAPProcessingException { 89 setNewElement(getNode(), node); 90 } 91 92 public SOAPFaultNode getNode() { 93 return (SOAPFaultNode) this.getChildWithName(SOAP12Constants.SOAP_FAULT_NODE_LOCAL_NAME); 94 } 95 96 public void setRole(SOAPFaultRole role) throws SOAPProcessingException { 97 setNewElement(getRole(), role); 98 } 99 100 public SOAPFaultRole getRole() { 101 return (SOAPFaultRoleImpl) this.getChildWithName(SOAP12Constants.SOAP_FAULT_ROLE_LOCAL_NAME); 102 } 103 104 public void setDetail(SOAPFaultDetail detail) throws SOAPProcessingException { 105 setNewElement(getDetail(), detail); 106 } 107 108 public SOAPFaultDetail getDetail() { 109 return (SOAPFaultDetail) this.getChildWithName(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME); 110 } 111 112 113 115 public Exception getException() throws OMException { 116 getDetail(); 117 if (getDetail() == null) { 118 return new Exception ("No Exception element found in the SOAP Detail element"); 119 } 120 121 OMElement exceptionElement = getDetail().getFirstChildWithName(new QName (SOAPConstants.SOAP_FAULT_DETAIL_EXCEPTION_ENTRY)); 122 if (exceptionElement != null) { 123 return new Exception (exceptionElement.getText()); 124 } 125 return new Exception ("No Exception element found in the SOAP Detail element"); 126 } 127 128 protected void putExceptionToSOAPFault(Exception e) throws SOAPProcessingException { 129 StringWriter sw = new StringWriter (); 130 e.printStackTrace(new PrintWriter (sw)); 131 132 getDetail(); 133 if (getDetail() == null) { 134 setDetail(getNewSOAPFaultDetail(this)); 135 136 } 137 OMElement faultDetailEnty = new OMElementImpl(SOAPConstants.SOAP_FAULT_DETAIL_EXCEPTION_ENTRY, this.getNamespace()); 138 faultDetailEnty.setText(sw.getBuffer().toString()); 139 getDetail().addChild(faultDetailEnty); 140 } 141 142 protected void setNewElement(OMElement myElement, OMElement newElement) { 143 if (myElement != null) { 144 myElement.discard(); 145 } 146 if(newElement != null && newElement.getParent() != null){ 147 newElement.discard(); 148 } 149 this.addChild(newElement); 150 myElement = newElement; 151 } 152 153 protected OMElement getChildWithName(String childName) { 154 Iterator childrenIter = getChildren(); 155 while (childrenIter.hasNext()) { 156 OMNode node = (OMNode) childrenIter.next(); 157 if (node.getType() == OMNode.ELEMENT_NODE && childName.equals(((OMElement) node).getLocalName())) { 158 return (OMElement) node; 159 } 160 } 161 return null; 162 } 163 164 } 165 | Popular Tags |