KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > ws_rm > common > HeaderSnooper


1 package demo.ws_rm.common;
2
3
4 import java.util.Iterator JavaDoc;
5
6 import javax.xml.soap.Name JavaDoc;
7 import javax.xml.soap.Node JavaDoc;
8 import javax.xml.soap.SOAPException JavaDoc;
9 import javax.xml.soap.SOAPHeader JavaDoc;
10 import javax.xml.soap.SOAPHeaderElement JavaDoc;
11 import javax.xml.ws.handler.soap.SOAPMessageContext;
12
13 import org.w3c.dom.NamedNodeMap JavaDoc;
14
15
16 /**
17  * Snoops SOAP headers.
18  */

19 public class HeaderSnooper extends HandlerBase {
20
21     public boolean handleMessage(SOAPMessageContext context) {
22         snoop(context);
23         return true;
24     }
25
26     public boolean handleFault(SOAPMessageContext context) {
27         snoop(context);
28         return true;
29     }
30
31     /**
32      * Snoops WS-* headers in the current message.
33      * Synchronized to avoid the output for asynchrously retransmitted
34      * messages being interleaved.
35      */

36     private synchronized void snoop(SOAPMessageContext context) {
37         try {
38             SOAPHeader JavaDoc header =
39                 context.getMessage().getSOAPPart().getEnvelope().getHeader();
40             if (header != null) {
41                 System.out.println(getMessageSummary(context));
42                 displayWSHeaders(header, WSA_NAMESPACE_URI, "WS-Addressing");
43                 displayWSHeaders(header, WSRM_NAMESPACE_URI, "WS-RM");
44             }
45         } catch (SOAPException JavaDoc se) {
46             System.out.println("SOAP header snoop failed: " + se);
47         }
48     }
49
50     /**
51      * @return a text summary of message type (out-of-band, application level
52      * etc.)
53      */

54     protected String JavaDoc getMessageSummary(SOAPMessageContext context) {
55         String JavaDoc action = getAction(context);
56         return getDirection(context)
57                + " Headers "
58                + (action != null
59                   ? action.startsWith(WSRM_NAMESPACE_URI)
60                     ? "[out-of-band RM protocol message]"
61                     : "[application-level message]"
62                   : "[partial response]");
63     }
64
65     /**
66      * @return a String specifying the direction of the current message
67      */

68     private String JavaDoc getDirection(SOAPMessageContext context) {
69         return isOutbound(context)
70                ? "\nOutbound"
71                : "\nInbound";
72     }
73
74     /**
75      * Display WS headers.
76      */

77     private void displayWSHeaders(SOAPHeader JavaDoc header,
78                                   String JavaDoc uri,
79                                   String JavaDoc display) throws SOAPException JavaDoc {
80         System.out.println(" " + display);
81         Iterator JavaDoc headerElements = header.examineAllHeaderElements();
82         boolean found = false;
83         while (headerElements.hasNext()) {
84             SOAPHeaderElement JavaDoc headerElement =
85                 (SOAPHeaderElement JavaDoc)headerElements.next();
86             Name JavaDoc headerName = headerElement.getElementName();
87             if (uri.equals(headerName.getURI())) {
88                 found = true;
89                 System.out.println(" " + headerName.getLocalName()
90                                    + getText(headerElement));
91             }
92         }
93         if (!found) {
94             System.out.println(" None");
95         }
96     }
97
98     /**
99      * @return a text summary of header element content
100      */

101     private String JavaDoc getText(SOAPHeaderElement JavaDoc headerElement) {
102         String JavaDoc text = " : ";
103         Iterator JavaDoc children = headerElement.getChildElements();
104         while (children.hasNext()) {
105             Node JavaDoc n = (Node JavaDoc)children.next();
106             text += n.getLocalName() != null ? n.getLocalName() + "=" : "";
107             text += getValue(n) + " ";
108         }
109         return text;
110     }
111
112     /**
113      * @return either the element value or a list of attribute values
114      */

115     private String JavaDoc getValue(Node JavaDoc node) {
116         String JavaDoc value = "";
117         if (node.getValue() != null) {
118             value = node.getValue();
119         } else {
120             NamedNodeMap JavaDoc attributes = node.getAttributes();
121             if (attributes != null) {
122                 for (int i = 0; i < attributes.getLength(); i++) {
123                     org.w3c.dom.Node JavaDoc attr = attributes.item(i);
124                     value += attr.getTextContent();
125                     if (i + 1 != attributes.getLength()) {
126                         value += ",";
127                     }
128                 }
129             }
130         }
131         return value;
132     }
133 }
134
135
Popular Tags