Example usage for javax.xml.ws.handler.soap SOAPMessageContext getMessage

List of usage examples for javax.xml.ws.handler.soap SOAPMessageContext getMessage

Introduction

In this page you can find the example usage for javax.xml.ws.handler.soap SOAPMessageContext getMessage.

Prototype

public SOAPMessage getMessage();

Source Link

Document

Gets the SOAPMessage from this message context.

Usage

From source file:cn.com.ttblog.ssmbootstrap_table.webservice.LicenseHandler.java

@SuppressWarnings("unchecked")
@Override//  ww  w. j a va2 s  . c  o  m
public boolean handleMessage(SOAPMessageContext context) {
    try {
        Boolean out = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        logger.debug("LicenseHandler:{}", out);
        if (!out) {
            SOAPMessage message = context.getMessage();
            logger.debug("SOAPMessage:{}", ToStringBuilder.reflectionToString(message));
            SOAPEnvelope enve = message.getSOAPPart().getEnvelope();
            SOAPHeader header = enve.getHeader();
            SOAPBody body = enve.getBody();
            Node bn = body.getChildNodes().item(0);
            String partname = bn.getLocalName();
            if ("getUser".equals(partname)) {
                if (header == null) {
                    // ?
                    SOAPFault fault = body.addFault();
                    fault.setFaultString("??!");
                    throw new SOAPFaultException(fault);
                }
                Iterator<SOAPHeaderElement> iterator = header.extractAllHeaderElements();
                if (!iterator.hasNext()) {
                    // ?
                    SOAPFault fault = body.addFault();
                    fault.setFaultString("??!");
                    throw new SOAPFaultException(fault);
                }
                while (iterator.hasNext()) {
                    SOAPHeaderElement ele = iterator.next();
                    System.out.println(ele.getTextContent());
                }
            }
        }
    } catch (SOAPException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:be.e_contract.mycarenet.ehealth.common.WSSecuritySOAPHandler.java

private void handleOutboundMessage(SOAPMessageContext context)
        throws WSSecurityException, SAXException, IOException {
    LOG.debug("adding WS-Security header");
    SOAPMessage soapMessage = context.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    WSSecHeader wsSecHeader = new WSSecHeader();
    wsSecHeader.insertSecurityHeader(soapPart);

    WSSecTimestamp wsSecTimeStamp = new WSSecTimestamp();
    wsSecTimeStamp.setTimeToLive(60);// ww  w.  j a  v a  2 s. com
    wsSecTimeStamp.build(soapPart, wsSecHeader);

    Document assertionDocument = this.documentBuilder
            .parse(new InputSource(new StringReader(this.samlAssertion)));
    Element assertionElement = assertionDocument.getDocumentElement();
    String assertionId = assertionElement.getAttribute("AssertionID");
    Element importedAssertionElement = (Element) soapPart.importNode(assertionElement, true);
    Element securityHeaderElement = wsSecHeader.getSecurityHeader();
    securityHeaderElement.appendChild(importedAssertionElement);

    WSSecSignature wsSecSignature = new WSSecSignature();
    wsSecSignature.setSignatureAlgorithm(WSConstants.RSA);
    wsSecSignature.setKeyIdentifierType(WSConstants.CUSTOM_KEY_IDENTIFIER);
    wsSecSignature.setCustomTokenValueType(WSConstants.WSS_SAML_KI_VALUE_TYPE);
    wsSecSignature.setCustomTokenId(assertionId);
    Crypto crypto = new WSSecurityCrypto(this.privateKey, null);
    wsSecSignature.prepare(soapPart, crypto, wsSecHeader);
    Vector<WSEncryptionPart> signParts = new Vector<WSEncryptionPart>();
    SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(soapPart.getDocumentElement());
    signParts.add(new WSEncryptionPart(soapConstants.getBodyQName().getLocalPart(),
            soapConstants.getEnvelopeURI(), "Content"));
    signParts.add(new WSEncryptionPart(wsSecTimeStamp.getId()));
    List<Reference> referenceList = wsSecSignature.addReferencesToSign(signParts, wsSecHeader);
    wsSecSignature.computeSignature(referenceList, false, null);
}

From source file:com.konakart.bl.modules.ordertotal.thomson.HeaderSecrityHandler.java

public boolean handleMessage(SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {
        SOAPMessage message = smc.getMessage();

        if (log.isInfoEnabled()) {
            log.info("Adding Credentials : " + getUName() + "/" + getPWord());
        }/* ww w . j  av a2 s  .  c  o m*/

        try {
            SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
            envelope.setPrefix("soapenv");
            envelope.getBody().setPrefix("soapenv");

            SOAPHeader header = envelope.addHeader();
            SOAPElement security = header.addChildElement("Security", "wsse",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

            SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
            usernameToken.addAttribute(new QName("wsu:Id"), "UsernameToken-1");
            usernameToken.setAttribute("wsu:Id", "UsernameToken-1");

            usernameToken.addAttribute(new QName("xmlns:wsu"),
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

            SOAPElement username = usernameToken.addChildElement("Username", "wsse");
            username.addTextNode(getUName());

            SOAPElement password = usernameToken.addChildElement("Password", "wsse");
            password.setAttribute("Type",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
            password.addTextNode(getPWord());

            SOAPElement encodingType = usernameToken.addChildElement("Nonce", "wsse");
            encodingType.setAttribute("EncodingType",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
            encodingType.addTextNode("Encoding");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return outboundProperty;
}

From source file:be.fedict.hsm.client.WSSecuritySOAPHandler.java

private void handleOutboundMessage(SOAPMessageContext context) throws SOAPException, WSSecurityException {

    if (null == this.privateKey) {
        LOG.warn("no adding a WS-Security header");
        return;/*from  ww  w  . j  a va  2s. com*/
    }

    SOAPMessage soapMessage = context.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    soapMessage.getSOAPHeader();
    WSSecHeader wsSecHeader = new WSSecHeader();
    wsSecHeader.setMustUnderstand(true);
    wsSecHeader.insertSecurityHeader(soapPart);

    WSSecTimestamp wsSecTimeStamp = new WSSecTimestamp();
    wsSecTimeStamp.prepare(soapPart);
    wsSecTimeStamp.prependToHeader(wsSecHeader);

    WSSecurityCrypto crypto = new WSSecurityCrypto(this.privateKey, this.certificate);
    WSSConfig wssConfig = new WSSConfig();
    WSSecSignature sign = new WSSecSignature(wssConfig);
    sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
    sign.prepare(soapPart, crypto, wsSecHeader);
    String bstId = sign.getBSTTokenId();
    sign.appendBSTElementToHeader(wsSecHeader);
    sign.setDigestAlgo("http://www.w3.org/2001/04/xmlenc#sha256");
    sign.setSignatureAlgorithm("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
    Vector<WSEncryptionPart> signParts = new Vector<WSEncryptionPart>();
    signParts.add(new WSEncryptionPart(wsSecTimeStamp.getId()));
    signParts.add(new WSEncryptionPart(WSConstants.ELEM_BODY, WSConstants.URI_SOAP12_ENV, ""));
    signParts.add(new WSEncryptionPart(bstId));
    sign.addReferencesToSign(signParts, wsSecHeader);
    List<Reference> referenceList = sign.addReferencesToSign(signParts, wsSecHeader);
    sign.computeSignature(referenceList, false, null);
}

From source file:be.fedict.eid.idp.protocol.saml2.artifact.ArtifactServiceServerHandler.java

public boolean handleMessage(SOAPMessageContext soapMessageContext) {

    Boolean outbound = (Boolean) soapMessageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    SOAPMessage soapMessage = soapMessageContext.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    if (outbound) {

        handleOutboundDocument(soapPart, soapMessageContext);
    } else {/*  w ww. j a v a 2 s.  c  om*/
        handleInboundDocument(soapPart);
    }

    return true;

}

From source file:be.fedict.eid.idp.sp.protocol.ws_federation.sts.LoggingSoapHandler.java

public boolean handleFault(SOAPMessageContext context) {
    if (false == LOG.isDebugEnabled()) {
        return true;
    }/*from  w  w w. j a v  a2  s . c o  m*/
    Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    LOG.debug("outbound message: " + outboundProperty);
    SOAPMessage soapMessage = context.getMessage();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        soapMessage.writeTo(output);
    } catch (Exception e) {
        LOG.error("SOAP error: " + e.getMessage());
    }
    LOG.debug("SOAP fault: " + output.toString());
    return true;
}

From source file:be.agiv.security.handler.LoggingHandler.java

public boolean handleMessage(SOAPMessageContext context) {
    if (false == LOG.isDebugEnabled()) {
        return true;
    }/* w w  w . j av a  2s. c o  m*/
    Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    LOG.debug("outbound message: " + outboundProperty);
    SOAPMessage soapMessage = context.getMessage();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        soapMessage.writeTo(output);
    } catch (Exception e) {
        LOG.error("SOAP error: " + e.getMessage());
    }
    String outputStr = output.toString();
    LOG.debug("SOAP message: " + outputStr);
    return true;
}

From source file:jp.primecloud.auto.nifty.soap.security.SignatureHandler.java

/**
 * {@inheritDoc}/*w  ww . j  ava  2 s . co  m*/
 */
@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outbound != null && outbound.booleanValue()) {
        try {
            Document document = context.getMessage().getSOAPPart();

            WSSecHeader header = new WSSecHeader();
            header.insertSecurityHeader(document);

            WSSecSignature signature = new WSSecSignature();
            signature.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
            signature.prepare(document, crypto, header);

            signature.appendBSTElementToHeader(header);
            signature.appendToHeader(header);
            signature.computeSignature();
        } catch (WSSecurityException e) {
            throw new RuntimeException(e);
        }
    }

    if (log.isDebugEnabled()) {
        try {
            String envelope = transform(context.getMessage().getSOAPPart());
            log.debug(envelope);
        } catch (TransformerException e) {
            log.warn(e.getMessage());
        }
    }

    return true;
}

From source file:be.fedict.eid.idp.sp.protocol.saml2.artifact.ArtifactServiceClientHandler.java

/**
 * {@inheritDoc}//from  w  ww.j  a v a 2s  .  c o  m
 */
public boolean handleMessage(SOAPMessageContext soapMessageContext) {

    Boolean outbound = (Boolean) soapMessageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    SOAPMessage soapMessage = soapMessageContext.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    if (outbound) {

        handleOutboundDocument(soapPart);
    } else {
        handleInboundDocument(soapPart);
    }

    return true;

}

From source file:com.profesorfalken.payzen.webservices.sdk.handler.soap.HeaderHandler.java

/**
 * Takes the outgoing SOAP message and modifies it adding the header 
 * information//from   www . ja v a2 s. co  m
 * 
 * @param smc SOAP message context
 * @return 
 */
@Override
public boolean handleMessage(SOAPMessageContext smc) {

    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (Boolean.TRUE.equals(outboundProperty)) {

        SOAPMessage message = smc.getMessage();

        try {
            SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();

            //Creates header into SOAP envelope
            SOAPHeader header = envelope.getHeader();
            if (header == null) {
                header = envelope.addHeader();
            }

            // Add shopId
            addHeaderField(header, "shopId", this.shopId);

            // Timestamp
            TimeZone tz = TimeZone.getTimeZone("UTC");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            df.setTimeZone(tz);
            String nowAsISO = df.format(new Date());
            addHeaderField(header, "timestamp", nowAsISO);

            // Mode
            addHeaderField(header, "mode", this.mode);

            // Add requestId
            String requestId = UUID.randomUUID().toString();
            addHeaderField(header, "requestId", requestId);

            // Authentication token
            String tokenString = requestId + nowAsISO;
            addHeaderField(header, "authToken", sign(tokenString, shopKey));

        } catch (SOAPException e) {
            logger.error("Error sending header", e);
        }
    }

    return outboundProperty;

}