@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); } }
/** Adds an "Access" element to the SOAP header */ public boolean handleRequest(MessageContext msgct) { if (msgct instanceof SOAPMessageContext) { SOAPMessageContext smsgct = (SOAPMessageContext) msgct; try { SOAPMessage msg = smsgct.getMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); SOAPHeader header = msg.getSOAPHeader(); if (header == null) header = envelope.addHeader(); // add an header if non exists SOAPHeaderElement accessElement = header.addHeaderElement( envelope.createName( "Access", "ns0", "http://www.ipd.uni-karlsruhe.de/jplag/types")); SOAPElement usernameelem = accessElement.addChildElement("username"); usernameelem.addTextNode(username); SOAPElement passwordelem = accessElement.addChildElement("password"); passwordelem.addTextNode(password); SOAPElement compatelem = accessElement.addChildElement("compatLevel"); compatelem.addTextNode(compatibilityLevel + ""); } catch (SOAPException x) { System.out.println("Unable to create access SOAP header!"); x.printStackTrace(); } } return true; }
public void writeTo(SOAPMessage saaj) throws SOAPException { SOAPHeader header = saaj.getSOAPHeader(); SOAPHeaderElement she = header.addHeaderElement(new QName(getNamespaceURI(), getLocalPart())); she.addChildElement(actionLocalName); she.addTextNode(action); if (soapAction != null) { she.addChildElement(soapActionLocalName); she.addTextNode(soapAction); } }
/** * @param authHeader * @param qnameAuthHeader * @param soapHeader * @return SOAPHeaderElement */ private SOAPHeaderElement ProcessAuthHeader( AuthHeader authHeader, QName qnameAuthHeader, SOAPHeader soapHeader) { SOAPHeaderElement headerElement; try { /* * Create the authentication header element */ headerElement = soapHeader.addHeaderElement(qnameAuthHeader); /* * Check if Identifier is specified */ if (authHeader.getIdentifier() != null) { /* * Create Identifier element */ QName qName = new QName( qnameAuthHeader.getNamespaceURI(), AuthHeader.STR_Identifier, qnameAuthHeader.getPrefix()); SOAPElement element = soapFactory.createElement(qName); element.addTextNode(authHeader.getIdentifier()); headerElement.addChildElement(element); } /* * Check if PassKey is specified */ if (authHeader.getPasskey() != null) { /* * Create PassKey element */ QName qName = new QName( qnameAuthHeader.getNamespaceURI(), AuthHeader.STR_Passkey, qnameAuthHeader.getPrefix()); SOAPElement element = soapFactory.createElement(qName); element.addTextNode(authHeader.getPasskey()); headerElement.addChildElement(element); } } catch (SOAPException ex) { headerElement = null; } return headerElement; }
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; }
/** * Clear the soap MustUnderstand. * * @param soapHeader The SOAP header. * @param soapHeaderElement The SOAP header element. */ private void clearMustUnderstand( final SOAPHeader soapHeader, final SOAPHeaderElement soapHeaderElement) throws SOAPException { final Name headerName = soapHeader.getElementName(); final SOAPFactory factory = SOAPFactory.newInstance(); final Name attributeName = factory.createName("mustUnderstand", headerName.getPrefix(), headerName.getURI()); soapHeaderElement.removeAttribute(attributeName); }
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(); } } }
@Override public boolean handleMessage(SOAPMessageContext context) { System.out.println("Client : handleMessage()......"); Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); // if this is a request, true for outbound messages, false for inbound if (isRequest) { try { SOAPMessage soapMsg = context.getMessage(); SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope(); SOAPHeader soapHeader = soapEnv.getHeader(); // if no header, add one if (soapHeader == null) { soapHeader = soapEnv.addHeader(); } // add a soap header, name as "mac address" QName qname = new QName("http://service.coupon.ronx.com/", "couponList"); SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname); soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT); // soapHeaderElement.addTextNode(mac); soapMsg.saveChanges(); // tracking soapMsg.writeTo(System.out); } catch (SOAPException | IOException e) { System.err.println(e); } } // continue other handler chain return true; }
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 testExamineHeaderElements() throws Exception { javax.xml.soap.SOAPMessage soapMessage = javax.xml.soap.MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(); javax.xml.soap.SOAPEnvelope soapEnv = soapMessage.getSOAPPart().getEnvelope(); javax.xml.soap.SOAPHeader header = soapEnv.getHeader(); SOAPHeaderElement soapHeaderElement = header.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI")); Iterator iterator = null; soapHeaderElement.setRole("role-URI"); iterator = header.examineHeaderElements("role1-URI"); int count = 0; while (iterator.hasNext()) { count++; iterator.next(); } assertEquals(0, count); }
/** * 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); } }
public SOAPMessage buildPullMessage( URI uri, String enumerationContext, String resourceUri, int maxElements, int timeout) throws SOAPException { MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope(); env.removeNamespaceDeclaration(env.getPrefix()); env.setPrefix("s"); env.addNamespaceDeclaration("a", NS_WS_ADDRESSING); env.addNamespaceDeclaration("w", NS_WS_MANAGEMENT); env.addNamespaceDeclaration("n", NS_WS_ENUMERATION); env.addNamespaceDeclaration("ev", NS_WS_EVENTING); QName mustUnderstand = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "mustUnderstand", "s"); SOAPHeader soapHeader = soapMessage.getSOAPHeader(); soapHeader.setPrefix("s"); SOAPHeaderElement toHeader = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "To", "a")); toHeader.setValue(uri.toString()); SOAPHeaderElement resourceURI = soapHeader.addHeaderElement(new QName(NS_WS_MANAGEMENT, "ResourceURI", "w")); resourceURI.addAttribute(mustUnderstand, "true"); resourceURI.setValue(resourceUri); SOAPHeaderElement replyTo = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "ReplyTo", "a")); SOAPElement addressElement = replyTo.addChildElement(new QName(NS_WS_ADDRESSING, "Address", "a")); addressElement.addAttribute(mustUnderstand, "true"); addressElement.addTextNode("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"); SOAPElement actionElement = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "Action", "a")); actionElement.addAttribute(mustUnderstand, "true"); actionElement.addTextNode(ENUMERATION_PULL); // MaxEnvelopeSize SOAPElement maxEnvelopeSize = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "MaxEnvelopeSize", "w")); maxEnvelopeSize.addAttribute(mustUnderstand, "true"); maxEnvelopeSize.addTextNode("51200"); // Message ID SOAPElement messageId = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "MessageID", "a")); messageId.addTextNode("uuid:" + UUID.randomUUID().toString()); // Locale // SOAPElement locale = soapHeader.addChildElement(new // QName(NS_WS_MANAGEMENT, "Locale", "w")); // locale.addAttribute(mustUnderstand, "false"); // locale.addAttribute(new QName("xml", "lang"), "en-US"); SOAPElement operationTimeout = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "OperationTimeout", "w")); operationTimeout.addTextNode(String.format("PT%d.000S", timeout)); SOAPBody soapBody = soapMessage.getSOAPBody(); soapBody.setPrefix("s"); SOAPElement pullElement = soapBody.addBodyElement(new QName(NS_WS_ENUMERATION, "Pull", "n")); SOAPElement enumerationContextElement = pullElement.addChildElement(new QName(NS_WS_ENUMERATION, "EnumerationContext", "n")); enumerationContextElement.setTextContent(enumerationContext); // SOAPElement maxTime = pullElement.addChildElement(new // QName(NS_WS_ENUMERATION, "MaxTime", "n")); // maxTime.setTextContent("PS60S"); SOAPElement maxElement = pullElement.addChildElement(new QName(NS_WS_ENUMERATION, "MaxElements", "n")); maxElement.setTextContent(String.valueOf(maxElements)); // SOAPElement maxCharacters = pullElement.addChildElement(new // QName(NS_WS_ENUMERATION, "MaxCharacters", "n")); // enumerationContextElement.setTextContent("250000"); return soapMessage; }
public SOAPMessage buildWQLQuery( URI uri, String namespace, String query, int maxElements, int timeout) throws SOAPException { MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope(); env.removeNamespaceDeclaration(env.getPrefix()); env.setPrefix("s"); env.addNamespaceDeclaration("a", NS_WS_ADDRESSING); env.addNamespaceDeclaration("w", NS_WS_MANAGEMENT); env.addNamespaceDeclaration("en", NS_WS_ENUMERATION); QName mustUnderstand = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "mustUnderstand", "s"); SOAPHeader soapHeader = soapMessage.getSOAPHeader(); soapHeader.setPrefix("s"); SOAPHeaderElement toHeader = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "To", "a")); toHeader.setValue(uri.toString()); SOAPHeaderElement resourceURI = soapHeader.addHeaderElement(new QName(NS_WS_MANAGEMENT, "ResourceURI", "w")); resourceURI.addAttribute(mustUnderstand, "true"); resourceURI.setValue(WSMAN_WMI_PREFIX + "/" + namespace + "/*"); SOAPHeaderElement replyTo = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "ReplyTo", "a")); SOAPElement addressElement = replyTo.addChildElement(new QName(NS_WS_ADDRESSING, "Address", "a")); addressElement.addAttribute(mustUnderstand, "true"); addressElement.addTextNode("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"); SOAPElement actionElement = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "Action", "a")); actionElement.addTextNode(ENUMERATION_ENUMERATE); // MaxEnvelopeSize SOAPElement maxEnvelopeSize = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "MaxEnvelopeSize", "w")); maxEnvelopeSize.addAttribute(mustUnderstand, "true"); maxEnvelopeSize.addTextNode("51200"); // Message ID SOAPElement messageId = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "MessageID", "a")); messageId.addTextNode("uuid:" + UUID.randomUUID().toString()); // Locale // SOAPElement locale = soapHeader.addChildElement(new // QName(NS_WS_MANAGEMENT, "Locale", "w")); // locale.addAttribute(mustUnderstand, "false"); // locale.addAttribute(new QName("xml", "lang"), "en-US"); SOAPElement operationTimeout = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "OperationTimeout", "w")); operationTimeout.addTextNode(String.format("PT%d.000S", timeout)); SOAPBody soapBody = soapMessage.getSOAPBody(); soapBody.setPrefix("s"); SOAPElement enumerateElement = soapBody.addBodyElement(new QName(NS_WS_ENUMERATION, "Enumerate", "en")); // Return in the same response enumerateElement.addChildElement(new QName(NS_WS_MANAGEMENT, "OptimizeEnumeration", "w")); SOAPElement maxElement = enumerateElement.addChildElement(new QName(NS_WS_MANAGEMENT, "MaxElements", "w")); maxElement.addTextNode(String.valueOf(maxElements)); SOAPElement filterElement = enumerateElement.addChildElement(new QName(NS_WS_MANAGEMENT, "Filter", "w")); filterElement.setAttribute("Dialect", DIALECT_WQL); filterElement.addTextNode(query); return soapMessage; }
public SOAPMessage buildEventSubscription( URI uri, String subscriptionQuery, int maxElements, int timeout) throws SOAPException { // EVENT_LOG_NOTIFICATION_QUERY = """ // SELECT * FROM __InstanceCreationEvent // WHERE TargetInstance ISA 'Win32_NTLogEvent' // AND TargetInstance.EventType <= %d // """ MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope(); env.removeNamespaceDeclaration(env.getPrefix()); env.setPrefix("s"); env.addNamespaceDeclaration("a", NS_WS_ADDRESSING); env.addNamespaceDeclaration("w", NS_WS_MANAGEMENT); env.addNamespaceDeclaration("en", NS_WS_ENUMERATION); env.addNamespaceDeclaration("ev", NS_WS_EVENTING); QName mustUnderstand = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "mustUnderstand", "s"); SOAPHeader soapHeader = soapMessage.getSOAPHeader(); soapHeader.setPrefix("s"); SOAPHeaderElement toHeader = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "To", "a")); toHeader.setValue(uri.toString()); SOAPHeaderElement resourceURI = soapHeader.addHeaderElement(new QName(NS_WS_MANAGEMENT, "ResourceURI", "w")); resourceURI.addAttribute(mustUnderstand, "true"); resourceURI.setValue(WSMAN_EVENTLOG_PREFIX); SOAPHeaderElement replyTo = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "ReplyTo", "a")); SOAPElement addressElement = replyTo.addChildElement(new QName(NS_WS_ADDRESSING, "Address", "a")); addressElement.addAttribute(mustUnderstand, "true"); addressElement.addTextNode("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"); SOAPElement actionElement = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "Action", "a")); actionElement.addTextNode(EVENTING_SUBSCRIBE); // MaxEnvelopeSize SOAPElement maxEnvelopeSize = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "MaxEnvelopeSize", "w")); maxEnvelopeSize.addAttribute(mustUnderstand, "true"); maxEnvelopeSize.addTextNode("51200"); // Message ID SOAPElement messageId = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "MessageID", "a")); messageId.addTextNode("uuid:" + UUID.randomUUID().toString()); // Locale // SOAPElement locale = soapHeader.addChildElement(new // QName(NS_WS_MANAGEMENT, "Locale", "w")); // locale.addAttribute(mustUnderstand, "false"); // locale.addAttribute(new QName("xml", "lang"), "en-US"); SOAPElement operationTimeout = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "OperationTimeout", "w")); operationTimeout.addTextNode(String.format("PT%d.000S", timeout)); SOAPBody soapBody = soapMessage.getSOAPBody(); soapBody.setPrefix("s"); SOAPElement enumerateElement = soapBody.addBodyElement(new QName(NS_WS_EVENTING, "Subscribe", "ev")); SOAPElement deliveryElement = enumerateElement.addChildElement(new QName(NS_WS_EVENTING, "Delivery", "ev")); deliveryElement.setAttribute("Mode", "http://schemas.dmtf.org/wbem/wsman/1/wsman/Pull"); SOAPElement expiresElement = enumerateElement.addChildElement(new QName(NS_WS_EVENTING, "Expires", "ev")); // EXPIRE in 60 seconds - don't do this normally! expiresElement.addTextNode(String.format("PT%dS", new Date().getTime() + 500)); // "PT60S"); // <ev:Expires>[xs:dateTime | xs:duration]</ev:Expires> SOAPElement filterElement = enumerateElement.addChildElement(new QName(NS_WS_MANAGEMENT, "Filter", "w")); filterElement.setAttribute("Dialect", DIALECT_EVENTQUERY); // Need to convert the XML snippet specified in the subscriptionQuery try { filterElement.addChildElement(new Util().xmlToSOAPElement(subscriptionQuery)); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Tell them to send bookmarks so we can catch up later if we get // disconnected enumerateElement.addChildElement(new QName(NS_WS_MANAGEMENT, "SendBookmarks", "w")); return soapMessage; }
public SOAPMessage buildUnsubscribeMessage( URI uri, String enumerationContext, String subscriptionId, int maxElements, int timeout) throws SOAPException { MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope(); env.removeNamespaceDeclaration(env.getPrefix()); env.setPrefix("s"); env.addNamespaceDeclaration("a", NS_WS_ADDRESSING); env.addNamespaceDeclaration("w", NS_WS_MANAGEMENT); env.addNamespaceDeclaration("en", NS_WS_ENUMERATION); env.addNamespaceDeclaration("ev", NS_WS_EVENTING); QName mustUnderstand = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "mustUnderstand", "s"); SOAPHeader soapHeader = soapMessage.getSOAPHeader(); soapHeader.setPrefix("s"); SOAPHeaderElement toHeader = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "To", "a")); toHeader.setValue(uri.toString()); SOAPHeaderElement resourceURI = soapHeader.addHeaderElement(new QName(NS_WS_MANAGEMENT, "ResourceURI", "w")); resourceURI.addAttribute(mustUnderstand, "true"); resourceURI.setValue(WSMAN_EVENTLOG_PREFIX); SOAPHeaderElement replyTo = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "ReplyTo", "a")); SOAPElement addressElement = replyTo.addChildElement(new QName(NS_WS_ADDRESSING, "Address", "a")); addressElement.addAttribute(mustUnderstand, "true"); addressElement.addTextNode("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"); SOAPElement actionElement = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "Action", "a")); actionElement.addTextNode(EVENTING_UNSUBSCRIBE); // MaxEnvelopeSize SOAPElement maxEnvelopeSize = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "MaxEnvelopeSize", "w")); maxEnvelopeSize.addAttribute(mustUnderstand, "true"); maxEnvelopeSize.addTextNode("51200"); // Message ID SOAPElement messageId = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "MessageID", "a")); messageId.addTextNode("uuid:" + UUID.randomUUID().toString()); SOAPElement operationTimeout = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "OperationTimeout", "w")); operationTimeout.addTextNode(String.format("PT%d.000S", timeout)); SOAPElement hsubId = soapHeader.addChildElement(new QName(NS_WS_EVENTING, "Identifier", "ev")); hsubId.addTextNode(subscriptionId); SOAPBody soapBody = soapMessage.getSOAPBody(); soapBody.setPrefix("s"); soapBody.addChildElement(new QName(NS_WS_EVENTING, "Unsubscribe", "ev")); return soapMessage; }
public SOAPMessage buildGetMessage( URI uri, String namespace, String className, Map<String, String> selectors, int maxElements, int timeout) throws TransformerException, SOAPException, IOException { MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope(); env.removeNamespaceDeclaration(env.getPrefix()); env.setPrefix("s"); env.addNamespaceDeclaration("a", NS_WS_ADDRESSING); env.addNamespaceDeclaration("w", NS_WS_MANAGEMENT); QName mustUnderstand = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "mustUnderstand", "s"); SOAPHeader soapHeader = soapMessage.getSOAPHeader(); soapHeader.setPrefix("s"); SOAPHeaderElement toHeader = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "To", "a")); toHeader.setValue(uri.toString()); SOAPHeaderElement resourceURI = soapHeader.addHeaderElement(new QName(NS_WS_MANAGEMENT, "ResourceURI", "w")); resourceURI.addAttribute(mustUnderstand, "true"); resourceURI.setValue(wmiClassUri("root/cimv2", className)); SOAPHeaderElement replyTo = soapHeader.addHeaderElement(new QName(NS_WS_ADDRESSING, "ReplyTo", "a")); SOAPElement addressElement = replyTo.addChildElement(new QName(NS_WS_ADDRESSING, "Address", "a")); addressElement.addAttribute(mustUnderstand, "true"); addressElement.addTextNode("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"); SOAPElement actionElement = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "Action", "a")); actionElement.addTextNode(TRANSFER_GET); // MaxEnvelopeSize // SOAPElement maxEnvelopeSize = soapHeader.addChildElement(new // QName(NS_WS_MANAGEMENT, "MaxEnvelopeSize", "w")); // maxEnvelopeSize.addAttribute(mustUnderstand, "true"); // maxEnvelopeSize.addTextNode("153600"); // Message ID SOAPElement messageId = soapHeader.addChildElement(new QName(NS_WS_ADDRESSING, "MessageID", "a")); messageId.addTextNode("uuid:" + UUID.randomUUID().toString()); // Locale // SOAPElement locale = soapHeader.addChildElement(new // QName(NS_WS_MANAGEMENT, "Locale", "w")); // locale.addAttribute(mustUnderstand, "false"); // locale.addAttribute(new QName("xml", "lang"), "en-US"); SOAPElement selectorSet = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "SelectorSet", "w")); for (String key : selectors.keySet()) { SOAPElement selector = selectorSet.addChildElement(new QName(NS_WS_MANAGEMENT, "Selector", "w")); selector.setAttribute("Name", key); selector.addTextNode(selectors.get(key)); } // <w:OperationTimeout>PT60.000S</w:OperationTimeout> SOAPElement operationTimeout = soapHeader.addChildElement(new QName(NS_WS_MANAGEMENT, "OperationTimeout", "w")); operationTimeout.addTextNode(String.format("PT%d.000S", timeout)); // SOAPElement fragmentTransfer = soapHeader.addChildElement(new // QName(NS_WS_MANAGEMENT, "FragmentTransfer", "w")); // fragmentTransfer.addAttribute(mustUnderstand, "true"); // fragmentTransfer.addTextNode("FreeSpace"); SOAPBody soapBody = soapMessage.getSOAPBody(); soapBody.setPrefix("s"); return soapMessage; }
public boolean handleMessage(SOAPMessageContext smc) { Boolean outbound = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outbound) { // outbound message // *** #8 *** // get token from response context String propertyValue = (String) smc.get(RESPONSE_PROPERTY); System.out.printf("%s received '%s'%n", CLASS_NAME, propertyValue); // put token in response SOAP header try { // get SOAP envelope SOAPMessage msg = smc.getMessage(); SOAPPart sp = msg.getSOAPPart(); SOAPEnvelope se = sp.getEnvelope(); // add header SOAPHeader sh = se.getHeader(); if (sh == null) sh = se.addHeader(); // add header element (name, namespace prefix, namespace) Name name = se.createName(RESPONSE_HEADER, "e", RESPONSE_NS); SOAPHeaderElement element = sh.addHeaderElement(name); // *** #9 *** // add header element value String newValue = propertyValue + "," + TOKEN; element.addTextNode(newValue); System.out.printf("%s put token '%s' on response message header%n", CLASS_NAME, TOKEN); } catch (SOAPException e) { System.out.printf("Failed to add SOAP header because of %s%n", e); } } else { // inbound message // get token from request SOAP header try { // get SOAP envelope header SOAPMessage msg = smc.getMessage(); SOAPPart sp = msg.getSOAPPart(); SOAPEnvelope se = sp.getEnvelope(); SOAPHeader sh = se.getHeader(); // check header if (sh == null) { System.out.println("Header not found."); return true; } // get Ticket header element Name nameTicket = se.createName(REQUEST_TICKET_HEADER, "e", REQUEST_NS); Iterator it = sh.getChildElements(nameTicket); // check header element if (!it.hasNext()) { System.out.printf("Header element %s not found.%n", REQUEST_TICKET_HEADER); return true; } SOAPElement elementTicket = (SOAPElement) it.next(); // *** #4 *** // get header element value String headerTicketValue = elementTicket.getValue(); System.out.printf("%s got '%s'%n", CLASS_NAME, headerTicketValue); // *** #5 *** // put Ticket token in request context System.out.printf("%s put token '%s' on request context%n", CLASS_NAME, headerTicketValue); smc.put(REQUEST_TICKET_PROPERTY, headerTicketValue); // set property scope to application so that server class can access property smc.setScope(REQUEST_TICKET_PROPERTY, Scope.APPLICATION); // get Author header element Name nameAuthor = se.createName(REQUEST_AUTHOR_HEADER, "e", REQUEST_NS); Iterator itAuthor = sh.getChildElements(nameAuthor); // check header element if (!itAuthor.hasNext()) { System.out.printf("Header element %s not found.%n", REQUEST_AUTHOR_HEADER); return true; } SOAPElement elementAuthor = (SOAPElement) itAuthor.next(); // *** #4 *** // get Author header element value String headerAuthorValue = elementAuthor.getValue(); System.out.printf("%s got '%s'%n", CLASS_NAME, headerAuthorValue); // *** #5 *** // put Author token in request context System.out.printf("%s put token '%s' on request context%n", CLASS_NAME, headerAuthorValue); smc.put(REQUEST_AUTHOR_PROPERTY, headerAuthorValue); // set property scope to application so that server class can access property smc.setScope(REQUEST_AUTHOR_PROPERTY, Scope.APPLICATION); // MAC // get MAC header element Name nameMac = se.createName(REQUEST_MAC_HEADER, "e", REQUEST_NS); Iterator itMac = sh.getChildElements(nameAuthor); // check header element if (!itMac.hasNext()) { System.out.printf("Header element %s not found.%n", REQUEST_MAC_HEADER); return true; } SOAPElement elementMac = (SOAPElement) itMac.next(); // *** #4 *** // get MAC header element value String headerMacValue = elementMac.getTextContent(); System.out.printf("%s got '%s'%n", CLASS_NAME, headerMacValue); // *** #5 *** // put MAC token in request context System.out.printf("%s put token '%s' on request context%n", CLASS_NAME, headerMacValue); smc.put(REQUEST_MAC_PROPERTY, headerMacValue); // set property scope to application so that server class can access property smc.setScope(REQUEST_MAC_PROPERTY, Scope.APPLICATION); String bodyValue = se.getBody().getTextContent(); // *** #5 *** // put Body token in request context System.out.printf("%s put token '%s' on request context%n", CLASS_NAME, bodyValue); smc.put(REQUEST_BODY_PROPERTY, bodyValue); // set property scope to application so that server class can access property smc.setScope(REQUEST_BODY_PROPERTY, Scope.APPLICATION); msg.writeTo(System.out); } catch (SOAPException e) { System.out.printf("Failed to get SOAP header because of %s%n", e); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return true; }
@Validated @Test public void testAddHeaderElements() 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(); try { header.addChildElement("ebxmlms1"); } catch (Exception e) { assertTrue(e instanceof SOAPException); } assertTrue( header.addChildElement("ebxmlms1", "ns-prefix", "http://test.apache.org") instanceof SOAPHeaderElement); ((SOAPHeaderElement) header.getFirstChild()).addTextNode("test add"); assertTrue( header.addHeaderElement( soapEnv.createName("ebxmlms2", "ns-prefix", "http://test2.apache.org")) != null); assertTrue( header.addHeaderElement( new PrefixedQName("http://test3.apache.org", "ebxmlms3", "ns-prefix")) != null); SOAPHeaderElement firstChild = (SOAPHeaderElement) header.getFirstChild(); assertEquals("ebxmlms1", firstChild.getLocalName()); assertEquals("ns-prefix", firstChild.getPrefix()); assertEquals("http://test.apache.org", firstChild.getNamespaceURI()); SOAPHeaderElement secondChild = (SOAPHeaderElement) firstChild.getNextSibling(); assertEquals("ebxmlms2", secondChild.getLocalName()); assertEquals("ns-prefix", secondChild.getPrefix()); assertEquals("http://test2.apache.org", secondChild.getNamespaceURI()); SOAPHeaderElement lastChild = (SOAPHeaderElement) header.getLastChild(); assertEquals("ebxmlms3", lastChild.getLocalName()); assertEquals("ns-prefix", lastChild.getPrefix()); assertEquals("http://test3.apache.org", lastChild.getNamespaceURI()); SOAPHeaderElement fourthChild = (SOAPHeaderElement) lastChild.getPreviousSibling(); assertEquals("ebxmlms2", fourthChild.getLocalName()); assertEquals("ns-prefix", fourthChild.getPrefix()); assertEquals("http://test2.apache.org", fourthChild.getNamespaceURI()); Iterator it = header.getChildElements(); int numOfHeaderElements = 0; while (it.hasNext()) { Object o = it.next(); assertTrue(o instanceof SOAPHeaderElement); SOAPHeaderElement el = (SOAPHeaderElement) o; String lName = el.getLocalName(); assertTrue(lName.equals("ebxmlms" + ++numOfHeaderElements)); } assertEquals(3, numOfHeaderElements); }
@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); } }