private SOAPHeaderElement getHeaderElement( final SOAPHeader soapHeader, final String uri, final String name) throws SOAPException { if (soapHeader != null) { final Iterator headerIter = SOAPUtil.getChildElements(soapHeader); while (headerIter.hasNext()) { final SOAPHeaderElement current = (SOAPHeaderElement) headerIter.next(); final Name currentName = current.getElementName(); if ((currentName != null) && match(name, currentName.getLocalName()) && match(uri, currentName.getURI())) { return current; } } } return null; }
@Validated @Test public void testHeaders() { try { // Create message factory and SOAP factory MessageFactory messageFactory = MessageFactory.newInstance(); SOAPFactory soapFactory = SOAPFactory.newInstance(); // Create a message SOAPMessage message = messageFactory.createMessage(); // Get the SOAP header from the message and // add headers to it SOAPHeader header = message.getSOAPHeader(); String nameSpace = "ns"; String nameSpaceURI = "http://gizmos.com/NSURI"; Name order = soapFactory.createName("orderDesk", nameSpace, nameSpaceURI); SOAPHeaderElement orderHeader = header.addHeaderElement(order); orderHeader.setActor("http://gizmos.com/orders"); Name shipping = soapFactory.createName("shippingDesk", nameSpace, nameSpaceURI); SOAPHeaderElement shippingHeader = header.addHeaderElement(shipping); shippingHeader.setActor("http://gizmos.com/shipping"); Name confirmation = soapFactory.createName("confirmationDesk", nameSpace, nameSpaceURI); SOAPHeaderElement confirmationHeader = header.addHeaderElement(confirmation); confirmationHeader.setActor("http://gizmos.com/confirmations"); Name billing = soapFactory.createName("billingDesk", nameSpace, nameSpaceURI); SOAPHeaderElement billingHeader = header.addHeaderElement(billing); billingHeader.setActor("http://gizmos.com/billing"); // Add header with mustUnderstand attribute Name tName = soapFactory.createName("Transaction", "t", "http://gizmos.com/orders"); SOAPHeaderElement transaction = header.addHeaderElement(tName); transaction.setMustUnderstand(true); transaction.addTextNode("5"); // Get the SOAP body from the message but leave // it empty SOAPBody body = message.getSOAPBody(); message.saveChanges(); // Display the message that would be sent // System.out.println("\n----- Request Message ----\n"); // message.writeTo(System.out); // Look at the headers Iterator allHeaders = header.examineAllHeaderElements(); while (allHeaders.hasNext()) { SOAPHeaderElement headerElement = (SOAPHeaderElement) allHeaders.next(); Name headerName = headerElement.getElementName(); // System.out.println("\nHeader name is " + // headerName.getQualifiedName()); // System.out.println("Actor is " + headerElement.getActor()); // System.out.println("mustUnderstand is " + // headerElement.getMustUnderstand()); } } catch (Exception e) { fail("Enexpected Exception " + e); } }