1 package test.saaj; 2 3 import test.AxisTestBase; 4 5 import javax.xml.soap.Detail ; 6 import javax.xml.soap.MessageFactory ; 7 import javax.xml.soap.SOAPFactory ; 8 import javax.xml.soap.SOAPFault ; 9 import javax.xml.soap.SOAPMessage ; 10 import javax.xml.soap.SOAPElement ; 11 import javax.xml.soap.DetailEntry ; 12 import javax.xml.soap.Name ; 13 import javax.xml.soap.SOAPPart ; 14 import javax.xml.soap.SOAPEnvelope ; 15 import javax.xml.soap.SOAPBody ; 16 import javax.xml.soap.SOAPHeader ; 17 import javax.xml.soap.SOAPHeaderElement ; 18 import java.io.ByteArrayOutputStream ; 19 import java.util.Iterator ; 20 21 public class TestSOAPFaults extends AxisTestBase { 22 public TestSOAPFaults(String name) { 23 super(name); 24 } 25 26 public void testQuick() throws Exception { 27 MessageFactory msgfactory = MessageFactory.newInstance(); 28 SOAPFactory factory = SOAPFactory.newInstance(); 29 SOAPMessage outputmsg = msgfactory.createMessage(); 30 String valueCode = "faultcode"; 31 String valueString = "faultString"; 32 SOAPFault fault = outputmsg.getSOAPPart().getEnvelope().getBody().addFault(); 33 fault.setFaultCode(valueCode); 34 fault.setFaultString(valueString); 35 Detail d; 36 d = fault.addDetail(); 37 d.addDetailEntry(factory.createName("Hello")); 38 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 39 if (outputmsg != null) { 40 if (outputmsg.saveRequired()) { 41 outputmsg.saveChanges(); 42 } 43 outputmsg.writeTo(baos); 44 } 45 String xml = new String (baos.toByteArray()); 46 assertTrue(xml.indexOf("Hello")!=-1); 47 } 48 49 public void testSOAPFaultSaveChanges() throws Exception { 50 MessageFactory msgFactory = 51 MessageFactory.newInstance(); 52 SOAPMessage msg = msgFactory.createMessage(); 53 SOAPEnvelope envelope = 54 msg.getSOAPPart().getEnvelope(); 55 SOAPBody body = envelope.getBody(); 56 SOAPFault fault = body.addFault(); 57 58 fault.setFaultCode("Client"); 59 fault.setFaultString( 60 "Message does not have necessary info"); 61 fault.setFaultActor("http://gizmos.com/order"); 62 63 Detail detail = fault.addDetail(); 64 65 Name entryName = envelope.createName("order", "PO", 66 "http://gizmos.com/orders/"); 67 DetailEntry entry = detail.addDetailEntry(entryName); 68 entry.addTextNode( 69 "quantity element does not have a value"); 70 71 Name entryName2 = envelope.createName("confirmation", 72 "PO", "http://gizmos.com/confirm"); 73 DetailEntry entry2 = detail.addDetailEntry(entryName2); 74 entry2.addTextNode("Incomplete address: no zip code"); 75 76 msg.saveChanges(); 77 78 81 if (body.hasFault()) { 82 fault = body.getFault(); 83 String code = fault.getFaultCode(); 84 String string = fault.getFaultString(); 85 String actor = fault.getFaultActor(); 86 87 System.out.println("SOAP fault contains: "); 88 System.out.println(" fault code = " + code); 89 System.out.println(" fault string = " + string); 90 if (actor != null) { 91 System.out.println(" fault actor = " + actor); 92 } 93 94 detail = fault.getDetail(); 95 if (detail != null) { 96 Iterator it = detail.getDetailEntries(); 97 while (it.hasNext()) { 98 entry = (DetailEntry ) it.next(); 99 String value = entry.getValue(); 100 System.out.println(" Detail entry = " + value); 101 } 102 } 103 } 104 105 } 106 107 public void testAxis1432() throws Exception { 108 String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:cwmp=\"http://cwmp.com\">\n" + 109 " <soapenv:Header>\n" + 110 " <cwmp:ID soapenv:mustUnderstand=\"1\">HEADERID-7867678</cwmp:ID>\n" + 111 " </soapenv:Header>\n" + 112 " <soapenv:Body>\n" + 113 " <soapenv:Fault>\n" + 114 " <faultcode>soapenv:Client</faultcode>\n" + 115 " <faultstring>CWMP fault</faultstring>\n" + 116 " <detail>\n" + 117 " <cwmp:Fault>\n" + 118 " <cwmp:FaultCode>This is the fault code</cwmp:FaultCode>\n" + 119 " <cwmp:FaultString>Fault Message</cwmp:FaultString>\n" + 120 " </cwmp:Fault>\n" + 121 " </detail>\n" + 122 " </soapenv:Fault>\n" + 123 " </soapenv:Body>\n" + 124 "</soapenv:Envelope>"; 125 126 MessageFactory fac = MessageFactory.newInstance(); 127 SOAPMessage faultMessage = fac.createMessage(); 128 129 faultMessage = fac.createMessage(); 131 SOAPPart part = faultMessage.getSOAPPart(); 132 SOAPEnvelope envelope = part.getEnvelope(); 133 envelope.addNamespaceDeclaration("cwmp", "http://cwmp.com"); 134 SOAPBody body = envelope.getBody(); 135 SOAPHeader header = envelope.getHeader(); 136 Name idName = envelope.createName("ID", "cwmp", "http://cwmp.com"); 137 SOAPHeaderElement id = header.addHeaderElement(idName); 138 id.setMustUnderstand(true); 139 id.addTextNode("HEADERID-7867678"); 140 id.setActor(null); 141 142 SOAPFault fault = body.addFault(); 144 fault.setFaultCode("Client"); 145 fault.setFaultString("CWMP fault"); 146 fault.setFaultActor(null); 147 148 Detail faultDetail = fault.addDetail(); 150 Name cwmpFaultName = envelope.createName("Fault", "cwmp", 151 "http://cwmp.com"); 152 DetailEntry cwmpFaultDetail = 153 faultDetail.addDetailEntry(cwmpFaultName); 154 SOAPElement e = cwmpFaultDetail.addChildElement("FaultCode"); 155 156 e.addTextNode("This is the fault code"); 157 SOAPElement e2 = cwmpFaultDetail.addChildElement(envelope.createName("FaultString", "cwmp", "http://cwmp.com")); 158 e2.addTextNode("Fault Message"); 159 faultMessage.saveChanges(); 160 161 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 162 faultMessage.writeTo(baos); 163 String xml2 = new String (baos.toByteArray()); 164 assertXMLEqual(xml,xml2); 165 } 166 167 public static void main(String [] args) throws Exception { 168 TestSOAPFaults detailTest = new TestSOAPFaults("TestSOAPFaults"); 169 detailTest.testQuick(); 170 } 171 } 172 | Popular Tags |