Java Code Examples for javax.xml.soap.MessageFactory#createMessage()

The following examples show how to use javax.xml.soap.MessageFactory#createMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceDiscovery.java    From onvif with Apache License 2.0 6 votes vote down vote up
private static Collection<String> parseSoapResponseForUrls(byte[] data)
    throws SOAPException, IOException {
  // System.out.println(new String(data));
  final Collection<String> urls = new ArrayList<>();
  MessageFactory factory = MessageFactory.newInstance(WS_DISCOVERY_SOAP_VERSION);
  final MimeHeaders headers = new MimeHeaders();
  headers.addHeader("Content-type", WS_DISCOVERY_CONTENT_TYPE);
  SOAPMessage message = factory.createMessage(headers, new ByteArrayInputStream(data));
  SOAPBody body = message.getSOAPBody();
  for (Node node : getNodeMatching(body, ".*:XAddrs")) {
    if (node.getTextContent().length() > 0) {
      urls.addAll(Arrays.asList(node.getTextContent().split(" ")));
    }
  }
  return urls;
}
 
Example 2
Source File: XmlTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private SOAPMessage createMessage(MessageFactory mf) throws SOAPException, IOException {
    SOAPMessage msg = mf.createMessage();
    SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
    Name name = envelope.createName("hello", "ex", "http://example.com");
    envelope.getBody().addChildElement(name).addTextNode("THERE!");

    String s = "<root><hello>THERE!</hello></root>";

    AttachmentPart ap = msg.createAttachmentPart(
            new StreamSource(new ByteArrayInputStream(s.getBytes())),
            "text/xml"
    );
    msg.addAttachmentPart(ap);
    msg.saveChanges();

    return msg;
}
 
Example 3
Source File: MailTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
void addSoapAttachement() {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        AttachmentPart a = message.createAttachmentPart();
        a.setContentType("binary/octet-stream");
        message.addAttachmentPart(a);
    } catch (SOAPException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: ArcherAuthenticatingRestConnection.java    From FortifyBugTrackerUtility with MIT License 5 votes vote down vote up
public Long addValueToValuesList(Long valueListId, String value) {
	LOG.info("[Archer] Adding value '"+value+"' to value list id "+valueListId);
	// Adding items to value lists is not supported via REST API, so we need to revert to SOAP API
	// TODO Simplify this method?
	// TODO Make this method more fail-safe (like checking for the correct response element)?
	Long result = null;
	try {
		MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        SOAPPart soapPart = message.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        SOAPBody body = envelope.getBody();
        SOAPElement bodyElement = body.addChildElement(envelope.createName("CreateValuesListValue", "", "http://archer-tech.com/webservices/"));
        bodyElement.addChildElement("sessionToken").addTextNode(tokenProviderRest.getToken());
        bodyElement.addChildElement("valuesListId").addTextNode(valueListId+"");
        bodyElement.addChildElement("valuesListValueName").addTextNode(value);
        message.saveChanges();
 
        SOAPMessage response = executeRequest(HttpMethod.POST, 
        		getBaseResource().path("/ws/field.asmx")
        		.request()
        		.header("SOAPAction", "\"http://archer-tech.com/webservices/CreateValuesListValue\"")
        		.accept("text/xml"),
        		Entity.entity(message, "text/xml"), SOAPMessage.class);
        @SuppressWarnings("unchecked")
		Iterator<Object> it = response.getSOAPBody().getChildElements();
        while (it.hasNext()){
        	Object o = it.next();
        	if ( o instanceof SOAPElement ) {	
        		result = new Long(((SOAPElement)o).getTextContent());
        	}
          }
        System.out.println(response);
	} catch (SOAPException e) {
		throw new RuntimeException("Error executing SOAP request", e);
	}
	return result;
}
 
Example 5
Source File: ArcherBasicRestConnection.java    From FortifyBugTrackerUtility with MIT License 5 votes vote down vote up
public SOAPMessage readFrom(Class<SOAPMessage> soapEnvelopeClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> stringStringMultivaluedMap, InputStream inputStream) throws IOException, WebApplicationException {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        StreamSource messageSource = new StreamSource(inputStream);
        SOAPMessage message = messageFactory.createMessage();
        SOAPPart soapPart = message.getSOAPPart();
        soapPart.setContent(messageSource);
        return message;
    } catch (SOAPException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 6
Source File: MdwRpcWebServiceAdapter.java    From mdw with Apache License 2.0 5 votes vote down vote up
/**
 * Populate the SOAP request message.
 */
protected SOAPMessage createSoapRequest(Object requestObj) throws ActivityException {
    try {
        MessageFactory messageFactory = getSoapMessageFactory();
        SOAPMessage soapMessage = messageFactory.createMessage();
        Map<Name,String> soapReqHeaders = getSoapRequestHeaders();
        if (soapReqHeaders != null) {
            SOAPHeader header = soapMessage.getSOAPHeader();
            for (Name name : soapReqHeaders.keySet()) {
                header.addHeaderElement(name).setTextContent(soapReqHeaders.get(name));
            }
        }

        SOAPBody soapBody = soapMessage.getSOAPBody();

        Document requestDoc = null;
        if (requestObj instanceof String) {
            requestDoc = DomHelper.toDomDocument((String)requestObj);
        }
        else {
            Variable reqVar = getProcessDefinition().getVariable(getAttributeValue(REQUEST_VARIABLE));
            XmlDocumentTranslator docRefTrans = (XmlDocumentTranslator)getPackage().getTranslator(reqVar.getType());
            requestDoc = docRefTrans.toDomDocument(requestObj);
        }

        SOAPBodyElement bodyElem = soapBody.addBodyElement(getOperation());
        String requestLabel = getRequestLabelPartName();
        if (requestLabel != null) {
            SOAPElement serviceNameElem = bodyElem.addChildElement(requestLabel);
            serviceNameElem.addTextNode(getRequestLabel());
        }
        SOAPElement requestDetailsElem = bodyElem.addChildElement(getRequestPartName());
        requestDetailsElem.addTextNode("<![CDATA[" + DomHelper.toXml(requestDoc) + "]]>");

        return soapMessage;
    }
    catch (Exception ex) {
        throw new ActivityException(ex.getMessage(), ex);
    }
}
 
Example 7
Source File: MailTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void addSoapAttachement() {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        AttachmentPart a = message.createAttachmentPart();
        a.setContentType("binary/octet-stream");
        message.addAttachmentPart(a);
    } catch (SOAPException e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: XmlTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void test() throws Exception {

        File file = new File("message.xml");
        file.deleteOnExit();

        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage msg = createMessage(mf);

        // Save the soap message to file
        try (FileOutputStream sentFile = new FileOutputStream(file)) {
            msg.writeTo(sentFile);
        }

        // See if we get the image object back
        try (FileInputStream fin = new FileInputStream(file)) {
            SOAPMessage newMsg = mf.createMessage(msg.getMimeHeaders(), fin);

            newMsg.writeTo(new ByteArrayOutputStream());

            Iterator<?> i = newMsg.getAttachments();
            while (i.hasNext()) {
                AttachmentPart att = (AttachmentPart) i.next();
                Object obj = att.getContent();
                if (!(obj instanceof StreamSource)) {
                    fail("Got incorrect attachment type [" + obj.getClass() + "], " +
                         "expected [javax.xml.transform.stream.StreamSource]");
                }
            }
        }

    }
 
Example 9
Source File: MailTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
void addSoapAttachement() {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        AttachmentPart a = message.createAttachmentPart();
        a.setContentType("binary/octet-stream");
        message.addAttachmentPart(a);
    } catch (SOAPException e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: SaajEmptyNamespaceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static SOAPMessage createSoapMessage() throws SOAPException, UnsupportedEncodingException {
    String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                +"<SOAP-ENV:Body/></SOAP-ENV:Envelope>";
    MessageFactory mFactory = MessageFactory.newInstance();
    SOAPMessage msg = mFactory.createMessage();
    msg.getSOAPPart().setContent(new StreamSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
    return msg;
}
 
Example 11
Source File: MailTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
void addSoapAttachement() {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        AttachmentPart a = message.createAttachmentPart();
        a.setContentType("binary/octet-stream");
        message.addAttachmentPart(a);
    } catch (SOAPException e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: SaajEmptyNamespaceTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static SOAPMessage createSoapMessage() throws SOAPException, UnsupportedEncodingException {
    String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                +"<SOAP-ENV:Body/></SOAP-ENV:Envelope>";
    MessageFactory mFactory = MessageFactory.newInstance();
    SOAPMessage msg = mFactory.createMessage();
    msg.getSOAPPart().setContent(new StreamSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
    return msg;
}
 
Example 13
Source File: MailTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void addSoapAttachement() {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        AttachmentPart a = message.createAttachmentPart();
        a.setContentType("binary/octet-stream");
        message.addAttachmentPart(a);
    } catch (SOAPException e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: SaajEmptyNamespaceTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static SOAPMessage createSoapMessage() throws SOAPException, UnsupportedEncodingException {
    String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                +"<SOAP-ENV:Body/></SOAP-ENV:Envelope>";
    MessageFactory mFactory = MessageFactory.newInstance();
    SOAPMessage msg = mFactory.createMessage();
    msg.getSOAPPart().setContent(new StreamSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
    return msg;
}
 
Example 15
Source File: MailTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void addSoapAttachement() {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        AttachmentPart a = message.createAttachmentPart();
        a.setContentType("binary/octet-stream");
        message.addAttachmentPart(a);
    } catch (SOAPException e) {
        e.printStackTrace();
    }
}
 
Example 16
Source File: DocumentWebServiceAdapter.java    From mdw with Apache License 2.0 5 votes vote down vote up
/**
 * Create the SOAP request object based on the document variable value.
 */
protected SOAPMessage createSoapRequest(Object requestObj) throws ActivityException {
    try {
        MessageFactory messageFactory = getSoapMessageFactory();
        SOAPMessage soapMessage = messageFactory.createMessage();
        Map<Name,String> soapReqHeaders = getSoapRequestHeaders();
        if (soapReqHeaders != null) {
            SOAPHeader header = soapMessage.getSOAPHeader();
            for (Name name : soapReqHeaders.keySet()) {
                header.addHeaderElement(name).setTextContent(soapReqHeaders.get(name));
            }
        }

        SOAPBody soapBody = soapMessage.getSOAPBody();

        Document requestDoc = null;
        if (requestObj instanceof String) {
            requestDoc = DomHelper.toDomDocument((String)requestObj);
            soapBody.addDocument(requestDoc);
        }
        else {
            Variable reqVar = getProcessDefinition().getVariable(getAttributeValue(REQUEST_VARIABLE));
            XmlDocumentTranslator docRefTrans = (XmlDocumentTranslator)getPackage().getTranslator(reqVar.getType());
            requestDoc = docRefTrans.toDomDocument(requestObj);
            Document copiedDocument = DomHelper.copyDomDocument(requestDoc);
            soapBody.addDocument(copiedDocument);
        }

        return soapMessage;
    }
    catch (Exception ex) {
        throw new ActivityException(ex.getMessage(), ex);
    }
}
 
Example 17
Source File: SaajEmptyNamespaceTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static SOAPMessage createSoapMessage() throws SOAPException, UnsupportedEncodingException {
    String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                +"<SOAP-ENV:Body/></SOAP-ENV:Envelope>";
    MessageFactory mFactory = MessageFactory.newInstance();
    SOAPMessage msg = mFactory.createMessage();
    msg.getSOAPPart().setContent(new StreamSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
    return msg;
}
 
Example 18
Source File: SaajEmptyNamespaceTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static SOAPMessage createSoapMessage() throws SOAPException, UnsupportedEncodingException {
    String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                +"<SOAP-ENV:Body/></SOAP-ENV:Envelope>";
    MessageFactory mFactory = MessageFactory.newInstance();
    SOAPMessage msg = mFactory.createMessage();
    msg.getSOAPPart().setContent(new StreamSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
    return msg;
}
 
Example 19
Source File: SendMTConverterUtils.java    From opencps-v2 with GNU Affero General Public License v3.0 4 votes vote down vote up
private static SOAPMessage convertSendMTToXML(SendMT sendMT) {
	try {
		JAXBContext jaxbContext = JAXBContext.newInstance(SendMT.class);
		MessageFactory mf = MessageFactory.newInstance();
		SOAPMessage message = mf.createMessage();

		SOAPBody body = message.getSOAPBody();

		SOAPHeader soapheader = message.getSOAPHeader();
		soapheader.detachNode();

		Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
		// output pretty printed
		jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		//process marshaller
		jaxbMarshaller.marshal(sendMT, body);
		//jaxbMarshaller.marshal(sendMT, System.out);
		message.saveChanges();

		// Process convert SOAP-ENV to soapenv
		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();
		// SOAPHeader header = message.getSOAPHeader();
		SOAPBody bodyConvert = message.getSOAPBody();
		SOAPFault fault = bodyConvert.getFault();
		envelope.removeNamespaceDeclaration(envelope.getPrefix());
		envelope.addNamespaceDeclaration(Constants.PREFERRED_PREFIX, Constants.SOAP_ENV_NAMESPACE);
		envelope.addNamespaceDeclaration(Constants.PREFERRED_PREFIX_TEM, Constants.SOAP_ENV_NAMESPACE_TEM);
		envelope.setPrefix(Constants.PREFERRED_PREFIX);
		bodyConvert.setPrefix(Constants.PREFERRED_PREFIX);
		if (fault != null) {
			fault.setPrefix(Constants.PREFERRED_PREFIX);
		}
		message.saveChanges();

		return message;
	} catch (Exception e) {
		_log.error(e);
	}

	return null;
}
 
Example 20
Source File: JRXmlaQueryExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected SOAPMessage createQueryMessage()
{
	String queryStr = getQueryString();

	if (log.isDebugEnabled())
	{
		log.debug("MDX query: " + queryStr);
	}
	
	try
	{
		MessageFactory mf = MessageFactory.newInstance();
		SOAPMessage message = mf.createMessage();

		MimeHeaders mh = message.getMimeHeaders();
		mh.setHeader("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Execute\"");

		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();
		SOAPBody body = envelope.getBody();
		Name nEx = envelope.createName("Execute", "", XMLA_URI);

		SOAPElement eEx = body.addChildElement(nEx);

		// add the parameters

		// COMMAND parameter
		// <Command>
		// <Statement>queryStr</Statement>
		// </Command>
		Name nCom = envelope.createName("Command", "", XMLA_URI);
		SOAPElement eCommand = eEx.addChildElement(nCom);
		Name nSta = envelope.createName("Statement", "", XMLA_URI);
		SOAPElement eStatement = eCommand.addChildElement(nSta);
		eStatement.addTextNode(queryStr);

		// <Properties>
		// <PropertyList>
		// <DataSourceInfo>dataSource</DataSourceInfo>
		// <Catalog>catalog</Catalog>
		// <Format>Multidimensional</Format>
		// <AxisFormat>TupleFormat</AxisFormat>
		// </PropertyList>
		// </Properties>
		Map<String, String> paraList = new HashMap<String, String>();
		String datasource = (String) getParameterValue(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_DATASOURCE);
		paraList.put("DataSourceInfo", datasource);
		String catalog = (String) getParameterValue(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_CATALOG);
		paraList.put("Catalog", catalog);
		paraList.put("Format", "Multidimensional");
		paraList.put("AxisFormat", "TupleFormat");
		addParameterList(envelope, eEx, "Properties", "PropertyList", paraList);
		message.saveChanges();

		if (log.isDebugEnabled())
		{
			log.debug("XML/A query message: \n" + prettyPrintSOAP(message.getSOAPPart().getEnvelope()));
		}

		return message;
	}
	catch (SOAPException e)
	{
		throw new JRRuntimeException(e);
	}
}