Java Code Examples for javax.xml.soap.SOAPBody#getFirstChild()

The following examples show how to use javax.xml.soap.SOAPBody#getFirstChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaValidatorHandler.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private void validate(SOAPMessageContext context, String mode) {
   try {
      SOAPBody body = context.getMessage().getSOAPBody();
      SOAPFault fault = body.getFault();
      if (fault != null) {
         return;
      }

      Node payloadNode = body.getFirstChild();
      ValidatorHelper.validate(new DOMSource(payloadNode), this.isXOPEnabled(context), this.schemaFiles);
   } catch (Exception var6) {
      dumpMessage(context.getMessage(), mode, LOG);
      LOG.error(var6.getClass().getSimpleName() + ": " + var6.getMessage());
      throw SOAPFaultFactory.newSOAPFaultException(var6.getMessage(), var6);
   }

   LOG.info("Message validation done.");
}
 
Example 2
Source File: EcpAuthenticationHandler.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public AuthOutcome handle(OnSessionCreated onCreateSession) {
    String header = facade.getRequest().getHeader(PAOS_HEADER);

    if (header != null) {
        return doHandle(new SamlInvocationContext(), onCreateSession);
    } else {
        try {
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage(null, facade.getRequest().getInputStream());
            SOAPBody soapBody = soapMessage.getSOAPBody();
            Node authnRequestNode = soapBody.getFirstChild();
            Document document = DocumentUtil.createDocument();

            document.appendChild(document.importNode(authnRequestNode, true));

            String samlResponse = PostBindingUtil.base64Encode(DocumentUtil.asString(document));

            return doHandle(new SamlInvocationContext(null, samlResponse, null), onCreateSession);
        } catch (Exception e) {
            throw new RuntimeException("Error creating fault message.", e);
        }
    }
}
 
Example 3
Source File: Soap.java    From keycloak with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Returns a string encoded accordingly with the SAML HTTP POST Binding specification based on the
 * given <code>inputStream</code> which must contain a valid SOAP message.
 *
 * <p>The resulting string is based on the Body of the SOAP message, which should map to a valid SAML message.
 *
 * @param inputStream the input stream containing a valid SOAP message with a Body that contains a SAML message
 *
 * @return a string encoded accordingly with the SAML HTTP POST Binding specification
 */
public static String toSamlHttpPostMessage(InputStream inputStream) {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage(null, inputStream);
        SOAPBody soapBody = soapMessage.getSOAPBody();
        Node authnRequestNode = soapBody.getFirstChild();
        Document document = DocumentUtil.createDocument();

        document.appendChild(document.importNode(authnRequestNode, true));

        return PostBindingUtil.base64Encode(DocumentUtil.asString(document));
    } catch (Exception e) {
        throw new RuntimeException("Error creating fault message.", e);
    }
}
 
Example 4
Source File: Increment.java    From tomee with Apache License 2.0 6 votes vote down vote up
public boolean handleMessage(SOAPMessageContext mc) {
    try {
        final SOAPMessage message = mc.getMessage();
        final SOAPBody body = message.getSOAPBody();
        final String localName = body.getFirstChild().getLocalName();

        if ("sumResponse".equals(localName) || "multiplyResponse".equals(localName)) {
            final Node responseNode = body.getFirstChild();
            final Node returnNode = responseNode.getFirstChild();
            final Node intNode = returnNode.getFirstChild();

            final int value = new Integer(intNode.getNodeValue());
            intNode.setNodeValue(Integer.toString(value + 1));
        }

        return true;
    } catch (SOAPException e) {
        return false;
    }
}
 
Example 5
Source File: Inflate.java    From tomee with Apache License 2.0 6 votes vote down vote up
public boolean handleMessage(SOAPMessageContext mc) {
    try {
        final SOAPMessage message = mc.getMessage();
        final SOAPBody body = message.getSOAPBody();
        final String localName = body.getFirstChild().getLocalName();

        if ("sumResponse".equals(localName) || "multiplyResponse".equals(localName)) {
            final Node responseNode = body.getFirstChild();
            final Node returnNode = responseNode.getFirstChild();
            final Node intNode = returnNode.getFirstChild();

            final int value = new Integer(intNode.getNodeValue());
            intNode.setNodeValue(Integer.toString(value * 1000));
        }

        return true;
    } catch (SOAPException e) {
        return false;
    }
}
 
Example 6
Source File: SOAPHandlerInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected QName getOpQName(Exchange ex, Object data) {
    SOAPMessageContextImpl sm = (SOAPMessageContextImpl)data;
    try {
        SOAPMessage msg = sm.getMessage();
        if (msg == null) {
            return null;
        }
        SOAPBody body = SAAJUtils.getBody(msg);
        if (body == null) {
            return null;
        }
        org.w3c.dom.Node nd = body.getFirstChild();
        while (nd != null && !(nd instanceof org.w3c.dom.Element)) {
            nd = nd.getNextSibling();
        }
        if (nd != null) {
            return new QName(nd.getNamespaceURI(), nd.getLocalName());
        }
    } catch (SOAPException e) {
        //ignore, nothing we can do
    }
    return null;
}
 
Example 7
Source File: HWStreamSourceMessageProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public StreamSource invoke(StreamSource request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    StreamSource response = new StreamSource();
    try {
        SOAPMessage msg = factory.createMessage();
        msg.getSOAPPart().setContent(request);
        SOAPBody body = msg.getSOAPBody();
        Node n = body.getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response.setInputStream(sayHiInputStream);
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response.setInputStream(greetMeInputStream);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 8
Source File: HWSAXSourceMessageProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public SAXSource invoke(SAXSource request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    SAXSource response = new SAXSource();
    try {
        SOAPMessage msg = factory.createMessage();
        msg.getSOAPPart().setContent(request);
        SOAPBody body = msg.getSOAPBody();
        Node n = body.getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response.setInputSource(sayHiInputSource);
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response.setInputSource(greetMeInputSource);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 9
Source File: SchemaValidatorHandler.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private void validate(SOAPMessageContext context, String mode) {
   try {
      SOAPBody body = context.getMessage().getSOAPBody();
      SOAPFault fault = body.getFault();
      if (fault != null) {
         return;
      }

      Node payloadNode = body.getFirstChild();
      ValidatorHelper.Companion.validate(new DOMSource(payloadNode), this.isXOPEnabled(context), this.schemaFiles);
   } catch (Exception var6) {
      dumpMessage(context.getMessage(), mode, LOG);
      LOG.error(var6.getClass().getSimpleName() + ": " + var6.getMessage());
      throw SOAPFaultFactory.newSOAPFaultException(var6.getMessage(), var6);
   }

   LOG.info("Message validation done.");
}
 
Example 10
Source File: SchemaValidatorHandler.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private void validate(SOAPMessageContext context, String mode) {
   try {
      SOAPBody body = context.getMessage().getSOAPBody();
      SOAPFault fault = body.getFault();
      if (fault != null) {
         return;
      }

      Node payloadNode = body.getFirstChild();
      ValidatorHelper.validate(new DOMSource(payloadNode), this.isXOPEnabled(context), this.schemaFiles);
   } catch (Exception var6) {
      dumpMessage(context.getMessage(), mode, LOG);
      LOG.error(var6.getClass().getSimpleName() + ": " + var6.getMessage());
      throw SOAPFaultFactory.newSOAPFaultException(var6.getMessage(), var6);
   }

   LOG.info("Message validation done.");
}
 
Example 11
Source File: SchemaValidatorHandler.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private void validate(SOAPMessageContext context, String mode) {
   try {
      SOAPBody body = context.getMessage().getSOAPBody();
      SOAPFault fault = body.getFault();
      if (fault != null) {
         return;
      }

      Node payloadNode = body.getFirstChild();
      ValidatorHelper.validate(new DOMSource(payloadNode), this.isXOPEnabled(context), this.schemaFiles);
   } catch (Exception var6) {
      dumpMessage(context.getMessage(), mode, LOG);
      LOG.error(var6.getClass().getSimpleName() + ": " + var6.getMessage());
      throw SOAPFaultFactory.newSOAPFaultException(var6.getMessage(), var6);
   }

   LOG.info("Message validation done.");
}
 
Example 12
Source File: SchemaValidatorHandler.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private void validate(SOAPMessageContext context, String mode) {
   try {
      SOAPBody body = context.getMessage().getSOAPBody();
      SOAPFault fault = body.getFault();
      if (fault != null) {
         return;
      }

      Node payloadNode = body.getFirstChild();
      ValidatorHelper.validate(new DOMSource(payloadNode), this.isXOPEnabled(context), this.schemaFiles);
   } catch (Exception var6) {
      dumpMessage(context.getMessage(), mode, LOG);
      LOG.error(var6.getClass().getSimpleName() + ": " + var6.getMessage());
      throw SOAPFaultFactory.newSOAPFaultException(var6.getMessage(), var6);
   }

   LOG.info("Message validation done.");
}
 
Example 13
Source File: HWSoapMessageProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public SOAPMessage invoke(SOAPMessage request) {
    SOAPMessage response = null;
    try {
        SOAPBody body = SAAJUtils.getBody(request);
        Node n = body.getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response = sayHiResponse;
            if (request.countAttachments() > 0) {
                MessageFactory factory = MessageFactory.newInstance();
                InputStream is = getClass().getResourceAsStream("resources/sayHiRpcLiteralResp.xml");
                response = factory.createMessage(null, is);
                is.close();
                Iterator<AttachmentPart> it = CastUtils.cast(request.getAttachments(),
                                                             AttachmentPart.class);
                while (it.hasNext()) {
                    response.addAttachmentPart(it.next());
                }
            }
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response = greetMeResponse;
        } else {
            response = request;
            //response.writeTo(System.out);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 14
Source File: NBSoapMessageDocProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public SOAPMessage invoke(SOAPMessage request) {
    SOAPBody body = null;
    try {
        body = SAAJUtils.getBody(request);
    } catch (SOAPException e) {
        throw new RuntimeException("soap body expected");
    }
    if (body.getFirstChild() != null) {
        throw new RuntimeException("no body expected");
    }
    return sayHiResponse;
}
 
Example 15
Source File: HWSoapMessageDocProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public SOAPMessage resumeMessage(SOAPMessage request) {
    if (IncomingMessageCounterInterceptor.getMessageCount() != 1) {
        throw new RuntimeException("IncomingMessageCounterInterceptor get invoked twice");
    }
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    SOAPMessage response = null;
    try {
        SOAPBody body = request.getSOAPBody();
        Node n = body.getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response = sayHiResponse;
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            Element el = DOMUtils.getFirstElement(n);
            String v = DOMUtils.getContent(el);
            if (v.contains("Return sayHi")) {
                response = sayHiResponse;
            } else {
                response = greetMeResponse;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 16
Source File: HWDOMSourceMessageProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public DOMSource invoke(DOMSource request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    //XMLUtils.writeTo(request, System.out);
    DOMSource response = new DOMSource();
    try {
        SOAPMessage msg = factory.createMessage();
        msg.getSOAPPart().setContent(request);
        SOAPBody body = msg.getSOAPBody();
        Node n = body.getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response.setNode(sayHiResponse.getSOAPPart());
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response.setNode(greetMeResponse.getSOAPPart());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 17
Source File: CougarHelpers.java    From cougar with Apache License 2.0 5 votes vote down vote up
public Node extractResponseNode(SOAPMessage response) throws SOAPException{

		Node responseNode;

		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		try {
			response.writeTo(outStream);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
//		logger.LogBetfairDebugEntry("SOAP Response: " + outStream.toString());

		if (response.getSOAPBody().hasFault()) {
			responseNode = response.getSOAPBody().getFault();
		}
		else if(response.getSOAPBody().getFirstChild() == null){ // Response type is void
			responseNode = response.getSOAPBody();
		}
		else {
			// extract the body
			SOAPBody respBody = response.getSOAPBody();
			// First child should be the service name object
			Node serviceResponseNode = respBody.getFirstChild();

			// second child
			responseNode = serviceResponseNode.getFirstChild();
		}

		return responseNode;
	}
 
Example 18
Source File: HWSoapMessageDocProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
public SOAPMessage invoke(SOAPMessage request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    SOAPMessage response = null;
    SOAPBody body = null;
    try {
        body = SAAJUtils.getBody(request);
    } catch (SOAPException e) {
        return null;
    }
    Node n = body.getFirstChild();

    while (n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();
    }
    if (n.getLocalName().equals(sayHi.getLocalPart())) {
        response = sayHiResponse;
    } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
        Element el = DOMUtils.getFirstElement(n);
        String v = DOMUtils.getContent(el);
        if (v.contains("Return sayHi")) {
            response = sayHiResponse;
        } else if (v.contains("exceed maxLength")) {
            response = greetMeResponseExceedMaxLengthRestriction;
        } else if (v.contains("throwFault")) {
            try {
                SOAPFactory f = SOAPFactory.newInstance();
                SOAPFault soapFault = f.createFault();

                soapFault.setFaultString("Test Fault String ****");

                Detail detail = soapFault.addDetail();
                detail = soapFault.getDetail();

                QName qName = new QName("http://www.Hello.org/greeter", "TestFault", "ns");
                DetailEntry de = detail.addDetailEntry(qName);

                qName = new QName("http://www.Hello.org/greeter", "ErrorCode", "ns");
                SOAPElement errorElement = de.addChildElement(qName);
                errorElement.setTextContent("errorcode");
                throw new SOAPFaultException(soapFault);
            } catch (SOAPException ex) {
                //ignore
            }

        } else {
            response = greetMeResponse;
        }
    }
    return response;
}