1 package demo.handlers.common; 2 3 import java.util.Map ; 4 import javax.xml.bind.JAXBContext; 5 import javax.xml.bind.JAXBException; 6 import javax.xml.ws.LogicalMessage; 7 import javax.xml.ws.ProtocolException; 8 import javax.xml.ws.handler.LogicalHandler; 9 import javax.xml.ws.handler.LogicalMessageContext; 10 import javax.xml.ws.handler.MessageContext; 11 import org.objectweb.handlers.types.AddNumbers; 12 import org.objectweb.handlers.types.AddNumbersResponse; 13 14 15 16 19 public class SmallNumberHandler implements LogicalHandler<LogicalMessageContext> { 20 21 22 24 public final boolean handleMessage(LogicalMessageContext messageContext) { 25 System.out.println("LogicalMessageHandler handleMessage called"); 26 27 try { 28 boolean outbound = (Boolean )messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 29 30 if (outbound) { 31 LogicalMessage msg = messageContext.getMessage(); 34 35 JAXBContext jaxbContext = JAXBContext.newInstance(AddNumbers.class); 38 Object payload = msg.getPayload(jaxbContext); 39 40 if (payload instanceof AddNumbers) { 41 AddNumbers req = (AddNumbers)payload; 42 43 int a = req.getArg0(); 46 int b = req.getArg1(); 47 48 if (isSmall(a) && isSmall(b)) { 49 int answer = a + b; 50 51 System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer); 52 AddNumbersResponse resp = new AddNumbersResponse(); 55 resp.setReturn(answer); 56 msg.setPayload(resp, jaxbContext); 57 58 return false; 62 } 63 } 64 } 65 return true; 66 } catch (JAXBException ex) { 67 throw new ProtocolException(ex); 68 } 69 70 } 71 72 public final boolean handleFault(LogicalMessageContext messageContext) { 73 System.out.println("LogicalMessageHandler handleFault called"); 74 System.out.println(messageContext); 75 76 return true; 77 } 78 79 public void close(MessageContext ctx) { 80 System.out.println("LogicalHandler close called"); 81 } 82 83 public void init(Map config) { 84 System.out.println("LogicalHandler init called"); 85 } 86 87 public void destroy() { 88 System.out.println("LogicalHandler close called"); 89 } 90 91 private boolean isSmall(int i) { 92 return i > 0 && i <= 10; 93 } 94 } 95 | Popular Tags |