001: package org.objectweb.celtix.systest.handlers;
002:
003: import java.util.Map;
004: import java.util.StringTokenizer;
005:
006: import javax.xml.bind.JAXBContext;
007: import javax.xml.bind.JAXBException;
008: import javax.xml.namespace.QName;
009: import javax.xml.ws.LogicalMessage;
010: import javax.xml.ws.ProtocolException;
011: import javax.xml.ws.handler.LogicalHandler;
012: import javax.xml.ws.handler.LogicalMessageContext;
013: import javax.xml.ws.handler.MessageContext;
014:
015: import org.objectweb.handler_test.types.PingResponse;
016: import org.objectweb.handler_test.types.PingWithArgs;
017: import org.objectweb.hello_world_soap_http.types.GreetMe;
018:
019: public class TestHandler<T extends LogicalMessageContext> extends
020: TestHandlerBase implements LogicalHandler<T> {
021:
022: private final JAXBContext jaxbCtx;
023:
024: public TestHandler() {
025: this (true);
026: }
027:
028: public TestHandler(boolean serverSide) {
029: super (serverSide);
030:
031: try {
032: jaxbCtx = JAXBContext.newInstance(GreetMe.class
033: .getPackage().getName());
034: } catch (JAXBException e) {
035: throw new RuntimeException(e);
036: }
037:
038: }
039:
040: public String getHandlerId() {
041: return "handler" + getId();
042: }
043:
044: public boolean handleMessage(T ctx) {
045: methodCalled("handleMessage");
046: printHandlerInfo("handleMessage", isOutbound(ctx));
047:
048: boolean outbound = (Boolean) ctx
049: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
050: boolean ret = handleMessageRet;
051:
052: if (!isServerSideHandler()) {
053: return true;
054: }
055:
056: QName operationName = (QName) ctx
057: .get(MessageContext.WSDL_OPERATION);
058: assert operationName != null : "unable to get operation name from "
059: + ctx;
060:
061: if ("ping".equals(operationName.getLocalPart())) {
062: ret = handlePingMessage(outbound, ctx);
063: } else if ("pingWithArgs".equals(operationName.getLocalPart())) {
064: ret = handlePingWithArgsMessage(outbound, ctx);
065: }
066: return ret;
067: }
068:
069: private boolean handlePingWithArgsMessage(boolean outbound, T ctx) {
070:
071: LogicalMessage msg = ctx.getMessage();
072: Object payload = msg.getPayload(jaxbCtx);
073: addHandlerId(ctx.getMessage(), ctx, outbound);
074:
075: boolean ret = true;
076: if (payload instanceof PingWithArgs) {
077: String arg = ((PingWithArgs) payload).getHandlersCommand();
078:
079: StringTokenizer strtok = new StringTokenizer(arg, " ");
080: String hid = strtok.nextToken();
081: String direction = strtok.nextToken();
082: String command = strtok.nextToken();
083:
084: if (outbound) {
085: return ret;
086: }
087:
088: if (getHandlerId().equals(hid)
089: && "inbound".equals(direction)) {
090:
091: if ("stop".equals(command)) {
092: PingResponse resp = new PingResponse();
093: getHandlerInfoList(ctx).add(getHandlerId());
094: resp.getHandlersInfo().addAll(
095: getHandlerInfoList(ctx));
096: msg.setPayload(resp, jaxbCtx);
097: ret = false;
098: } else if ("throw".equals(command)) {
099: throwException(strtok.nextToken());
100: }
101: }
102: }
103: return ret;
104: }
105:
106: private void throwException(String exType) {
107: if (exType.contains("ProtocolException")) {
108: throw new ProtocolException("from server handler");
109: }
110: }
111:
112: private boolean handlePingMessage(boolean outbound, T ctx) {
113:
114: LogicalMessage msg = ctx.getMessage();
115: addHandlerId(msg, ctx, outbound);
116: return handleMessageRet;
117: }
118:
119: private void addHandlerId(LogicalMessage msg, T ctx,
120: boolean outbound) {
121:
122: Object obj = msg.getPayload(jaxbCtx);
123: if (obj instanceof PingResponse) {
124: PingResponse origResp = (PingResponse) obj;
125: PingResponse newResp = new PingResponse();
126: newResp.getHandlersInfo()
127: .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: public boolean handleFault(T ctx) {
136: methodCalled("handleFault");
137: printHandlerInfo("handleFault", isOutbound(ctx));
138: //boolean outbound = (Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
139: return true;
140: }
141:
142: public void close(MessageContext arg0) {
143: methodCalled("close");
144: }
145:
146: public void init(Map arg0) {
147: methodCalled("init");
148: }
149:
150: public void destroy() {
151: methodCalled("destroy");
152: }
153:
154: public String toString() {
155: return getHandlerId();
156: }
157: }
|