Search in sources :

Example 21 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project openhab1-addons by openhab.

the class Tr064Comm method constructTr064Msg.

/***
     * sets all required namespaces and prepares the SOAP message to send
     * creates skeleton + body data
     *
     * @param bodyData is attached to skeleton to form entire SOAP message
     * @return ready to send SOAP message
     */
private SOAPMessage constructTr064Msg(SOAPBodyElement bodyData) {
    SOAPMessage soapMsg = null;
    try {
        MessageFactory msgFac;
        msgFac = MessageFactory.newInstance();
        soapMsg = msgFac.createMessage();
        soapMsg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
        soapMsg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
        SOAPPart part = soapMsg.getSOAPPart();
        // valid for entire SOAP msg
        String namespace = "s";
        // create suitable fbox envelope
        SOAPEnvelope envelope = part.getEnvelope();
        envelope.setPrefix(namespace);
        // delete standard namespace which was already set
        envelope.removeNamespaceDeclaration("SOAP-ENV");
        envelope.addNamespaceDeclaration(namespace, "http://schemas.xmlsoap.org/soap/envelope/");
        Name nEncoding = envelope.createName("encodingStyle", namespace, "http://schemas.xmlsoap.org/soap/encoding/");
        envelope.addAttribute(nEncoding, "http://schemas.xmlsoap.org/soap/encoding/");
        // create empty header
        SOAPHeader header = envelope.getHeader();
        header.setPrefix(namespace);
        // create body with command based on parameter
        SOAPBody body = envelope.getBody();
        body.setPrefix(namespace);
        // bodyData already prepared. Needs only be added
        body.addChildElement(bodyData);
    } catch (Exception e) {
        logger.error("Error creating SOAP message for fbox request with data {}", bodyData);
        e.printStackTrace();
    }
    return soapMsg;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) MessageFactory(javax.xml.soap.MessageFactory) SOAPPart(javax.xml.soap.SOAPPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader) XPathExpressionException(javax.xml.xpath.XPathExpressionException) URISyntaxException(java.net.URISyntaxException) SOAPException(javax.xml.soap.SOAPException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) Name(javax.xml.soap.Name) QName(javax.xml.namespace.QName)

Example 22 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project OpenAM by OpenRock.

the class MessageProcessor method secureRequest.

/**
     * Secures the request by getting the credential from the discovery
     * service.
     *
     * @param offering Resource Offering of the discovery service.
     * @param credentials List of credentials that are required to access
     *        the discovery service.
     * @param serviceType Service Type that the discovery service should
     *        need to look for.
     * @param soapMessage SOAPMessage that needs to be secured.
     * @param sharedData Any shared data that may be used between the request
     *        and the response.
     * @return SOAPMessage Secured SOAP Message.
     * @throws SOAPBindingException for any failure.
     */
public SOAPMessage secureRequest(ResourceOffering offering, List credentials, String serviceType, SOAPMessage soapMessage, Map sharedData) throws SOAPBindingException {
    Utils.debug.message("MessageProcessor.secureRequest:Init");
    try {
        SOAPHeader header = addCorrelationHeader(soapMessage, null);
        QueryResponse discoResponse = getWebserviceOffering(offering, credentials, serviceType);
        if (Utils.debug.messageEnabled()) {
            Utils.debug.message("MessageProcessor.secureRequest: " + "Discovery Response: " + discoResponse.toString());
        }
        ResourceOffering serviceOffering = (ResourceOffering) discoResponse.getResourceOffering().get(0);
        List creds = discoResponse.getCredentials();
        String securityProfile = processResourceOffering(serviceOffering);
        SecurityAssertion securityAssertion = null;
        // security token for this profile.
        if (securityProfile.equals(Message.NULL_SAML) || securityProfile.equals(Message.TLS_SAML) || securityProfile.equals(Message.CLIENT_TLS_SAML) || securityProfile.equals(Message.NULL_BEARER) || securityProfile.equals(Message.TLS_BEARER) || securityProfile.equals(Message.CLIENT_TLS_BEARER) || securityProfile.equals(Message.NULL_SAML_WSF11) || securityProfile.equals(Message.TLS_SAML_WSF11) || securityProfile.equals(Message.CLIENT_TLS_SAML_WSF11) || securityProfile.equals(Message.NULL_BEARER_WSF11) || securityProfile.equals(Message.TLS_BEARER_WSF11) || securityProfile.equals(Message.CLIENT_TLS_BEARER_WSF11)) {
            if (creds != null && creds.size() != 0) {
                securityAssertion = (SecurityAssertion) creds.get(0);
                securityAssertion.addToParent(header);
            }
        }
        if (securityProfile.equals(Message.NULL_SAML) || securityProfile.equals(Message.TLS_SAML) || securityProfile.equals(Message.CLIENT_TLS_SAML) || securityProfile.equals(Message.NULL_X509) || securityProfile.equals(Message.TLS_X509) || securityProfile.equals(Message.CLIENT_TLS_X509) || securityProfile.equals(Message.NULL_SAML_WSF11) || securityProfile.equals(Message.TLS_SAML_WSF11) || securityProfile.equals(Message.CLIENT_TLS_SAML_WSF11) || securityProfile.equals(Message.NULL_X509_WSF11) || securityProfile.equals(Message.TLS_X509_WSF11) || securityProfile.equals(Message.CLIENT_TLS_X509_WSF11)) {
            soapMessage = signMessage(soapMessage, securityProfile, securityAssertion);
        }
        if (Utils.debug.messageEnabled()) {
            Utils.debug.message("MessageProcessor.secureRequest: " + XMLUtils.print(soapMessage.getSOAPPart().getEnvelope()));
        }
        return soapMessage;
    } catch (Exception ex) {
        Utils.debug.error("MessageProcessor.secureRequest: Failure in " + "Securing the request.", ex);
        throw new SOAPBindingException(Utils.bundle.getString("secureRequestFailed"));
    }
}
Also used : ResourceOffering(com.sun.identity.liberty.ws.disco.ResourceOffering) QueryResponse(com.sun.identity.liberty.ws.disco.QueryResponse) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) SecurityAssertion(com.sun.identity.liberty.ws.security.SecurityAssertion) SOAPHeader(javax.xml.soap.SOAPHeader) SOAPException(javax.xml.soap.SOAPException)

Example 23 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project OpenAM by OpenRock.

the class MessageProcessor method signMessage.

/**
     * Signs the message.
     * @param soapMessage SOAPMessage that needs to be signed.
     * @param profile Security profile that needs to be used for signing.
     * @param assertion Security Assertion
     * @return SOAPMessage signed SOAPMessage.
     */
private SOAPMessage signMessage(SOAPMessage soapMessage, String profile, SecurityAssertion assertion) throws SOAPBindingException {
    try {
        SOAPHeader soapHeader = soapMessage.getSOAPPart().getEnvelope().getHeader();
        if (soapHeader == null) {
            soapMessage.getSOAPPart().getEnvelope().addHeader();
        }
        SOAPBody soapBody = soapMessage.getSOAPPart().getEnvelope().getBody();
        if (soapBody == null) {
            throw new SOAPBindingException(Utils.bundle.getString("nullSOAPBody"));
        }
        String bodyId = SAMLUtils.generateID();
        soapBody.setAttributeNS(WSSEConstants.NS_WSU_WSF11, WSSEConstants.WSU_ID, bodyId);
        List ids = new ArrayList();
        ids.add(bodyId);
        if (correlationId != null) {
            ids.add(correlationId);
        }
        Certificate cert = null;
        Element sigElem = null;
        ByteArrayInputStream bin = null;
        ByteArrayOutputStream bop = new ByteArrayOutputStream();
        Document doc = null;
        if (profile == null || profile.equals(Message.NULL_X509) || profile.equals(Message.TLS_X509) || profile.equals(Message.CLIENT_TLS_X509) || profile.equals(Message.NULL_X509_WSF11) || profile.equals(Message.TLS_X509_WSF11) || profile.equals(Message.CLIENT_TLS_X509_WSF11)) {
            BinarySecurityToken binaryToken = addBinaryToken(soapMessage);
            cert = SecurityUtils.getCertificate(binaryToken);
            soapMessage.writeTo(bop);
            bin = new ByteArrayInputStream(bop.toByteArray());
            doc = XMLUtils.toDOMDocument(bin, Utils.debug);
            sigElem = SecurityUtils.getSignatureManager().signWithWSSX509TokenProfile(doc, cert, "", ids, SOAPBindingConstants.WSF_11_VERSION);
        } else if (profile.equals(Message.NULL_SAML) || profile.equals(Message.TLS_SAML) || profile.equals(Message.CLIENT_TLS_SAML) || profile.equals(Message.NULL_SAML_WSF11) || profile.equals(Message.TLS_SAML_WSF11) || profile.equals(Message.CLIENT_TLS_SAML_WSF11)) {
            cert = SecurityUtils.getCertificate(assertion);
            soapMessage.writeTo(bop);
            new ByteArrayInputStream(bop.toByteArray());
            bin = new ByteArrayInputStream(bop.toByteArray());
            doc = XMLUtils.toDOMDocument(bin, Utils.debug);
            sigElem = SecurityUtils.getSignatureManager().signWithWSSSAMLTokenProfile(doc, cert, assertion.getAssertionID(), "", ids, SOAPBindingConstants.WSF_11_VERSION);
        }
        if (sigElem == null) {
            Utils.debug.error("MessageProcessor.signMessage: " + "SigElement is null");
            throw new SOAPBindingException(Utils.bundle.getString("cannotSignMessage"));
        }
        Element securityHeader = getSecurityHeader(soapMessage);
        securityHeader.appendChild(securityHeader.getOwnerDocument().importNode(sigElem, true));
        return Utils.DocumentToSOAPMessage(sigElem.getOwnerDocument());
    } catch (Exception ex) {
        Utils.debug.error("MessageProcessor.signMessage: " + "Signing failed.", ex);
        throw new SOAPBindingException(Utils.bundle.getString("cannotSignMessage"));
    }
}
Also used : BinarySecurityToken(com.sun.identity.liberty.ws.common.wsse.BinarySecurityToken) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) SOAPException(javax.xml.soap.SOAPException) SOAPBody(javax.xml.soap.SOAPBody) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) SOAPHeader(javax.xml.soap.SOAPHeader) Certificate(java.security.cert.Certificate)

Example 24 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project OpenAM by OpenRock.

the class MessageProcessor method addBinaryToken.

/**
     * Adds binary token to the security header.
     */
private BinarySecurityToken addBinaryToken(SOAPMessage msg) throws SOAPBindingException {
    try {
        SOAPHeader header = msg.getSOAPPart().getEnvelope().getHeader();
        if (header == null) {
            header = msg.getSOAPPart().getEnvelope().addHeader();
        }
        SecurityTokenManager manager = new SecurityTokenManager(null);
        BinarySecurityToken binaryToken = manager.getX509CertificateToken();
        binaryToken.setWSFVersion(SOAPBindingConstants.WSF_11_VERSION);
        binaryToken.addToParent(header);
        return binaryToken;
    } catch (Exception ex) {
        Utils.debug.error("MessageProcessor.addBinaryToken: " + "Could not add binary security token", ex);
        throw new SOAPBindingException(Utils.bundle.getString("cannotAddCorrelationHeader"));
    }
}
Also used : BinarySecurityToken(com.sun.identity.liberty.ws.common.wsse.BinarySecurityToken) SecurityTokenManager(com.sun.identity.liberty.ws.security.SecurityTokenManager) SOAPHeader(javax.xml.soap.SOAPHeader) SOAPException(javax.xml.soap.SOAPException)

Example 25 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project OpenAM by OpenRock.

the class MessageProcessor method getSecurityHeader.

/**
     * Returns the security header element.
     */
private Element getSecurityHeader(SOAPMessage soapMessage) throws SOAPBindingException {
    try {
        SOAPHeader header = soapMessage.getSOAPPart().getEnvelope().getHeader();
        NodeList headerChildNodes = header.getChildNodes();
        if ((headerChildNodes == null) || (headerChildNodes.getLength() == 0)) {
            throw new SOAPBindingException(Utils.bundle.getString("noSecurityHeader"));
        }
        for (int i = 0; i < headerChildNodes.getLength(); i++) {
            Node currentNode = headerChildNodes.item(i);
            if (currentNode.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            if ((WSSEConstants.TAG_SECURITYT.equals(currentNode.getLocalName())) && (WSSEConstants.NS_WSSE_WSF11.equals(currentNode.getNamespaceURI()))) {
                return (Element) currentNode;
            }
        }
        return null;
    } catch (SOAPException se) {
        Utils.debug.error("MessageProcess.getSecurityHeader:: " + "SOAPException", se);
        throw new SOAPBindingException(Utils.bundle.getString("noSecurityHeader"));
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) SOAPException(javax.xml.soap.SOAPException) SOAPHeader(javax.xml.soap.SOAPHeader)

Aggregations

SOAPHeader (javax.xml.soap.SOAPHeader)49 SOAPException (javax.xml.soap.SOAPException)30 SOAPMessage (javax.xml.soap.SOAPMessage)29 SOAPHeaderElement (javax.xml.soap.SOAPHeaderElement)22 SOAPElement (javax.xml.soap.SOAPElement)18 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)17 QName (javax.xml.namespace.QName)13 SOAPBody (javax.xml.soap.SOAPBody)13 Name (javax.xml.soap.Name)10 WebServiceException (javax.xml.ws.WebServiceException)9 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)8 ArrayList (java.util.ArrayList)7 SOAPPart (javax.xml.soap.SOAPPart)7 Test (org.junit.Test)7 Element (org.w3c.dom.Element)7 IOException (java.io.IOException)6 SOAPFactory (javax.xml.soap.SOAPFactory)6 NodeList (org.w3c.dom.NodeList)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4