001: package demo.ws_rm.common;
002:
003: import java.util.Iterator;
004:
005: import javax.xml.soap.Name;
006: import javax.xml.soap.Node;
007: import javax.xml.soap.SOAPException;
008: import javax.xml.soap.SOAPHeader;
009: import javax.xml.soap.SOAPHeaderElement;
010: import javax.xml.ws.handler.soap.SOAPMessageContext;
011:
012: import org.w3c.dom.NamedNodeMap;
013:
014: /**
015: * Snoops SOAP headers.
016: */
017: public class HeaderSnooper extends HandlerBase {
018:
019: public boolean handleMessage(SOAPMessageContext context) {
020: snoop(context);
021: return true;
022: }
023:
024: public boolean handleFault(SOAPMessageContext context) {
025: snoop(context);
026: return true;
027: }
028:
029: /**
030: * Snoops WS-* headers in the current message.
031: * Synchronized to avoid the output for asynchrously retransmitted
032: * messages being interleaved.
033: */
034: private synchronized void snoop(SOAPMessageContext context) {
035: try {
036: SOAPHeader header = context.getMessage().getSOAPPart()
037: .getEnvelope().getHeader();
038: if (header != null) {
039: System.out.println(getMessageSummary(context));
040: displayWSHeaders(header, WSA_NAMESPACE_URI,
041: "WS-Addressing");
042: displayWSHeaders(header, WSRM_NAMESPACE_URI, "WS-RM");
043: }
044: } catch (SOAPException se) {
045: System.out.println("SOAP header snoop failed: " + se);
046: }
047: }
048:
049: /**
050: * @return a text summary of message type (out-of-band, application level
051: * etc.)
052: */
053: protected String getMessageSummary(SOAPMessageContext context) {
054: String action = getAction(context);
055: return getDirection(context)
056: + " Headers "
057: + (action != null ? action
058: .startsWith(WSRM_NAMESPACE_URI) ? "[out-of-band RM protocol message]"
059: : "[application-level message]"
060: : "[partial response]");
061: }
062:
063: /**
064: * @return a String specifying the direction of the current message
065: */
066: private String getDirection(SOAPMessageContext context) {
067: return isOutbound(context) ? "\nOutbound" : "\nInbound";
068: }
069:
070: /**
071: * Display WS headers.
072: */
073: private void displayWSHeaders(SOAPHeader header, String uri,
074: String display) throws SOAPException {
075: System.out.println(" " + display);
076: Iterator headerElements = header.examineAllHeaderElements();
077: boolean found = false;
078: while (headerElements.hasNext()) {
079: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
080: .next();
081: Name headerName = headerElement.getElementName();
082: if (uri.equals(headerName.getURI())) {
083: found = true;
084: System.out.println(" " + headerName.getLocalName()
085: + getText(headerElement));
086: }
087: }
088: if (!found) {
089: System.out.println(" None");
090: }
091: }
092:
093: /**
094: * @return a text summary of header element content
095: */
096: private String getText(SOAPHeaderElement headerElement) {
097: String text = " : ";
098: Iterator children = headerElement.getChildElements();
099: while (children.hasNext()) {
100: Node n = (Node) children.next();
101: text += n.getLocalName() != null ? n.getLocalName() + "="
102: : "";
103: text += getValue(n) + " ";
104: }
105: return text;
106: }
107:
108: /**
109: * @return either the element value or a list of attribute values
110: */
111: private String getValue(Node node) {
112: String value = "";
113: if (node.getValue() != null) {
114: value = node.getValue();
115: } else {
116: NamedNodeMap attributes = node.getAttributes();
117: if (attributes != null) {
118: for (int i = 0; i < attributes.getLength(); i++) {
119: org.w3c.dom.Node attr = attributes.item(i);
120: value += attr.getTextContent();
121: if (i + 1 != attributes.getLength()) {
122: value += ",";
123: }
124: }
125: }
126: }
127: return value;
128: }
129: }
|