Search in sources :

Example 21 with SOAPElement

use of javax.xml.soap.SOAPElement 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 22 with SOAPElement

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

Example 23 with SOAPElement

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

the class FSSOAPService method parseSOAPMessage.

/*
     * Parses the SOAPMessage and return the Element
     * corresponding to the liberty message(request/response).
     * @param message the <code>SOAPMessage</code> to be parsed
     * @return Element corresponding to liberty request/response
     */
public Element parseSOAPMessage(SOAPMessage message) {
    FSUtils.debug.message("FSSOAPService.parseSOAPMessage: Called");
    ByteArrayOutputStream bop = null;
    String xmlString = null;
    // first check if there was any soap error during verifyHost()
    try {
        // check the SOAP message for any SOAP
        // related errros before passing control to SAM processor
        bop = new ByteArrayOutputStream();
        message.writeTo(bop);
        xmlString = bop.toString(IFSConstants.DEFAULT_ENCODING);
        Document doc = XMLUtils.toDOMDocument(xmlString, FSUtils.debug);
        Element root = doc.getDocumentElement();
        String rootName = root.getLocalName();
        if ((rootName == null) || (rootName.length() == 0)) {
            FSUtils.debug.error("FSSOAPService.parseSOAPMessage: " + "Local name of the SOAPElement in  the" + " SOAPMessage passed seems to be missing");
            return null;
        }
        if (!(rootName.equals("Envelope")) || (!(root.getNamespaceURI().equals(IFSConstants.SOAP_URI)))) {
            FSUtils.debug.error("FSSOAPService.parseSOAPMessage: Could not" + "parse SOAPMessage, either root element is not Envelope" + " or invalid name space");
            return null;
        }
        NodeList nlbody = root.getChildNodes();
        int blength = nlbody.getLength();
        if (blength <= 0) {
            FSUtils.debug.error("FSSOAPService.parseSOAPMessage: Message " + "does not have body");
            return null;
        }
        Node child = null;
        boolean found = false;
        for (int i = 0; i < blength; i++) {
            child = (Node) nlbody.item(i);
            if (child.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            String childName = child.getLocalName();
            if (childName.equals("Body")) {
                found = true;
                // found the message body
                break;
            }
        }
        if (!found) {
            FSUtils.debug.error("FSSOAPService.parseSOAPMessage: Message " + "does not have body1");
            return null;
        }
        Element body = (Element) child;
        //Is soap-env:Fault
        NodeList nl = body.getElementsByTagNameNS(IFSConstants.SOAP_URI, "Fault");
        int length = nl.getLength();
        if (length > 1) {
            return null;
        }
        if (length != 0) {
            child = (Node) nl.item(0);
            if (child.getNodeType() != Node.ELEMENT_NODE) {
                return null;
            }
            if (child.getLocalName().equalsIgnoreCase("Fault")) {
                if (FSUtils.debug.messageEnabled()) {
                    FSUtils.debug.message("FSSOAPService." + " parseSOAPMessage soap-env:Fault found in the " + "SOAPMessage");
                }
                return (Element) child;
            }
        }
        try {
            //Is samlp:Request
            child = getSAMLElement(body, "Request");
            if (child != null) {
                return (Element) child;
            }
            //Is samlp:Response
            child = getSAMLElement(body, "Response");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:AuthnResponseEnvelope
            child = getFederationElement(body, "AuthnResponseEnvelope");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:AuthnRequest
            child = getFederationElement(body, "AuthnRequest");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:RegisterNameIdentifierRequest
            child = getFederationElement(body, "RegisterNameIdentifierRequest");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:RegisterNameIdentifierResponse
            child = getFederationElement(body, "RegisterNameIdentifierResponse");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:FederationTerminationNotification
            child = getFederationElement(body, "FederationTerminationNotification");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:LogoutRequest
            child = getFederationElement(body, "LogoutRequest");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:LogoutResponse
            child = getFederationElement(body, "LogoutResponse");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:NameIdentifierMappingRequest
            child = getFederationElement(body, "NameIdentifierMappingRequest");
            if (child != null) {
                return (Element) child;
            }
            //Is lib:NameIdentifierMappingResponse
            child = getFederationElement(body, "NameIdentifierMappingResponse");
            if (child != null) {
                return (Element) child;
            }
            FSUtils.debug.error("FSSOAPService.parseMessage:Invalid message type.");
        } catch (FSException e) {
            if (FSUtils.debug.messageEnabled()) {
                FSUtils.debug.message("SOAPService.parseSOAPMessage: " + "Couldn't object protocol message element.");
            }
        }
        return null;
    } catch (Exception e) {
        FSUtils.debug.error("FSSOAPService.parseSOAPMessage: Exception ", e);
        return null;
    }
}
Also used : SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) FSException(com.sun.identity.federation.common.FSException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) SOAPException(javax.xml.soap.SOAPException) FSException(com.sun.identity.federation.common.FSException) IOException(java.io.IOException)

Example 24 with SOAPElement

use of javax.xml.soap.SOAPElement in project wildfly by wildfly.

the class ReliableCheckHandler method verifySequenceAcknowledgement.

private boolean verifySequenceAcknowledgement(SOAPMessage message) throws SOAPException {
    Iterator headerElements = message.getSOAPHeader().getChildElements();
    boolean found = false;
    boolean otherRMHeadersFound = false;
    final QName sequenceAckQName = new QName("http://schemas.xmlsoap.org/ws/2005/02/rm", "SequenceAcknowledgement");
    while (headerElements.hasNext()) {
        SOAPElement soapElement = (SOAPElement) headerElements.next();
        if (sequenceAckQName.equals(soapElement.getElementQName())) {
            found = true;
        } else if ("http://schemas.xmlsoap.org/ws/2005/02/rm".equals(soapElement.getNamespaceURI())) {
            otherRMHeadersFound = true;
        }
    }
    //fail if we did not find the sequence ack and the message has other WS-RM headers (hence out of order) or has body contents (hence a non-reliable message is being processed)
    if (!found && (otherRMHeadersFound || message.getSOAPBody().getChildElements().hasNext())) {
        throw new WebServiceException("wsrm:SequenceAcknowledgement is not present in soap header");
    }
    return found;
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) SOAPElement(javax.xml.soap.SOAPElement)

Example 25 with SOAPElement

use of javax.xml.soap.SOAPElement in project jdk8u_jdk by JetBrains.

the class SaajEmptyNamespaceTest method testResetDefaultNamespaceSAAJ.

/*
     * Test that SOAP message with default namespace declaration that contains empty
     * string is properly processed by SAAJ reader.
     */
@Test
public void testResetDefaultNamespaceSAAJ() throws Exception {
    // Create SOAP message from XML string and process it with SAAJ reader
    XMLStreamReader envelope = XMLInputFactory.newFactory().createXMLStreamReader(new StringReader(INPUT_SOAP_MESSAGE));
    StreamMessage streamMessage = new StreamMessage(SOAPVersion.SOAP_11, envelope, null);
    SAAJFactory saajFact = new SAAJFactory();
    SOAPMessage soapMessage = saajFact.readAsSOAPMessage(SOAPVersion.SOAP_11, streamMessage);
    // Check if constructed object model meets local names and namespace expectations
    SOAPElement request = (SOAPElement) soapMessage.getSOAPBody().getFirstChild();
    // Check top body element name
    Assert.assertEquals(request.getLocalName(), "SampleServiceRequest");
    // Check top body element namespace
    Assert.assertEquals(request.getNamespaceURI(), TEST_NS);
    SOAPElement params = (SOAPElement) request.getFirstChild();
    // Check first child name
    Assert.assertEquals(params.getLocalName(), "RequestParams");
    // Check if first child namespace is null
    Assert.assertNull(params.getNamespaceURI());
    // Check inner elements of the first child
    SOAPElement param1 = (SOAPElement) params.getFirstChild();
    Assert.assertEquals(param1.getLocalName(), "Param1");
    Assert.assertNull(param1.getNamespaceURI());
    SOAPElement param2 = (SOAPElement) params.getChildNodes().item(1);
    Assert.assertEquals(param2.getLocalName(), "Param2");
    Assert.assertNull(param2.getNamespaceURI());
    // Check full content of SOAP body
    Assert.assertEquals(nodeToText(request), EXPECTED_RESULT);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SAAJFactory(com.sun.xml.internal.ws.api.message.saaj.SAAJFactory) StringReader(java.io.StringReader) SOAPElement(javax.xml.soap.SOAPElement) StreamMessage(com.sun.xml.internal.ws.message.stream.StreamMessage) SOAPMessage(javax.xml.soap.SOAPMessage) Test(org.testng.annotations.Test)

Aggregations

SOAPElement (javax.xml.soap.SOAPElement)29 SOAPMessage (javax.xml.soap.SOAPMessage)20 SOAPBody (javax.xml.soap.SOAPBody)17 SOAPException (javax.xml.soap.SOAPException)14 QName (javax.xml.namespace.QName)12 Test (org.testng.annotations.Test)7 NodeList (org.w3c.dom.NodeList)7 Node (org.w3c.dom.Node)6 IOException (java.io.IOException)5 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)5 Detail (javax.xml.soap.Detail)4 MessageFactory (javax.xml.soap.MessageFactory)4 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)4 SOAPFault (javax.xml.soap.SOAPFault)4 Iterator (java.util.Iterator)3 SOAPFactory (javax.xml.soap.SOAPFactory)3 SOAPHeaderElement (javax.xml.soap.SOAPHeaderElement)3 SOAPPart (javax.xml.soap.SOAPPart)3 WebServiceException (javax.xml.ws.WebServiceException)3 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)3