View Javadoc

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;
27  import javax.xml.soap.SOAPException;
28  import java.util.Iterator;
29  
30  /***
31   * class AckRequested
32   *
33   * @author Amila Navarathna
34   * @author Jaliya Ekanayaka
35   * @author Sudar Nimalan
36   */
37  public class AckRequested extends MessageElement implements IRmElement {
38  
39      /***
40       * Field ackRequested
41       */
42      private MessageElement ackRequested;
43  
44      /***
45       * Field identifier
46       */
47      private Identifier identifier;
48  
49      /***
50       * Field messageNumber
51       */
52      private MessageNumber messageNumber;
53  
54      /***
55       * Constructor AckRequested
56       */
57      public AckRequested() {
58          ackRequested = new MessageElement();
59          ackRequested.setName(Constants.WSRM.NS_PREFIX_RM + Constants.COLON + Constants.WSRM.ACK_REQUESTED);
60      }
61  
62  
63      /***
64       * Method getSoapElement
65       *
66       * @return MessageElement
67       * @throws SOAPException
68       */
69      public MessageElement getSoapElement() throws SOAPException {
70  
71          ackRequested.addChildElement(identifier.getSoapElement());
72          ackRequested.addChildElement(messageNumber.getSoapElement());
73  
74          return ackRequested;
75      }
76  
77      /***
78       * Method toSoapEnvelop
79       *
80       * @param envelope
81       * @return SOAPEnvelope
82       * @throws SOAPException
83       */
84      public SOAPEnvelope toSoapEnvelop(SOAPEnvelope envelope) throws Exception {
85  
86          SOAPEnvelope env = envelope;
87          removeHeaders(env);
88  
89          if (env.getHeader() == null) {
90              env.addHeader();
91          }
92  
93          Name name = env.createName("", Constants.WSRM.NS_PREFIX_RM, Constants.WSRM.NS_URI_RM);
94          SOAPHeaderElement headerElement = (SOAPHeaderElement) env.getHeader().addHeaderElement(name);
95  
96          // .setActor(null);
97          headerElement.setActor(null);
98          headerElement.setName(Constants.WSRM.ACK_REQUESTED);
99          headerElement.setMustUnderstand(true);
100 
101         if (identifier != null) {
102             identifier.toSOAPEnvelope(headerElement);
103         }
104 
105         if (messageNumber != null) {
106             messageNumber.toSOAPEnvelope(headerElement);
107         }
108 
109         return env;
110     }
111 
112     /***
113      * Method fromSOAPEnveploe
114      *
115      * @param headerElement
116      * @return AckRequested
117      */
118     public AckRequested fromSOAPEnveploe(SOAPHeaderElement headerElement) throws AxisFault {
119 
120         Iterator iterator = headerElement.getChildElements();
121         MessageElement childElement;
122 
123         while (iterator.hasNext()) {
124             childElement = (MessageElement) iterator.next();
125 
126             if (childElement.getName().equals(Constants.WSRM.NS_PREFIX_RM + Constants.COLON + Constants.WSRM.IDENTIFIER)) {
127                 identifier = new Identifier();
128                 identifier.fromSOAPEnvelope(childElement);
129             }
130 
131             if (childElement.getName().equals(Constants.WSRM.IDENTIFIER)) {
132                 identifier = new Identifier();
133                 identifier.fromSOAPEnvelope(childElement);
134             }
135 
136             if (childElement.getName().equals(Constants.WSRM.NS_PREFIX_RM + Constants.COLON + Constants.WSRM.MSG_NUMBER)) {
137                 messageNumber = new MessageNumber();
138                 messageNumber.fromSOAPEnvelope(childElement);
139             }
140 
141             if (childElement.getName().equals(Constants.WSRM.MSG_NUMBER)) {
142                 messageNumber = new MessageNumber();
143                 messageNumber.fromSOAPEnvelope(childElement);
144             }
145         }
146 
147         return this;
148     }
149 
150     /***
151      * Method addChildElement
152      *
153      * @param element
154      * @throws SOAPException
155      */
156     public void addChildElement(MessageElement element) throws SOAPException {
157         ackRequested.addChildElement(element);
158     }
159 
160     /***
161      * Method getIdentifier
162      *
163      * @return Identifier
164      */
165     public Identifier getIdentifier() {
166         return identifier;
167     }
168 
169     /***
170      * Method getMessageNumber
171      *
172      * @return MessageNumber
173      */
174     public MessageNumber getMessageNumber() {
175         return messageNumber;
176     }
177 
178     /***
179      * Method setIdentifier
180      *
181      * @param identifier
182      */
183     public void setIdentifier(Identifier identifier) {
184         this.identifier = identifier;
185     }
186 
187     /***
188      * Method setMessageNumber
189      *
190      * @param number
191      */
192     public void setMessageNumber(MessageNumber number) {
193         messageNumber = number;
194     }
195 
196     public void removeHeaders(SOAPEnvelope soapEnvelope) throws Exception {
197 
198         Iterator iterator = soapEnvelope.getHeader().getChildElements();
199         MessageElement childElement;
200 
201         while (iterator.hasNext()) {
202             childElement = (MessageElement) iterator.next();
203 
204             if (Constants.WSRM.ACK_REQUESTED.equals(childElement.getName()) &&
205                     (Constants.WSRM.NS_URI_RM.equals(childElement.getNamespaceURI()))) {
206                 childElement.detachNode();
207                 break;
208             }
209         }
210     }
211 }