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 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 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 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; }