KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > util > RMMessageCreator


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
18 package org.apache.sandesha.util;
19
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.Message;
22 import org.apache.axis.MessageContext;
23 import org.apache.axis.components.uuid.UUIDGen;
24 import org.apache.axis.components.uuid.UUIDGenFactory;
25 import org.apache.axis.message.SOAPEnvelope;
26 import org.apache.axis.message.addressing.*;
27 import org.apache.axis.types.URI;
28 import org.apache.sandesha.Constants;
29 import org.apache.sandesha.RMMessageContext;
30 import org.apache.sandesha.ws.rm.*;
31
32 /**
33  * This is the message creater for Sandesha. This will create all the RM related messages
34  * with requried headers but only at he obect level. No serialization will occure.
35  */

36 public class RMMessageCreator {
37     private static final UUIDGen uuidGen = UUIDGenFactory.getUUIDGen();
38
39     public static synchronized RMMessageContext createCreateSeqMsg(RMMessageContext rmMsgCtx, byte endPoint,
40                                                                    String JavaDoc msgID, String JavaDoc offer) throws Exception JavaDoc {
41         RMMessageContext createSeqRMMsgContext = new RMMessageContext();
42         rmMsgCtx.copyContents(createSeqRMMsgContext);
43
44         RMHeaders rmHeaders = new RMHeaders();
45
46         CreateSequence createSeq = new CreateSequence();
47         AcksTo acksTo = getAcksTo(rmMsgCtx, endPoint);
48         createSeq.setAcksTo(acksTo);
49
50         if (offer != null) {
51             SequenceOffer seqOffer = new SequenceOffer();
52             Identifier id = new Identifier();
53             id.setIdentifier(offer);
54
55             seqOffer.setIdentifier(id);
56             createSeq.setOffer(seqOffer);
57         }
58
59         rmHeaders.setCreateSequence(createSeq);
60         createSeqRMMsgContext.setRMHeaders(rmHeaders);
61
62         AddressingHeaders csAddrHeaders = getAddressingHeaedersForCreateSequenceRequest(rmMsgCtx,
63                 endPoint, msgID);
64
65         csAddrHeaders.setAction(new Action(new URI(Constants.WSRM.ACTION_CREATE_SEQUENCE)));
66         createSeqRMMsgContext.setAddressingHeaders(csAddrHeaders);
67
68         createSeqRMMsgContext.setMessageID(msgID);
69         createSeqRMMsgContext.addToMsgIdList(msgID);
70         createSeqRMMsgContext.setSync(rmMsgCtx.getSync());
71         createSeqRMMsgContext.setMessageType(Constants.MSG_TYPE_CREATE_SEQUENCE_REQUEST);
72
73         String JavaDoc toAddress = rmMsgCtx.getOutGoingAddress();
74
75         createSeqRMMsgContext.setOutGoingAddress(toAddress);
76         MessageContext msgContext = rmMsgCtx.getMsgContext();
77         MessageContext createSeqMsgContext = new MessageContext(msgContext.getAxisEngine());
78         RMMessageContext.copyMessageContext(msgContext, createSeqMsgContext);
79         createSeqRMMsgContext.setMsgContext(createSeqMsgContext);
80
81         return createSeqRMMsgContext;
82     }
83
84     private static synchronized AddressingHeaders getAddressingHeaedersForCreateSequenceRequest(RMMessageContext rmMsgCtx, byte endPoint, String JavaDoc msgID) throws Exception JavaDoc {
85         AddressingHeaders csAddrHeaders = new AddressingHeaders();
86         csAddrHeaders.setMessageID(new MessageID(new URI(msgID)));
87         if (endPoint == Constants.SERVER) {
88             AddressingHeaders ah = rmMsgCtx.getAddressingHeaders();
89             csAddrHeaders.setFrom(new EndpointReference(ah.getTo().toString()));
90             csAddrHeaders.setReplyTo(new EndpointReference(ah.getTo().toString()));
91             csAddrHeaders.setFaultTo(new EndpointReference(ah.getTo().toString()));
92             csAddrHeaders.setTo(ah.getReplyTo().getAddress());
93         } else {
94             csAddrHeaders.setTo(new To(rmMsgCtx.getTo()));
95             if (rmMsgCtx.getSync()) {
96                 setSyncAddressingHeaders(csAddrHeaders);
97                 csAddrHeaders.setReplyTo(new EndpointReference(Constants.WSA.NS_ADDRESSING_ANONYMOUS));
98
99             } else {
100                 String JavaDoc sourceURL = rmMsgCtx.getSourceURL();
101                 if (rmMsgCtx.getFrom() != null)
102                     csAddrHeaders.setFrom(new EndpointReference(rmMsgCtx.getFrom()));
103                 else
104                     csAddrHeaders.setFrom(new EndpointReference(sourceURL));
105                 if (rmMsgCtx.getReplyTo() != null)
106                     csAddrHeaders.setReplyTo(new EndpointReference(rmMsgCtx.getReplyTo()));
107                 else
108                     csAddrHeaders.setReplyTo(new EndpointReference(sourceURL));
109                 if (rmMsgCtx.getFaultTo() != null)
110                     csAddrHeaders.setFaultTo(new EndpointReference(rmMsgCtx.getFaultTo()));
111             }
112         }
113         return csAddrHeaders;
114     }
115
116     private static synchronized void setSyncAddressingHeaders(AddressingHeaders csAddrHeaders)
117             throws URI.MalformedURIException {
118         csAddrHeaders.setFrom(new EndpointReference(Constants.WSA.NS_ADDRESSING_ANONYMOUS));
119         csAddrHeaders.setFaultTo(new EndpointReference(Constants.WSA.NS_ADDRESSING_ANONYMOUS));
120
121     }
122
123     private static synchronized void setAsyncAddressingHeaders(RMMessageContext rmMsgCtx,
124                                                                AddressingHeaders csAddrHeaders)
125             throws URI.MalformedURIException {
126         String JavaDoc sourceURL = rmMsgCtx.getSourceURL();
127         if (rmMsgCtx.getFrom() != null)
128             csAddrHeaders.setFrom(new EndpointReference(rmMsgCtx.getFrom()));
129         else
130             csAddrHeaders.setFrom(new EndpointReference(sourceURL));
131
132         if (rmMsgCtx.getReplyTo() != null)
133             csAddrHeaders.setReplyTo(new EndpointReference(rmMsgCtx.getReplyTo()));
134
135         if (rmMsgCtx.getFaultTo() != null)
136             csAddrHeaders.setFaultTo(new EndpointReference(rmMsgCtx.getFaultTo()));
137     }
138
139
140     private static synchronized AcksTo getAcksTo(RMMessageContext rmMsgCtx, byte endPoint) throws Exception JavaDoc {
141         AcksTo acksTo = null;
142         if (endPoint == Constants.CLIENT) {
143             if (rmMsgCtx.getSync()) {
144                 acksTo = new AcksTo(new Address(new URI(Constants.WSA.NS_ADDRESSING_ANONYMOUS)));
145             } else {
146                 if (rmMsgCtx.getAcksTo() != null)
147                     acksTo = new AcksTo(new Address(new URI(rmMsgCtx.getAcksTo())));
148                 // else if (rmMsgCtx.getReplyTo() != null)
149
// acksTo = new AcksTo(new Address(new URI(rmMsgCtx.getReplyTo())));
150
else if (rmMsgCtx.getSourceURL() != null)
151                     acksTo = new AcksTo(new Address(new URI(rmMsgCtx.getSourceURL())));
152             }
153         } else if (endPoint == Constants.SERVER) {
154             AddressingHeaders addrHeaders = rmMsgCtx.getAddressingHeaders();
155
156             To incommingTo = addrHeaders.getTo();
157             acksTo = new AcksTo(new Address(incommingTo.toString()));
158         }
159         return acksTo;
160     }
161
162     public static synchronized RMMessageContext createCreateSeqResponseMsg(RMMessageContext rmMsgCtx)
163             throws Exception JavaDoc {
164         return new RMMessageContext();
165     }
166
167     public static synchronized RMMessageContext createTerminateSeqMsg(RMMessageContext rmMsgCtx, byte endPoint)
168             throws Exception JavaDoc {
169
170         RMMessageContext terSeqRMMsgContext = new RMMessageContext();
171         MessageContext terSeqMsgContext = new MessageContext(rmMsgCtx.getMsgContext().getAxisEngine());
172         terSeqRMMsgContext.setSequenceID(rmMsgCtx.getSequenceID());
173
174         AddressingHeaders addHeaders = getAddressingHeadersForTerminateSequence(rmMsgCtx, endPoint);
175         SOAPEnvelope soe = new SOAPEnvelope();
176         addHeaders.toEnvelope(soe);
177         AddressingHeaders terSqAddrHeaders = new AddressingHeaders(soe);
178         terSqAddrHeaders.setAction(new Action(new URI(Constants.WSRM.ACTION_TERMINATE_SEQUENCE)));
179
180         terSeqRMMsgContext.setAddressingHeaders(terSqAddrHeaders);
181         terSeqRMMsgContext.setOutGoingAddress(rmMsgCtx.getOutGoingAddress());
182         terSeqRMMsgContext.setMsgContext(terSeqMsgContext);
183         terSeqRMMsgContext.setMessageType(Constants.MSG_TYPE_TERMINATE_SEQUENCE);
184         return terSeqRMMsgContext;
185     }
186
187     private static synchronized AddressingHeaders getAddressingHeadersForTerminateSequence(RMMessageContext rmMsgCtx, byte endPoint) throws Exception JavaDoc {
188         AddressingHeaders csAddrHeaders = new AddressingHeaders();
189         if (endPoint == Constants.SERVER) {
190             AddressingHeaders ah = rmMsgCtx.getAddressingHeaders();
191             csAddrHeaders.setTo(ah.getReplyTo().getAddress());
192             csAddrHeaders.setFrom(new EndpointReference(ah.getTo().toString()));
193         } else {
194             csAddrHeaders.setTo(new To(rmMsgCtx.getTo()));
195             if (rmMsgCtx.getSync()) {
196                 setSyncAddressingHeaders(csAddrHeaders);
197             } else {
198                 setAsyncAddressingHeaders(rmMsgCtx, csAddrHeaders);
199
200             }
201         }
202         setAddressingHeadersSetToMsgCtx(rmMsgCtx, csAddrHeaders);
203         return csAddrHeaders;
204     }
205
206     public static synchronized RMMessageContext createAcknowledgementMsg(RMMessageContext rmMessageContext)
207             throws Exception JavaDoc {
208         return new RMMessageContext();
209     }
210
211     public static synchronized RMMessageContext createServiceResponseMessage(RMMessageContext rmMsgCtx)
212             throws Exception JavaDoc {
213         return new RMMessageContext();
214     }
215
216     public static synchronized RMMessageContext createServiceRequestMessage(RMMessageContext rmMsgCtx)
217             throws Exception JavaDoc {
218         AddressingHeaders addrHeaders = getAddressingHeaedersForServiceRequest(rmMsgCtx);
219         if (rmMsgCtx.getAction() != null)
220             addrHeaders.setAction(new Action(new URI(rmMsgCtx.getAction())));
221         rmMsgCtx.setAddressingHeaders(addrHeaders);
222         rmMsgCtx.setMessageType(Constants.MSG_TYPE_SERVICE_REQUEST);
223         rmMsgCtx.setMessageID(Constants.UUID + uuidGen.nextUUID());
224         return rmMsgCtx;
225
226     }
227
228     private static synchronized AddressingHeaders getAddressingHeaedersForServiceRequest(RMMessageContext rmMsgCtx) throws Exception JavaDoc {
229         AddressingHeaders csAddrHeaders = new AddressingHeaders();
230         csAddrHeaders.setTo(new To(rmMsgCtx.getTo()));
231         if (rmMsgCtx.getSync()) {
232             setSyncAddressingHeaders(csAddrHeaders);
233         } else {
234             setAsyncAddressingHeaders(rmMsgCtx, csAddrHeaders);
235             if (rmMsgCtx.isHasResponse() && csAddrHeaders.getReplyTo() == null)
236                 csAddrHeaders.setReplyTo(new EndpointReference(rmMsgCtx.getSourceURL()));
237         }
238
239         setAddressingHeadersSetToMsgCtx(rmMsgCtx, csAddrHeaders);
240
241         return csAddrHeaders;
242     }
243
244     private static synchronized void setAddressingHeadersSetToMsgCtx(RMMessageContext rmMsgCtx, AddressingHeaders addrHeaders) throws URI.MalformedURIException {
245         if (addrHeaders.getFaultTo() == null && rmMsgCtx.getFaultTo() != null) {
246             addrHeaders.setFaultTo(new EndpointReference(new Address(rmMsgCtx.getFaultTo())));
247         }
248         if (addrHeaders.getFrom() == null && rmMsgCtx.getFrom() != null) {
249             addrHeaders.setFrom((new EndpointReference(new Address(rmMsgCtx.getFrom()))));
250         }
251         if (addrHeaders.getReplyTo() == null && rmMsgCtx.getReplyTo() != null) {
252             addrHeaders.setReplyTo(new EndpointReference(new Address(rmMsgCtx.getReplyTo())));
253         }
254     }
255
256     public static synchronized MessageContext cloneMsgContext(MessageContext msgContext) throws AxisFault {
257         MessageContext clone = new MessageContext(msgContext.getAxisEngine());
258         String JavaDoc str = msgContext.getRequestMessage().getSOAPPartAsString();
259         Message msg = new Message(str);
260         clone.setRequestMessage(msg);
261         RMMessageContext.copyMessageContext(msgContext, clone);
262         return clone;
263     }
264
265 }
Popular Tags