1 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 ; 29 30 import javax.xml.namespace.QName ; 31 import javax.xml.soap.Name ; 32 import javax.xml.soap.SOAPElement ; 33 import javax.xml.soap.SOAPException ; 34 import javax.xml.soap.SOAPHeaderElement ; 35 import java.util.ArrayList ; 36 import java.util.Enumeration ; 37 import java.util.Iterator ; 38 import java.util.Vector ; 39 40 41 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 ex) 64 { 65 log.fatal(Messages.getMessage("exception00"), ex); 67 } 68 } 69 70 public SOAPHeaderAxisImpl(String namespace, String localPart, String prefix, 71 Attributes 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 parent) throws SOAPException 79 { 80 81 if (parent == null) 82 throw new IllegalArgumentException (Messages.getMessage("nullParent00")); 83 84 try 85 { 86 SOAPEnvelopeAxisImpl env = (SOAPEnvelopeAxisImpl)parent; 87 super.setParentElement(env); 88 setEnvelope(env); 89 } 90 catch (Throwable t) 91 { 92 throw new SOAPException (t); 93 } 94 } 95 96 public SOAPHeaderElement addHeaderElement(Name name) 97 throws SOAPException 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 examineHeaderElements(String actor) 108 { 109 ArrayList results = new ArrayList (); 110 getHeaderElements(results, actor); 111 return results.iterator(); 112 } 113 114 public Iterator extractHeaderElements(String actor) 115 { 116 ArrayList results = new ArrayList (); 117 getHeaderElements(results, actor); 118 119 Iterator iterator = results.iterator(); 121 while (iterator.hasNext()) 122 { 123 removeChild((SOAPHeaderElementAxisImpl)iterator.next()); 124 } 125 126 return results.iterator(); 127 } 128 129 public Iterator examineMustUnderstandHeaderElements(String actor) 130 { 131 ArrayList results = new ArrayList (); 132 getHeaderElements(results, actor); 133 134 Iterator 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 examineAllHeaderElements() 146 { 147 return getChildren().iterator(); 148 } 149 150 public Iterator extractAllHeaderElements() 151 { 152 Vector result = new Vector (getChildren()); 153 removeChildren(); 154 return result.iterator(); 155 } 156 157 private void getHeaderElements(ArrayList results, String actor) 158 { 159 String nextActor = SOAPConstants.SOAP11_CONSTANTS.getNextRoleURI(); 160 161 Iterator it = getChildren().iterator(); 162 while (it.hasNext()) 163 { 164 SOAPHeaderElementAxisImpl she = (SOAPHeaderElementAxisImpl)it.next(); 165 String 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 ex) 184 { 185 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 202 SOAPHeaderElementAxisImpl getHeaderByName(String namespace, 203 String localPart, 204 boolean accessAllHeaders) 205 { 206 SOAPHeaderElementAxisImpl header = (SOAPHeaderElementAxisImpl)findElement(namespace, localPart); 207 208 if (!accessAllHeaders) 211 { 212 MessageContext mc = MessageContext.getCurrentContext(); 213 if (mc != null) 214 { 215 if (header != null) 216 { 217 String actor = header.getActor(); 218 219 String 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 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 namespace, String localPart) 243 { 244 if (hasChildNodes() == false) 245 return null; 246 247 QName qname = new QName (namespace, localPart); 248 Iterator 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 269 Enumeration getHeadersByName(String namespace, 270 String localPart, 271 boolean accessAllHeaders) 272 { 273 ArrayList actors = null; 274 boolean firstTime = false; 275 276 280 Vector v = new Vector (); 281 String nextActor = getEnvelope().getSOAPConstants().getNextRoleURI(); 282 283 Iterator 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 MessageContext mc = MessageContext.getCurrentContext(); 297 if (mc != null) 298 actors = mc.getAxisEngine().getActorURIs(); 299 300 firstTime = false; 301 } 302 303 String 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 319 { 320 boolean oldPretty = context.getPretty(); 321 context.setPretty(true); 322 323 if (hasChildNodes()) 324 { 325 context.startElement(new QName (soapConstants.getEnvelopeURI(), 327 Constants.ELEM_HEADER), null); 328 329 Iterator it = getChildren().iterator(); 330 while (it.hasNext()) 331 { 332 SOAPHeaderElementAxisImpl she = (SOAPHeaderElementAxisImpl)it.next(); 333 ((SOAPHeaderElementAxisImpl)she).output(context); 335 } 336 context.endElement(); 338 } 339 340 context.setPretty(oldPretty); 341 } 342 343 352 public SOAPElement addChildElement(SOAPElement element) 353 throws SOAPException 354 { 355 if (!(element instanceof javax.xml.soap.SOAPHeaderElement )) 356 { 357 throw new SOAPException (Messages.getMessage("badSOAPHeader00")); 358 } 359 return super.addChildElement(element); 360 } 361 362 public SOAPElement addChildElement(Name name) throws SOAPException 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 addChildElement(String localName) throws SOAPException 372 { 373 SOAPHeaderElementAxisImpl child = new SOAPHeaderElementAxisImpl(getNamespaceURI(), 375 localName); 376 addChild(child); 377 child.setEnvelope(getEnvelope()); 378 return child; 379 } 380 381 public SOAPElement addChildElement(String localName, 382 String prefix) throws SOAPException 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 addChildElement(String localName, 392 String prefix, 393 String uri) throws SOAPException 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 |