001: /*
002: /*
003: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
004: * Government Rights - Commercial software. Government users are subject
005: * to the Sun Microsystems, Inc. standard license agreement and
006: * applicable provisions of the FAR and its supplements. Use is subject
007: * to license terms.
008: *
009: * This distribution may include materials developed by third parties.
010: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
011: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
012: * other countries.
013: *
014: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
015: *
016: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
017: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
018: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
019: * en vigueur de la FAR (Federal Acquisition Regulations) et des
020: * supplements a celles-ci. Distribue par des licences qui en
021: * restreignent l'utilisation.
022: *
023: * Cette distribution peut comprendre des composants developpes par des
024: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
025: * sont des marques de fabrique ou des marques deposees de Sun
026: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
027: */
028:
029: package mypackage;
030:
031: import javax.xml.soap.*;
032: import java.util.*;
033:
034: public class SOAPFaultTest {
035: public static void main(String[] args) {
036: try {
037: MessageFactory messageFactory = MessageFactory
038: .newInstance();
039: SOAPFactory soapFactory = SOAPFactory.newInstance();
040: SOAPMessage message = messageFactory.createMessage();
041: SOAPBody body = message.getSOAPBody();
042: SOAPFault fault = body.addFault();
043:
044: Name faultName = soapFactory.createName("Client", "",
045: SOAPConstants.URI_NS_SOAP_ENVELOPE);
046: fault.setFaultCode(faultName);
047:
048: fault
049: .setFaultString("Message does not have necessary info");
050: fault.setFaultActor("http://gizmos.com/order");
051:
052: Detail detail = fault.addDetail();
053:
054: Name entryName = soapFactory.createName("order", "PO",
055: "http://gizmos.com/orders/");
056: DetailEntry entry = detail.addDetailEntry(entryName);
057: entry.addTextNode("Quantity element does not have a value");
058:
059: Name entryName2 = soapFactory.createName("confirmation",
060: "PO", "http://gizmos.com/confirm");
061: DetailEntry entry2 = detail.addDetailEntry(entryName2);
062: entry2.addTextNode("Incomplete address: " + "no zip code");
063:
064: message.saveChanges();
065:
066: System.out
067: .println("Here is what the XML message looks like:");
068: message.writeTo(System.out);
069: System.out.println();
070: System.out.println();
071:
072: // Now retrieve the SOAPFault object and
073: // its contents, after checking to see that
074: // there is one
075: if (body.hasFault()) {
076: SOAPFault newFault = body.getFault();
077:
078: // Get the qualified name of the fault code
079: Name code = newFault.getFaultCodeAsName();
080:
081: String string = newFault.getFaultString();
082: String actor = newFault.getFaultActor();
083:
084: System.out.println("SOAP fault contains: ");
085: System.out.println(" Fault code = "
086: + code.getQualifiedName());
087: System.out.println(" Local name = "
088: + code.getLocalName());
089: System.out.println(" Namespace prefix = "
090: + code.getPrefix() + ", bound to "
091: + code.getURI());
092: System.out.println(" Fault string = " + string);
093:
094: if (actor != null) {
095: System.out.println(" Fault actor = " + actor);
096: }
097:
098: Detail newDetail = newFault.getDetail();
099:
100: if (newDetail != null) {
101: Iterator entries = newDetail.getDetailEntries();
102:
103: while (entries.hasNext()) {
104: DetailEntry newEntry = (DetailEntry) entries
105: .next();
106: String value = newEntry.getValue();
107: System.out.println(" Detail entry = " + value);
108: }
109: }
110: }
111: } catch (Exception ex) {
112: ex.printStackTrace();
113: }
114: }
115: }
|