01: package demo.ws_rm.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: /**
19: * Base handler logic for WS-RM demo.
20: */
21: public abstract class HandlerBase implements
22: SOAPHandler<SOAPMessageContext> {
23:
24: protected static final String WSA_NAMESPACE_URI = "http://schemas.xmlsoap.org/ws/2004/08/addressing";
25: protected static final String WSA_ACTION = "Action";
26: protected static final String WSRM_NAMESPACE_URI = "http://schemas.xmlsoap.org/ws/2005/02/rm";
27:
28: public void init(Map<String, Object> map) {
29: }
30:
31: public Set<QName> getHeaders() {
32: return null;
33: }
34:
35: public void close(MessageContext context) {
36: }
37:
38: public void destroy() {
39: }
40:
41: /**
42: * @return true if the current message is outbound
43: */
44: protected boolean isOutbound(SOAPMessageContext context) {
45: Boolean outbound = (Boolean) context
46: .get(MESSAGE_OUTBOUND_PROPERTY);
47: return outbound != null && outbound.booleanValue();
48: }
49:
50: /**
51: * @return the WS-A Action header
52: */
53: protected String getAction(SOAPMessageContext context) {
54: String action = null;
55: try {
56: SOAPHeader header = context.getMessage().getSOAPPart()
57: .getEnvelope().getHeader();
58: Iterator headerElements = header.examineAllHeaderElements();
59: while (headerElements.hasNext()) {
60: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
61: .next();
62: Name headerName = headerElement.getElementName();
63: if (WSA_NAMESPACE_URI.equals(headerName.getURI())
64: && WSA_ACTION.equals(headerName.getLocalName())) {
65: Iterator children = headerElement
66: .getChildElements();
67: if (children.hasNext()) {
68: action = ((Node) children.next()).getValue();
69: }
70: }
71: }
72: } catch (SOAPException e) {
73: System.out.println("failed to determine WS-A Action: " + e);
74: }
75: return action;
76: }
77: }
|