KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > ws > rm > Sequence


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.ws.rm;
19
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.message.MessageElement;
22 import org.apache.axis.message.SOAPEnvelope;
23 import org.apache.axis.message.SOAPHeaderElement;
24 import org.apache.sandesha.Constants;
25
26 import javax.xml.soap.Name JavaDoc;
27 import javax.xml.soap.SOAPException JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * class Sequence
32  *
33  * @author Amila Navarathna
34  * @author Jaliya Ekanayaka
35  * @author Sudar Nimalan
36  */

37 public class Sequence extends MessageElement implements IRmElement {
38
39     /**
40      * Field seqElement
41      */

42     private MessageElement seqElement;
43
44     /**
45      * Field identifier
46      */

47     private Identifier identifier;
48
49     /**
50      * Field messageNumber
51      */

52     private MessageNumber messageNumber;
53
54     /**
55      * Field lastMessage
56      */

57     private LastMessage lastMessage;
58
59     /**
60      * Constructor Sequence
61      */

62     public Sequence() {
63         seqElement = new MessageElement();
64         seqElement.setName(Constants.WSRM.NS_PREFIX_RM + Constants.COLON + Constants.WSRM.SEQUENCE);
65     }
66
67     /**
68      * Method getSoapElement
69      *
70      * @return MessageElement
71      * @throws SOAPException
72      */

73     public MessageElement getSoapElement() throws SOAPException JavaDoc {
74
75         seqElement.addChildElement(identifier.getSoapElement());
76         seqElement.addChildElement(messageNumber.getSoapElement());
77         seqElement.addChildElement(lastMessage.getSoapElement());
78
79         return seqElement;
80     }
81
82     /**
83      * Method toSoapEnvelop
84      *
85      * @param envelope
86      * @return SOAPEnvelope
87      * @throws Exception
88      */

89     public SOAPEnvelope toSoapEnvelop(SOAPEnvelope envelope) throws Exception JavaDoc {
90
91         SOAPEnvelope env = envelope;
92
93         if (env.getHeader() == null) {
94             env.addHeader();
95         }
96         removeHeaders(envelope);
97
98         Name JavaDoc name = env.createName("", Constants.WSRM.NS_PREFIX_RM, Constants.WSRM.NS_URI_RM);
99         SOAPHeaderElement headerElement = (SOAPHeaderElement) env.getHeader().addHeaderElement(name);
100
101         headerElement.setActor(null);
102         headerElement.setName(Constants.WSRM.SEQUENCE);
103         headerElement.setMustUnderstand(true);
104
105
106         if (identifier != null) {
107             identifier.toSOAPEnvelope(headerElement);
108         }
109
110         if (messageNumber != null) {
111             messageNumber.toSOAPEnvelope(headerElement);
112         }
113
114         if (lastMessage != null) {
115             lastMessage.toSOAPEnvelope(headerElement);
116         }
117
118         return env;
119     }
120
121     /**
122      * Method fromSOAPEnveploe
123      *
124      * @param headerElement
125      * @return Sequence
126      */

127     public Sequence fromSOAPEnveploe(SOAPHeaderElement headerElement) throws AxisFault {
128
129         Iterator JavaDoc iterator = headerElement.getChildElements();
130         MessageElement childElement;
131
132         while (iterator.hasNext()) {
133             childElement = (MessageElement) iterator.next();
134
135             if (childElement.getName().equals(Constants.WSRM.NS_PREFIX_RM + Constants.COLON + Constants.WSRM.IDENTIFIER)) {
136                 identifier = new Identifier();
137                 identifier.fromSOAPEnvelope(childElement);
138             }
139
140             if (childElement.getName().equals(Constants.WSRM.IDENTIFIER)) {
141                 identifier = new Identifier();
142                 identifier.fromSOAPEnvelope(childElement);
143             }
144
145             if (childElement.getName().equals(Constants.WSRM.NS_PREFIX_RM + Constants.COLON + Constants.WSRM.MSG_NUMBER)) {
146                 messageNumber = new MessageNumber();
147                 messageNumber.fromSOAPEnvelope(childElement);
148             }
149
150             if (childElement.getName().equals(Constants.WSRM.MSG_NUMBER)) {
151                 messageNumber = new MessageNumber();
152                 messageNumber.fromSOAPEnvelope(childElement);
153             }
154
155             if (childElement.getName().equals(Constants.WSRM.NS_PREFIX_RM + Constants.COLON + Constants.WSRM.LAST_MSG)) {
156                 lastMessage = new LastMessage();
157                 lastMessage.fromSOAPEnvelope(childElement);
158             }
159
160             if (childElement.getName().equals(Constants.WSRM.LAST_MSG)) {
161                 lastMessage = new LastMessage();
162                 lastMessage.fromSOAPEnvelope(childElement);
163             }
164         }
165
166         return this;
167     }
168
169     public void removeHeaders(SOAPEnvelope soapEnvelope) throws Exception JavaDoc {
170
171         Iterator JavaDoc iterator = soapEnvelope.getHeader().getChildElements();
172         MessageElement childElement;
173
174         while (iterator.hasNext()) {
175             childElement = (MessageElement) iterator.next();
176
177             if (Constants.WSRM.SEQUENCE.equals(childElement.getName()) &&
178                     (Constants.WSRM.NS_URI_RM.equals(childElement.getNamespaceURI()))) {
179                 childElement.detachNode();
180                 break;
181             }
182         }
183     }
184
185     /**
186      * Method addChildElement
187      *
188      * @param element
189      * @throws SOAPException
190      */

191     public void addChildElement(MessageElement element) throws SOAPException JavaDoc {
192         seqElement.addChildElement(element);
193     }
194
195     /**
196      * Method getIdentifier
197      *
198      * @return Identifier
199      */

200     public Identifier getIdentifier() {
201         return identifier;
202     }
203
204     /**
205      * Method getLastMessage
206      *
207      * @return LastMessage
208      */

209     public LastMessage getLastMessage() {
210         return lastMessage;
211     }
212
213     /**
214      * Method getMessageNumber
215      *
216      * @return MessageNumber
217      */

218     public MessageNumber getMessageNumber() {
219         return messageNumber;
220     }
221
222     /**
223      * Method setIdentifier
224      *
225      * @param identifer
226      */

227     public void setIdentifier(Identifier identifer) {
228         identifier = identifer;
229     }
230
231     /**
232      * Method setLastMessage
233      *
234      * @param message
235      */

236     public void setLastMessage(LastMessage message) {
237         lastMessage = message;
238     }
239
240     /**
241      * Method setMessageNumber
242      *
243      * @param number
244      */

245     public void setMessageNumber(MessageNumber number) {
246         messageNumber = number;
247     }
248 }
Popular Tags