Example usage for javax.xml.soap MessageFactory createMessage

List of usage examples for javax.xml.soap MessageFactory createMessage

Introduction

In this page you can find the example usage for javax.xml.soap MessageFactory createMessage.

Prototype

public abstract SOAPMessage createMessage() throws SOAPException;

Source Link

Document

Creates a new SOAPMessage object with the default SOAPPart , SOAPEnvelope , SOAPBody , and SOAPHeader objects.

Usage

From source file:org.apache.axis2.jaxws.marshaller.impl.alt.MethodMarshallerUtils.java

/**
 * Create a system exception/*from  ww w . j  a  v  a 2  s .  c om*/
 *
 * @param message
 * @return
 */
public static ProtocolException createSystemException(XMLFault xmlFault, Message message) {
    ProtocolException e = null;
    Protocol protocol = message.getProtocol();
    String text = xmlFault.getReason().getText();

    if (protocol == Protocol.soap11 || protocol == Protocol.soap12) {
        // Throw a SOAPFaultException
        if (log.isDebugEnabled()) {
            log.debug("Constructing SOAPFaultException for " + text);
        }
        String protocolNS = (protocol == Protocol.soap11) ? SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE
                : SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE;
        try {
            // The following set of instructions is used to avoid 
            // some unimplemented methods in the Axis2 SAAJ implementation
            javax.xml.soap.MessageFactory mf = SAAJFactory.createMessageFactory(protocolNS);
            SOAPBody body = mf.createMessage().getSOAPBody();
            SOAPFault soapFault = XMLFaultUtils.createSAAJFault(xmlFault, body);
            e = new SOAPFaultException(soapFault);
        } catch (Exception ex) {
            // Exception occurred during exception processing.
            // TODO Probably should do something better here
            if (log.isDebugEnabled()) {
                log.debug("Exception occurred during fault processing:", ex);
            }
            e = ExceptionFactory.makeProtocolException(text, null);
        }
    } else if (protocol == Protocol.rest) {
        if (log.isDebugEnabled()) {
            log.debug("Constructing ProtocolException for " + text);
        }
        // TODO Is there an explicit exception for REST
        e = ExceptionFactory.makeProtocolException(text, null);
    } else if (protocol == Protocol.unknown) {
        // REVIEW What should happen if there is no protocol
        if (log.isDebugEnabled()) {
            log.debug("Constructing ProtocolException for " + text);
        }
        e = ExceptionFactory.makeProtocolException(text, null);
    }
    return e;
}

From source file:org.apache.axis2.jaxws.message.util.impl.SAAJConverterImpl.java

public SOAPEnvelope toSAAJ(org.apache.axiom.soap.SOAPEnvelope omEnvelope) throws WebServiceException {
    if (log.isDebugEnabled()) {
        log.debug("Converting OM SOAPEnvelope to SAAJ SOAPEnvelope");
        log.debug("The conversion occurs due to " + JavaUtils.stackToString());
    }/*w  w w. j a  va 2  s  . c  o m*/

    SOAPEnvelope soapEnvelope = null;
    try {
        // Build the default envelope
        OMNamespace ns = omEnvelope.getNamespace();
        MessageFactory mf = createMessageFactory(ns.getNamespaceURI());
        SOAPMessage sm = mf.createMessage();
        SOAPPart sp = sm.getSOAPPart();
        soapEnvelope = sp.getEnvelope();

        // The getSOAPEnvelope() call creates a default SOAPEnvelope with a SOAPHeader and SOAPBody.
        // The SOAPHeader and SOAPBody are removed (they will be added back in if they are present in the
        // OMEnvelope).
        SOAPBody soapBody = soapEnvelope.getBody();
        if (soapBody != null) {
            soapBody.detachNode();
        }
        SOAPHeader soapHeader = soapEnvelope.getHeader();
        if (soapHeader != null) {
            soapHeader.detachNode();
        }

        // We don't know if there is a real OM tree or just a backing XMLStreamReader.
        // The best way to walk the data is to get the XMLStreamReader and use this
        // to build the SOAPElements
        XMLStreamReader reader = omEnvelope.getXMLStreamReader();

        NameCreator nc = new NameCreator(soapEnvelope);
        buildSOAPTree(nc, soapEnvelope, null, reader, false);
    } catch (WebServiceException e) {
        throw e;
    } catch (SOAPException e) {
        throw ExceptionFactory.makeWebServiceException(e);
    }
    return soapEnvelope;
}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test/*ww w.  j  a v  a 2s  . c o  m*/
public void testStringAttachment() throws Exception {

    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();
    AttachmentPart attachment = message.createAttachmentPart();
    String stringContent = "Update address for Sunny Skies "
            + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";

    attachment.setContent(stringContent, "text/plain");
    attachment.setContentId("update_address");
    message.addAttachmentPart(attachment);

    assertTrue(message.countAttachments() == 1);

    Iterator it = message.getAttachments();
    while (it.hasNext()) {
        attachment = (AttachmentPart) it.next();
        Object content = attachment.getContent();
        String id = attachment.getContentId();
        assertEquals(content, stringContent);
    }
    message.removeAllAttachments();
    assertTrue(message.countAttachments() == 0);
}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test/*from   w  ww  .  j  av  a2 s. c o  m*/
public void testMultipleAttachments() throws Exception {

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

    AttachmentPart a1 = msg.createAttachmentPart(new DataHandler("<some_xml/>", "text/xml"));
    a1.setContentType("text/xml");
    msg.addAttachmentPart(a1);
    AttachmentPart a2 = msg.createAttachmentPart(new DataHandler("<some_xml/>", "text/xml"));
    a2.setContentType("text/xml");
    msg.addAttachmentPart(a2);
    AttachmentPart a3 = msg.createAttachmentPart(new DataHandler("text", "text/plain"));
    a3.setContentType("text/plain");
    msg.addAttachmentPart(a3);

    assertTrue(msg.countAttachments() == 3);

    MimeHeaders mimeHeaders = new MimeHeaders();
    mimeHeaders.addHeader("Content-Type", "text/xml");

    int nAttachments = 0;
    Iterator iterator = msg.getAttachments(mimeHeaders);
    while (iterator.hasNext()) {
        nAttachments++;
        AttachmentPart ap = (AttachmentPart) iterator.next();
        assertTrue(ap.equals(a1) || ap.equals(a2));
    }
    assertTrue(nAttachments == 2);
}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Test
public void testBadAttSize() throws Exception {
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();

    ByteArrayInputStream ins = new ByteArrayInputStream(new byte[5]);
    DataHandler dh = new DataHandler(new Src(ins, "text/plain"));
    AttachmentPart part = message.createAttachmentPart(dh);
    assertEquals("Size should match", 5, part.getSize());
}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test//  ww w.  j  a va  2  s .  c o m
public void testClearContent() throws Exception {
    try {
        InputStream in1 = TestUtils.getTestFile("attach.xml");

        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();
        AttachmentPart ap = message.createAttachmentPart();
        MimeHeader mh = null;

        //Setting Mime Header
        ap.setMimeHeader("Content-Description", "some text");

        //Setting Content Id Header
        ap.setContentId("id@abc.com");

        //Setting Content
        ap.setContent(new StreamSource(in1), "text/xml");

        //Clearing Content
        ap.clearContent();

        try {

            //Getting Content
            InputStream is = (InputStream) ap.getContent();
            fail("Error: SOAPException should have been thrown");
        } catch (SOAPException e) {
            //Error thrown.(expected)
        }

        Iterator iterator = ap.getAllMimeHeaders();
        int cnt = 0;
        boolean foundHeader1 = false;
        boolean foundHeader2 = false;
        boolean foundDefaultHeader = false;
        while (iterator.hasNext()) {
            cnt++;
            mh = (MimeHeader) iterator.next();
            String name = mh.getName();
            String value = mh.getValue();
            if (name.equals("Content-Description") && value.equals("some text")) {
                if (!foundHeader1) {
                    foundHeader1 = true;
                    //MimeHeaders do match for header1
                } else {
                    fail("Error: Received the same header1 header twice");
                }
            } else if (name.equalsIgnoreCase("Content-Id") && value.equals("id@abc.com")) {
                if (!foundHeader2) {
                    foundHeader2 = true;
                    //MimeHeaders do match for header2
                } else {
                    fail("Error: Received the same header2 header twice");
                }
            } else if (name.equals("Content-Type") && value.equals("text/xml")) {
                if (!foundDefaultHeader) {
                    foundDefaultHeader = true;
                    //MimeHeaders do match for default header
                } else {
                    fail("Error: Received the same default header header twice");
                }
            } else {
                fail("Error: Received an invalid header");
            }
        }

        if (!(foundHeader1 && foundHeader2)) {
            fail("Error: did not receive both headers");
        }

    } catch (Exception e) {
        fail("Exception: " + e);
    }

}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test/*from w  w  w  .j  a  v a 2 s  .  c o  m*/
public void testGetContent() throws Exception {
    try {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage msg = factory.createMessage();
        AttachmentPart ap = msg.createAttachmentPart();
        Image image = ImageIO.read(TestUtils.getTestFileURL("attach.gif"));
        ap = msg.createAttachmentPart(image, "image/gif");

        //Getting Content should return an Image object
        Object o = ap.getContent();
        if (o != null) {
            if (o instanceof Image) {
                //Image object was returned (ok)
            } else {
                fail("Unexpected object was returned");
            }
        }
    } catch (Exception e) {
        fail("Exception: " + e);
    }
}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test/*from w  ww  .  j av  a2s . c om*/
public void testGetRawContents() {
    try {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage msg = factory.createMessage();
        AttachmentPart ap = msg.createAttachmentPart();
        ap = msg.createAttachmentPart();
        byte data1[] = null;
        data1 = ap.getRawContentBytes();

    } catch (SOAPException e) {
        //Caught expected SOAPException
    } catch (NullPointerException e) {
        //Caught expected NullPointerException
    } catch (Exception e) {
        fail();
    }
}

From source file:org.apache.axis2.saaj.AttachmentTest.java

@Validated
@Test//from ww w. j  a v a2 s  .c  o  m
public void testSetBase64Content() {
    try {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage msg = factory.createMessage();
        AttachmentPart ap = msg.createAttachmentPart();

        String urlString = "http://ws.apache.org/images/project-logo.jpg";
        if (isNetworkedResourceAvailable(urlString)) {
            URL url = new URL(urlString);
            DataHandler dh = new DataHandler(url);
            //Create InputStream from DataHandler's InputStream
            InputStream is = dh.getInputStream();

            byte buf[] = IOUtils.getStreamAsByteArray(is);
            //Setting Content via InputStream for image/jpeg mime type
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Base64.encode(buf, 0, buf.length, bos);
            buf = bos.toByteArray();
            InputStream stream = new ByteArrayInputStream(buf);
            ap.setBase64Content(stream, "image/jpeg");

            //Getting Content.. should return InputStream object
            InputStream r = ap.getBase64Content();
            if (r != null) {
                if (r instanceof InputStream) {
                    //InputStream object was returned (ok)
                } else {
                    fail("Unexpected object was returned");
                }
            }
        }
    } catch (Exception e) {
        fail("Exception: " + e);
    }
}