001: package org.objectweb.celtix.systest.handlers;
002:
003: import java.util.Map;
004: import java.util.Set;
005: import java.util.StringTokenizer;
006:
007: import javax.xml.namespace.QName;
008: import javax.xml.soap.SOAPBody;
009: import javax.xml.soap.SOAPMessage;
010: import javax.xml.ws.handler.MessageContext;
011: import javax.xml.ws.handler.soap.SOAPHandler;
012: import javax.xml.ws.handler.soap.SOAPMessageContext;
013:
014: import org.w3c.dom.DOMException;
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Node;
017:
018: /**
019: * Describe class TestSOAPHandler here.
020: *
021: *
022: * Created: Fri Oct 21 13:24:05 2005
023: *
024: * @author <a href="mailto:codea@iona.com">codea</a>
025: * @version 1.0
026: */
027: public class TestSOAPHandler<T extends SOAPMessageContext> extends
028: TestHandlerBase implements SOAPHandler<T> {
029:
030: public TestSOAPHandler() {
031: this (true);
032: }
033:
034: public TestSOAPHandler(boolean serverSide) {
035: super (serverSide);
036: }
037:
038: // Implementation of javax.xml.ws.handler.soap.SOAPHandler
039:
040: public final Set<QName> getHeaders() {
041: return null;
042: }
043:
044: public String getHandlerId() {
045: return "soapHandler" + getId();
046: }
047:
048: public final boolean handleMessage(T ctx) {
049:
050: boolean continueProcessing = true;
051:
052: try {
053: methodCalled("handleMessage");
054: printHandlerInfo("handleMessage", isOutbound(ctx));
055: Object b = ctx
056: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
057: boolean outbound = (Boolean) b;
058: SOAPMessage msg = ctx.getMessage();
059:
060: if (isServerSideHandler()) {
061: if (outbound) {
062: continueProcessing = true;
063: } else {
064: continueProcessing = getReturnValue(outbound, ctx);
065: if (!continueProcessing) {
066: outbound = true;
067: }
068: }
069:
070: if (outbound) {
071: try {
072: // append handler id to SOAP response message
073: SOAPBody body = msg.getSOAPBody();
074: Node resp = body.getFirstChild();
075:
076: if (resp.getNodeName().contains("pingResponse")) {
077: Node child = resp.getFirstChild();
078: Document doc = resp.getOwnerDocument();
079: Node info = doc.createElementNS(child
080: .getNamespaceURI(), child
081: .getLocalName());
082: info.setPrefix("ns4");
083: info.appendChild(doc
084: .createTextNode(getHandlerId()));
085: resp.appendChild(info);
086: msg.saveChanges();
087: }
088: } catch (DOMException e) {
089: e.printStackTrace();
090: }
091: } else {
092: getHandlerInfoList(ctx).add(getHandlerId());
093: }
094: }
095: } catch (Exception e) {
096: e.printStackTrace();
097: }
098: return continueProcessing;
099: }
100:
101: public final boolean handleFault(T ctx) {
102: methodCalled("handleFault");
103: printHandlerInfo("handleFault", isOutbound(ctx));
104: return true;
105: }
106:
107: public final void init(final Map map) {
108: methodCalled("init");
109:
110: }
111:
112: public final void destroy() {
113: methodCalled("destroy");
114: }
115:
116: public final void close(MessageContext messageContext) {
117: methodCalled("close");
118: }
119:
120: private boolean getReturnValue(boolean outbound, T ctx) {
121:
122: if (outbound) {
123: return true;
124: }
125:
126: boolean ret = true;
127: try {
128: SOAPMessage msg = ctx.getMessage();
129: SOAPBody body = msg.getSOAPBody();
130:
131: if (body.getFirstChild().getFirstChild() == null) {
132: return true;
133: }
134:
135: Node commandNode = body.getFirstChild().getFirstChild()
136: .getFirstChild();
137: String arg = commandNode.getNodeValue();
138: String namespace = body.getFirstChild().getFirstChild()
139: .getNamespaceURI();
140:
141: StringTokenizer strtok = new StringTokenizer(arg, " ");
142: String hid = strtok.nextToken();
143: String direction = strtok.nextToken();
144: String command = strtok.nextToken();
145:
146: if (getHandlerId().equals(hid)
147: && "inbound".equals(direction)) {
148: if ("stop".equals(command)) {
149:
150: // remove the incoming request body.
151: Document doc = body.getOwnerDocument();
152: // build the SOAP response for this message
153: //
154: Node wrapper = doc.createElementNS(namespace,
155: "pingResponse");
156: wrapper.setPrefix("ns4");
157: body.removeChild(body.getFirstChild());
158: body.appendChild(wrapper);
159:
160: for (String info : getHandlerInfoList(ctx)) {
161: // copy the the previously invoked handler list into the response.
162: // Ignore this handlers information as it will be added again later.
163: //
164: if (!info.contains(getHandlerId())) {
165: Node newEl = doc.createElementNS(namespace,
166: "HandlersInfo");
167: newEl.setPrefix("ns4");
168: newEl.appendChild(doc.createTextNode(info));
169: wrapper.appendChild(newEl);
170: }
171: }
172: ret = false;
173: } else if ("throw".equals(command)) {
174: //throwException(strtok.nextToken());
175: }
176: }
177:
178: } catch (Exception e) {
179: e.printStackTrace();
180: }
181:
182: return ret;
183: }
184:
185: public String toString() {
186: return getHandlerId();
187: }
188: }
|