KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > SOAPHeaderAxisImpl


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.jboss.axis.message;
18
19 import org.jboss.axis.AxisFault;
20 import org.jboss.axis.Constants;
21 import org.jboss.axis.MessageContext;
22 import org.jboss.axis.encoding.DeserializationContext;
23 import org.jboss.axis.encoding.SerializationContext;
24 import org.jboss.axis.handlers.soap.SOAPService;
25 import org.jboss.axis.soap.SOAPConstants;
26 import org.jboss.axis.utils.Messages;
27 import org.jboss.logging.Logger;
28 import org.xml.sax.Attributes JavaDoc;
29
30 import javax.xml.namespace.QName JavaDoc;
31 import javax.xml.soap.Name JavaDoc;
32 import javax.xml.soap.SOAPElement JavaDoc;
33 import javax.xml.soap.SOAPException JavaDoc;
34 import javax.xml.soap.SOAPHeaderElement JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Vector JavaDoc;
39
40
41 /**
42  * Holder for header elements.
43  *
44  * @author Glyn Normington (glyn@apache.org)
45  */

46 public class SOAPHeaderAxisImpl extends SOAPHeaderImpl
47 {
48
49    private static Logger log = Logger.getLogger(SOAPHeaderAxisImpl.class.getName());
50
51    private SOAPConstants soapConstants;
52
53    SOAPHeaderAxisImpl(SOAPEnvelopeAxisImpl env, SOAPConstants soapConsts)
54    {
55       super(Constants.ELEM_HEADER, Constants.NS_PREFIX_SOAP_ENV,
56               (soapConsts != null) ? soapConsts.getEnvelopeURI() : Constants.DEFAULT_SOAP_VERSION.getEnvelopeURI());
57       soapConstants = (soapConsts != null) ? soapConsts : Constants.DEFAULT_SOAP_VERSION;
58       try
59       {
60          setParentElement(env);
61          setEnvelope(env);
62       }
63       catch (SOAPException JavaDoc ex)
64       {
65          // class cast should never fail when parent is a SOAPEnvelope
66
log.fatal(Messages.getMessage("exception00"), ex);
67       }
68    }
69
70    public SOAPHeaderAxisImpl(String JavaDoc namespace, String JavaDoc localPart, String JavaDoc prefix,
71                              Attributes JavaDoc attributes, DeserializationContext context,
72                              SOAPConstants soapConsts) throws AxisFault
73    {
74       super(namespace, localPart, prefix, attributes, context);
75       soapConstants = (soapConsts != null) ? soapConsts : Constants.DEFAULT_SOAP_VERSION;
76    }
77
78    public void setParentElement(SOAPElement JavaDoc parent) throws SOAPException JavaDoc
79    {
80
81       if (parent == null)
82          throw new IllegalArgumentException JavaDoc(Messages.getMessage("nullParent00"));
83
84       try
85       {
86          SOAPEnvelopeAxisImpl env = (SOAPEnvelopeAxisImpl)parent;
87          super.setParentElement(env);
88          setEnvelope(env);
89       }
90       catch (Throwable JavaDoc t)
91       {
92          throw new SOAPException JavaDoc(t);
93       }
94    }
95
96    public SOAPHeaderElement JavaDoc addHeaderElement(Name JavaDoc name)
97            throws SOAPException JavaDoc
98    {
99       SOAPHeaderElementAxisImpl headerElement = new SOAPHeaderElementAxisImpl(name);
100       SOAPEnvelopeAxisImpl envelope = getEnvelope();
101       headerElement.setEnvelope(envelope);
102       addHeader(headerElement);
103       envelope.setDirty(true);
104       return headerElement;
105    }
106
107    public Iterator JavaDoc examineHeaderElements(String JavaDoc actor)
108    {
109       ArrayList JavaDoc results = new ArrayList JavaDoc();
110       getHeaderElements(results, actor);
111       return results.iterator();
112    }
113
114    public Iterator JavaDoc extractHeaderElements(String JavaDoc actor)
115    {
116       ArrayList JavaDoc results = new ArrayList JavaDoc();
117       getHeaderElements(results, actor);
118
119       // Detach the header elements from the header
120
Iterator JavaDoc iterator = results.iterator();
121       while (iterator.hasNext())
122       {
123          removeChild((SOAPHeaderElementAxisImpl)iterator.next());
124       }
125
126       return results.iterator();
127    }
128
129    public Iterator JavaDoc examineMustUnderstandHeaderElements(String JavaDoc actor)
130    {
131       ArrayList JavaDoc results = new ArrayList JavaDoc();
132       getHeaderElements(results, actor);
133
134       Iterator JavaDoc it = results.iterator();
135       while (it.hasNext())
136       {
137          SOAPHeaderElementAxisImpl she = (SOAPHeaderElementAxisImpl)it.next();
138          if (she.getMustUnderstand() == false)
139             it.remove();
140       }
141
142       return results.iterator();
143    }
144
145    public Iterator JavaDoc examineAllHeaderElements()
146    {
147       return getChildren().iterator();
148    }
149
150    public Iterator JavaDoc extractAllHeaderElements()
151    {
152       Vector JavaDoc result = new Vector JavaDoc(getChildren());
153       removeChildren();
154       return result.iterator();
155    }
156
157    private void getHeaderElements(ArrayList JavaDoc results, String JavaDoc actor)
158    {
159       String JavaDoc nextActor = SOAPConstants.SOAP11_CONSTANTS.getNextRoleURI();
160
161       Iterator JavaDoc it = getChildren().iterator();
162       while (it.hasNext())
163       {
164          SOAPHeaderElementAxisImpl she = (SOAPHeaderElementAxisImpl)it.next();
165          String JavaDoc headerActor = she.getActor();
166
167          if ((nextActor.equals(headerActor) && she.getMustUnderstand() == false)
168                  || (actor != null && actor.equals(headerActor)))
169          {
170             results.add(she);
171          }
172       }
173    }
174
175    void addHeader(SOAPHeaderElementAxisImpl header)
176    {
177       if (log.isDebugEnabled())
178          log.debug(Messages.getMessage("addHeader00"));
179       try
180       {
181          header.setParentElement(this);
182       }
183       catch (SOAPException JavaDoc ex)
184       {
185          // class cast should never fail when parent is a SOAPHeader
186
log.fatal(Messages.getMessage("exception00"), ex);
187       }
188    }
189
190    void removeHeader(SOAPHeaderElementAxisImpl header)
191    {
192       if (log.isDebugEnabled())
193          log.debug(Messages.getMessage("removeHeader00"));
194
195       removeChild(header);
196    }
197
198    /**
199     * Get a header by name, filtering for headers targeted at this
200     * engine depending on the accessAllHeaders parameter.
201     */

202    SOAPHeaderElementAxisImpl getHeaderByName(String JavaDoc namespace,
203                                              String JavaDoc localPart,
204                                              boolean accessAllHeaders)
205    {
206       SOAPHeaderElementAxisImpl header = (SOAPHeaderElementAxisImpl)findElement(namespace, localPart);
207
208       // If we're operating within an AxisEngine, respect its actor list
209
// unless told otherwise
210
if (!accessAllHeaders)
211       {
212          MessageContext mc = MessageContext.getCurrentContext();
213          if (mc != null)
214          {
215             if (header != null)
216             {
217                String JavaDoc actor = header.getActor();
218
219                // Always respect "next" role
220
String JavaDoc nextActor =
221                        getEnvelope().getSOAPConstants().getNextRoleURI();
222                if (nextActor.equals(actor))
223                   return header;
224
225                SOAPService soapService = mc.getService();
226                if (soapService != null)
227                {
228                   ArrayList JavaDoc actors = mc.getService().getActors();
229                   if ((actor != null) &&
230                           (actors == null || !actors.contains(actor)))
231                   {
232                      header = null;
233                   }
234                }
235             }
236          }
237       }
238
239       return header;
240    }
241
242    private SOAPElementAxisImpl findElement(String JavaDoc namespace, String JavaDoc localPart)
243    {
244       if (hasChildNodes() == false)
245          return null;
246
247       QName JavaDoc qname = new QName JavaDoc(namespace, localPart);
248       Iterator JavaDoc it = getChildren().iterator();
249       while (it.hasNext())
250       {
251          SOAPHeaderElementAxisImpl she = (SOAPHeaderElementAxisImpl)it.next();
252          if (she.getQName().equals(qname))
253             return she;
254       }
255
256       return null;
257    }
258
259    /**
260     * Return an Enumeration of headers which match the given namespace
261     * and localPart. Depending on the value of the accessAllHeaders
262     * parameter, we will attempt to filter on the current engine's list
263     * of actors.
264     * <p/>
265     * !!! NOTE THAT RIGHT NOW WE ALWAYS ASSUME WE'RE THE "ULTIMATE
266     * DESTINATION" (i.e. we match on null actor). IF WE WANT TO FULLY SUPPORT
267     * INTERMEDIARIES WE'LL NEED TO FIX THIS.
268     */

269    Enumeration JavaDoc getHeadersByName(String JavaDoc namespace,
270                                 String JavaDoc localPart,
271                                 boolean accessAllHeaders)
272    {
273       ArrayList JavaDoc actors = null;
274       boolean firstTime = false;
275
276       /** This might be optimizable by creating a custom Enumeration
277        * which moves through the headers list (parsing on demand, again),
278        * returning only the next one each time.... this is Q&D for now.
279        */

280       Vector JavaDoc v = new Vector JavaDoc();
281       String JavaDoc nextActor = getEnvelope().getSOAPConstants().getNextRoleURI();
282
283       Iterator JavaDoc it = getChildren().iterator();
284       while (it.hasNext())
285       {
286          SOAPHeaderElementAxisImpl she = (SOAPHeaderElementAxisImpl)it.next();
287          if (she.getNamespaceURI().equals(namespace) &&
288                  she.getName().equals(localPart))
289          {
290
291             if (!accessAllHeaders)
292             {
293                if (firstTime)
294                {
295                   // Do one-time setup
296
MessageContext mc = MessageContext.getCurrentContext();
297                   if (mc != null)
298                      actors = mc.getAxisEngine().getActorURIs();
299
300                   firstTime = false;
301                }
302
303                String JavaDoc actor = she.getActor();
304                if ((actor != null) && !nextActor.equals(actor) &&
305                        (actors == null || !actors.contains(actor)))
306                {
307                   continue;
308                }
309             }
310
311             v.addElement(she);
312          }
313       }
314
315       return v.elements();
316    }
317
318    protected void outputImpl(SerializationContext context) throws Exception JavaDoc
319    {
320       boolean oldPretty = context.getPretty();
321       context.setPretty(true);
322
323       if (hasChildNodes())
324       {
325          // Output <SOAP-ENV:Header>
326
context.startElement(new QName JavaDoc(soapConstants.getEnvelopeURI(),
327                  Constants.ELEM_HEADER), null);
328
329          Iterator JavaDoc it = getChildren().iterator();
330          while (it.hasNext())
331          {
332             SOAPHeaderElementAxisImpl she = (SOAPHeaderElementAxisImpl)it.next();
333             // Output this header element
334
((SOAPHeaderElementAxisImpl)she).output(context);
335          }
336          // Output </SOAP-ENV:Header>
337
context.endElement();
338       }
339
340       context.setPretty(oldPretty);
341    }
342
343    /**
344     * we have to override this to enforce that SOAPHeader immediate
345     * children are exclusively of type SOAPHeaderElement (otherwise
346     * we'll get mysterious ClassCastExceptions down the road... )
347     *
348     * @param element child element
349     * @return soap element
350     * @throws SOAPException
351     */

352    public SOAPElement JavaDoc addChildElement(SOAPElement JavaDoc element)
353            throws SOAPException JavaDoc
354    {
355       if (!(element instanceof javax.xml.soap.SOAPHeaderElement JavaDoc))
356       {
357          throw new SOAPException JavaDoc(Messages.getMessage("badSOAPHeader00"));
358       }
359       return super.addChildElement(element);
360    }
361
362    public SOAPElement JavaDoc addChildElement(Name JavaDoc name) throws SOAPException JavaDoc
363    {
364       SOAPHeaderElementAxisImpl child = new SOAPHeaderElementAxisImpl(name.getURI(),
365               name.getLocalName());
366       addChild(child);
367       child.setEnvelope(getEnvelope());
368       return child;
369    }
370
371    public SOAPElement JavaDoc addChildElement(String JavaDoc localName) throws SOAPException JavaDoc
372    {
373       // Inherit parent's namespace
374
SOAPHeaderElementAxisImpl child = new SOAPHeaderElementAxisImpl(getNamespaceURI(),
375               localName);
376       addChild(child);
377       child.setEnvelope(getEnvelope());
378       return child;
379    }
380
381    public SOAPElement JavaDoc addChildElement(String JavaDoc localName,
382                                       String JavaDoc prefix) throws SOAPException JavaDoc
383    {
384       SOAPHeaderElementAxisImpl child = new SOAPHeaderElementAxisImpl(getNamespaceURI(prefix),
385               localName);
386       addChild(child);
387       child.setEnvelope(getEnvelope());
388       return child;
389    }
390
391    public SOAPElement JavaDoc addChildElement(String JavaDoc localName,
392                                       String JavaDoc prefix,
393                                       String JavaDoc uri) throws SOAPException JavaDoc
394    {
395       SOAPHeaderElementAxisImpl child = new SOAPHeaderElementAxisImpl(uri,
396               localName);
397       child.setPrefix(prefix);
398       child.addNamespaceDeclaration(prefix, uri);
399       addChild(child);
400       child.setEnvelope(getEnvelope());
401       return child;
402    }
403 }
404
Popular Tags