1 package test.wsdl.jaxrpchandlereval; 2 3 import javax.xml.namespace.QName ; 4 import javax.xml.rpc.handler.Handler ; 5 import javax.xml.rpc.handler.HandlerInfo ; 6 import javax.xml.rpc.handler.MessageContext ; 7 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 8 import javax.xml.soap.Name ; 9 import javax.xml.soap.SOAPEnvelope ; 10 import javax.xml.soap.SOAPHeader ; 11 import javax.xml.soap.SOAPBody ; 12 import javax.xml.soap.SOAPHeaderElement ; 13 import javax.xml.soap.SOAPMessage ; 14 import javax.xml.soap.SOAPPart ; 15 import java.util.Iterator ; 16 import javax.xml.soap.*; 17 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 18 import javax.xml.rpc.soap.*; 19 import javax.xml.rpc.*; 20 21 22 24 public class ClientHandler implements Handler { 25 26 private final static String _actorURI = "myActorURI"; 27 30 public ClientHandler() { 31 System.out.println("ClientHandler:Constructor"); 32 } 33 34 37 public boolean handleRequest(MessageContext context) { 38 System.out.println("ClientHandler:handleRequest"); 39 HandlerTracker.addClientHandler("clienthandler1.handleRequest"); 40 if (context instanceof SOAPMessageContext ) { 41 try { 42 SOAPMessageContext soapMsgCtx = (SOAPMessageContext )context; 43 SOAPMessage soapMsg = soapMsgCtx.getMessage(); 44 SOAPPart sp = soapMsg.getSOAPPart(); 45 SOAPEnvelope se = sp.getEnvelope(); 46 SOAPBody sb = se.getBody(); 47 SOAPHeader sh = se.getHeader(); 48 49 Name name = se.createName("ClientHandler-handleRequest", "", ""); 50 SOAPHeaderElement hdr = sh.addHeaderElement(name); 51 hdr.addTextNode("Processed"); 52 53 String instruction = sb.toString(); 54 if (instruction.indexOf("client-return-false") >= 0) { 55 soapMsgCtx.setProperty("fault", "Returning false from ClientHandler.handleRequest"); 56 return false; 57 } 58 } catch (SOAPException ex) { 59 ex.printStackTrace(); 60 return false; 61 } 62 } 63 return true; 64 } 65 66 public SOAPMessage prepareError(SOAPMessageContext soapMsgCtx) throws Exception { 67 MessageFactory messageFactory = MessageFactory.newInstance(); 68 SOAPMessage soapMsg = messageFactory.createMessage(); 69 soapMsgCtx.setMessage(soapMsg); 70 71 String fault = (String )soapMsgCtx.getProperty("fault"); 72 if (fault != null) { 73 SOAPFault soapFault = 74 soapMsg.getSOAPPart().getEnvelope().getBody().addFault(); 75 soapFault.setFaultString(fault); 76 soapFault.setFaultCode("client.Exception"); 77 } 78 return soapMsg; 79 } 80 81 private void trackServerHandler(SOAPHeader sh) { 82 Iterator i = sh.getChildElements(); 83 while (i.hasNext()) { 84 Node n = (Node) i.next(); 85 if (n instanceof Text) { 86 continue; 87 } 88 SOAPElement e = (SOAPElement) n; 89 String local = e.getLocalName(); 90 if (local.equals("ServiceHandler2-handleFault")) { 91 HandlerTracker.addServerHandler("serverhandler2.handleFault"); 92 } else if (local.equals("ServiceHandler1-handleFault")) { 93 HandlerTracker.addServerHandler("serverhandler1.handleFault"); 94 } else if (local.equals("ServiceHandler2-handleResponse")) { 95 HandlerTracker.addServerHandler("serverhandler2.handleResponse"); 96 } else if (local.equals("ServiceHandler1-handleResponse")) { 97 HandlerTracker.addServerHandler("serverhandler1.handleResponse"); 98 } 99 } 100 } 101 102 103 106 public boolean handleResponse(MessageContext context) { 107 System.out.println("ClientHandler:handleResponse"); 108 HandlerTracker.addClientHandler("clienthandler1.handleResponse"); 109 110 if (context instanceof SOAPMessageContext ) { 111 try { 112 SOAPMessageContext soapMsgCtx = (SOAPMessageContext )context; 113 SOAPMessage soapMsg = soapMsgCtx.getMessage(); 114 if (soapMsg == null) { 115 soapMsg = prepareError(soapMsgCtx); 116 } 117 SOAPPart sp = soapMsg.getSOAPPart(); 118 SOAPEnvelope se = sp.getEnvelope(); 119 SOAPBody sb = se.getBody(); 120 SOAPHeader sh = se.getHeader(); 121 122 trackServerHandler(sh); 123 124 Name name = se.createName("ClientHandler-handleResponse", "", ""); 125 SOAPHeaderElement hdr = sh.addHeaderElement(name); 126 hdr.addTextNode("Processed"); 127 } catch (SOAPException ex) { 128 ex.printStackTrace(); 129 return false; 130 } catch (Exception ex) { 131 throw new JAXRPCException (ex); 132 } 133 } 134 return true; 135 } 136 137 140 public boolean handleFault(MessageContext context) { 141 System.out.println("ClientHandler:handleFault"); 142 HandlerTracker.addClientHandler("clienthandler1.handleFault"); 143 if (context instanceof SOAPMessageContext ) { 144 try { 145 SOAPMessageContext soapMsgCtx = (SOAPMessageContext )context; 146 SOAPMessage soapMsg = soapMsgCtx.getMessage(); 147 SOAPPart sp = soapMsg.getSOAPPart(); 148 SOAPEnvelope se = sp.getEnvelope(); 149 SOAPBody sb = se.getBody(); 150 SOAPHeader sh = se.getHeader(); 151 152 Name name = se.createName("ClientHandler-handleFault", "", ""); 153 SOAPHeaderElement hdr = sh.addHeaderElement(name); 154 hdr.addTextNode("Processed"); 155 } catch (SOAPException ex) { 156 ex.printStackTrace(); 157 return false; 158 } 159 } 160 return false; 161 } 162 163 166 public void init(HandlerInfo config) { 167 System.out.println("ClientHandler.init"); 168 169 } 170 171 174 public void destroy() { 175 System.out.println("ClientHandler.destroy"); 176 } 177 178 181 public QName [] getHeaders() { 182 System.out.println("ClientHandler.getheaders"); 183 184 return null; 185 } 186 187 } 188 189 | Popular Tags |