KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > systest > ws > rm > MessageLossSimulator


1 package org.objectweb.celtix.systest.ws.rm;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import javax.xml.namespace.QName JavaDoc;
8 import javax.xml.soap.Name JavaDoc;
9 import javax.xml.soap.Node JavaDoc;
10 import javax.xml.soap.SOAPException JavaDoc;
11 import javax.xml.soap.SOAPHeader JavaDoc;
12 import javax.xml.soap.SOAPHeaderElement JavaDoc;
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 /**
23  * Discards a protion of inbound application-level messages to simulate
24  * message loss. Note that out-of-band WS-RM protocol messages are always
25  * left intact.
26  */

27 public class MessageLossSimulator implements SOAPHandler<SOAPMessageContext> {
28     protected static final String JavaDoc WSA_NAMESPACE_URI =
29         "http://schemas.xmlsoap.org/ws/2004/08/addressing";
30     protected static final String JavaDoc WSA_ACTION = "Action";
31     protected static final String JavaDoc WSRM_NAMESPACE_URI =
32         "http://schemas.xmlsoap.org/ws/2005/02/rm";
33     
34     /**
35      * Discard every second message
36      */

37     private static final int LOSS_FACTOR = 2;
38     private int inboundMessageCount;
39
40     public void init(Map JavaDoc<String JavaDoc, Object JavaDoc> map) {
41     }
42
43     public Set JavaDoc<QName JavaDoc> 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     /**
64      * @return true if the current message is outbound
65      */

66     protected boolean isOutbound(SOAPMessageContext context) {
67         Boolean JavaDoc outbound = (Boolean JavaDoc)context.get(MESSAGE_OUTBOUND_PROPERTY);
68         return outbound != null && outbound.booleanValue();
69     }
70
71     /**
72      * @return the WS-A Action header
73      */

74     protected String JavaDoc getAction(SOAPMessageContext context) {
75         String JavaDoc action = null;
76         try {
77             SOAPHeader JavaDoc header =
78                 context.getMessage().getSOAPPart().getEnvelope().getHeader();
79             Iterator JavaDoc headerElements = header.examineAllHeaderElements();
80             while (headerElements.hasNext()) {
81                 SOAPHeaderElement JavaDoc headerElement =
82                     (SOAPHeaderElement JavaDoc)headerElements.next();
83                 Name JavaDoc headerName = headerElement.getElementName();
84                 if (WSA_NAMESPACE_URI.equals(headerName.getURI())
85                     && WSA_ACTION.equals(headerName.getLocalName())) {
86                     Iterator JavaDoc children = headerElement.getChildElements();
87                     if (children.hasNext()) {
88                         action = ((Node JavaDoc)children.next()).getValue();
89                     }
90                 }
91             }
92         } catch (SOAPException JavaDoc e) {
93             System.out.println("*** failed to determine WS-A Action: " + e);
94         }
95         return action;
96     }
97
98
99     /**
100      * @return true if the current message should not be discarded
101      */

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     /**
115      * @return true if this is a WS-RM out-of-band protocol message
116      */

117     protected boolean isRMOutOfBand(SOAPMessageContext context) {
118         String JavaDoc action = getAction(context);
119         return action != null && action.startsWith(WSRM_NAMESPACE_URI);
120     }
121
122     /**
123      * Discard any WS-* headers from the message
124      */

125     private void discardWSHeaders(SOAPMessageContext context) {
126         try {
127             SOAPHeader JavaDoc header =
128                 context.getMessage().getSOAPPart().getEnvelope().getHeader();
129             Iterator JavaDoc headerElements = header.examineAllHeaderElements();
130             while (headerElements.hasNext()) {
131                 SOAPHeaderElement JavaDoc headerElement =
132                     (SOAPHeaderElement JavaDoc)headerElements.next();
133                 Name JavaDoc 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 JavaDoc e) {
140             System.out.println("*** discard WS headers failed: " + e);
141         }
142     }
143     
144     
145     /**
146      * Discard the body from the message to avoid assertion failure when
147      * unmarshaling partial response (occuring when system tests are run in
148      * fork mode 'none')
149      */

150     private void discardBody(SOAPMessageContext context) {
151         try {
152             context.getMessage().getSOAPBody().removeContents();
153         } catch (SOAPException JavaDoc e) {
154             System.out.println("*** discard body failed: " + e);
155         }
156     }
157 }
158
Popular Tags