KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > message > SOAPHeaderElement


1 /*
2  * Copyright 2001-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 package org.apache.axis.message;
17
18 import org.apache.axis.AxisFault;
19 import org.apache.axis.Constants;
20 import org.apache.axis.encoding.DeserializationContext;
21 import org.apache.axis.encoding.SerializationContext;
22 import org.apache.axis.soap.SOAPConstants;
23 import org.apache.axis.utils.Messages;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.NamedNodeMap JavaDoc;
26 import org.xml.sax.Attributes JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.soap.Name JavaDoc;
30 import javax.xml.soap.SOAPElement JavaDoc;
31 import javax.xml.soap.SOAPException JavaDoc;
32
33 /**
34  * A simple header element abstraction. Extends MessageElement with
35  * header-specific stuff like mustUnderstand, actor, and a 'processed' flag.
36  *
37  * @author Glen Daniels (gdaniels@apache.org)
38  * @author Glyn Normington (glyn@apache.org)
39  */

40 public class SOAPHeaderElement extends MessageElement
41     implements javax.xml.soap.SOAPHeaderElement JavaDoc
42 {
43
44     protected boolean processed = false;
45
46     protected String JavaDoc actor = "http://schemas.xmlsoap.org/soap/actor/next";
47     protected boolean mustUnderstand = false;
48     protected boolean relay = false;
49
50     public SOAPHeaderElement(String JavaDoc namespace, String JavaDoc localPart)
51     {
52         super(namespace, localPart);
53     }
54
55     public SOAPHeaderElement(Name JavaDoc name)
56     {
57         super(name);
58     }
59
60     public SOAPHeaderElement(QName JavaDoc qname)
61     {
62         super(qname);
63     }
64
65     public SOAPHeaderElement(String JavaDoc namespace, String JavaDoc localPart,
66                              Object JavaDoc value)
67     {
68         super(namespace, localPart, value);
69     }
70     
71     public SOAPHeaderElement(QName JavaDoc qname, Object JavaDoc value)
72     {
73         super(qname, value);
74     }
75
76     public SOAPHeaderElement(Element JavaDoc elem)
77     {
78         super(elem);
79
80         // FIXME : This needs to come from someplace reasonable, perhaps
81
// TLS (SOAPConstants.getCurrentVersion() ?)
82
SOAPConstants soapConstants = getSOAPConstants();
83
84         String JavaDoc val = elem.getAttributeNS(soapConstants.getEnvelopeURI(),
85                                          Constants.ATTR_MUST_UNDERSTAND);
86
87         try {
88             setMustUnderstandFromString(val, (soapConstants ==
89                                               SOAPConstants.SOAP12_CONSTANTS));
90         } catch (AxisFault axisFault) {
91             // Log the bad MU value, since this constructor can't throw
92
log.error(axisFault);
93         }
94
95         QName JavaDoc roleQName = soapConstants.getRoleAttributeQName();
96         actor = elem.getAttributeNS(roleQName.getNamespaceURI(),
97                                     roleQName.getLocalPart());
98 // if (actor == null) {
99
// actor = "";
100
// }
101

102         if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
103             String JavaDoc relayVal = elem.getAttributeNS(soapConstants.getEnvelopeURI(),
104                                                   Constants.ATTR_RELAY);
105             relay = ((relayVal != null) && (relayVal.equals("true") || relayVal.equals("1"))) ? true : false;
106         }
107     }
108
109     public void setParentElement(SOAPElement JavaDoc parent) throws SOAPException JavaDoc
110     {
111         if(parent == null) {
112             throw new IllegalArgumentException JavaDoc(Messages.getMessage("nullParent00"));
113         }
114         // migration aid
115
if (parent instanceof SOAPEnvelope) {
116             log.warn(Messages.getMessage("bodyHeaderParent"));
117             parent = ((SOAPEnvelope)parent).getHeader();
118         }
119         if (!(parent instanceof SOAPHeader)) {
120             throw new IllegalArgumentException JavaDoc(Messages.getMessage("illegalArgumentException00"));
121         }
122
123         super.setParentElement(parent);
124     }
125
126     public SOAPHeaderElement(String JavaDoc namespace,
127                              String JavaDoc localPart,
128                              String JavaDoc prefix,
129                              Attributes JavaDoc attributes,
130                              DeserializationContext context)
131         throws AxisFault
132     {
133         super(namespace, localPart, prefix, attributes, context);
134
135         SOAPConstants soapConstants = getSOAPConstants();
136
137         // Check for mustUnderstand
138
String JavaDoc val = attributes.getValue(soapConstants.getEnvelopeURI(),
139                                          Constants.ATTR_MUST_UNDERSTAND);
140
141         setMustUnderstandFromString(val, (soapConstants ==
142                                           SOAPConstants.SOAP12_CONSTANTS));
143
144         QName JavaDoc roleQName = soapConstants.getRoleAttributeQName();
145         actor = attributes.getValue(roleQName.getNamespaceURI(),
146                                     roleQName.getLocalPart());
147 // if (actor == null) {
148
// actor = "";
149
// }
150

151         if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
152             String JavaDoc relayVal = attributes.getValue(soapConstants.getEnvelopeURI(),
153                                                   Constants.ATTR_RELAY);
154             relay = ((relayVal != null) && (relayVal.equals("true") || relayVal.equals("1"))) ? true : false;
155         }
156
157         processed = false;
158         alreadySerialized = true;
159     }
160
161     private void setMustUnderstandFromString(String JavaDoc val, boolean isSOAP12)
162         throws AxisFault {
163         if (val != null && val.length() > 0) {
164             if ("0".equals(val)) {
165                 mustUnderstand = false;
166             } else if ("1".equals(val)) {
167                 mustUnderstand = true;
168             } else if (isSOAP12) {
169                 if ("true".equalsIgnoreCase(val)) {
170                     mustUnderstand = true;
171                 } else if ("false".equalsIgnoreCase(val)) {
172                     mustUnderstand = false;
173                 } else {
174                     throw new AxisFault(
175                             Messages.getMessage("badMUVal",
176                                                 val,
177                                                 new QName JavaDoc(namespaceURI,
178                                                           name).toString()));
179                 }
180             } else {
181                 throw new AxisFault(
182                         Messages.getMessage("badMUVal",
183                                             val,
184                                             new QName JavaDoc(namespaceURI,
185                                                       name).toString()));
186             }
187         }
188     }
189     
190     public boolean getMustUnderstand() { return( mustUnderstand ); }
191     public void setMustUnderstand(boolean b) {
192         mustUnderstand = b ;
193     }
194
195     public String JavaDoc getActor() { return( actor ); }
196     public void setActor(String JavaDoc a) {
197         actor = a ;
198     }
199     
200     public String JavaDoc getRole() { return( actor ); }
201     public void setRole(String JavaDoc a) {
202         actor = a ;
203     }
204
205     public boolean getRelay() {
206         return relay;
207     }
208     public void setRelay(boolean relay) {
209         this.relay = relay;
210     }
211
212     public void setProcessed(boolean value) {
213         processed = value ;
214     }
215
216     public boolean isProcessed() {
217         return( processed );
218     }
219
220     boolean alreadySerialized = false;
221
222     /** Subclasses can override
223      */

224     protected void outputImpl(SerializationContext context) throws Exception JavaDoc {
225         if (!alreadySerialized) {
226             SOAPConstants soapVer = getSOAPConstants();
227             QName JavaDoc roleQName = soapVer.getRoleAttributeQName();
228
229             if (actor != null) {
230                 setAttribute(roleQName.getNamespaceURI(),
231                              roleQName.getLocalPart(), actor);
232             }
233             
234             String JavaDoc val;
235             if (context.getMessageContext() != null && context.getMessageContext().getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS)
236                 val = mustUnderstand ? "true" : "false";
237             else
238                 val = mustUnderstand ? "1" : "0";
239
240             setAttribute(soapVer.getEnvelopeURI(),
241                          Constants.ATTR_MUST_UNDERSTAND,
242                          val);
243             
244             if (soapVer == SOAPConstants.SOAP12_CONSTANTS && relay) {
245                 setAttribute(soapVer.getEnvelopeURI(), Constants.ATTR_RELAY,
246                              "true");
247             }
248         }
249
250         super.outputImpl(context);
251     }
252
253     public NamedNodeMap JavaDoc getAttributes() {
254         makeAttributesEditable();
255         SOAPConstants soapConstants = getSOAPConstants();
256         String JavaDoc mustUnderstand = attributes.getValue(soapConstants.getEnvelopeURI(),
257                                          Constants.ATTR_MUST_UNDERSTAND);
258         QName JavaDoc roleQName = soapConstants.getRoleAttributeQName();
259         String JavaDoc actor = attributes.getValue(roleQName.getNamespaceURI(),roleQName.getLocalPart());
260         
261         if(mustUnderstand == null){
262             if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
263                 setAttributeNS(soapConstants.getEnvelopeURI(),
264                             Constants.ATTR_MUST_UNDERSTAND,"false");
265             } else {
266                 setAttributeNS(soapConstants.getEnvelopeURI(),
267                             Constants.ATTR_MUST_UNDERSTAND,"0");
268             }
269         }
270         if(actor == null){
271             setAttributeNS(roleQName.getNamespaceURI(),
272                          roleQName.getLocalPart(), this.actor);
273         }
274         return super.getAttributes();
275     }
276
277     private SOAPConstants getSOAPConstants() {
278         SOAPConstants soapConstants = null;
279         if (context != null) {
280             return context.getSOAPConstants();
281         }
282         if (getNamespaceURI() != null &&
283                 getNamespaceURI().equals(SOAPConstants.SOAP12_CONSTANTS.getEnvelopeURI())) {
284             soapConstants = SOAPConstants.SOAP12_CONSTANTS;
285         }
286         if (soapConstants == null && getEnvelope() != null) {
287             soapConstants = getEnvelope().getSOAPConstants();
288         }
289         if (soapConstants == null) {
290             soapConstants = SOAPConstants.SOAP11_CONSTANTS;
291         }
292         return soapConstants;
293     }
294 }
295
Popular Tags