01: package demo.ws_addressing.common;
02:
03: import java.util.Iterator;
04: import java.util.Map;
05: import java.util.Set;
06:
07: import javax.xml.namespace.QName;
08: import javax.xml.soap.Name;
09: import javax.xml.soap.Node;
10: import javax.xml.soap.SOAPException;
11: import javax.xml.soap.SOAPHeader;
12: import javax.xml.soap.SOAPHeaderElement;
13: import javax.xml.ws.handler.MessageContext;
14: import javax.xml.ws.handler.soap.SOAPHandler;
15: import javax.xml.ws.handler.soap.SOAPMessageContext;
16: import static javax.xml.ws.handler.MessageContext.MESSAGE_OUTBOUND_PROPERTY;
17:
18: import org.objectweb.celtix.ws.addressing.AddressingBuilder;
19: import org.objectweb.celtix.ws.addressing.AddressingConstants;
20:
21: /**
22: * Snoops SOAP headers.
23: */
24: public class HeaderSnooper implements SOAPHandler<SOAPMessageContext> {
25:
26: private static final AddressingConstants ADDRESSING_CONSTANTS = AddressingBuilder
27: .getAddressingBuilder().newAddressingConstants();
28: private static final String WSA_NAMESPACE_URI = ADDRESSING_CONSTANTS
29: .getNamespaceURI();
30:
31: public void init(Map<String, Object> map) {
32: }
33:
34: public Set<QName> getHeaders() {
35: return null;
36: }
37:
38: public boolean handleMessage(SOAPMessageContext context) {
39: snoop(context);
40: return true;
41: }
42:
43: public boolean handleFault(SOAPMessageContext context) {
44: snoop(context);
45: return true;
46: }
47:
48: public void close(MessageContext context) {
49: }
50:
51: public void destroy() {
52: }
53:
54: private void snoop(SOAPMessageContext context) {
55: try {
56: SOAPHeader header = context.getMessage().getSOAPPart()
57: .getEnvelope().getHeader();
58: if (header != null) {
59: System.out.println(getDirection(context)
60: + " WS-Addressing headers");
61: Iterator headerElements = header
62: .examineAllHeaderElements();
63: while (headerElements.hasNext()) {
64: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
65: .next();
66: Name headerName = headerElement.getElementName();
67: if (WSA_NAMESPACE_URI.equals(headerName.getURI())) {
68: System.out.println(headerName.getLocalName()
69: + getText(headerElement));
70: }
71: }
72: System.out.println();
73: }
74: } catch (SOAPException se) {
75: System.out.println("SOAP header snoop failed: " + se);
76: }
77: }
78:
79: private String getDirection(SOAPMessageContext context) {
80: Boolean outbound = (Boolean) context
81: .get(MESSAGE_OUTBOUND_PROPERTY);
82: return outbound != null && outbound.booleanValue() ? "Outbound"
83: : "Inbound";
84: }
85:
86: private String getText(SOAPHeaderElement headerElement) {
87: String text = " : ";
88: Iterator children = headerElement.getChildElements();
89: if (children.hasNext()) {
90: text += ((Node) children.next()).getValue();
91: }
92: return text;
93: }
94:
95: }
|