KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-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.axis.message;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Constants;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.components.logger.LogFactory;
23 import org.apache.axis.encoding.DeserializationContext;
24 import org.apache.axis.encoding.SerializationContext;
25 import org.apache.axis.handlers.soap.SOAPService;
26 import org.apache.axis.soap.SOAPConstants;
27 import org.apache.axis.utils.Messages;
28 import org.apache.commons.logging.Log;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.DOMException JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33
34 import javax.xml.namespace.QName JavaDoc;
35 import javax.xml.soap.Name JavaDoc;
36 import javax.xml.soap.SOAPElement JavaDoc;
37 import javax.xml.soap.SOAPException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Enumeration JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Vector JavaDoc;
43
44
45 /**
46  * Holder for header elements.
47  *
48  * @author Glyn Normington (glyn@apache.org)
49  */

50 public class SOAPHeader extends MessageElement
51     implements javax.xml.soap.SOAPHeader JavaDoc {
52
53     private static Log log = LogFactory.getLog(SOAPHeader.class.getName());
54
55     private SOAPConstants soapConstants;
56
57     SOAPHeader(SOAPEnvelope env, SOAPConstants soapConsts) {
58         super(Constants.ELEM_HEADER,
59               Constants.NS_PREFIX_SOAP_ENV,
60               (soapConsts != null) ? soapConsts.getEnvelopeURI() : Constants.DEFAULT_SOAP_VERSION.getEnvelopeURI());
61         soapConstants = (soapConsts != null) ? soapConsts : Constants.DEFAULT_SOAP_VERSION;
62         try {
63             setParentElement(env);
64         } catch (SOAPException JavaDoc ex) {
65             // class cast should never fail when parent is a SOAPEnvelope
66
log.fatal(Messages.getMessage("exception00"), ex);
67         }
68     }
69
70     public SOAPHeader(String JavaDoc namespace, String JavaDoc localPart, String JavaDoc prefix,
71                       Attributes JavaDoc attributes, DeserializationContext context,
72                       SOAPConstants soapConsts) throws AxisFault {
73         super(namespace, localPart, prefix, attributes, context);
74         soapConstants = (soapConsts != null) ? soapConsts : Constants.DEFAULT_SOAP_VERSION;
75     }
76
77     public void setParentElement(SOAPElement JavaDoc parent) throws SOAPException JavaDoc {
78         if(parent == null) {
79             throw new IllegalArgumentException JavaDoc(Messages.getMessage("nullParent00"));
80         }
81         try {
82             // cast to force exception if wrong type
83
SOAPEnvelope env = (SOAPEnvelope)parent;
84             super.setParentElement(env);
85             setEnvelope(env);
86         } catch (Throwable JavaDoc t) {
87             throw new SOAPException JavaDoc(t);
88         }
89     }
90
91     public javax.xml.soap.SOAPHeaderElement JavaDoc addHeaderElement(Name JavaDoc name)
92         throws SOAPException JavaDoc {
93         SOAPHeaderElement headerElement = new SOAPHeaderElement(name);
94         addChildElement(headerElement);
95         return headerElement;
96     }
97     
98     private Vector JavaDoc findHeaderElements(String JavaDoc actor) {
99         ArrayList JavaDoc actors = new ArrayList JavaDoc();
100         actors.add(actor);
101         return getHeadersByActor(actors);
102     }
103
104     public Iterator JavaDoc examineHeaderElements(String JavaDoc actor) {
105         return findHeaderElements(actor).iterator();
106     }
107
108     public Iterator JavaDoc extractHeaderElements(String JavaDoc actor) {
109         Vector JavaDoc results = findHeaderElements(actor);
110
111         Iterator JavaDoc iterator = results.iterator();
112         // Detach the header elements from the header
113
while (iterator.hasNext()) {
114             ((SOAPHeaderElement)iterator.next()).detachNode();
115         }
116
117         return results.iterator();
118     }
119
120     public Iterator JavaDoc examineMustUnderstandHeaderElements(String JavaDoc actor) {
121         if (actor == null) return null;
122
123         Vector JavaDoc result = new Vector JavaDoc();
124         List JavaDoc headers = getChildren();
125         if (headers != null) {
126             for(int i = 0; i < headers.size(); i++) {
127                 SOAPHeaderElement she = (SOAPHeaderElement)headers.get(i);
128                 if (she.getMustUnderstand()) {
129                     String JavaDoc candidate = she.getActor();
130                     if (actor.equals(candidate)) {
131                         result.add(headers.get(i));
132                     }
133                 }
134             }
135         }
136         return result.iterator();
137     }
138
139     public Iterator JavaDoc examineAllHeaderElements() {
140         return getChildElements();
141     }
142
143     public Iterator JavaDoc extractAllHeaderElements() {
144         Vector JavaDoc result = new Vector JavaDoc();
145         List JavaDoc headers = getChildren();
146         if (headers != null) {
147             for(int i = 0; i < headers.size(); i++) {
148                 result.add(headers.get(i));
149             }
150             headers.clear();
151         }
152         return result.iterator();
153     }
154
155     Vector JavaDoc getHeaders() {
156         initializeChildren();
157         return new Vector JavaDoc(getChildren());
158     }
159
160     /**
161      * Get all the headers targeted at a list of actors.
162      */

163     Vector JavaDoc getHeadersByActor(ArrayList JavaDoc actors) {
164         Vector JavaDoc results = new Vector JavaDoc();
165         List JavaDoc headers = getChildren();
166         if (headers == null) {
167             return results;
168         }
169         Iterator JavaDoc i = headers.iterator();
170         SOAPConstants soapVer = getEnvelope().getSOAPConstants();
171         boolean isSOAP12 = soapVer == SOAPConstants.SOAP12_CONSTANTS;
172         String JavaDoc nextActor = soapVer.getNextRoleURI();
173         while (i.hasNext()) {
174             SOAPHeaderElement header = (SOAPHeaderElement)i.next();
175             String JavaDoc actor = header.getActor();
176             
177             // Skip it if we're SOAP 1.2 and it's the "none" role.
178
if (isSOAP12 && Constants.URI_SOAP12_NONE_ROLE.equals(actor)) {
179                 continue;
180             }
181             
182             // Always process NEXT's, and then anything else in our list
183
// For now, also always process ultimateReceiver role if SOAP 1.2
184
if (actor == null ||
185                     nextActor.equals(actor) ||
186                 (isSOAP12 &&
187                     Constants.URI_SOAP12_ULTIMATE_ROLE.equals(actor)) ||
188                 (actors != null && actors.contains(actor))) {
189                 results.add(header);
190             }
191         }
192         return results;
193     }
194
195     void addHeader(SOAPHeaderElement header) {
196         if (log.isDebugEnabled())
197             log.debug(Messages.getMessage("addHeader00"));
198         try {
199             addChildElement(header);
200         } catch (SOAPException JavaDoc ex) {
201             // class cast should never fail when parent is a SOAPHeader
202
log.fatal(Messages.getMessage("exception00"), ex);
203         }
204     }
205
206     void removeHeader(SOAPHeaderElement header) {
207         if (log.isDebugEnabled())
208             log.debug(Messages.getMessage("removeHeader00"));
209         removeChild(header);
210     }
211
212     /**
213      * Get a header by name, filtering for headers targeted at this
214      * engine depending on the accessAllHeaders parameter.
215      */

216     SOAPHeaderElement getHeaderByName(String JavaDoc namespace,
217                                       String JavaDoc localPart,
218                                       boolean accessAllHeaders) {
219         QName JavaDoc name = new QName JavaDoc(namespace, localPart);
220         SOAPHeaderElement header = (SOAPHeaderElement)getChildElement(name);
221
222         // If we're operating within an AxisEngine, respect its actor list
223
// unless told otherwise
224
if (!accessAllHeaders) {
225             MessageContext mc = MessageContext.getCurrentContext();
226             if (mc != null) {
227                 if (header != null) {
228                     String JavaDoc actor = header.getActor();
229                     
230                     // Always respect "next" role
231
String JavaDoc nextActor =
232                             getEnvelope().getSOAPConstants().getNextRoleURI();
233                     if (nextActor.equals(actor))
234                         return header;
235                     
236                     SOAPService soapService = mc.getService();
237                     if (soapService != null) {
238                         ArrayList JavaDoc actors = mc.getService().getActors();
239                         if ((actor != null) &&
240                                 (actors == null || !actors.contains(actor))) {
241                             header = null;
242                         }
243                     }
244                 }
245             }
246         }
247         
248         return header;
249     }
250
251     /**
252      * Return an Enumeration of headers which match the given namespace
253      * and localPart. Depending on the value of the accessAllHeaders
254      * parameter, we will attempt to filter on the current engine's list
255      * of actors.
256      *
257      * !!! NOTE THAT RIGHT NOW WE ALWAYS ASSUME WE'RE THE "ULTIMATE
258      * DESTINATION" (i.e. we match on null actor). IF WE WANT TO FULLY SUPPORT
259      * INTERMEDIARIES WE'LL NEED TO FIX THIS.
260      */

261     Enumeration JavaDoc getHeadersByName(String JavaDoc namespace,
262                                  String JavaDoc localPart,
263                                  boolean accessAllHeaders) {
264         ArrayList JavaDoc actors = null;
265         boolean firstTime = false;
266         
267         /** This might be optimizable by creating a custom Enumeration
268          * which moves through the headers list (parsing on demand, again),
269          * returning only the next one each time.... this is Q&D for now.
270          */

271         Vector JavaDoc v = new Vector JavaDoc();
272         List JavaDoc headers = getChildren();
273         if (headers == null) {
274             return v.elements();
275         }
276         Iterator JavaDoc e = headers.iterator();
277         SOAPHeaderElement header;
278         String JavaDoc nextActor = getEnvelope().getSOAPConstants().getNextRoleURI();
279         
280         while (e.hasNext()) {
281             header = (SOAPHeaderElement)e.next();
282             if (header.getNamespaceURI().equals(namespace) &&
283                 header.getName().equals(localPart)) {
284
285                 if (!accessAllHeaders) {
286                     if (firstTime) {
287                         // Do one-time setup
288
MessageContext mc = MessageContext.getCurrentContext();
289                         if (mc != null && mc.getAxisEngine() != null) {
290                             actors = mc.getAxisEngine().getActorURIs();
291                         }
292                         firstTime = false;
293                     }
294
295                     String JavaDoc actor = header.getActor();
296                     if ((actor != null) && !nextActor.equals(actor) &&
297                             (actors == null || !actors.contains(actor))) {
298                         continue;
299                     }
300                 }
301
302                 v.addElement(header);
303             }
304         }
305         
306         return v.elements();
307     }
308
309     protected void outputImpl(SerializationContext context) throws Exception JavaDoc {
310         List JavaDoc headers = getChildren();
311         if (headers == null) {
312             return;
313         }
314         boolean oldPretty = context.getPretty();
315         context.setPretty(true);
316
317         if (log.isDebugEnabled())
318             log.debug(headers.size() + " "
319                     + Messages.getMessage("headers00"));
320
321         if (!headers.isEmpty()) {
322             // Output <SOAP-ENV:Header>
323
context.startElement(new QName JavaDoc(soapConstants.getEnvelopeURI(),
324                                            Constants.ELEM_HEADER), null);
325             Iterator JavaDoc enumeration = headers.iterator();
326             while (enumeration.hasNext()) {
327                 // Output this header element
328
((NodeImpl)enumeration.next()).output(context);
329             }
330             // Output </SOAP-ENV:Header>
331
context.endElement();
332         }
333
334         context.setPretty(oldPretty);
335     }
336
337     // overwrite the one in MessageElement and set envelope
338
public void addChild(MessageElement element) throws SOAPException JavaDoc {
339         if (!(element instanceof SOAPHeaderElement)) {
340             throw new SOAPException JavaDoc(Messages.getMessage("badSOAPHeader00"));
341         }
342         element.setEnvelope(getEnvelope());
343         super.addChild(element);
344     }
345
346     // overwrite the one in MessageElement and sets dirty flag
347
public SOAPElement JavaDoc addChildElement(SOAPElement JavaDoc element)
348         throws SOAPException JavaDoc {
349         if (!(element instanceof SOAPHeaderElement)) {
350             throw new SOAPException JavaDoc(Messages.getMessage("badSOAPHeader00"));
351         }
352         SOAPElement JavaDoc child = super.addChildElement(element);
353         setDirty(true);
354         return child;
355     }
356
357     public SOAPElement JavaDoc addChildElement(Name JavaDoc name) throws SOAPException JavaDoc {
358         SOAPHeaderElement child = new SOAPHeaderElement(name);
359         addChildElement(child);
360         return child;
361     }
362
363     public SOAPElement JavaDoc addChildElement(String JavaDoc localName) throws SOAPException JavaDoc {
364         // Inherit parent's namespace
365
SOAPHeaderElement child = new SOAPHeaderElement(getNamespaceURI(),
366                                                         localName);
367         addChildElement(child);
368         return child;
369     }
370
371     public SOAPElement JavaDoc addChildElement(String JavaDoc localName,
372                                        String JavaDoc prefix) throws SOAPException JavaDoc {
373         SOAPHeaderElement child =
374             new SOAPHeaderElement(getNamespaceURI(prefix), localName);
375         child.setPrefix(prefix);
376         addChildElement(child);
377         return child;
378     }
379
380     public SOAPElement JavaDoc addChildElement(String JavaDoc localName,
381                                        String JavaDoc prefix,
382                                        String JavaDoc uri) throws SOAPException JavaDoc {
383         SOAPHeaderElement child = new SOAPHeaderElement(uri, localName);
384         child.setPrefix(prefix);
385         child.addNamespaceDeclaration(prefix, uri);
386         addChildElement(child);
387         return child;
388     }
389
390     public Node JavaDoc appendChild(Node JavaDoc newChild) throws DOMException JavaDoc {
391         SOAPHeaderElement headerElement = null;
392         if(newChild instanceof SOAPHeaderElement)
393             headerElement = (SOAPHeaderElement)newChild;
394         else
395             headerElement = new SOAPHeaderElement((Element JavaDoc)newChild);
396         try {
397             addChildElement(headerElement);
398         } catch (SOAPException JavaDoc e) {
399             throw new DOMException JavaDoc(DOMException.INVALID_STATE_ERR,e.toString());
400         }
401         return headerElement;
402     }
403 }
404
Popular Tags