Search in sources :

Example 21 with SOAPBody

use of javax.xml.soap.SOAPBody 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 SOAPBody

use of javax.xml.soap.SOAPBody 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 23 with SOAPBody

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

the class SAMLSOAPReceiver method FormSOAPError.

/**
     * This method forms  a SOAP Fault and puts it in the SOAP Message's
     * Body.
     */
private SOAPMessage FormSOAPError(HttpServletResponse resp, String faultCode, String faultString, String detail) {
    SOAPMessage msg = null;
    SOAPEnvelope envelope = null;
    SOAPFault sf = null;
    SOAPBody body = null;
    SOAPElement se = null;
    try {
        msg = msgFactory.createMessage();
        envelope = msg.getSOAPPart().getEnvelope();
        body = envelope.getBody();
        sf = body.addFault();
        Name qName = envelope.createName(faultCode, null, SOAPConstants.URI_NS_SOAP_ENVELOPE);
        sf.setFaultCode(qName);
        sf.setFaultString(SAMLUtils.bundle.getString(faultString));
        if ((detail != null) && !(detail.length() == 0)) {
            Detail det = sf.addDetail();
            se = (SOAPElement) det.addDetailEntry(envelope.createName("Problem"));
            se.addAttribute(envelope.createName("details"), SAMLUtils.bundle.getString(detail));
        }
    } catch (SOAPException e) {
        SAMLUtils.debug.error("FormSOAPError:", e);
        String[] data = { SAMLUtils.bundle.getString("soapFaultError") };
        LogUtils.error(java.util.logging.Level.INFO, LogUtils.SOAP_FAULT_ERROR, data);
        resp.setStatus(resp.SC_INTERNAL_SERVER_ERROR);
    }
    return msg;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) SOAPElement(javax.xml.soap.SOAPElement) SOAPFault(javax.xml.soap.SOAPFault) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) Detail(javax.xml.soap.Detail) Name(javax.xml.soap.Name)

Example 24 with SOAPBody

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

the class SAMLSOAPReceiver method containsFault.

/**
     * containsFault is a utiltiy method to see if msg contains a soapfault.
     */
private boolean containsFault(SOAPMessage msg) {
    try {
        SOAPPart sp = msg.getSOAPPart();
        SOAPEnvelope se = sp.getEnvelope();
        SOAPBody sb = se.getBody();
        return (sb.hasFault());
    } catch (Exception e) {
        if (SAMLUtils.debug.messageEnabled()) {
            SAMLUtils.debug.message("Error in containFault!");
        }
        return false;
    }
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SOAPPart(javax.xml.soap.SOAPPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) ServletException(javax.servlet.ServletException) SOAPException(javax.xml.soap.SOAPException) SAMLRequestVersionTooHighException(com.sun.identity.saml.common.SAMLRequestVersionTooHighException) SAMLRequesterException(com.sun.identity.saml.common.SAMLRequesterException) SAMLRequestVersionTooLowException(com.sun.identity.saml.common.SAMLRequestVersionTooLowException) SAMLException(com.sun.identity.saml.common.SAMLException)

Example 25 with SOAPBody

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

the class FSSOAPService method formSOAPError.

/**
     * Forms a SOAP Fault and puts it in the SOAP Message's Body.
     *
     * @param faultcode fault code to be set in SOAPMEssage
     * @param faultString fault string
     * @param detail the details of the fault condition
     * @return <code>SOAPMessage</code> containing the SOAP fault
     */
public SOAPMessage formSOAPError(String faultcode, String faultString, String detail) {
    SOAPMessage msg = null;
    SOAPEnvelope envelope = null;
    SOAPFault sf = null;
    SOAPBody body = null;
    SOAPElement se = null;
    try {
        msg = fac.createMessage();
        envelope = msg.getSOAPPart().getEnvelope();
        body = envelope.getBody();
        sf = body.addFault();
        Name qname = envelope.createName(faultcode, null, IFSConstants.SOAP_URI);
        sf.setFaultCode(qname);
        sf.setFaultString(FSUtils.bundle.getString(faultString));
        if ((detail != null) && !(detail.length() == 0)) {
            Detail det = sf.addDetail();
            se = (SOAPElement) det.addDetailEntry(envelope.createName("Problem"));
            se.addAttribute(envelope.createName("details"), FSUtils.bundle.getString(detail));
        }
    } catch (SOAPException e) {
        FSUtils.debug.error("FSSOAPService.formSOAPError:", e);
        return null;
    }
    return msg;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) SOAPElement(javax.xml.soap.SOAPElement) SOAPFault(javax.xml.soap.SOAPFault) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) Detail(javax.xml.soap.Detail) Name(javax.xml.soap.Name)

Aggregations

SOAPBody (javax.xml.soap.SOAPBody)34 SOAPMessage (javax.xml.soap.SOAPMessage)30 SOAPElement (javax.xml.soap.SOAPElement)17 SOAPException (javax.xml.soap.SOAPException)16 SOAPPart (javax.xml.soap.SOAPPart)14 MessageFactory (javax.xml.soap.MessageFactory)13 NodeList (org.w3c.dom.NodeList)12 QName (javax.xml.namespace.QName)9 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)9 Element (org.w3c.dom.Element)9 StreamSource (javax.xml.transform.stream.StreamSource)8 BufferedWriter (java.io.BufferedWriter)7 InputStream (java.io.InputStream)7 OutputStreamWriter (java.io.OutputStreamWriter)7 HttpURLConnection (java.net.HttpURLConnection)7 Node (org.w3c.dom.Node)7 IOException (java.io.IOException)6 Test (org.testng.annotations.Test)6 Vector (java.util.Vector)5 SOAPHeader (javax.xml.soap.SOAPHeader)5