1 17 package org.apache.sandesha; 18 19 import org.apache.axis.Message; 20 import org.apache.axis.components.uuid.UUIDGenFactory; 21 import org.apache.axis.message.MessageElement; 22 import org.apache.axis.message.SOAPBody; 23 import org.apache.axis.message.SOAPBodyElement; 24 import org.apache.axis.message.SOAPEnvelope; 25 import org.apache.axis.message.addressing.*; 26 import org.apache.axis.types.URI; 27 import org.apache.sandesha.ws.rm.*; 28 29 import javax.xml.namespace.QName ; 30 import javax.xml.soap.SOAPException ; 31 import java.util.List ; 32 33 41 public class EnvelopeCreator { 42 43 44 public static SOAPEnvelope createCreateSequenceResponseEnvelope(String seqId, 45 RMMessageContext rmMessageContext, 46 boolean hasOffer, 47 boolean offerAccepted) 48 throws Exception { 49 50 AddressingHeaders addressingHeaders = rmMessageContext.getAddressingHeaders(); 51 SOAPEnvelope envelope = createBasicEnvelop(); 52 53 AddressingHeaders outGoingAddressingHaders = new AddressingHeaders(envelope); 54 Action action = new Action(new URI(Constants.WSRM.ACTION_CREATE_SEQUENCE_RESPONSE)); 55 56 outGoingAddressingHaders.setAction(action); 57 58 String str = getNextUUID(); 59 60 MessageID messageId = new MessageID(new URI(str)); 61 outGoingAddressingHaders.setMessageID(messageId); 62 63 To incommingTo = addressingHeaders.getTo(); 64 URI fromAddressURI = new URI(incommingTo.toString()); 65 66 Address fromAddress = new Address(fromAddressURI); 67 From from = new From(fromAddress); 68 outGoingAddressingHaders.setFrom(from); 69 70 MessageID incommingMessageId = addressingHeaders.getMessageID(); 72 outGoingAddressingHaders.addRelatesTo(incommingMessageId.toString(), 73 new QName (org.apache.axis.message.addressing.Constants.NS_PREFIX_ADDRESSING, 74 org.apache.axis.message.addressing.Constants.NS_URI_ADDRESSING_DEFAULT)); 75 76 AttributedURI incommingAddress = null; 78 if (!rmMessageContext.getSync()) { 79 if (addressingHeaders.getReplyTo() != null) { 80 ReplyTo incommingReplyTo = (ReplyTo) addressingHeaders.getReplyTo(); 81 incommingAddress = incommingReplyTo.getAddress(); 82 } 83 84 To to = new To(new URI(incommingAddress.toString())); 85 outGoingAddressingHaders.setTo(to); 86 } 87 outGoingAddressingHaders.toEnvelope(envelope, null); 88 89 CreateSequenceResponse response = new CreateSequenceResponse(); 90 Identifier id = new Identifier(); 91 id.setIdentifier(seqId); 92 response.setIdentifier(id); 93 94 if (hasOffer && offerAccepted) { 95 Accept accept = new Accept(); 96 AcksTo acksTo = new AcksTo(); 97 acksTo.setAddress(new Address(addressingHeaders.getTo().toString())); 98 accept.setAcksTo(acksTo); 99 response.setAccept(accept); 100 } 101 102 response.toSoapEnvelop(envelope); 103 return envelope; 104 } 105 106 public static SOAPEnvelope createCreateSequenceEnvelope(RMMessageContext rmMsgCtx) 107 throws Exception { 108 109 AddressingHeaders addressingHeaders = rmMsgCtx.getAddressingHeaders(); 110 SOAPEnvelope envelope = createBasicEnvelop(); 111 addressingHeaders.toEnvelope(envelope); 112 CreateSequence crSeq = rmMsgCtx.getRMHeaders().getCreateSequence(); 113 114 crSeq.toSoapEnvelop(envelope); 115 return envelope; 116 } 117 118 public static SOAPEnvelope createBasicEnvelop() throws Exception { 119 120 SOAPEnvelope soapEnv = new SOAPEnvelope(); 121 addNamespaceDeclarations(soapEnv); 122 return soapEnv; 123 } 124 125 public static void addNamespaceDeclarations(SOAPEnvelope soapEnv) throws Exception { 126 127 soapEnv.addNamespaceDeclaration(Constants.WSRM.NS_PREFIX_RM, Constants.WSRM.NS_URI_RM); 128 soapEnv.addNamespaceDeclaration(org.apache.axis.message.addressing.Constants.NS_PREFIX_ADDRESSING, 129 org.apache.axis.message.addressing.Constants.NS_URI_ADDRESSING_DEFAULT); 130 soapEnv.addNamespaceDeclaration(Constants.WSRM.NS_PREFIX_RM, Constants.WSRM.NS_URI_RM); 131 } 132 133 public static SOAPEnvelope createAcknowledgementEnvelope(RMMessageContext rmMessageContext, 134 String toAddress, 135 List ackRangeList) 136 throws Exception { 137 138 AddressingHeaders addressingHeaders = rmMessageContext.getAddressingHeaders(); 139 SOAPEnvelope envelope = createBasicEnvelop(); 140 141 AddressingHeaders outGoingAddressingHaders = new AddressingHeaders(envelope); 142 Action action = new Action(new URI(Constants.WSRM.SEQUENCE_ACKNOWLEDGEMENT_ACTION)); 143 144 outGoingAddressingHaders.setAction(action); 145 146 MessageID messageId = new MessageID(new URI(getNextUUID())); 147 outGoingAddressingHaders.setMessageID(messageId); 148 149 To incommingTo = addressingHeaders.getTo(); 150 URI fromAddressURI = new URI(incommingTo.toString()); 151 152 Address fromAddress = new Address(fromAddressURI); 153 From from = new From(fromAddress); 154 outGoingAddressingHaders.setFrom(from); 155 156 To to = new To(new URI(toAddress)); 157 outGoingAddressingHaders.setTo(to); 158 outGoingAddressingHaders.toEnvelope(envelope, null); 159 160 SequenceAcknowledgement seqAck = new SequenceAcknowledgement(); 161 seqAck.setAckRanges(ackRangeList); 162 seqAck.setIdentifier(rmMessageContext.getRMHeaders().getSequence().getIdentifier()); 163 seqAck.toSoapEnvelop(envelope); 164 165 return envelope; 166 } 167 168 private static void setBodyForResponseEnvelope(SOAPEnvelope env, RMMessageContext rmMsgCtx) 169 throws SOAPException { 170 171 SOAPBody sb = (SOAPBody) rmMsgCtx.getMsgContext().getResponseMessage().getSOAPBody(); 172 MessageElement mBody = (MessageElement) sb.getFirstChild(); 173 174 env.removeBody(); 175 env.addBodyElement((SOAPBodyElement) mBody); 176 } 177 178 public static SOAPEnvelope createServiceResponseEnvelope(RMMessageContext rmMessageContext) 179 throws Exception { 180 AddressingHeaders addressingHeaders = rmMessageContext.getAddressingHeaders(); 181 SOAPEnvelope responseEnvelope = createBasicEnvelop(); 182 183 setBodyForResponseEnvelope(responseEnvelope, rmMessageContext); 184 185 AddressingHeaders outGoingAddressingHaders = new AddressingHeaders(responseEnvelope); 186 187 if (rmMessageContext.getOldSequenceID() != null) 188 outGoingAddressingHaders.addRelatesTo(rmMessageContext.getMessageID(), null); 189 190 MessageID messageId = new MessageID(new URI(getNextUUID())); 192 outGoingAddressingHaders.setMessageID(messageId); 193 194 To incommingTo = addressingHeaders.getTo(); 196 URI fromAddressURI = new URI(incommingTo.toString()); 197 198 Address fromAddress = new Address(fromAddressURI); 199 From from = new From(fromAddress); 200 outGoingAddressingHaders.setFrom(from); 201 202 if (addressingHeaders.getReplyTo() == null) 203 throw new Exception ("wsa:ReplyTo is required to send Responses"); 204 AttributedURI inReplyTo = addressingHeaders.getReplyTo().getAddress(); 205 To to = new To(new URI(inReplyTo.toString())); 206 outGoingAddressingHaders.setTo(to); 207 208 if (addressingHeaders.getAction() != null) { 209 Action action = new Action(new URI(addressingHeaders.getAction().toString())); 210 outGoingAddressingHaders.setAction(action); 211 } 212 213 outGoingAddressingHaders.toEnvelope(responseEnvelope, null); 214 215 Identifier seqId = new Identifier(); 216 seqId.setIdentifier(rmMessageContext.getSequenceID()); 217 Sequence seq = new Sequence(); 218 seq.setIdentifier(seqId); 219 220 MessageNumber msgNumber = new MessageNumber(); 221 msgNumber.setMessageNumber(rmMessageContext.getMsgNumber()); 222 223 seq.setMessageNumber(msgNumber); 224 if (rmMessageContext.isLastMessage()) { 225 seq.setLastMessage(new LastMessage()); 226 } 227 228 seq.toSoapEnvelop(responseEnvelope); 229 230 if (rmMessageContext.getReTransmissionCount() > 0) { 231 AckRequested ackReq = new AckRequested(); 232 ackReq.setIdentifier(seqId); 233 ackReq.toSoapEnvelop(responseEnvelope); 234 } 235 236 return responseEnvelope; 237 } 238 239 public static SOAPEnvelope createServiceRequestEnvelope(RMMessageContext rmMessageContext) 240 throws Exception { 241 SOAPEnvelope requestEnvelope; 242 243 String str = rmMessageContext.getMsgContext().getRequestMessage().getSOAPPartAsString(); 244 requestEnvelope = new Message(str).getSOAPEnvelope(); 245 246 AddressingHeaders addressingHeaders = rmMessageContext.getAddressingHeaders(); 247 RMHeaders rmHeaders = new RMHeaders(); 248 249 AddressingHeaders outGoingAddressingHaders = new AddressingHeaders(requestEnvelope); 250 251 MessageID messageId = new MessageID(new URI(rmMessageContext.getMessageID())); 252 outGoingAddressingHaders.setMessageID(messageId); 253 254 outGoingAddressingHaders.setFrom(addressingHeaders.getFrom()); 255 outGoingAddressingHaders.setTo(addressingHeaders.getTo()); 256 257 if (addressingHeaders.getReplyTo() != null) 258 outGoingAddressingHaders.setReplyTo(addressingHeaders.getReplyTo()); 259 260 if (addressingHeaders.getFaultTo() != null) 261 outGoingAddressingHaders.setFaultTo(addressingHeaders.getFaultTo()); 262 try { 263 Action action = new Action(new URI(rmMessageContext.getAction())); 264 outGoingAddressingHaders.setAction(action); 265 } catch (Exception e) { 266 267 } 268 269 if (rmMessageContext.getFaultTo() != null) 270 outGoingAddressingHaders.setFaultTo(new FaultTo(new Address(rmMessageContext.getFaultTo()))); 271 272 outGoingAddressingHaders.toEnvelope(requestEnvelope, null); 273 274 Sequence seq = new Sequence(); 275 Identifier id = new Identifier(); 276 id.setIdentifier(rmMessageContext.getSequenceID()); 277 seq.setIdentifier(id); 278 279 if (rmMessageContext.getReTransmissionCount() > 0) { 280 AckRequested ackReq = new AckRequested(); 281 ackReq.setIdentifier(id); 282 rmHeaders.setAckRequest(ackReq); 283 } 284 285 if (rmMessageContext.isLastMessage()) { 286 seq.setLastMessage(new LastMessage()); 287 } 288 289 MessageNumber msgNumber = new MessageNumber(); 290 msgNumber.setMessageNumber(rmMessageContext.getMsgNumber()); 291 seq.setMessageNumber(msgNumber); 292 293 rmHeaders.setSequence(seq); 294 rmMessageContext.setRMHeaders(rmHeaders); 295 296 rmMessageContext.getRMHeaders().toSoapEnvelop(requestEnvelope); 297 return requestEnvelope; 298 } 299 300 301 public static SOAPEnvelope createTerminatSeqMessage(RMMessageContext rmMessageContext) 302 throws Exception { 303 AddressingHeaders addressingHeaders = rmMessageContext.getAddressingHeaders(); 304 SOAPEnvelope terSeqEnv = createBasicEnvelop(); 305 306 AddressingHeaders outGoingAddressingHaders = new AddressingHeaders(terSeqEnv); 307 Identifier seqId = new Identifier(); 308 seqId.setIdentifier(rmMessageContext.getSequenceID()); 309 310 Action terSeqAction = new Action(new URI(Constants.WSRM.ACTION_TERMINATE_SEQUENCE)); 311 outGoingAddressingHaders.setAction(terSeqAction); 312 313 MessageID messageId = new MessageID(new URI(getNextUUID())); 314 outGoingAddressingHaders.setMessageID(messageId); 315 316 outGoingAddressingHaders.setFrom(addressingHeaders.getFrom()); 317 outGoingAddressingHaders.setTo(addressingHeaders.getTo()); 318 319 if (addressingHeaders.getReplyTo() != null) 320 outGoingAddressingHaders.setReplyTo(addressingHeaders.getReplyTo()); 321 if (addressingHeaders.getFaultTo() != null) 322 outGoingAddressingHaders.setFaultTo(addressingHeaders.getFaultTo()); 323 324 outGoingAddressingHaders.toEnvelope(terSeqEnv, null); 325 326 TerminateSequence terSeq = new TerminateSequence(); 327 terSeq.setIdentifier(seqId); 328 terSeq.toSoapEnvelop(terSeqEnv); 329 330 return terSeqEnv; 331 } 332 333 private static synchronized String getNextUUID() { 334 return Constants.UUID + UUIDGenFactory.getUUIDGen().nextUUID(); 335 } 336 337 } | Popular Tags |