Example usage for javax.xml.soap SOAPConstants SOAP_1_2_PROTOCOL

List of usage examples for javax.xml.soap SOAPConstants SOAP_1_2_PROTOCOL

Introduction

In this page you can find the example usage for javax.xml.soap SOAPConstants SOAP_1_2_PROTOCOL.

Prototype

String SOAP_1_2_PROTOCOL

To view the source code for javax.xml.soap SOAPConstants SOAP_1_2_PROTOCOL.

Click Source Link

Document

Used to create MessageFactory instances that create SOAPMessages whose behavior supports the SOAP 1.2 specification

Usage

From source file:org.cerberus.service.soap.impl.SoapService.java

@Override
public SOAPMessage createSoapRequest(String envelope, String method)
        throws SOAPException, IOException, SAXException, ParserConfigurationException {
    String unescapedEnvelope = StringEscapeUtils.unescapeXml(envelope);
    boolean is12SoapVersion = SOAP_1_2_NAMESPACE_PATTERN.matcher(unescapedEnvelope).matches();

    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("SOAPAction", "\"" + method + "\"");
    headers.addHeader("Content-Type",
            is12SoapVersion ? SOAPConstants.SOAP_1_2_CONTENT_TYPE : SOAPConstants.SOAP_1_1_CONTENT_TYPE);

    InputStream input = new ByteArrayInputStream(unescapedEnvelope.getBytes("UTF-8"));
    MessageFactory messageFactory = MessageFactory
            .newInstance(is12SoapVersion ? SOAPConstants.SOAP_1_2_PROTOCOL : SOAPConstants.SOAP_1_1_PROTOCOL);
    return messageFactory.createMessage(headers, input);
}

From source file:org.springframework.ws.soap.saaj.SaajSoapMessageFactory.java

public void setSoapVersion(SoapVersion version) {
    if (SaajUtils.getSaajVersion() >= SaajUtils.SAAJ_13) {
        if (SoapVersion.SOAP_11 == version) {
            messageFactoryProtocol = SOAPConstants.SOAP_1_1_PROTOCOL;
        } else if (SoapVersion.SOAP_12 == version) {
            messageFactoryProtocol = SOAPConstants.SOAP_1_2_PROTOCOL;
        } else {//from   w  w  w. j  a  v a  2 s .c om
            throw new IllegalArgumentException(
                    "Invalid version [" + version + "]. Expected the SOAP_11 or SOAP_12 constant");
        }
    } else if (SoapVersion.SOAP_11 != version) {
        throw new IllegalArgumentException("SAAJ 1.1 and 1.2 only support SOAP 1.1");
    }
}

From source file:test.unit.be.agiv.security.handler.WSSecurityHandlerTest.java

@Test
public void testVerifyTimestampExpired() throws Exception {
    // setup//from w w w.  j av  a2  s . com
    SOAPMessageContext mockContext = EasyMock.createMock(SOAPMessageContext.class);

    EasyMock.expect(mockContext.get("javax.xml.ws.handler.message.outbound")).andStubReturn(Boolean.FALSE);

    InputStream requestInputStream = WSSecurityHandlerTest.class.getResourceAsStream("/ip-sts-response.xml");
    SOAPMessage soapMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null,
            requestInputStream);
    EasyMock.expect(mockContext.getMessage()).andStubReturn(soapMessage);

    // prepare
    EasyMock.replay(mockContext);

    // operate
    try {
        this.testedInstance.handleMessage(mockContext);
        fail();
    } catch (ProtocolException e) {
        // verify
        EasyMock.verify(mockContext);
    }
}

From source file:test.unit.be.agiv.security.handler.WSSecurityHandlerTest.java

@Test
public void testVerifyTimestamp() throws Exception {
    // setup/* w  w w.ja v a  2s. c  o  m*/
    SOAPMessageContext mockContext = EasyMock.createMock(SOAPMessageContext.class);

    EasyMock.expect(mockContext.get("javax.xml.ws.handler.message.outbound")).andStubReturn(Boolean.FALSE);

    SOAPMessage soapMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();

    SOAPBody soapBody = soapMessage.getSOAPBody();
    soapBody.addBodyElement(new QName("test"));

    SOAPPart soapPart = soapMessage.getSOAPPart();
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(soapPart);
    WSSecTimestamp timestamp = new WSSecTimestamp();
    timestamp.build(soapPart, secHeader);

    LOG.debug("SOAP message: " + toString(soapMessage.getSOAPPart()));
    EasyMock.expect(mockContext.getMessage()).andStubReturn(soapMessage);

    // prepare
    EasyMock.replay(mockContext);

    // operate
    boolean result = this.testedInstance.handleMessage(mockContext);

    // verify
    EasyMock.verify(mockContext);
    assertTrue(result);
}

From source file:test.unit.be.agiv.security.handler.WSSecurityHandlerTest.java

@Test
public void testVerifyTimestampMissing() throws Exception {
    // setup/*from  ww  w  .  j  a v a  2s  .  co  m*/
    SOAPMessageContext mockContext = EasyMock.createMock(SOAPMessageContext.class);

    EasyMock.expect(mockContext.get("javax.xml.ws.handler.message.outbound")).andStubReturn(Boolean.FALSE);

    SOAPMessage soapMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();

    SOAPBody soapBody = soapMessage.getSOAPBody();
    soapBody.addBodyElement(new QName("test"));

    LOG.debug("SOAP message: " + toString(soapMessage.getSOAPPart()));
    EasyMock.expect(mockContext.getMessage()).andStubReturn(soapMessage);

    // prepare
    EasyMock.replay(mockContext);

    // operate
    try {
        this.testedInstance.handleMessage(mockContext);
        fail();
    } catch (ProtocolException e) {
        // verify
        EasyMock.verify(mockContext);
    }
}

From source file:test.unit.be.agiv.security.handler.WSSecurityHandlerTest.java

@Test
public void testSignature() throws Exception {
    // setup/*from   ww w .  j a  v  a 2  s .com*/
    SOAPMessageContext mockContext = EasyMock.createMock(SOAPMessageContext.class);

    EasyMock.expect(mockContext.get("javax.xml.ws.handler.message.outbound")).andStubReturn(Boolean.TRUE);

    byte[] secret = new byte[256 / 8];
    SecureRandom secureRandom = new SecureRandom();
    secureRandom.nextBytes(secret);

    String tokenIdentifier = "#saml-token-test";
    this.testedInstance.setKey(secret, tokenIdentifier, null, false);

    InputStream requestInputStream = WSSecurityHandlerTest.class
            .getResourceAsStream("/r-sts-request-before-signing.xml");
    SOAPMessage soapMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null,
            requestInputStream);
    EasyMock.expect(mockContext.getMessage()).andStubReturn(soapMessage);

    // prepare
    EasyMock.replay(mockContext);

    // operate
    boolean result = this.testedInstance.handleMessage(mockContext);

    // verify
    EasyMock.verify(mockContext);
    assertTrue(result);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    soapMessage.writeTo(outputStream);
    LOG.debug("SOAP message: " + new String(outputStream.toByteArray()));

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
    Document resultDocument = documentBuilder.parse(byteArrayInputStream);
    TestUtils.markAllIdAttributesAsId(resultDocument);

    NodeList signatureNodeList = resultDocument.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature");
    assertEquals(1, signatureNodeList.getLength());
    Element signatureElement = (Element) signatureNodeList.item(0);

    XMLSignature xmlSignature = new XMLSignature(signatureElement, null);
    Key key = WSSecurityUtil.prepareSecretKey(SignatureMethod.HMAC_SHA1, secret);
    boolean signatureResult = xmlSignature.checkSignatureValue(key);
    assertTrue(signatureResult);

    LOG.debug("signed SOAP: " + toString(resultDocument));
}

From source file:test.unit.be.agiv.security.handler.WSSecurityHandlerTest.java

@Test
public void testCertificateSignature() throws Exception {
    // setup/*  w  ww.j  a  v a 2s.  co  m*/
    SOAPMessageContext mockContext = EasyMock.createMock(SOAPMessageContext.class);

    EasyMock.expect(mockContext.get("javax.xml.ws.handler.message.outbound")).andStubReturn(Boolean.TRUE);

    SOAPMessage soapMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null,
            new ByteArrayInputStream(
                    ("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
                            + "<soap:Header>"
                            + "<wsa:To soap:mustUnderstand=\"1\" wsu:Id=\"toId\">destination</wsa:To>"
                            + "</soap:Header>" + "<soap:Body>test</soap:Body>" + "</soap:Envelope>")
                                    .getBytes()));
    EasyMock.expect(mockContext.getMessage()).andStubReturn(soapMessage);

    EasyMock.expect(mockContext.get(WSAddressingHandler.class.getName() + ".toId")).andStubReturn("toId");

    KeyPair keyPair = generateKeyPair();
    PrivateKey privateKey = keyPair.getPrivate();

    X509Certificate certificate = generateSelfSignedCertificate(keyPair);
    this.testedInstance.setCredentials(privateKey, certificate);

    // prepare
    EasyMock.replay(mockContext);

    // operate
    boolean result = this.testedInstance.handleMessage(mockContext);

    // verify
    EasyMock.verify(mockContext);
    assertTrue(result);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    soapMessage.writeTo(outputStream);
    LOG.debug("SOAP message: " + new String(outputStream.toByteArray()));

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
    Document resultDocument = documentBuilder.parse(byteArrayInputStream);
    TestUtils.markAllIdAttributesAsId(resultDocument);

    NodeList signatureNodeList = resultDocument.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature");
    assertEquals(1, signatureNodeList.getLength());
    Element signatureElement = (Element) signatureNodeList.item(0);

    XMLSignature xmlSignature = new XMLSignature(signatureElement, null);
    boolean signatureResult = xmlSignature.checkSignatureValue(certificate);
    assertTrue(signatureResult);

    LOG.debug("signed SOAP: " + toString(resultDocument));
}

From source file:test.unit.be.agiv.security.handler.WSSecurityHandlerTest.java

@Test
public void testWSSecurityWithoutInitialHeader() throws Exception {
    // setup// w w w . j a  v a2  s  . co  m
    SOAPMessageContext mockContext = EasyMock.createMock(SOAPMessageContext.class);

    EasyMock.expect(mockContext.get("javax.xml.ws.handler.message.outbound")).andStubReturn(Boolean.TRUE);
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.token")).andStubReturn(null);
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.username"))
            .andStubReturn("username");
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.password"))
            .andStubReturn("password");
    EasyMock.expect(mockContext.get("be.agiv.security.handler.WSSecurityHandler.key")).andStubReturn(null);

    SOAPMessage soapMessage = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null,
            new ByteArrayInputStream(
                    "<Envelope xmlns=\"http://www.w3.org/2003/05/soap-envelope\"><Body>test</Body></Envelope>"
                            .getBytes()));

    LOG.debug("SOAP message: " + toString(soapMessage.getSOAPPart()));
    EasyMock.expect(mockContext.getMessage()).andStubReturn(soapMessage);

    EasyMock.expect(mockContext.get(WSSecurityHandler.class.getName() + ".certificate")).andStubReturn(null);

    // prepare
    EasyMock.replay(mockContext);

    // operate
    this.testedInstance.handleMessage(mockContext);

    // verify
    EasyMock.verify(mockContext);
}