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.wso2.carbon.identity.sso.saml.util.SAMLSOAPUtils.java

/**
 *
 * Creates a SOAP Fault message including the fault code and fault string.
 * @param faultString detailed error message
 * @param faultcode//from   ww w  .  jav a  2s. co  m
 * @return
 */
public static String createSOAPFault(String faultString, String faultcode)
        throws TransformerException, SOAPException {
    SOAPMessage soapMsg;
    MessageFactory factory = MessageFactory.newInstance();
    soapMsg = factory.createMessage();
    SOAPPart part = soapMsg.getSOAPPart();
    SOAPEnvelope envelope = part.getEnvelope();
    SOAPBody body = envelope.getBody();
    SOAPFault fault = body.addFault();
    fault.setFaultString(faultString);
    fault.setFaultCode(new QName(SAMLECPConstants.SOAPNamespaceURI.SOAP_NAMESPACE_URI, faultcode));
    return convertSOAPMsgToString(soapMsg).replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
}

From source file:org.wso2.carbon.identity.sso.saml.util.SAMLSOAPUtils.java

/**
 *
 * @param samlRes SAML Response/*from   ww  w .ja  va2  s  . c  om*/
 * @param acUrl Assertion Consumer URL
 * @return
 */
public static String createSOAPMessage(String samlRes, String acUrl)
        throws TransformerException, SOAPException {
    SOAPMessage soapMsg;
    MessageFactory factory = MessageFactory.newInstance();
    soapMsg = factory.createMessage();
    SOAPPart part = soapMsg.getSOAPPart();
    SOAPEnvelope envelope = part.getEnvelope();
    SOAPHeader header = envelope.getHeader();
    SOAPHeaderElement soapHeaderElement = header.addHeaderElement(
            envelope.createName(SAMLECPConstants.SOAPECPHeaderElements.SOAP_ECP_HEADER_LOCAL_NAME,
                    SAMLECPConstants.SOAPECPHeaderElements.SOAP_ECP_HEADER_PREFIX,
                    SAMLECPConstants.SOAPECPHeaderElements.SOAP_ECP_HEADER_URI));
    soapHeaderElement.setMustUnderstand(true);
    soapHeaderElement.setActor(SAMLECPConstants.SOAPHeaderElements.SOAP_HEADER_ELEMENT_ACTOR);
    soapHeaderElement.addAttribute(new QName(SAMLECPConstants.SOAPHeaderElements.SOAP_HEADER_ELEMENT_ACS_URL),
            acUrl);
    SOAPBody body = envelope.getBody();
    String rawxml = "<![CDATA[" + samlRes + "]]>";
    body.addTextNode(rawxml);
    return convertSOAPMsgToString(soapMsg).replace("<![CDATA[", "").replace("]]>", "");
}

From source file:test.functional.TestJAXMSamples.java

public void testJWSFault() throws Exception {
    try {/*from   w  w w .j  av a2  s .c  o  m*/
        SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection con = scFactory.createConnection();

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

        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        SOAPBody body = envelope.getBody();

        Name bodyName = envelope.createName("echo");
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

        Name name = envelope.createName("arg0");
        SOAPElement symbol = bodyElement.addChildElement(name);
        symbol.addTextNode("Hello");

        URLEndpoint endpoint = new URLEndpoint("http://localhost:8080/jws/FaultTest.jws");
        SOAPMessage response = con.call(message, endpoint);
        SOAPBody respBody = response.getSOAPPart().getEnvelope().getBody();
        assertTrue(respBody.hasFault());
    } catch (javax.xml.soap.SOAPException e) {
        Throwable t = e.getCause();
        if (t != null) {
            t.printStackTrace();
            if (t instanceof AxisFault) {
                AxisFault af = (AxisFault) t;
                if ((af.detail instanceof SocketException)
                        || (af.getFaultCode().getLocalPart().equals("HTTP"))) {
                    System.out.println("Connect failure caused testJWSFault to be skipped.");
                    return;
                }
            }
            throw new Exception("Fault returned from test: " + t);
        } else {
            e.printStackTrace();
            throw new Exception("Exception returned from test: " + e);
        }
    } catch (Throwable t) {
        t.printStackTrace();
        throw new Exception("Fault returned from test: " + t);
    }
}

From source file:test.saaj.TestAttachmentSerialization.java

public int saveMsgWithAttachments(OutputStream os) throws Exception {
    MessageFactory mf = MessageFactory.newInstance();
    SOAPMessage msg = mf.createMessage();

    SOAPPart sp = msg.getSOAPPart();
    SOAPEnvelope envelope = sp.getEnvelope();
    SOAPHeader header = envelope.getHeader();
    SOAPBody body = envelope.getBody();

    SOAPElement el = header.addHeaderElement(envelope.createName("field4", NS_PREFIX, NS_URI));
    SOAPElement el2 = el.addChildElement("field4b", NS_PREFIX);
    SOAPElement el3 = el2.addTextNode("field4value");

    el = body.addBodyElement(envelope.createName("bodyfield3", NS_PREFIX, NS_URI));
    el2 = el.addChildElement("bodyfield3a", NS_PREFIX);
    el2.addTextNode("bodyvalue3a");
    el2 = el.addChildElement("bodyfield3b", NS_PREFIX);
    el2.addTextNode("bodyvalue3b");
    el2 = el.addChildElement("datefield", NS_PREFIX);

    AttachmentPart ap = msg.createAttachmentPart();
    ap.setContent("some attachment text...", "text/plain");
    msg.addAttachmentPart(ap);//w w  w . j  a va  2  s .co  m

    String jpgfilename = "docs/images/axis.jpg";
    File myfile = new File(jpgfilename);
    FileDataSource fds = new FileDataSource(myfile);
    DataHandler dh = new DataHandler(fds);
    AttachmentPart ap2 = msg.createAttachmentPart(dh);
    ap2.setContentType("image/jpg");
    msg.addAttachmentPart(ap2);

    // Test for Bug #17664
    if (msg.saveRequired()) {
        msg.saveChanges();
    }
    MimeHeaders headers = msg.getMimeHeaders();
    assertTrue(headers != null);
    String[] contentType = headers.getHeader("Content-Type");
    assertTrue(contentType != null);

    msg.writeTo(os);
    os.flush();
    return msg.countAttachments();
}

From source file:unit.test.be.e_contract.mycarenet.ehealth.common.WSSecuritySOAPHandlerTest.java

@Test
public void testHandleMessage() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    PrivateKey privateKey = keyPair.getPrivate();

    String samlAssertion = "<Assertion xmlns=\"urn:oasis:names:tc:SAML:1.0:assertion\""
            + " AssertionID=\"_42e7a00652420d86ee884f295a3fbf02\">" + "</Assertion>";

    WSSecuritySOAPHandler testedInstance = new WSSecuritySOAPHandler();
    testedInstance.setPrivateKey(privateKey);
    testedInstance.setAssertion(samlAssertion);

    SOAPMessageContext mockSoapMessageContext = EasyMock.createMock(SOAPMessageContext.class);
    EasyMock.expect(mockSoapMessageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)).andReturn(true);

    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addBodyElement(new QName("http://www.example.com", "Test"));

    EasyMock.expect(mockSoapMessageContext.getMessage()).andReturn(soapMessage);

    // prepare/*from   www  .j  a  v  a  2  s . com*/
    EasyMock.replay(mockSoapMessageContext);

    // operate
    testedInstance.handleMessage(mockSoapMessageContext);

    // verify
    EasyMock.verify(mockSoapMessageContext);
    LOG.debug(toString(soapPart));
}