KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > EnvelopeCreator


1 /*
2 * Copyright 1999-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */

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 JavaDoc;
30 import javax.xml.soap.SOAPException JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * This class creates the SOAPEnvelopes using the RMMessageContexts passed-in to various
35  * methods. All the methods follow a simple process of creating SOAPEnvelopes using the
36  * WS-Addressing and, WS-RM headers and Body Elements.
37  *
38  * @author Jaliya Ekanayake
39  * @author Chamikara Jayalath
40  */

41 public class EnvelopeCreator {
42
43
44     public static SOAPEnvelope createCreateSequenceResponseEnvelope(String JavaDoc seqId,
45                                                                     RMMessageContext rmMessageContext,
46                                                                     boolean hasOffer,
47                                                                     boolean offerAccepted)
48             throws Exception JavaDoc {
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 JavaDoc 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         //RelatesTo
71
MessageID incommingMessageId = addressingHeaders.getMessageID();
72         outGoingAddressingHaders.addRelatesTo(incommingMessageId.toString(),
73                 new QName JavaDoc(org.apache.axis.message.addressing.Constants.NS_PREFIX_ADDRESSING,
74                         org.apache.axis.message.addressing.Constants.NS_URI_ADDRESSING_DEFAULT));
75
76         //SettingTo
77
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 JavaDoc {
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 JavaDoc {
119
120         SOAPEnvelope soapEnv = new SOAPEnvelope();
121         addNamespaceDeclarations(soapEnv);
122         return soapEnv;
123     }
124
125     public static void addNamespaceDeclarations(SOAPEnvelope soapEnv) throws Exception JavaDoc {
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 JavaDoc toAddress,
135                                                              List JavaDoc ackRangeList)
136             throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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         //Set the messageID
191
MessageID messageId = new MessageID(new URI(getNextUUID()));
192         outGoingAddressingHaders.setMessageID(messageId);
193
194         //Set the <wsa:From> address from the incoming <wsa:To>
195
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 JavaDoc("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 JavaDoc {
241         SOAPEnvelope requestEnvelope;
242
243         String JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc getNextUUID() {
334         return Constants.UUID + UUIDGenFactory.getUUIDGen().nextUUID();
335     }
336
337 }
Popular Tags