@Validated @Test public void testExamineHeaderElements2() throws Exception { javax.xml.soap.SOAPMessage soapMessage = javax.xml.soap.MessageFactory.newInstance().createMessage(); javax.xml.soap.SOAPEnvelope soapEnv = soapMessage.getSOAPPart().getEnvelope(); javax.xml.soap.SOAPHeader header = soapEnv.getHeader(); SOAPHeaderElement soapHeaderElement = null; try { // Add some soap header elements SOAPElement se = header .addHeaderElement(envelope.createName("Header1", "prefix", "http://myuri")) .addTextNode("This is Header1"); soapHeaderElement = (SOAPHeaderElement) se; soapHeaderElement.setMustUnderstand(true); se = header .addHeaderElement(envelope.createName("Header2", "prefix", "http://myuri")) .addTextNode("This is Header2"); soapHeaderElement = (SOAPHeaderElement) se; soapHeaderElement.setMustUnderstand(false); se = header .addHeaderElement(envelope.createName("Header3", "prefix", "http://myuri")) .addTextNode("This is Header3"); soapHeaderElement = (SOAPHeaderElement) se; soapHeaderElement.setMustUnderstand(true); se = header .addHeaderElement(envelope.createName("Header4", "prefix", "http://myuri")) .addTextNode("This is Header4"); soapHeaderElement = (SOAPHeaderElement) se; soapHeaderElement.setMustUnderstand(false); Iterator iterator = header.examineAllHeaderElements(); // validating Iterator count .... should be 4"); int cnt = 0; while (iterator.hasNext()) { cnt++; soapHeaderElement = (SOAPHeaderElement) iterator.next(); } assertEquals(cnt, 4); iterator = header.examineHeaderElements(SOAPConstants.URI_SOAP_ACTOR_NEXT); cnt = 0; while (iterator.hasNext()) { cnt++; soapHeaderElement = (SOAPHeaderElement) iterator.next(); } assertEquals(cnt, 0); } catch (Exception e) { fail("Unexpected Exception: " + e); } }
private SOAPMessage actionToSoap(RpcInvokeAction action) throws Exception { useFirstElementPrefix = true; SOAPMessage message = messageFactory.createMessage(); actionData = WsRpcActionUtil.getWsRpcActionData(((SimpleRpcInvokeAction) action).getProperties()); SOAPEnvelope env = message.getSOAPPart().getEnvelope(); env.addNamespaceDeclaration(ACTION_PREFIX, actionData.getMethodInputNameSpace()); setFields(action.getFields(), env.getBody()); if (actionData.getSoapAction().length() > 0) { message.getMimeHeaders().addHeader("SOAPAction", actionData.getSoapAction()); } // Base auth headers from http://www.whitemesa.com/soapauth.html#S322 /* * all auth standarts: http://www.soapui.org/testing-dojo/best-practices/authentication.html */ if (props.getPassword() != null && props.getUserName() != null && !props.getUserName().trim().equals("")) { SOAPHeaderElement auth = message .getSOAPHeader() .addHeaderElement( new QName("http://soap-authentication.org/basic/2001/10/", "BasicAuth", "h")); auth.setMustUnderstand(true); auth.addChildElement("Name").setValue(props.getUserName()); auth.addChildElement("Password").setValue(props.getPassword()); } logMessage("Request message:", message); return message; }
@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); } }