KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mypackage > SOAPFaultTest


1 /*
2  /*
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
4  * Government Rights - Commercial software. Government users are subject
5  * to the Sun Microsystems, Inc. standard license agreement and
6  * applicable provisions of the FAR and its supplements. Use is subject
7  * to license terms.
8  *
9  * This distribution may include materials developed by third parties.
10  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
11  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
12  * other countries.
13  *
14  * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
15  *
16  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
17  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
18  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
19  * en vigueur de la FAR (Federal Acquisition Regulations) et des
20  * supplements a celles-ci. Distribue par des licences qui en
21  * restreignent l'utilisation.
22  *
23  * Cette distribution peut comprendre des composants developpes par des
24  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
25  * sont des marques de fabrique ou des marques deposees de Sun
26  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
27  */

28
29 package mypackage;
30
31 import javax.xml.soap.*;
32 import java.util.*;
33
34
35 public class SOAPFaultTest {
36     public static void main(String JavaDoc[] args) {
37         try {
38             MessageFactory messageFactory = MessageFactory.newInstance();
39             SOAPFactory soapFactory = SOAPFactory.newInstance();
40             SOAPMessage message = messageFactory.createMessage();
41             SOAPBody body = message.getSOAPBody();
42             SOAPFault fault = body.addFault();
43
44             Name faultName =
45                 soapFactory.createName("Client", "",
46                     SOAPConstants.URI_NS_SOAP_ENVELOPE);
47             fault.setFaultCode(faultName);
48
49             fault.setFaultString("Message does not have necessary info");
50             fault.setFaultActor("http://gizmos.com/order");
51
52             Detail detail = fault.addDetail();
53
54             Name entryName =
55                 soapFactory.createName("order", "PO",
56                     "http://gizmos.com/orders/");
57             DetailEntry entry = detail.addDetailEntry(entryName);
58             entry.addTextNode("Quantity element does not have a value");
59
60             Name entryName2 =
61                 soapFactory.createName("confirmation", "PO",
62                     "http://gizmos.com/confirm");
63             DetailEntry entry2 = detail.addDetailEntry(entryName2);
64             entry2.addTextNode("Incomplete address: " + "no zip code");
65
66             message.saveChanges();
67
68             System.out.println("Here is what the XML message looks like:");
69             message.writeTo(System.out);
70             System.out.println();
71             System.out.println();
72
73             // Now retrieve the SOAPFault object and
74
// its contents, after checking to see that
75
// there is one
76
if (body.hasFault()) {
77                 SOAPFault newFault = body.getFault();
78
79                 // Get the qualified name of the fault code
80
Name code = newFault.getFaultCodeAsName();
81
82                 String JavaDoc string = newFault.getFaultString();
83                 String JavaDoc actor = newFault.getFaultActor();
84
85                 System.out.println("SOAP fault contains: ");
86                 System.out.println(" Fault code = " + code.getQualifiedName());
87                 System.out.println(" Local name = " + code.getLocalName());
88                 System.out.println(" Namespace prefix = " + code.getPrefix() +
89                     ", bound to " + code.getURI());
90                 System.out.println(" Fault string = " + string);
91
92                 if (actor != null) {
93                     System.out.println(" Fault actor = " + actor);
94                 }
95
96                 Detail newDetail = newFault.getDetail();
97
98                 if (newDetail != null) {
99                     Iterator entries = newDetail.getDetailEntries();
100
101                     while (entries.hasNext()) {
102                         DetailEntry newEntry = (DetailEntry) entries.next();
103                         String JavaDoc value = newEntry.getValue();
104                         System.out.println(" Detail entry = " + value);
105                     }
106                 }
107             }
108         } catch (Exception JavaDoc ex) {
109             ex.printStackTrace();
110         }
111     }
112 }
113
Popular Tags