1 package org.objectweb.celtix.systest.handlers; 2 3 4 import java.util.Map ; 5 import java.util.StringTokenizer ; 6 7 import javax.xml.bind.JAXBContext; 8 import javax.xml.bind.JAXBException; 9 import javax.xml.namespace.QName ; 10 import javax.xml.ws.LogicalMessage; 11 import javax.xml.ws.ProtocolException; 12 import javax.xml.ws.handler.LogicalHandler; 13 import javax.xml.ws.handler.LogicalMessageContext; 14 import javax.xml.ws.handler.MessageContext; 15 16 import org.objectweb.handler_test.types.PingResponse; 17 import org.objectweb.handler_test.types.PingWithArgs; 18 import org.objectweb.hello_world_soap_http.types.GreetMe; 19 20 21 public class TestHandler<T extends LogicalMessageContext> 22 extends TestHandlerBase implements LogicalHandler<T> { 23 24 private final JAXBContext jaxbCtx; 25 26 public TestHandler() { 27 this(true); 28 } 29 30 public TestHandler(boolean serverSide) { 31 super(serverSide); 32 33 try { 34 jaxbCtx = JAXBContext.newInstance(GreetMe.class.getPackage().getName()); 35 } catch (JAXBException e) { 36 throw new RuntimeException (e); 37 } 38 39 } 40 41 public String getHandlerId() { 42 return "handler" + getId(); 43 } 44 45 public boolean handleMessage(T ctx) { 46 methodCalled("handleMessage"); 47 printHandlerInfo("handleMessage", isOutbound(ctx)); 48 49 50 boolean outbound = (Boolean )ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 51 boolean ret = handleMessageRet; 52 53 if (!isServerSideHandler()) { 54 return true; 55 } 56 57 58 QName operationName = (QName )ctx.get(MessageContext.WSDL_OPERATION); 59 assert operationName != null : "unable to get operation name from " + ctx; 60 61 if ("ping".equals(operationName.getLocalPart())) { 62 ret = handlePingMessage(outbound, ctx); 63 } else if ("pingWithArgs".equals(operationName.getLocalPart())) { 64 ret = handlePingWithArgsMessage(outbound, ctx); 65 } 66 return ret; 67 } 68 69 70 private boolean handlePingWithArgsMessage(boolean outbound, T ctx) { 71 72 73 LogicalMessage msg = ctx.getMessage(); 74 Object payload = msg.getPayload(jaxbCtx); 75 addHandlerId(ctx.getMessage(), ctx, outbound); 76 77 boolean ret = true; 78 if (payload instanceof PingWithArgs) { 79 String arg = ((PingWithArgs)payload).getHandlersCommand(); 80 81 StringTokenizer strtok = new StringTokenizer (arg, " "); 82 String hid = strtok.nextToken(); 83 String direction = strtok.nextToken(); 84 String command = strtok.nextToken(); 85 86 if (outbound) { 87 return ret; 88 } 89 90 if (getHandlerId().equals(hid) 91 && "inbound".equals(direction)) { 92 93 if ("stop".equals(command)) { 94 PingResponse resp = new PingResponse(); 95 getHandlerInfoList(ctx).add(getHandlerId()); 96 resp.getHandlersInfo().addAll(getHandlerInfoList(ctx)); 97 msg.setPayload(resp, jaxbCtx); 98 ret = false; 99 } else if ("throw".equals(command)) { 100 throwException(strtok.nextToken()); 101 } 102 } 103 } 104 return ret; 105 } 106 107 108 private void throwException(String exType) { 109 if (exType.contains("ProtocolException")) { 110 throw new ProtocolException("from server handler"); 111 } 112 } 113 114 private boolean handlePingMessage(boolean outbound, T ctx) { 115 116 LogicalMessage msg = ctx.getMessage(); 117 addHandlerId(msg, ctx, outbound); 118 return handleMessageRet; 119 } 120 121 private void addHandlerId(LogicalMessage msg, T ctx, boolean outbound) { 122 123 Object obj = msg.getPayload(jaxbCtx); 124 if (obj instanceof PingResponse) { 125 PingResponse origResp = (PingResponse)obj; 126 PingResponse newResp = new PingResponse(); 127 newResp.getHandlersInfo().addAll(origResp.getHandlersInfo()); 128 newResp.getHandlersInfo().add(getHandlerId()); 129 msg.setPayload(newResp, jaxbCtx); 130 } else if (!outbound && obj == null) { 131 getHandlerInfoList(ctx).add(getHandlerId()); 132 } 133 } 134 135 136 public boolean handleFault(T ctx) { 137 methodCalled("handleFault"); 138 printHandlerInfo("handleFault", isOutbound(ctx)); 139 return true; 141 } 142 143 public void close(MessageContext arg0) { 144 methodCalled("close"); 145 } 146 147 public void init(Map arg0) { 148 methodCalled("init"); 149 } 150 151 public void destroy() { 152 methodCalled("destroy"); 153 } 154 155 public String toString() { 156 return getHandlerId(); 157 } 158 } 159 | Popular Tags |