1 package org.objectweb.celtix.systest.ws.rm; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 import java.util.Set ; 6 7 import javax.xml.namespace.QName ; 8 import javax.xml.soap.Name ; 9 import javax.xml.soap.Node ; 10 import javax.xml.soap.SOAPException ; 11 import javax.xml.soap.SOAPHeader ; 12 import javax.xml.soap.SOAPHeaderElement ; 13 import javax.xml.ws.handler.MessageContext; 14 import javax.xml.ws.handler.soap.SOAPHandler; 15 import javax.xml.ws.handler.soap.SOAPMessageContext; 16 17 import static javax.xml.ws.handler.MessageContext.MESSAGE_OUTBOUND_PROPERTY; 18 19 20 21 22 27 public class MessageLossSimulator implements SOAPHandler<SOAPMessageContext> { 28 protected static final String WSA_NAMESPACE_URI = 29 "http://schemas.xmlsoap.org/ws/2004/08/addressing"; 30 protected static final String WSA_ACTION = "Action"; 31 protected static final String WSRM_NAMESPACE_URI = 32 "http://schemas.xmlsoap.org/ws/2005/02/rm"; 33 34 37 private static final int LOSS_FACTOR = 2; 38 private int inboundMessageCount; 39 40 public void init(Map <String , Object > map) { 41 } 42 43 public Set <QName > getHeaders() { 44 return null; 45 } 46 47 48 public void close(MessageContext context) { 49 } 50 51 public void destroy() { 52 } 53 54 public boolean handleMessage(SOAPMessageContext context) { 55 System.out.println("*** MessageLoss: handling message"); 56 return continueProcessing(context); 57 } 58 59 public boolean handleFault(SOAPMessageContext context) { 60 return true; 61 } 62 63 66 protected boolean isOutbound(SOAPMessageContext context) { 67 Boolean outbound = (Boolean )context.get(MESSAGE_OUTBOUND_PROPERTY); 68 return outbound != null && outbound.booleanValue(); 69 } 70 71 74 protected String getAction(SOAPMessageContext context) { 75 String action = null; 76 try { 77 SOAPHeader header = 78 context.getMessage().getSOAPPart().getEnvelope().getHeader(); 79 Iterator headerElements = header.examineAllHeaderElements(); 80 while (headerElements.hasNext()) { 81 SOAPHeaderElement headerElement = 82 (SOAPHeaderElement )headerElements.next(); 83 Name headerName = headerElement.getElementName(); 84 if (WSA_NAMESPACE_URI.equals(headerName.getURI()) 85 && WSA_ACTION.equals(headerName.getLocalName())) { 86 Iterator children = headerElement.getChildElements(); 87 if (children.hasNext()) { 88 action = ((Node )children.next()).getValue(); 89 } 90 } 91 } 92 } catch (SOAPException e) { 93 System.out.println("*** failed to determine WS-A Action: " + e); 94 } 95 return action; 96 } 97 98 99 102 private synchronized boolean continueProcessing(SOAPMessageContext context) { 103 System.out.println("*** inboundMessageCount: " + inboundMessageCount); 104 if (!(isOutbound(context) || isRMOutOfBand(context)) 105 && ++inboundMessageCount % LOSS_FACTOR == 0 && inboundMessageCount <= 4) { 106 discardWSHeaders(context); 107 discardBody(context); 108 System.out.println("*** Discarding current inbound message ***"); 109 return false; 110 } 111 return true; 112 } 113 114 117 protected boolean isRMOutOfBand(SOAPMessageContext context) { 118 String action = getAction(context); 119 return action != null && action.startsWith(WSRM_NAMESPACE_URI); 120 } 121 122 125 private void discardWSHeaders(SOAPMessageContext context) { 126 try { 127 SOAPHeader header = 128 context.getMessage().getSOAPPart().getEnvelope().getHeader(); 129 Iterator headerElements = header.examineAllHeaderElements(); 130 while (headerElements.hasNext()) { 131 SOAPHeaderElement headerElement = 132 (SOAPHeaderElement )headerElements.next(); 133 Name headerName = headerElement.getElementName(); 134 if (WSRM_NAMESPACE_URI.equals(headerName.getURI()) 135 || WSRM_NAMESPACE_URI.equals(headerName.getURI())) { 136 headerElement.detachNode(); 137 } 138 } 139 } catch (SOAPException e) { 140 System.out.println("*** discard WS headers failed: " + e); 141 } 142 } 143 144 145 150 private void discardBody(SOAPMessageContext context) { 151 try { 152 context.getMessage().getSOAPBody().removeContents(); 153 } catch (SOAPException e) { 154 System.out.println("*** discard body failed: " + e); 155 } 156 } 157 } 158 | Popular Tags |