1 package org.objectweb.celtix.systest.handlers; 2 3 4 import java.util.Map ; 5 import java.util.Set ; 6 import java.util.StringTokenizer ; 7 8 import javax.xml.namespace.QName ; 9 import javax.xml.soap.SOAPBody ; 10 import javax.xml.soap.SOAPMessage ; 11 import javax.xml.ws.handler.MessageContext; 12 import javax.xml.ws.handler.soap.SOAPHandler; 13 import javax.xml.ws.handler.soap.SOAPMessageContext; 14 15 import org.w3c.dom.DOMException ; 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.Node ; 18 19 28 public class TestSOAPHandler<T extends SOAPMessageContext> extends TestHandlerBase 29 implements SOAPHandler<T> { 30 31 public TestSOAPHandler() { 32 this(true); 33 } 34 35 public TestSOAPHandler(boolean serverSide) { 36 super(serverSide); 37 } 38 39 41 public final Set <QName > getHeaders() { 42 return null; 43 } 44 45 public String getHandlerId() { 46 return "soapHandler" + getId(); 47 } 48 49 public final boolean handleMessage(T ctx) { 50 51 boolean continueProcessing = true; 52 53 try { 54 methodCalled("handleMessage"); 55 printHandlerInfo("handleMessage", isOutbound(ctx)); 56 Object b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 57 boolean outbound = (Boolean )b; 58 SOAPMessage msg = ctx.getMessage(); 59 60 if (isServerSideHandler()) { 61 if (outbound) { 62 continueProcessing = true; 63 } else { 64 continueProcessing = getReturnValue(outbound, ctx); 65 if (!continueProcessing) { 66 outbound = true; 67 } 68 } 69 70 if (outbound) { 71 try { 72 SOAPBody body = msg.getSOAPBody(); 74 Node resp = body.getFirstChild(); 75 76 if (resp.getNodeName().contains("pingResponse")) { 77 Node child = resp.getFirstChild(); 78 Document doc = resp.getOwnerDocument(); 79 Node info = doc.createElementNS(child.getNamespaceURI(), child.getLocalName()); 80 info.setPrefix("ns4"); 81 info.appendChild(doc.createTextNode(getHandlerId())); 82 resp.appendChild(info); 83 msg.saveChanges(); 84 } 85 } catch (DOMException e) { 86 e.printStackTrace(); 87 } 88 } else { 89 getHandlerInfoList(ctx).add(getHandlerId()); 90 } 91 } 92 } catch (Exception e) { 93 e.printStackTrace(); 94 } 95 return continueProcessing; 96 } 97 98 public final boolean handleFault(T ctx) { 99 methodCalled("handleFault"); 100 printHandlerInfo("handleFault", isOutbound(ctx)); 101 return true; 102 } 103 104 public final void init(final Map map) { 105 methodCalled("init"); 106 107 } 108 109 public final void destroy() { 110 methodCalled("destroy"); 111 } 112 113 public final void close(MessageContext messageContext) { 114 methodCalled("close"); 115 } 116 117 private boolean getReturnValue(boolean outbound, T ctx) { 118 119 if (outbound) { 120 return true; 121 } 122 123 boolean ret = true; 124 try { 125 SOAPMessage msg = ctx.getMessage(); 126 SOAPBody body = msg.getSOAPBody(); 127 128 if (body.getFirstChild().getFirstChild() == null) { 129 return true; 130 } 131 132 Node commandNode = body.getFirstChild().getFirstChild().getFirstChild(); 133 String arg = commandNode.getNodeValue(); 134 String namespace = body.getFirstChild().getFirstChild().getNamespaceURI(); 135 136 StringTokenizer strtok = new StringTokenizer (arg, " "); 137 String hid = strtok.nextToken(); 138 String direction = strtok.nextToken(); 139 String command = strtok.nextToken(); 140 141 if (getHandlerId().equals(hid) 142 && "inbound".equals(direction)) { 143 if ("stop".equals(command)) { 144 145 Document doc = body.getOwnerDocument(); 147 Node wrapper = doc.createElementNS(namespace, "pingResponse"); 150 wrapper.setPrefix("ns4"); 151 body.removeChild(body.getFirstChild()); 152 body.appendChild(wrapper); 153 154 for (String info : getHandlerInfoList(ctx)) { 155 if (!info.contains(getHandlerId())) { 159 Node newEl = doc.createElementNS(namespace, "HandlersInfo"); 160 newEl.setPrefix("ns4"); 161 newEl.appendChild(doc.createTextNode(info)); 162 wrapper.appendChild(newEl); 163 } 164 } 165 ret = false; 166 } else if ("throw".equals(command)) { 167 } 169 } 170 171 } catch (Exception e) { 172 e.printStackTrace(); 173 } 174 175 return ret; 176 } 177 178 179 public String toString() { 180 return getHandlerId(); 181 } 182 } 183 | Popular Tags |