Search in sources :

Example 26 with SOAPBody

use of jakarta.xml.soap.SOAPBody in project tomee by apache.

the class Increment method handleMessage.

public boolean handleMessage(SOAPMessageContext mc) {
    try {
        final SOAPMessage message = mc.getMessage();
        final SOAPBody body = message.getSOAPBody();
        final String localName = body.getFirstChild().getLocalName();
        if ("sumResponse".equals(localName) || "multiplyResponse".equals(localName)) {
            final Node responseNode = body.getFirstChild();
            final Node returnNode = responseNode.getFirstChild();
            final Node intNode = returnNode.getFirstChild();
            final int value = new Integer(intNode.getNodeValue());
            intNode.setNodeValue(Integer.toString(value + 1));
        }
        return true;
    } catch (SOAPException e) {
        return false;
    }
}
Also used : SOAPBody(jakarta.xml.soap.SOAPBody) Node(org.w3c.dom.Node) SOAPException(jakarta.xml.soap.SOAPException) SOAPMessage(jakarta.xml.soap.SOAPMessage)

Example 27 with SOAPBody

use of jakarta.xml.soap.SOAPBody in project openmq by eclipse-ee4j.

the class SendSOAPMessage method sendMessage.

/**
 * send a simple soap message with JAXM API.
 */
public void sendMessage(String url) {
    try {
        /**
         * Construct a default SOAP message factory.
         */
        MessageFactory mf = MessageFactory.newInstance();
        /**
         * Create a SOAP message object.
         */
        SOAPMessage soapMessage = mf.createMessage();
        /**
         * Get SOAP part.
         */
        SOAPPart soapPart = soapMessage.getSOAPPart();
        /**
         * Get SOAP envelope.
         */
        SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
        /**
         * Get SOAP body.
         */
        SOAPBody soapBody = soapEnvelope.getBody();
        /**
         * Add child element with the specified name.
         */
        SOAPElement element = soapBody.addChildElement("HelloWorld");
        /**
         * Add text message
         */
        element.addTextNode("Welcome to SunOne Web Services!");
        soapMessage.saveChanges();
        /**
         * Construct a default SOAP connection factory.
         */
        SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
        /**
         * Get SOAP connection.
         */
        SOAPConnection soapConnection = connectionFactory.createConnection();
        /**
         * Send SOAP message.
         */
        SOAPMessage resp = soapConnection.call(soapMessage, url);
        /**
         * Print response to the std output.
         */
        resp.writeTo(System.out);
        /**
         * close the connection
         */
        soapConnection.close();
    } catch (java.io.IOException ioe) {
        ioe.printStackTrace();
    } catch (SOAPException soape) {
        soape.printStackTrace();
    }
}
Also used : SOAPConnectionFactory(jakarta.xml.soap.SOAPConnectionFactory) SOAPBody(jakarta.xml.soap.SOAPBody) MessageFactory(jakarta.xml.soap.MessageFactory) SOAPException(jakarta.xml.soap.SOAPException) SOAPPart(jakarta.xml.soap.SOAPPart) SOAPElement(jakarta.xml.soap.SOAPElement) SOAPConnection(jakarta.xml.soap.SOAPConnection) SOAPEnvelope(jakarta.xml.soap.SOAPEnvelope) SOAPMessage(jakarta.xml.soap.SOAPMessage)

Example 28 with SOAPBody

use of jakarta.xml.soap.SOAPBody in project openmq by eclipse-ee4j.

the class SendSOAPMessageWithJMS method send.

/**
 * Send SOAP message with JMS API.
 */
public void send() throws Exception {
    /**
     * Construct a default SOAP message factory.
     */
    MessageFactory mf = MessageFactory.newInstance();
    /**
     * Create a SOAP message object.
     */
    SOAPMessage soapMessage = mf.createMessage();
    /**
     * Get SOAP part.
     */
    SOAPPart soapPart = soapMessage.getSOAPPart();
    /**
     * Get SOAP envelope.
     */
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
    /**
     * Get SOAP body.
     */
    SOAPBody soapBody = soapEnvelope.getBody();
    /**
     * Create a name object. with name space http://www.sun.com/imq.
     */
    Name name = soapEnvelope.createName("HelloWorld", "hw", "http://www.sun.com/imq");
    /**
     * Add child element with the above name.
     */
    SOAPElement element = soapBody.addChildElement(name);
    /**
     * Add another child element.
     */
    element.addTextNode("Welcome to SunOne Web Services.");
    /**
     * Create an atachment with activation API.
     */
    URL url = new URL("https://projects.eclipse.org/projects/ee4j.openmq/contact");
    DataHandler dh = new DataHandler(url);
    AttachmentPart ap = soapMessage.createAttachmentPart(dh);
    /**
     * set content type/ID.
     */
    ap.setContentType("text/html");
    ap.setContentId("cid-001");
    /**
     *  add the attachment to the SOAP message.
     */
    soapMessage.addAttachmentPart(ap);
    soapMessage.saveChanges();
    /**
     * Convert SOAP to JMS message.
     */
    Message message = MessageTransformer.SOAPMessageIntoJMSMessage(soapMessage, session);
    /**
     * publish JMS message.
     */
    msgProducer.send(message);
}
Also used : SOAPBody(jakarta.xml.soap.SOAPBody) MessageFactory(jakarta.xml.soap.MessageFactory) Message(jakarta.jms.Message) SOAPMessage(jakarta.xml.soap.SOAPMessage) SOAPPart(jakarta.xml.soap.SOAPPart) SOAPElement(jakarta.xml.soap.SOAPElement) AttachmentPart(jakarta.xml.soap.AttachmentPart) SOAPEnvelope(jakarta.xml.soap.SOAPEnvelope) DataHandler(jakarta.activation.DataHandler) SOAPMessage(jakarta.xml.soap.SOAPMessage) URL(java.net.URL) Name(jakarta.xml.soap.Name)

Example 29 with SOAPBody

use of jakarta.xml.soap.SOAPBody in project openmq by eclipse-ee4j.

the class UMSService method createSOAPFaultMessage.

/**
 * Create a soap fault message and set its error code and error string as specified in the parameter.
 */
public static SOAPMessage createSOAPFaultMessage(Throwable t, String faultCode, String faultString) {
    SOAPMessage soapFault = null;
    try {
        MessageFactory mf = MessageFactory.newInstance();
        soapFault = mf.createMessage();
        SOAPEnvelope env = soapFault.getSOAPPart().getEnvelope();
        SOAPBody body = env.getBody();
        SOAPFault faultElement = body.addFault();
        String soapNs = env.getElementName().getPrefix();
        String fcode = soapNs + ":" + faultCode;
        faultElement.setFaultCode(fcode);
        faultElement.setFaultString(faultString);
        Detail detail = faultElement.getDetail();
        if (detail == null) {
            detail = faultElement.addDetail();
        }
        Name stname = MessageUtil.createJMSName("StackTrace");
        SOAPElement entryEle = detail.addDetailEntry(stname);
        // get stack trace
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        t.printStackTrace(ps);
        ps.close();
        String trace = baos.toString("utf8");
        entryEle.setValue(trace);
        soapFault.saveChanges();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return soapFault;
}
Also used : PrintStream(java.io.PrintStream) SOAPBody(jakarta.xml.soap.SOAPBody) MessageFactory(jakarta.xml.soap.MessageFactory) SOAPElement(jakarta.xml.soap.SOAPElement) SOAPFault(jakarta.xml.soap.SOAPFault) SOAPEnvelope(jakarta.xml.soap.SOAPEnvelope) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPMessage(jakarta.xml.soap.SOAPMessage) Detail(jakarta.xml.soap.Detail) SOAPException(jakarta.xml.soap.SOAPException) Name(jakarta.xml.soap.Name)

Example 30 with SOAPBody

use of jakarta.xml.soap.SOAPBody in project eclipselink by eclipse-ee4j.

the class SOAPResponseWriter method generateResponse.

public SOAPMessage generateResponse(Operation op, boolean useSOAP12, Object result) throws SOAPException {
    MessageFactory messageFactory = null;
    if (useSOAP12) {
        messageFactory = MessageFactory.newInstance(SOAP_1_2_PROTOCOL);
    } else {
        messageFactory = MessageFactory.newInstance();
    }
    SOAPMessage message = messageFactory.createMessage();
    message.getSOAPPart().getEnvelope().addNamespaceDeclaration(SCHEMA_PREFIX, W3C_XML_SCHEMA_NS_URI);
    message.getSOAPPart().getEnvelope().addNamespaceDeclaration(SCHEMA_INSTANCE_PREFIX, W3C_XML_SCHEMA_INSTANCE_NS_URI);
    SOAPBody body = message.getSOAPPart().getEnvelope().getBody();
    XMLDescriptor descriptor = resultDescriptors.get(op.getName());
    SOAPResponse response = null;
    try {
        @SuppressWarnings({ "unchecked" }) Class<? extends SOAPResponse> cls = descriptor.getJavaClass();
        response = cls.getConstructor().newInstance();
    } catch (ReflectiveOperationException ie) {
        throw new SOAPException(ie);
    }
    response.setResult(result);
    SOAPAttachmentHandler attachmentHandler = new SOAPAttachmentHandler();
    XMLMarshaller marshaller = dbwsAdapter.getXMLContext().createMarshaller();
    marshaller.setAttachmentMarshaller(attachmentHandler);
    marshaller.marshal(response, body);
    if (attachmentHandler.hasAttachments()) {
        // add attachments to message
        for (String id : attachmentHandler.getAttachments().keySet()) {
            DataHandler attachment = attachmentHandler.getAttachments().get(id);
            AttachmentPart part = message.createAttachmentPart(attachment);
            part.setContentType(attachment.getContentType());
            String contentId = "<" + id.substring(4) + ">";
            part.setContentId(contentId);
            part.setMimeHeader("Content-Transfer-Encoding", "binary");
            message.addAttachmentPart(part);
        }
    }
    message.saveChanges();
    return message;
}
Also used : MessageFactory(jakarta.xml.soap.MessageFactory) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) AttachmentPart(jakarta.xml.soap.AttachmentPart) DataHandler(jakarta.activation.DataHandler) SOAPMessage(jakarta.xml.soap.SOAPMessage) SOAPBody(jakarta.xml.soap.SOAPBody) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) SOAPException(jakarta.xml.soap.SOAPException)

Aggregations

SOAPBody (jakarta.xml.soap.SOAPBody)52 SOAPMessage (jakarta.xml.soap.SOAPMessage)46 SOAPException (jakarta.xml.soap.SOAPException)27 Node (org.w3c.dom.Node)22 QName (javax.xml.namespace.QName)17 WebServiceException (jakarta.xml.ws.WebServiceException)14 MessageFactory (jakarta.xml.soap.MessageFactory)11 SOAPElement (jakarta.xml.soap.SOAPElement)9 NodeList (org.w3c.dom.NodeList)9 Iterator (java.util.Iterator)8 DataHandler (jakarta.activation.DataHandler)6 SOAPEnvelope (jakarta.xml.soap.SOAPEnvelope)6 Service (jakarta.xml.ws.Service)6 StringReader (java.io.StringReader)6 Document (org.w3c.dom.Document)6 SOAPFault (jakarta.xml.soap.SOAPFault)5 Test (org.junit.Test)5 SOAPPart (jakarta.xml.soap.SOAPPart)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Map (java.util.Map)4