Search in sources :

Example 51 with SOAPException

use of javax.xml.soap.SOAPException in project jaffa-framework by jaffa-projects.

the class WebServiceInvoker method unmarshalResponse.

/**
 * Unmarshals the response into a corresponding Java object.
 * @param response the SOAPMessage returned by a WebService.
 * @return the corresponding Java object.
 * @throws SOAPException if any SOAP error occurs.
 * @throws SOAPFaultException if any SOAPFault occurs.
 * @throws JAXBException if any XML (un)marshalling error occurs.
 */
protected Object unmarshalResponse(SOAPMessage response) throws SOAPException, SOAPFaultException, JAXBException {
    if (log.isDebugEnabled()) {
        log.debug("Unmarshalling response: " + response);
        try {
            StringWriter sw = new StringWriter();
            TransformerFactory.newInstance().newTransformer().transform(new DOMSource(response.getSOAPPart()), new StreamResult(sw));
            log.debug("Contents of response: " + sw.toString());
        } catch (Exception e) {
        // do nothing
        }
    }
    // Determine the response type
    Class responseClass = null;
    boolean isCollection = false;
    for (Method m : getWebServiceClass().getMethods()) {
        if (m.getName().equals(getOperationName()) && m.getAnnotation(WebMethod.class) != null) {
            responseClass = m.getReturnType();
            if (Collection.class.isAssignableFrom(responseClass)) {
                if (ParameterizedType.class.isAssignableFrom(m.getGenericReturnType().getClass())) {
                    isCollection = true;
                    ParameterizedType genericType = (ParameterizedType) m.getGenericReturnType();
                    responseClass = ((Class) (genericType.getActualTypeArguments()[0]));
                }
            }
            break;
        }
    }
    if (responseClass == null) {
        String s = "Unable to locate the WebMethod '" + getOperationName() + "' on the class " + getWebServiceClass();
        log.error(s);
        throw new IllegalArgumentException(s);
    }
    if (response.getSOAPBody().hasFault()) {
        SOAPFault fault = response.getSOAPBody().getFault();
        log.error("SOAPFault occurred while invoking WebService " + getWebServiceClass() + " with faultcode " + fault.getFaultCode());
        throw new SOAPFaultException(fault);
    }
    // The response body will be of the format
    // <env:Body>
    // <j:performInquiryResponse>
    // <return><summaryStockBalancesOutDto>...</return>
    // <return><summaryStockBalancesOutDto>...</return>
    // ...
    // </j:performInquiryResponse>
    // </env:Body>
    // Unmarshal the contents of the "return" node to an instance of the responseClass
    Object output = null;
    NodeList returnNodes = response.getSOAPBody().getFirstChild().getChildNodes();
    if (returnNodes != null && returnNodes.getLength() > 0) {
        if (isCollection) {
            output = Array.newInstance(responseClass, returnNodes.getLength());
            JAXBContext jc = JAXBHelper.obtainJAXBContext(responseClass);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            for (int i = 0; i < returnNodes.getLength(); i++) {
                JAXBElement jaxbElement = unmarshaller.unmarshal(returnNodes.item(i), responseClass);
                Array.set(output, i, jaxbElement.getValue());
            }
            output = Arrays.asList(output);
        } else if (responseClass.isArray()) {
            responseClass = responseClass.getComponentType();
            output = Array.newInstance(responseClass, returnNodes.getLength());
            JAXBContext jc = JAXBHelper.obtainJAXBContext(responseClass);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            for (int i = 0; i < returnNodes.getLength(); i++) {
                JAXBElement jaxbElement = unmarshaller.unmarshal(returnNodes.item(i), responseClass);
                Array.set(output, i, jaxbElement.getValue());
            }
        } else {
            JAXBContext jc = JAXBHelper.obtainJAXBContext(responseClass);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            JAXBElement jaxbElement = unmarshaller.unmarshal(returnNodes.item(0), responseClass);
            output = jaxbElement.getValue();
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Response unmarshalled to: " + output);
    }
    return output;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) NodeList(org.w3c.dom.NodeList) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) JAXBContext(javax.xml.bind.JAXBContext) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) JAXBElement(javax.xml.bind.JAXBElement) SOAPException(javax.xml.soap.SOAPException) JAXBException(javax.xml.bind.JAXBException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) ParameterizedType(java.lang.reflect.ParameterizedType) StringWriter(java.io.StringWriter) SOAPFault(javax.xml.soap.SOAPFault) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 52 with SOAPException

use of javax.xml.soap.SOAPException in project jaffa-framework by jaffa-projects.

the class WebServiceInvoker method createSOAPMessage.

/**
 * Generate SOAPMessage based on the input arguments.
 * <p>
 * For example:
 *   <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:j="http://product1.mirotechnologies.com/material/core/StockBalanceFinder">
 *     <SOAP-ENV:Header/>
 *     <SOAP-ENV:Body>
 *       <j:performInquiry>
 *         <arg0>
 *           <part><operator>Equals</operator><values>P1000</values></part>
 *         </arg0>
 *       </j:performInquiry>
 *     </SOAP-ENV:Body>
 *   </SOAP-ENV:Envelope>
 * @param arguments the arguments for the WebService.
 * @return a SOAPMessage.
 * @throws SOAPException if any SOAP error occurs.
 * @throws JAXBException if any XML (un)marshalling error occurs.
 */
protected SOAPMessage createSOAPMessage(Object... arguments) throws SOAPException, JAXBException {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage message = messageFactory.createMessage();
    SOAPPart messagePart = message.getSOAPPart();
    SOAPEnvelope envelope = messagePart.getEnvelope();
    String tns = this.obtainTargetNamespace();
    if (tns != null) {
        envelope.addNamespaceDeclaration(CUSTOM_PREFIX, tns);
    }
    SOAPBody body = message.getSOAPBody();
    SOAPElement operationElement = tns != null ? body.addBodyElement(envelope.createQName(getOperationName(), CUSTOM_PREFIX)) : body.addBodyElement(envelope.createName(getOperationName()));
    // Add authentication
    addAuthorization(message);
    // An arg{i} node will be created for each argument
    if (arguments != null) {
        Annotation[][] parameterAnnotations = null;
        for (Method m : getWebServiceClass().getMethods()) {
            if (m.getName().equals(getOperationName()) && m.getAnnotation(WebMethod.class) != null) {
                parameterAnnotations = m.getParameterAnnotations();
            }
        }
        for (int i = 0; i < arguments.length; i++) {
            Object argument = arguments[i];
            Class argumentClass = argument.getClass();
            String webParamName = null;
            if (parameterAnnotations != null && parameterAnnotations.length > 0) {
                for (Annotation[] annotations : parameterAnnotations) {
                    for (Annotation annotation : annotations) {
                        if (annotation instanceof WebParam) {
                            WebParam webParam = (WebParam) annotation;
                            webParamName = webParam.name();
                            if (log.isDebugEnabled()) {
                                log.debug("webParamName :" + webParamName);
                            }
                        }
                    }
                }
            }
            if (Collection.class.isAssignableFrom(argumentClass)) {
                JAXBContext jc = null;
                Marshaller marshaller = null;
                List list = (List) argument;
                for (Object arrayElement : list) {
                    if (jc == null) {
                        jc = JAXBHelper.obtainJAXBContext(arrayElement.getClass());
                        marshaller = jc.createMarshaller();
                    }
                    if (webParamName != null) {
                        marshaller.marshal(new JAXBElement(new QName(webParamName), arrayElement.getClass(), arrayElement), operationElement);
                    } else {
                        marshaller.marshal(new JAXBElement(new QName("arg0"), arrayElement.getClass(), arrayElement), operationElement);
                    }
                }
            } else if (argumentClass.isArray()) {
                argumentClass = argumentClass.getComponentType();
                JAXBContext jc = JAXBHelper.obtainJAXBContext(argumentClass);
                Marshaller marshaller = jc.createMarshaller();
                for (int j = 0, len = Array.getLength(argument); j < len; j++) {
                    Object arrayElement = Array.get(argument, j);
                    if (webParamName != null) {
                        marshaller.marshal(new JAXBElement(new QName(webParamName), argumentClass, arrayElement), operationElement);
                    } else {
                        marshaller.marshal(new JAXBElement(new QName("arg0"), argumentClass, arrayElement), operationElement);
                    }
                }
            } else {
                JAXBContext jc = JAXBHelper.obtainJAXBContext(argumentClass);
                Marshaller marshaller = jc.createMarshaller();
                if (webParamName != null) {
                    marshaller.marshal(new JAXBElement(new QName(webParamName), argumentClass, argument), operationElement);
                } else {
                    marshaller.marshal(new JAXBElement(new QName("arg0"), argumentClass, argument), operationElement);
                }
            }
        }
    }
    // Save all changes to the Message
    message.saveChanges();
    if (log.isDebugEnabled()) {
        log.debug("Created SOAPMessage: " + message);
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            message.writeTo(os);
            log.debug("Contents of SOAPMessage: " + os.toString());
        } catch (Exception e) {
        // do nothing
        }
    }
    return message;
}
Also used : Marshaller(javax.xml.bind.Marshaller) MessageFactory(javax.xml.soap.MessageFactory) QName(javax.xml.namespace.QName) JAXBContext(javax.xml.bind.JAXBContext) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) JAXBElement(javax.xml.bind.JAXBElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPMessage(javax.xml.soap.SOAPMessage) Annotation(java.lang.annotation.Annotation) SOAPException(javax.xml.soap.SOAPException) JAXBException(javax.xml.bind.JAXBException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPBody(javax.xml.soap.SOAPBody) WebParam(javax.jws.WebParam) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 53 with SOAPException

use of javax.xml.soap.SOAPException in project pentaho-platform by pentaho.

the class XMLABaseComponent method discover.

/**
 * discover
 *
 * @param request
 * @param discoverUrl
 * @param restrictions
 * @param properties
 * @param rh
 * @throws XMLAException
 */
private void discover(final String request, final URL discoverUrl, final Map restrictions, final Map properties, final Rowhandler rh) throws XMLAException {
    try {
        SOAPConnection connection = scf.createConnection();
        SOAPMessage msg = mf.createMessage();
        MimeHeaders mh = msg.getMimeHeaders();
        // $NON-NLS-1$ //$NON-NLS-2$
        mh.setHeader("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Discover\"");
        SOAPPart soapPart = msg.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        // $NON-NLS-1$//$NON-NLS-2$
        envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        // $NON-NLS-1$ //$NON-NLS-2$
        envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
        SOAPBody body = envelope.getBody();
        // $NON-NLS-1$//$NON-NLS-2$
        Name nDiscover = envelope.createName("Discover", "", XMLABaseComponent.XMLA_URI);
        SOAPElement eDiscover = body.addChildElement(nDiscover);
        eDiscover.setEncodingStyle(XMLABaseComponent.ENCODING_STYLE);
        // $NON-NLS-1$//$NON-NLS-2$
        Name nPara = envelope.createName("RequestType", "", XMLABaseComponent.XMLA_URI);
        SOAPElement eRequestType = eDiscover.addChildElement(nPara);
        eRequestType.addTextNode(request);
        // add the parameters
        if (restrictions != null) {
            addParameterList(envelope, eDiscover, "Restrictions", "RestrictionList", // $NON-NLS-1$ //$NON-NLS-2$
            restrictions);
        }
        // $NON-NLS-1$//$NON-NLS-2$
        addParameterList(envelope, eDiscover, "Properties", "PropertyList", properties);
        msg.saveChanges();
        debug(// $NON-NLS-1$
        Messages.getInstance().getString("XMLABaseComponent.DEBUG_0006_DISCOVER_REQUEST") + request);
        logSoapMsg(msg);
        // run the call
        SOAPMessage reply = connection.call(msg, discoverUrl);
        debug(// $NON-NLS-1$
        Messages.getInstance().getString("XMLABaseComponent.DEBUG_0007_DISCOVER_RESPONSE") + request);
        logSoapMsg(reply);
        errorCheck(reply);
        SOAPElement eRoot = findDiscoverRoot(reply);
        // $NON-NLS-1$ //$NON-NLS-2$
        Name nRow = envelope.createName("row", "", XMLABaseComponent.ROWS_URI);
        Iterator itRow = eRoot.getChildElements(nRow);
        while (itRow.hasNext()) {
            // RowLoop
            SOAPElement eRow = (SOAPElement) itRow.next();
            rh.handleRow(eRow, envelope);
        }
        // RowLoop
        connection.close();
    } catch (UnsupportedOperationException e) {
        throw new XMLAException(e);
    } catch (SOAPException e) {
        throw new XMLAException(e);
    }
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) Iterator(java.util.Iterator) SOAPConnection(javax.xml.soap.SOAPConnection) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) Name(javax.xml.soap.Name)

Example 54 with SOAPException

use of javax.xml.soap.SOAPException in project wildfly-swarm by wildfly-swarm.

the class MySOAPHandler method handleMessage.

@Override
public boolean handleMessage(SOAPMessageContext context) {
    SOAPMessage msg = context.getMessage();
    try {
        SOAPBody body = msg.getSOAPBody();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(body), new StreamResult(System.out));
    } catch (SOAPException | TransformerException e) {
        e.printStackTrace();
    }
    return true;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) SOAPException(javax.xml.soap.SOAPException) SOAPMessage(javax.xml.soap.SOAPMessage) TransformerException(javax.xml.transform.TransformerException)

Example 55 with SOAPException

use of javax.xml.soap.SOAPException in project photon-model by vmware.

the class SamlTokenExtractionHandler method handleMessage.

@Override
public boolean handleMessage(SOAPMessageContext smc) {
    if (!SamlUtils.isOutgoingMessage(smc)) {
        try {
            // Extract the Token
            SOAPBody responseBody = smc.getMessage().getSOAPBody();
            Node firstChild = responseBody.getFirstChild();
            if (firstChild != null && "RequestSecurityTokenResponseCollection".equalsIgnoreCase(firstChild.getLocalName())) {
                if (firstChild.getFirstChild() != null && "RequestSecurityTokenResponse".equalsIgnoreCase(firstChild.getFirstChild().getLocalName())) {
                    Node rstrNode = firstChild.getFirstChild();
                    if (rstrNode.getFirstChild() != null && "RequestedSecurityToken".equalsIgnoreCase(rstrNode.getFirstChild().getLocalName())) {
                        Node rstNode = rstrNode.getFirstChild();
                        if (rstNode.getFirstChild() != null && "Assertion".equalsIgnoreCase(rstNode.getFirstChild().getLocalName())) {
                            this.token = rstNode.getFirstChild();
                        }
                    }
                }
            } else {
                if (firstChild != null && "RequestSecurityTokenResponse".equalsIgnoreCase(firstChild.getLocalName())) {
                    if (firstChild.getFirstChild() != null && "RequestedSecurityToken".equalsIgnoreCase(firstChild.getFirstChild().getLocalName())) {
                        Node rstNode = firstChild.getFirstChild();
                        if (rstNode.getFirstChild() != null && "Assertion".equalsIgnoreCase(rstNode.getFirstChild().getLocalName())) {
                            this.token = rstNode.getFirstChild();
                        }
                    }
                }
            }
        } catch (SOAPException e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) Node(org.w3c.dom.Node) SOAPException(javax.xml.soap.SOAPException)

Aggregations

SOAPException (javax.xml.soap.SOAPException)225 SOAPMessage (javax.xml.soap.SOAPMessage)135 SOAPElement (javax.xml.soap.SOAPElement)71 SOAPBody (javax.xml.soap.SOAPBody)62 WebServiceException (javax.xml.ws.WebServiceException)57 IOException (java.io.IOException)53 Element (org.w3c.dom.Element)36 QName (javax.xml.namespace.QName)33 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)33 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)28 SOAPFault (javax.xml.soap.SOAPFault)27 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)25 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)24 Node (org.w3c.dom.Node)23 SOAPFactory (javax.xml.soap.SOAPFactory)22 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)20 SOAPHeader (javax.xml.soap.SOAPHeader)19 SOAPPart (javax.xml.soap.SOAPPart)19 Iterator (java.util.Iterator)17 Fault (org.apache.cxf.interceptor.Fault)16