KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mypackage > HeaderExample


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

27
28 package mypackage;
29
30 import javax.xml.soap.*;
31 import java.net.*;
32 import java.util.*;
33 import java.io.*;
34
35
36 public class HeaderExample {
37     public static void main(String JavaDoc[] args) {
38         try {
39             // two arguments are passed in from build.xml
40
if (args.length != 0) {
41                 System.err.println("Usage: asant run");
42                 System.exit(1);
43             }
44
45             // Create message factory and SOAP factory
46
MessageFactory messageFactory = MessageFactory.newInstance();
47             SOAPFactory soapFactory = SOAPFactory.newInstance();
48
49             // Create a message
50
SOAPMessage message = messageFactory.createMessage();
51
52             // Get the SOAP header from the message and
53
// add headers to it
54
SOAPHeader header = message.getSOAPHeader();
55
56             String JavaDoc nameSpace = "ns";
57             String JavaDoc nameSpaceURI = "http://gizmos.com/NSURI";
58
59             Name order =
60                 soapFactory.createName("orderDesk", nameSpace, nameSpaceURI);
61             SOAPHeaderElement orderHeader = header.addHeaderElement(order);
62             orderHeader.setActor("http://gizmos.com/orders");
63
64             Name shipping =
65                 soapFactory.createName("shippingDesk", nameSpace, nameSpaceURI);
66             SOAPHeaderElement shippingHeader =
67                 header.addHeaderElement(shipping);
68             shippingHeader.setActor("http://gizmos.com/shipping");
69
70             Name confirmation =
71                 soapFactory.createName("confirmationDesk", nameSpace,
72                     nameSpaceURI);
73             SOAPHeaderElement confirmationHeader =
74                 header.addHeaderElement(confirmation);
75             confirmationHeader.setActor("http://gizmos.com/confirmations");
76
77             Name billing =
78                 soapFactory.createName("billingDesk", nameSpace, nameSpaceURI);
79             SOAPHeaderElement billingHeader = header.addHeaderElement(billing);
80             billingHeader.setActor("http://gizmos.com/billing");
81
82             // Add header with mustUnderstand attribute
83
Name tName =
84                 soapFactory.createName("Transaction", "t",
85                     "http://gizmos.com/orders");
86
87             SOAPHeaderElement transaction = header.addHeaderElement(tName);
88             transaction.setMustUnderstand(true);
89             transaction.addTextNode("5");
90
91             // Get the SOAP body from the message but leave
92
// it empty
93
SOAPBody body = message.getSOAPBody();
94
95             message.saveChanges();
96
97             // Display the message that would be sent
98
System.out.println("\n----- Request Message ----\n");
99             message.writeTo(System.out);
100
101             // Look at the headers
102
Iterator allHeaders = header.examineAllHeaderElements();
103
104             while (allHeaders.hasNext()) {
105                 SOAPHeaderElement headerElement =
106                     (SOAPHeaderElement) allHeaders.next();
107                 Name headerName = headerElement.getElementName();
108                 System.out.println("\nHeader name is " +
109                     headerName.getQualifiedName());
110                 System.out.println("Actor is " + headerElement.getActor());
111                 System.out.println("mustUnderstand is " +
112                     headerElement.getMustUnderstand());
113             }
114         } catch (Exception JavaDoc ex) {
115             ex.printStackTrace();
116         }
117     }
118 }
119
Popular Tags