e.g. Calendar Search Help
You must enter a value before pressing Search
axis

Class: org.apache.axis.message.SOAPHeader   ©

 OK to copy?
001 /*
002  * The Apache Software License, Version 1.1
003  *
004  *
005  * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights 
006  * reserved.
007  *
008  * Redistribution and use in source and binary forms, with or without
009  * modification, are permitted provided that the following conditions
010  * are met:
011  *
012  * 1. Redistributions of source code must retain the above copyright
013  *    notice, this list of conditions and the following disclaimer. 
014  *
015  * 2. Redistributions in binary form must reproduce the above copyright
016  *    notice, this list of conditions and the following disclaimer in
017  *    the documentation and/or other materials provided with the
018  *    distribution.
019  *
020  * 3. The end-user documentation included with the redistribution,
021  *    if any, must include the following acknowledgment:  
022  *       "This product includes software developed by the
023  *        Apache Software Foundation (http://www.apache.org/)."
024  *    Alternately, this acknowledgment may appear in the software itself,
025  *    if and wherever such third-party acknowledgments normally appear.
026  *
027  * 4. The names "Axis" and "Apache Software Foundation" must
028  *    not be used to endorse or promote products derived from this
029  *    software without prior written permission. For written 
030  *    permission, please contact apache@apache.org.
031  *
032  * 5. Products derived from this software may not be called "Apache",
033  *    nor may "Apache" appear in their name, without prior written
034  *    permission of the Apache Software Foundation.
035  *
036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047  * SUCH DAMAGE.
048  * ====================================================================
049  *
050  * This software consists of voluntary contributions made by many
051  * individuals on behalf of the Apache Software Foundation.  For more
052  * information on the Apache Software Foundation, please see
053  * <http://www.apache.org/>.
054  */
055 
056 package org.apache.axis.message;
057 
058 import org.apache.axis.Constants;
059 import org.apache.axis.MessageContext;
060 import org.apache.axis.AxisFault;
061 import org.apache.axis.components.logger.LogFactory;
062 import org.apache.axis.encoding.DeserializationContext;
063 import org.apache.axis.encoding.SerializationContext;
064 import org.apache.axis.handlers.soap.SOAPService;
065 import org.apache.axis.soap.SOAPConstants;
066 import org.apache.axis.utils.Messages;
067 import org.apache.commons.logging.Log;
068 import org.xml.sax.Attributes;
069 
070 import javax.xml.namespace.QName;
071 import javax.xml.soap.Name;
072 import javax.xml.soap.SOAPElement;
073 import javax.xml.soap.SOAPException;
074 import java.util.ArrayList;
075 import java.util.Enumeration;
076 import java.util.Iterator;
077 import java.util.Vector;
078 
079 
080 /**
081  * Holder for header elements.
082  *
083  * @author Glyn Normington (glyn@apache.org)
084  */
085 public class SOAPHeader extends MessageElement
086     implements javax.xml.soap.SOAPHeader {
087 
088     private static Log log = LogFactory.getLog(SOAPHeader.class.getName());
089 
090     private Vector headers = new Vector();
091 
092     private SOAPConstants soapConstants;
093 
094     SOAPHeader(SOAPEnvelope env, SOAPConstants soapConsts) {
095         super(Constants.ELEM_HEADER,
096               Constants.NS_PREFIX_SOAP_ENV,
097               (soapConsts != null) ? soapConsts.getEnvelopeURI() : Constants.DEFAULT_SOAP_VERSION.getEnvelopeURI());
098         soapConstants = (soapConsts != null) ? soapConsts : Constants.DEFAULT_SOAP_VERSION;
099         try {
100             setParentElement(env);
101             setEnvelope(env);
102         } catch (SOAPException ex) {
103             // class cast should never fail when parent is a SOAPEnvelope
104             log.fatal(Messages.getMessage("exception00"), ex);
105         }
106     }
107 
108     public SOAPHeader(String namespace, String localPart, String prefix,
109                       Attributes attributes, DeserializationContext context,
110                       SOAPConstants soapConsts) throws AxisFault {
111         super(namespace, localPart, prefix, attributes, context);
112         soapConstants = (soapConsts != null) ? soapConsts : Constants.DEFAULT_SOAP_VERSION;
113     }
114 
115     public void setParentElement(SOAPElement parent) throws SOAPException {
116         if(parent == null)
117             throw new IllegalArgumentException(Messages.getMessage("nullParent00")); 
118         try {
119             SOAPEnvelope env = (SOAPEnvelope)parent;
120             // cast to force exception if wrong type
121             super.setParentElement(env);
122             setEnvelope(env);
123         } catch (Throwable t) {
124             throw new SOAPException(t);
125         }
126     }
127 
128     public void detachNode() {
129         ((SOAPEnvelope)parent).removeHeaders();
130         super.detachNode();
131     }
132 
133     public javax.xml.soap.SOAPHeaderElement addHeaderElement(Name name)
134         throws SOAPException {
135         SOAPHeaderElement headerElement = new SOAPHeaderElement(name);
136         headerElement.setEnvelope(getEnvelope());
137         addHeader(headerElement);
138         return headerElement;
139     }
140     
141     private Vector findHeaderElements(String actor) {
142         ArrayList actors = new ArrayList();
143         actors.add(actor);
144         return getHeadersByActor(actors);
145     }
146 
147     public Iterator examineHeaderElements(String actor) {
148         return findHeaderElements(actor).iterator();
149     }
150 
151     public Iterator extractHeaderElements(String actor) {
152         Vector results = findHeaderElements(actor);
153 
154         Iterator iterator = results.iterator();
155         // Detach the header elements from the header
156         while (iterator.hasNext()) {
157             ((SOAPHeaderElement)iterator.next()).detachNode();
158         }
159 
160         return results.iterator();
161     }
162 
163     Vector getHeaders() {
164         return headers;
165     }
166 
167     /**
168      * Get all the headers targeted at a list of actors.
169      */ 
170     Vector getHeadersByActor(ArrayList actors) {
171         Vector results = new Vector();
172         Iterator i = headers.iterator();
173         SOAPConstants soapVer = getEnvelope().getSOAPConstants();
174         boolean isSOAP12 = soapVer == SOAPConstants.SOAP12_CONSTANTS;
175         String nextActor = soapVer.getNextRoleURI();
176         while (i.hasNext()) {
177             SOAPHeaderElement header = (SOAPHeaderElement)i.next();
178             String actor = header.getActor();
179             
180             // Skip it if we're SOAP 1.2 and it's the "none" role.
181             if (isSOAP12 && Constants.URI_SOAP12_NONE_ROLE.equals(actor)) {
182                 continue;
183             }
184             
185             // Always process NEXT's, and then anything else in our list
186             // For now, also always process ultimateReceiver role if SOAP 1.2
Rate187             if (actor == null ||
188                     nextActor.equals(actor) ||
189                 (isSOAP12 && 
190                     Constants.URI_SOAP12_ULTIMATE_ROLE.equals(actor)) ||
191                 (actors != null && actors.contains(actor))) {
192                 results.add(header);
193             }
194         }
195         return results;
196     }
197 
198     void addHeader(SOAPHeaderElement header) {
199         if (log.isDebugEnabled())
200             log.debug(Messages.getMessage("addHeader00"));
201         try {
202             header.setParentElement(this);
203         } catch (SOAPException ex) {
204             // class cast should never fail when parent is a SOAPHeader
205             log.fatal(Messages.getMessage("exception00"), ex);
206         }
207     }
208 
209     void removeHeader(SOAPHeaderElement header) {
210         if (log.isDebugEnabled())
211             log.debug(Messages.getMessage("removeHeader00"));
212         headers.removeElement(header);
213     }
214 
215     /**
216      * Get a header by name, filtering for headers targeted at this
217      * engine depending on the accessAllHeaders parameter.
218      */ 
219     SOAPHeaderElement getHeaderByName(String namespace,
220                                       String localPart,
221                                       boolean accessAllHeaders) {
222         SOAPHeaderElement header = (SOAPHeaderElement)findElement(headers,
223                                                                   namespace,
224                                                                   localPart);
225 
226         // If we're operating within an AxisEngine, respect its actor list
227         // unless told otherwise
228         if (!accessAllHeaders) {
229             MessageContext mc = MessageContext.getCurrentContext();
230             if (mc != null) {
231                 if (header != null) {
232                     String actor = header.getActor();
233                     
234                     // Always respect "next" role
235                     String nextActor = 
236                             getEnvelope().getSOAPConstants().getNextRoleURI();
237                     if (nextActor.equals(actor))
238                         return header;
239                     
240                     SOAPService soapService = mc.getService();
241                     if (soapService != null) {
242                         ArrayList actors = mc.getService().getActors();
Rate243                         if ((actor != null) && 
244                                 (actors == null || !actors.contains(actor))) {
245                             header = null;
246                         }
247                     }
248                 }
249             }
250         }
251         
252         return header;
253     }
254 
255     private MessageElement findElement(Vector vec, String namespace,
256                                        String localPart)
257     {
258         if (vec.isEmpty())
259             return null;
260         
261         QName qname = new QName(namespace, localPart);
262         Enumeration e = vec.elements();
263         MessageElement element;
264         while (e.hasMoreElements()) {
265             element = (MessageElement)e.nextElement();
266             if (element.getQName().equals(qname))
267                 return element;
268         }
269         
270         return null;
271     }
272 
273     /**
274      * Return an Enumeration of headers which match the given namespace
275      * and localPart.  Depending on the value of the accessAllHeaders
276      * parameter, we will attempt to filter on the current engine's list
277      * of actors.
278      * 
279      * !!! NOTE THAT RIGHT NOW WE ALWAYS ASSUME WE'RE THE "ULTIMATE
280      * DESTINATION" (i.e. we match on null actor).  IF WE WANT TO FULLY SUPPORT
281      * INTERMEDIARIES WE'LL NEED TO FIX THIS.
282      */ 
283     Enumeration getHeadersByName(String namespace,
284                                  String localPart,
285                                  boolean accessAllHeaders) {
286         ArrayList actors = null;
287         boolean firstTime = false;
288         
289         /** This might be optimizable by creating a custom Enumeration
290          * which moves through the headers list (parsing on demand, again),
291          * returning only the next one each time.... this is Q&D for now.
292          */
293         Vector v = new Vector();
294         Enumeration e = headers.elements();
295         SOAPHeaderElement header;
296         String nextActor = getEnvelope().getSOAPConstants().getNextRoleURI();
297         
298         while (e.hasMoreElements()) {
299             header = (SOAPHeaderElement)e.nextElement();
300             if (header.getNamespaceURI().equals(namespace) &&
301                 header.getName().equals(localPart)) {
302 
303                 if (!accessAllHeaders) {
304                     if (firstTime) {
305                         // Do one-time setup
306                         MessageContext mc = MessageContext.getCurrentContext();
307                         if (mc != null)
308                             actors = mc.getAxisEngine().getActorURIs();
309                             
310                         firstTime = false;
311                     }
312 
313                     String actor = header.getActor();
Rate314                     if ((actor != null) && !nextActor.equals(actor) &&
315                             (actors == null || !actors.contains(actor))) {
316                         continue;
317                     }
318                 }
319 
320                 v.addElement(header);
321             }
322         }
323         
324         return v.elements();
325     }
326 
327     protected void outputImpl(SerializationContext context) throws Exception {
328         boolean oldPretty = context.getPretty();
329         context.setPretty(true);
330 
331         if (log.isDebugEnabled())
332             log.debug(headers.size() + " "
333                     + Messages.getMessage("headers00"));
334 
335         if (!headers.isEmpty()) {
336             // Output <SOAP-ENV:Header>
337             context.startElement(new QName(soapConstants.getEnvelopeURI(),
338                                            Constants.ELEM_HEADER), null);
339             Enumeration enum = headers.elements();
340             while (enum.hasMoreElements()) {
341                 // Output this header element
342                 ((SOAPHeaderElement)enum.nextElement()).output(context);
343             }
344             // Output </SOAP-ENV:Header>
345             context.endElement();
346         }
347 
348         context.setPretty(oldPretty);
349     }
350 
351     public void addChild(MessageElement el) throws SOAPException {
352         headers.addElement(el);
353     }
354 
355     public java.util.Iterator getChildElements() {
356         return headers.iterator();
357     }
358 
359     public java.util.Iterator getChildElements(Name name) {
360         Vector v = new Vector();
361         Enumeration e = headers.elements();
362         SOAPHeaderElement header;
363         while (e.hasMoreElements()) {
364             header = (SOAPHeaderElement)e.nextElement();
365             if (header.getNamespaceURI().equals(name.getURI()) &&
366                 header.getName().equals(name.getLocalName())) {
367                 v.addElement(header);
368             }
369         }
370         return v.iterator();
371     }
372     public void removeChild(MessageElement child) {
373         // Remove all occurrences in case it has been added multiple times.
374         int i;
375         while ((i = headers.indexOf(child)) != -1) {
376             headers.remove(i);
377         }
378     }
379 
380     /**
381      * we have to override this to enforce that SOAPHeader immediate 
382      * children are exclusively of type SOAPHeaderElement (otherwise
383      * we'll get mysterious ClassCastExceptions down the road... )
384      * 
385      * @param element child element
386      * @return soap element
387      * @throws SOAPException
388      */ 
389     public SOAPElement addChildElement(SOAPElement element) 
390       throws SOAPException
391     {
392       if (!(element instanceof javax.xml.soap.SOAPHeaderElement)) {
393         throw new SOAPException(Messages.getMessage("badSOAPHeader00"));
394       } 
395       return super.addChildElement(element);
396     }
397 
398     public SOAPElement addChildElement(Name name) throws SOAPException {
399         SOAPHeaderElement child = new SOAPHeaderElement(name.getURI(),
400                                                   name.getLocalName());
401         addChild(child);
402         child.setEnvelope(getEnvelope());
403         return child;
404     }
405 
406     public SOAPElement addChildElement(String localName) throws SOAPException {
407         // Inherit parent's namespace
408         SOAPHeaderElement child = new SOAPHeaderElement(getNamespaceURI(),
409                                                   localName);
410         addChild(child);
411         child.setEnvelope(getEnvelope());
412         return child;
413     }
414 
415     public SOAPElement addChildElement(String localName,
416                                        String prefix) throws SOAPException {
417         SOAPHeaderElement child = new SOAPHeaderElement(getNamespaceURI(prefix),
418                                                   localName);
419         addChild(child);
420         child.setEnvelope(getEnvelope());
421         return child;
422     }
423 
424     public SOAPElement addChildElement(String localName,
425                                        String prefix,
426                                        String uri) throws SOAPException {
427         SOAPHeaderElement child = new SOAPHeaderElement(uri,
428                                                   localName);
429         child.setPrefix(prefix);
430         child.addNamespaceDeclaration(prefix, uri);
431         addChild(child);
432         child.setEnvelope(getEnvelope());
433         return child;
434     }
435 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
187 0% of 0
243 0% of 0
314 0% of 0