001: package demo.handlers.common;
002:
003: import java.util.Map;
004: import javax.xml.bind.JAXBContext;
005: import javax.xml.bind.JAXBException;
006: import javax.xml.ws.LogicalMessage;
007: import javax.xml.ws.ProtocolException;
008: import javax.xml.ws.handler.LogicalHandler;
009: import javax.xml.ws.handler.LogicalMessageContext;
010: import javax.xml.ws.handler.MessageContext;
011: import org.objectweb.handlers.types.AddNumbers;
012: import org.objectweb.handlers.types.AddNumbersResponse;
013:
014: /**
015: * handles addition of small numbers.
016: */
017: public class SmallNumberHandler implements
018: LogicalHandler<LogicalMessageContext> {
019:
020: // Implementation of javax.xml.ws.handler.Handler
021:
022: public final boolean handleMessage(
023: LogicalMessageContext messageContext) {
024: System.out
025: .println("LogicalMessageHandler handleMessage called");
026:
027: try {
028: boolean outbound = (Boolean) messageContext
029: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
030:
031: if (outbound) {
032: // get the LogicalMessage from our context
033: //
034: LogicalMessage msg = messageContext.getMessage();
035:
036: // check the payload, if its an AddNumbers request, we'll intervene
037: //
038: JAXBContext jaxbContext = JAXBContext
039: .newInstance(AddNumbers.class);
040: Object payload = msg.getPayload(jaxbContext);
041:
042: if (payload instanceof AddNumbers) {
043: AddNumbers req = (AddNumbers) payload;
044:
045: // now, if the arguments are small, let's do the calculation here
046: //
047: int a = req.getArg0();
048: int b = req.getArg1();
049:
050: if (isSmall(a) && isSmall(b)) {
051: int answer = a + b;
052:
053: System.out
054: .printf(
055: "SmallNumberHandler addNumbers(%d, %d) == %d\n",
056: a, b, answer);
057: // ok, we've done the calculation, so build the
058: // response and set it as the payload of the message
059: AddNumbersResponse resp = new AddNumbersResponse();
060: resp.setReturn(answer);
061: msg.setPayload(resp, jaxbContext);
062:
063: // finally, return false, indicating that request
064: // processing stops here and our answer will be
065: // returned to the client
066: return false;
067: }
068: }
069: }
070: return true;
071: } catch (JAXBException ex) {
072: throw new ProtocolException(ex);
073: }
074:
075: }
076:
077: public final boolean handleFault(
078: LogicalMessageContext messageContext) {
079: System.out.println("LogicalMessageHandler handleFault called");
080: System.out.println(messageContext);
081:
082: return true;
083: }
084:
085: public void close(MessageContext ctx) {
086: System.out.println("LogicalHandler close called");
087: }
088:
089: public void init(Map config) {
090: System.out.println("LogicalHandler init called");
091: }
092:
093: public void destroy() {
094: System.out.println("LogicalHandler close called");
095: }
096:
097: private boolean isSmall(int i) {
098: return i > 0 && i <= 10;
099: }
100: }
|