1 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 ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.DOMException ; 32 import org.w3c.dom.Element ; 33 34 import javax.xml.namespace.QName ; 35 import javax.xml.soap.Name ; 36 import javax.xml.soap.SOAPElement ; 37 import javax.xml.soap.SOAPException ; 38 import java.util.ArrayList ; 39 import java.util.Enumeration ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Vector ; 43 44 45 50 public class SOAPHeader extends MessageElement 51 implements javax.xml.soap.SOAPHeader { 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 ex) { 65 log.fatal(Messages.getMessage("exception00"), ex); 67 } 68 } 69 70 public SOAPHeader(String namespace, String localPart, String prefix, 71 Attributes 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 parent) throws SOAPException { 78 if(parent == null) { 79 throw new IllegalArgumentException (Messages.getMessage("nullParent00")); 80 } 81 try { 82 SOAPEnvelope env = (SOAPEnvelope)parent; 84 super.setParentElement(env); 85 setEnvelope(env); 86 } catch (Throwable t) { 87 throw new SOAPException (t); 88 } 89 } 90 91 public javax.xml.soap.SOAPHeaderElement addHeaderElement(Name name) 92 throws SOAPException { 93 SOAPHeaderElement headerElement = new SOAPHeaderElement(name); 94 addChildElement(headerElement); 95 return headerElement; 96 } 97 98 private Vector findHeaderElements(String actor) { 99 ArrayList actors = new ArrayList (); 100 actors.add(actor); 101 return getHeadersByActor(actors); 102 } 103 104 public Iterator examineHeaderElements(String actor) { 105 return findHeaderElements(actor).iterator(); 106 } 107 108 public Iterator extractHeaderElements(String actor) { 109 Vector results = findHeaderElements(actor); 110 111 Iterator iterator = results.iterator(); 112 while (iterator.hasNext()) { 114 ((SOAPHeaderElement)iterator.next()).detachNode(); 115 } 116 117 return results.iterator(); 118 } 119 120 public Iterator examineMustUnderstandHeaderElements(String actor) { 121 if (actor == null) return null; 122 123 Vector result = new Vector (); 124 List 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 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 examineAllHeaderElements() { 140 return getChildElements(); 141 } 142 143 public Iterator extractAllHeaderElements() { 144 Vector result = new Vector (); 145 List 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 getHeaders() { 156 initializeChildren(); 157 return new Vector (getChildren()); 158 } 159 160 163 Vector getHeadersByActor(ArrayList actors) { 164 Vector results = new Vector (); 165 List headers = getChildren(); 166 if (headers == null) { 167 return results; 168 } 169 Iterator i = headers.iterator(); 170 SOAPConstants soapVer = getEnvelope().getSOAPConstants(); 171 boolean isSOAP12 = soapVer == SOAPConstants.SOAP12_CONSTANTS; 172 String nextActor = soapVer.getNextRoleURI(); 173 while (i.hasNext()) { 174 SOAPHeaderElement header = (SOAPHeaderElement)i.next(); 175 String actor = header.getActor(); 176 177 if (isSOAP12 && Constants.URI_SOAP12_NONE_ROLE.equals(actor)) { 179 continue; 180 } 181 182 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 ex) { 201 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 216 SOAPHeaderElement getHeaderByName(String namespace, 217 String localPart, 218 boolean accessAllHeaders) { 219 QName name = new QName (namespace, localPart); 220 SOAPHeaderElement header = (SOAPHeaderElement)getChildElement(name); 221 222 if (!accessAllHeaders) { 225 MessageContext mc = MessageContext.getCurrentContext(); 226 if (mc != null) { 227 if (header != null) { 228 String actor = header.getActor(); 229 230 String 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 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 261 Enumeration getHeadersByName(String namespace, 262 String localPart, 263 boolean accessAllHeaders) { 264 ArrayList actors = null; 265 boolean firstTime = false; 266 267 271 Vector v = new Vector (); 272 List headers = getChildren(); 273 if (headers == null) { 274 return v.elements(); 275 } 276 Iterator e = headers.iterator(); 277 SOAPHeaderElement header; 278 String 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 MessageContext mc = MessageContext.getCurrentContext(); 289 if (mc != null && mc.getAxisEngine() != null) { 290 actors = mc.getAxisEngine().getActorURIs(); 291 } 292 firstTime = false; 293 } 294 295 String 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 { 310 List 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 context.startElement(new QName (soapConstants.getEnvelopeURI(), 324 Constants.ELEM_HEADER), null); 325 Iterator enumeration = headers.iterator(); 326 while (enumeration.hasNext()) { 327 ((NodeImpl)enumeration.next()).output(context); 329 } 330 context.endElement(); 332 } 333 334 context.setPretty(oldPretty); 335 } 336 337 public void addChild(MessageElement element) throws SOAPException { 339 if (!(element instanceof SOAPHeaderElement)) { 340 throw new SOAPException (Messages.getMessage("badSOAPHeader00")); 341 } 342 element.setEnvelope(getEnvelope()); 343 super.addChild(element); 344 } 345 346 public SOAPElement addChildElement(SOAPElement element) 348 throws SOAPException { 349 if (!(element instanceof SOAPHeaderElement)) { 350 throw new SOAPException (Messages.getMessage("badSOAPHeader00")); 351 } 352 SOAPElement child = super.addChildElement(element); 353 setDirty(true); 354 return child; 355 } 356 357 public SOAPElement addChildElement(Name name) throws SOAPException { 358 SOAPHeaderElement child = new SOAPHeaderElement(name); 359 addChildElement(child); 360 return child; 361 } 362 363 public SOAPElement addChildElement(String localName) throws SOAPException { 364 SOAPHeaderElement child = new SOAPHeaderElement(getNamespaceURI(), 366 localName); 367 addChildElement(child); 368 return child; 369 } 370 371 public SOAPElement addChildElement(String localName, 372 String prefix) throws SOAPException { 373 SOAPHeaderElement child = 374 new SOAPHeaderElement(getNamespaceURI(prefix), localName); 375 child.setPrefix(prefix); 376 addChildElement(child); 377 return child; 378 } 379 380 public SOAPElement addChildElement(String localName, 381 String prefix, 382 String uri) throws SOAPException { 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 appendChild(Node newChild) throws DOMException { 391 SOAPHeaderElement headerElement = null; 392 if(newChild instanceof SOAPHeaderElement) 393 headerElement = (SOAPHeaderElement)newChild; 394 else 395 headerElement = new SOAPHeaderElement((Element )newChild); 396 try { 397 addChildElement(headerElement); 398 } catch (SOAPException e) { 399 throw new DOMException (DOMException.INVALID_STATE_ERR,e.toString()); 400 } 401 return headerElement; 402 } 403 } 404 | Popular Tags |