1 54 package extensions.wsrm; 55 56 import org.apache.axis.handlers.BasicHandler; 57 import org.apache.axis.MessageContext; 58 import org.apache.axis.AxisFault; 59 import org.apache.axis.Message; 60 import org.apache.axis.message.SOAPHeaderElement; 61 import org.apache.axis.message.MessageElement; 62 import org.apache.axis.message.SOAPEnvelope; 63 import org.apache.axis.message.PrefixedQName; 64 65 import javax.xml.soap.SOAPException ; 66 import java.util.Iterator ; 67 import java.util.Map ; 68 import java.util.HashMap ; 69 import java.util.Date ; 70 71 public class ReliableMessagingHandler extends BasicHandler implements RMConstants { 72 static Map sequences = new HashMap (); 73 74 public ReliableMessagingHandler() { 75 } 76 77 86 public void invoke(MessageContext msgContext) throws AxisFault { 87 SOAPEnvelope req = msgContext.getRequestMessage().getSOAPEnvelope(); 88 SOAPHeaderElement header = req.getHeaderByName(NS_URI_WSRM, "Sequence"); 89 if (header == null) 90 return; 91 92 Iterator i = header.getChildElements(new PrefixedQName(NS_URI_WSU, "Identifier", null)); 93 if (!i.hasNext()) { 94 throw new AxisFault("WSRM.Fault", "Missing identifier in Sequence", null, null); 96 } 97 98 MessageElement el = (MessageElement)i.next(); 99 String id = el.getValue(); 100 Sequence seq = (Sequence)sequences.get(id); 101 if (seq == null) { 102 seq = new Sequence(id); 103 sequences.put(id, seq); 104 } 105 106 i = header.getChildElements(new PrefixedQName(NS_URI_WSRM, "MessageNumber", null)); 107 el = (MessageElement)i.next(); 108 109 int seqNum = Integer.parseInt(el.getValue()); 111 int nextSeq = seq.maxReceived + 1; 112 if (seqNum < nextSeq) { 113 Integer s = new Integer (seqNum); 114 if (seq.missing.contains(s)) { 115 seq.missing.remove(s); 116 } 117 } else { 118 if (seqNum > nextSeq) { 119 for (int n = nextSeq; n < seqNum; n++) { 121 seq.missing.add(new Integer (n)); 122 } 123 } 124 seq.maxReceived = seqNum; 125 } 126 127 header.setProcessed(true); 128 129 String from = null; 130 header = req.getHeaderByName(NS_URI_WSA, "From"); 131 if (header != null) { 132 i = header.getChildElements(new PrefixedQName(NS_URI_WSA, "Address", null)); 133 if (!i.hasNext()) { 134 throw new AxisFault("WSRM.NoAddress", "No <Address> element in <From> header", null, null); 135 } 136 el = (MessageElement)i.next(); 137 from = el.getValue(); 138 header.setProcessed(true); 139 } 140 141 msgContext.setProperty("wsrm.From", from); 142 143 boolean doSynchronousAcks = (URI_ANONYMOUS.equals(from)); 145 146 if (seq.endpoint == null) 147 seq.endpoint = from; 148 149 if (!doSynchronousAcks) { 150 AckTask.getSingleton().scheduleAck(seq); 151 } else { 152 Message respMsg = msgContext.getResponseMessage(); 153 if (respMsg == null) { 154 respMsg = new Message(new SOAPEnvelope()); 155 msgContext.setResponseMessage(respMsg); 156 } 157 SOAPEnvelope env = respMsg.getSOAPEnvelope(); 158 try { 159 generateAck(env, seq); 160 } catch (SOAPException e) { 161 e.printStackTrace(); } 163 } 164 165 header = req.getHeaderByName(NS_URI_WSA, "To"); 166 if (header != null) { 167 header.setProcessed(true); 168 } 169 170 header = req.getHeaderByName(NS_URI_WSA, "MessageID"); 171 if (header != null) { 172 header.setProcessed(true); 173 } 174 175 header = req.getHeaderByName(NS_URI_WSA, "Action"); 176 if (header != null) { 177 header.setProcessed(true); 178 } 179 180 } 181 182 public static void generateAck(SOAPEnvelope env, Sequence seq) throws SOAPException { 183 SOAPHeaderElement seqHeader = new SOAPHeaderElement(NS_URI_WSRM, "SequenceAcknowledgement"); 184 seqHeader.setMustUnderstand(true); 185 MessageElement el = new MessageElement(IDENTIFIER_QNAME, seq.id); 186 seqHeader.addChild(el); 187 188 Iterator i = seq.missing.iterator(); 189 int lastMissing = 1; 190 while (i.hasNext()) { 191 int id = ((Integer )i.next()).intValue(); 192 ackRange(lastMissing, id - 1, seqHeader); 193 lastMissing = id + 1; 194 } 195 ackRange(lastMissing, seq.maxReceived, seqHeader); 196 197 env.addHeader(seqHeader); 198 SOAPHeaderElement actionHeader = new SOAPHeaderElement(NS_URI_WSA, "Action", URI_ACK); 199 actionHeader.setMustUnderstand(true); 200 env.addHeader(actionHeader); 201 202 SOAPHeaderElement toHeader = new SOAPHeaderElement(NS_URI_WSA, "To", seq.endpoint); 203 env.addHeader(toHeader); 204 205 SOAPHeaderElement idHeader = new SOAPHeaderElement(NS_URI_WSA, "MessageID", generateNewMsgID()); 206 idHeader.setMustUnderstand(true); 207 env.addHeader(idHeader); 208 } 209 210 public static void ackRange(int start, int end, SOAPHeaderElement seqHeader) throws SOAPException { 211 if (start > end) return; 212 213 MessageElement rangeEl = new MessageElement(NS_URI_WSRM, "AcknowledgementRange"); 214 rangeEl.addAttribute("", "Upper", String.valueOf(end)); 215 rangeEl.addAttribute("", "Lower", String.valueOf(start)); 216 seqHeader.addChild(rangeEl); 217 } 218 219 public static synchronized String generateNewMsgID() { 220 return "urn:messageID-" + new Date ().getTime(); 221 } 222 223 public static synchronized String generateNewIdentifier() { 224 return "urn:identifier-" + new Date ().getTime(); 225 } 226 227 } 228 | Popular Tags |