KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > server > msgprocessors > CreateSequenceProcessor


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.server.msgprocessors;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Message;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.components.uuid.UUIDGen;
23 import org.apache.axis.components.uuid.UUIDGenFactory;
24 import org.apache.axis.message.SOAPEnvelope;
25 import org.apache.axis.message.addressing.AddressingHeaders;
26 import org.apache.sandesha.Constants;
27 import org.apache.sandesha.EnvelopeCreator;
28 import org.apache.sandesha.IStorageManager;
29 import org.apache.sandesha.RMMessageContext;
30 import org.apache.sandesha.ws.rm.CreateSequence;
31 import org.apache.sandesha.ws.rm.Identifier;
32 import org.apache.sandesha.ws.rm.RMHeaders;
33 import org.apache.sandesha.ws.rm.SequenceOffer;
34
35 /**
36  * This is the processor for the CreateSequence.
37  *
38  * @author Jaliya Ekanayake
39  */

40 public class CreateSequenceProcessor implements IRMMessageProcessor {
41     IStorageManager storageManager;
42
43     public UUIDGen uuidGen = UUIDGenFactory.getUUIDGen();
44
45     public CreateSequenceProcessor(IStorageManager storageManager) {
46         this.storageManager = storageManager;
47     }
48
49     public boolean processMessage(RMMessageContext rmMessageContext) throws AxisFault {
50
51         AddressingHeaders addrHeaders = rmMessageContext.getAddressingHeaders();
52         RMHeaders rmHeaders = rmMessageContext.getRMHeaders();
53
54         if (rmHeaders.getSequenceAcknowledgement() != null) {
55             processForAckIfAny(rmMessageContext);
56         }
57
58         //wsrm:CreateSequenceRefused
59
if (rmHeaders.getCreateSequence() == null)
60             throw new AxisFault();
61
62         String JavaDoc seqId = getNextUUID();
63         storageManager.addRequestedSequence(seqId);
64
65
66         CreateSequence createSeq = rmMessageContext.getRMHeaders().getCreateSequence();
67         //Support AcksTo
68
if (createSeq.getAcksTo() != null) {
69             storageManager.setAcksTo(seqId, createSeq.getAcksTo().getAddress().toString());
70         }
71
72         //Support offer
73
SequenceOffer offer = createSeq.getOffer();
74         boolean offerAccepted = false;
75         boolean hasOffer = false;
76         if (offer != null) {
77             hasOffer = true;
78             offerAccepted = acceptOrRejectOffer(offer);
79             if (offerAccepted) {
80                 String JavaDoc responseSeqId = null;
81                 if (offer != null) {
82                     Identifier id = offer.getIdentifier();
83                     if (id != null)
84                         responseSeqId = id.getIdentifier();
85                 }
86                 String JavaDoc incomingSeqId = seqId;
87                 if (responseSeqId != null) {
88                     storageManager.addOutgoingSequence(incomingSeqId);
89                     storageManager.setTemporaryOutSequence(incomingSeqId, responseSeqId);
90                     storageManager.setApprovedOutSequence(responseSeqId, responseSeqId);
91                     //Now it has a approved out sequence of responseSeqId
92
}
93             }
94         }
95
96         setOutGoingAddress(rmMessageContext, addrHeaders);
97
98         SOAPEnvelope resEnvelope;
99         try {
100             resEnvelope = EnvelopeCreator.createCreateSequenceResponseEnvelope(seqId,
101                     rmMessageContext, hasOffer, offerAccepted);
102         } catch (Exception JavaDoc e) {
103             throw new AxisFault(org.apache.sandesha.Constants.FaultCodes.WSRM_SERVER_INTERNAL_ERROR);
104         }
105         rmMessageContext.setMessageType(org.apache.sandesha.Constants.MSG_TYPE_CREATE_SEQUENCE_RESPONSE);
106
107         if (rmMessageContext.getSync()) {
108             rmMessageContext.getMsgContext().setResponseMessage(new Message(resEnvelope));
109             return true;
110
111         } else {
112             MessageContext msgContext = new MessageContext(rmMessageContext.getMsgContext().getAxisEngine());
113             msgContext.setResponseMessage(new Message(resEnvelope));
114             rmMessageContext.setMsgContext(msgContext);
115
116             rmMessageContext.setSync(false);
117             storageManager.addCreateSequenceResponse(rmMessageContext);
118             return false;
119         }
120
121     }
122
123     private synchronized String JavaDoc getNextUUID() {
124         return Constants.UUID + uuidGen.nextUUID();
125     }
126
127     //TODO Implent the strategy for accepting the offer or rejecting it.
128
private boolean acceptOrRejectOffer(SequenceOffer offer) {
129         return true;
130     }
131
132     private void processForAckIfAny(RMMessageContext rmMsgCtx) throws AxisFault {
133         AcknowledgementProcessor ackProcessor = new AcknowledgementProcessor(storageManager);
134         ackProcessor.processMessage(rmMsgCtx);
135     }
136
137     private void setOutGoingAddress(RMMessageContext rmMsgCtx, AddressingHeaders addrHeaders) {
138
139         if (addrHeaders.getReplyTo() == null || addrHeaders.getReplyTo().getAddress().toString()
140                 .equals(Constants.WSA.NS_ADDRESSING_ANONYMOUS)) {
141             rmMsgCtx.setSync(true);
142         } else {
143             rmMsgCtx.setSync(false);
144             rmMsgCtx.setOutGoingAddress(addrHeaders.getReplyTo().getAddress().toString());
145         }
146
147     }
148 }
Popular Tags