@Validated @Test public void testExamineHeader() { SOAPHeaderElement she = null; try { she1 = hdr.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI")); she1.setActor("actor-URI"); Iterator iterator = hdr.examineAllHeaderElements(); int cnt = 0; while (iterator.hasNext()) { cnt++; she = (SOAPHeaderElement) iterator.next(); if (!she.equals(she1)) { fail("SOAP Header Elements do not match"); } } if (cnt != 1) { fail("SOAPHeaderElement count mismatch: expected 1, received " + cnt); } iterator = hdr.examineAllHeaderElements(); if (!iterator.hasNext()) { fail("no elements in iterator - unexpected"); } } catch (Exception e) { fail("Unexpected Exception: " + e); } }
@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 void removeWSAHeader() throws SOAPOverUDPException { JAXBElement j = null; Unmarshaller u = SOAPOverUDPWsAddressingNamespaces.WS_ADDRESSING_2005_08.getUnmarshaller(); SOAPHeader header; try { header = soapMessage.getSOAPHeader(); } catch (SOAPException ex) { throw new SOAPOverUDPException("Unable to get SOAP header", ex); } // Loop through header and remove WSA elements for (Iterator<SOAPHeaderElement> i = header.examineAllHeaderElements(); i.hasNext(); ) { SOAPHeaderElement headerElement = i.next(); try { // Unmarshal the header element j = (JAXBElement) u.unmarshal(headerElement); } catch (UnmarshalException ex) { continue; // unknown element probably, just continue } catch (JAXBException ex) { throw new SOAPOverUDPException( "An unexpected error occured while unmarshalling header element " + headerElement.getTagName(), ex); } String tag = j.getName().getLocalPart(); if (j.getValue().getClass().equals(AttributedURIType.class)) { if (tag.equalsIgnoreCase("To") || tag.equalsIgnoreCase("Action") || tag.equalsIgnoreCase("MessageID")) headerElement.detachNode(); } else if (j.getValue().getClass().equals(RelatesToType.class)) { if (tag.equalsIgnoreCase("RelatesTo")) { headerElement.detachNode(); } } else if (j.getValue().getClass().equals(EndpointReferenceType.class)) { if (tag.equalsIgnoreCase("ReplyTo")) headerElement.detachNode(); } } }
/** * Find Base64 encoded certificate used to sign given message. No default constructor: Once * content has been created, remains unchanged for life of the instance. * * @param msg (received) SOAP message to parse * @exception JAXRException if any problem at all occurs, wrapping problems decoding content (from * Base64) and any caught CertificateException or SOAPException */ public ReceivedCertificate(SOAPMessage msg) throws JAXRException { // @wss:Id attribute value for <BinarySecurityToken/> element of interest final String tokenId = CanonicalConstants.CANONICAL_URI_SENDER_CERT; try { final Name binSecTokenName = SOAPFactory.newInstance().createName("BinarySecurityToken", "wsse", securityNS); SOAPHeader hdr = msg.getSOAPHeader(); Iterator hdrElemIter = hdr.examineAllHeaderElements(); while (hdrElemIter.hasNext()) { Object hdrElemObj = hdrElemIter.next(); if (hdrElemObj instanceof SOAPHeaderElement) { // found a SOAP header element of some type SOAPHeaderElement hdrElem = (SOAPHeaderElement) hdrElemObj; if ((hdrElem.getLocalName().equals("Security")) && (hdrElem.getNamespaceURI().equals(securityNS))) { // found a <wss:Security/> element // Name binSecTokenName = SOAPFactory.newInstance(). // createName("BinarySecurityToken", "wsse", securityNS); Iterator secTokensIter = hdrElem.getChildElements(binSecTokenName); while (secTokensIter.hasNext()) { Object binSecTokenObj = secTokensIter.next(); if (binSecTokenObj instanceof Element) { // found a <BinarySecurityToken/> element Element binSecTokenElem = (Element) binSecTokenObj; String _tokenId = binSecTokenElem.getAttributeNS(securityUtilityNS, "Id"); if (_tokenId.equals(tokenId)) { // found propery identified element if (null == cert) { // found first cert content InputStream is = null; String encodedData = binSecTokenElem.getFirstChild().getNodeValue(); try { try { is = new ByteArrayInputStream(encodedData.getBytes("UTF-8")); is = MimeUtility.decode(is, "base64"); } catch (Exception e) { throw new JAXRException( CommonResourceBundle.getInstance() .getString("message.UnableToDecodeData"), e); } CertificateFactory cf = CertificateFactory.getInstance("X.509"); cert = (X509Certificate) cf.generateCertificate(is); } finally { if (is != null) { try { is.close(); } catch (Exception e) { } } } } else { // found second cert content foundMultiple = true; break; } } } } } } } } catch (SOAPException e) { throw new JAXRException( CommonResourceBundle.getInstance().getString("message.CouldNotGetCertificate"), e); } catch (CertificateException e) { throw new JAXRException( CommonResourceBundle.getInstance().getString("message.CouldNotGetCertificate"), e); } }
@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); } }