Search in sources :

Example 56 with SOAPException

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

the class VcSessionHandler method handleMessage.

@Override
public boolean handleMessage(SOAPMessageContext smc) {
    if (isOutgoingMessage(smc)) {
        try {
            SOAPHeader header = getSOAPHeader(smc);
            SOAPElement vcsessionHeader = header.addChildElement(new javax.xml.namespace.QName("#", "vcSessionCookie"));
            vcsessionHeader.setValue(this.vcSessionCookie);
        } catch (DOMException e) {
            throw new RuntimeException(e);
        } catch (SOAPException e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : DOMException(org.w3c.dom.DOMException) SOAPException(javax.xml.soap.SOAPException) SOAPElement(javax.xml.soap.SOAPElement) QName(javax.xml.namespace.QName) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 57 with SOAPException

use of javax.xml.soap.SOAPException in project cerberus-source by cerberustesting.

the class SoapService method callSOAP.

@Override
public AnswerItem<AppService> callSOAP(String envelope, String servicePath, String soapOperation, String attachmentUrl, List<AppServiceHeader> header, String token, int timeOutMs, String system) {
    AnswerItem result = new AnswerItem();
    String unescapedEnvelope = StringEscapeUtils.unescapeXml(envelope);
    boolean is12SoapVersion = SOAP_1_2_NAMESPACE_PATTERN.matcher(unescapedEnvelope).matches();
    AppService serviceSOAP = factoryAppService.create("", AppService.TYPE_SOAP, null, "", "", envelope, "", servicePath, "", soapOperation, "", null, "", null);
    serviceSOAP.setTimeoutms(timeOutMs);
    ByteArrayOutputStream out = null;
    MessageEvent message = null;
    if (StringUtil.isNullOrEmpty(servicePath)) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_SERVICEPATHMISSING);
        result.setResultMessage(message);
        return result;
    }
    if (StringUtil.isNullOrEmpty(envelope)) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_ENVELOPEMISSING);
        result.setResultMessage(message);
        return result;
    }
    // If header is null we create the list empty.
    if (header == null) {
        header = new ArrayList<>();
    }
    // We feed the header with token + Standard SOAP header.
    if (token != null) {
        header.add(factoryAppServiceHeader.create(null, "cerberus-token", token, "Y", 0, "", "", null, "", null));
    }
    if (StringUtil.isNullOrEmpty(soapOperation)) {
        header.add(factoryAppServiceHeader.create(null, "SOAPAction", "", "Y", 0, "", "", null, "", null));
    } else {
        header.add(factoryAppServiceHeader.create(null, "SOAPAction", "\"" + soapOperation + "\"", "Y", 0, "", "", null, "", null));
    }
    header.add(factoryAppServiceHeader.create(null, "Content-Type", is12SoapVersion ? SOAPConstants.SOAP_1_2_CONTENT_TYPE : SOAPConstants.SOAP_1_1_CONTENT_TYPE, "Y", 0, "", "", null, "", null));
    serviceSOAP.setHeaderList(header);
    SOAPConnectionFactory soapConnectionFactory;
    SOAPConnection soapConnection = null;
    try {
        // Initialize SOAP Connection
        soapConnectionFactory = SOAPConnectionFactory.newInstance();
        soapConnection = soapConnectionFactory.createConnection();
        LOG.debug("Connection opened");
        // Create SOAP Request
        LOG.debug("Create request");
        SOAPMessage input = createSoapRequest(envelope, soapOperation, header, token);
        // Add attachment File if specified
        if (!StringUtil.isNullOrEmpty(attachmentUrl)) {
            this.addAttachmentPart(input, attachmentUrl);
            // Store the SOAP Call
            out = new ByteArrayOutputStream();
            input.writeTo(out);
            LOG.debug("WS call with attachement : " + out.toString());
            serviceSOAP.setServiceRequest(out.toString());
        } else {
            // Store the SOAP Call
            out = new ByteArrayOutputStream();
            input.writeTo(out);
            LOG.debug("WS call : " + out.toString());
        }
        // We already set the item in order to keep the request message in case of failure of SOAP calls.
        serviceSOAP.setService(servicePath);
        result.setItem(serviceSOAP);
        // Call the WS
        LOG.debug("Calling WS.");
        // Reset previous Authentification.
        Authenticator.setDefault(null);
        serviceSOAP.setProxyWithCredential(false);
        serviceSOAP.setProxyUser(null);
        SOAPMessage soapResponse = null;
        if (proxyService.useProxy(servicePath, system)) {
            // Get Proxy host and port from parameters.
            String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", system, DEFAULT_PROXY_HOST);
            int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", system, DEFAULT_PROXY_PORT);
            serviceSOAP.setProxy(true);
            serviceSOAP.setProxyHost(proxyHost);
            serviceSOAP.setProxyPort(proxyPort);
            // Create the Proxy.
            SocketAddress sockaddr = new InetSocketAddress(proxyHost, proxyPort);
            try (Socket socket = new Socket()) {
                socket.connect(sockaddr, 10000);
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(socket.getInetAddress(), proxyPort));
                if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", system, DEFAULT_PROXYAUTHENT_ACTIVATE)) {
                    // Get the credentials from parameters.
                    String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", system, DEFAULT_PROXYAUTHENT_USER);
                    String proxyPass = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", system, DEFAULT_PROXYAUTHENT_PASSWORD);
                    serviceSOAP.setProxyWithCredential(true);
                    serviceSOAP.setProxyUser(proxyUser);
                    // Define the credential to the proxy.
                    initializeProxyAuthenticator(proxyUser, proxyPass);
                }
                // Call with Proxy.
                soapResponse = sendSOAPMessage(input, servicePath, proxy, timeOutMs);
            } catch (Exception e) {
                LOG.warn(e.toString());
            }
        } else {
            serviceSOAP.setProxy(false);
            serviceSOAP.setProxyHost(null);
            serviceSOAP.setProxyPort(0);
            // Call without proxy.
            soapResponse = sendSOAPMessage(input, servicePath, null, timeOutMs);
        // soapResponse = soapConnection.call(input, servicePath);
        }
        LOG.debug("Called WS.");
        out = new ByteArrayOutputStream();
        // Store the response
        soapResponse.writeTo(out);
        LOG.debug("WS response received.");
        LOG.debug("WS response : " + out.toString());
        message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CALLSOAP);
        message.setDescription(message.getDescription().replace("%SERVICEPATH%", servicePath).replace("%SOAPMETHOD%", soapOperation));
        result.setResultMessage(message);
        // soapResponse.getSOAPPart().getEnvelope().getBody().getFault().getFaultCode();
        // We save convert to string the final response from SOAP request.
        serviceSOAP.setResponseHTTPBody(SoapUtil.convertSoapMessageToString(soapResponse));
        // Get result Content Type.
        serviceSOAP.setResponseHTTPBodyContentType(appServiceService.guessContentType(serviceSOAP, AppService.RESPONSEHTTPBODYCONTENTTYPE_XML));
        result.setItem(serviceSOAP);
    } catch (SOAPException | UnsupportedOperationException | IOException | SAXException | ParserConfigurationException | CerberusException e) {
        LOG.error(e.toString());
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP);
        message.setDescription(message.getDescription().replace("%SERVICEPATH%", servicePath).replace("%SOAPMETHOD%", soapOperation).replace("%DESCRIPTION%", e.getMessage()));
        result.setResultMessage(message);
        return result;
    } finally {
        try {
            if (soapConnection != null) {
                soapConnection.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (SOAPException | IOException ex) {
            LOG.error(ex);
        } finally {
            result.setResultMessage(message);
        }
    }
    return result;
}
Also used : SOAPConnectionFactory(javax.xml.soap.SOAPConnectionFactory) IFactoryAppService(org.cerberus.crud.factory.IFactoryAppService) AppService(org.cerberus.crud.entity.AppService) CerberusException(org.cerberus.exception.CerberusException) MessageEvent(org.cerberus.engine.entity.MessageEvent) InetSocketAddress(java.net.InetSocketAddress) SOAPConnection(javax.xml.soap.SOAPConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AnswerItem(org.cerberus.util.answer.AnswerItem) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) CerberusException(org.cerberus.exception.CerberusException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Proxy(java.net.Proxy) SOAPException(javax.xml.soap.SOAPException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 58 with SOAPException

use of javax.xml.soap.SOAPException in project cerberus-source by cerberustesting.

the class SoapService method sendSOAPMessage.

private static SOAPMessage sendSOAPMessage(SOAPMessage message, String url, final Proxy p, final int timeoutms) throws SOAPException, MalformedURLException {
    SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
    SOAPConnection connection = factory.createConnection();
    URL endpoint = new URL(null, url, new URLStreamHandler() {

        protected URLConnection openConnection(URL url) throws IOException {
            // The url is the parent of this stream handler, so must
            // create clone
            URL clone = new URL(url.toString());
            URLConnection connection = null;
            if (p == null) {
                connection = clone.openConnection();
            } else if (p.address().toString().equals("0.0.0.0/0.0.0.0:80")) {
                connection = clone.openConnection();
            } else {
                connection = clone.openConnection(p);
            }
            // Set Timeout
            connection.setConnectTimeout(timeoutms);
            connection.setReadTimeout(timeoutms);
            // connection.addRequestProperty("Developer-Mood", "Happy");
            return connection;
        }
    });
    try {
        SOAPMessage response = connection.call(message, endpoint);
        connection.close();
        return response;
    } catch (Exception e) {
        // Re-try if the connection failed
        SOAPMessage response = connection.call(message, endpoint);
        connection.close();
        return response;
    }
}
Also used : URLStreamHandler(java.net.URLStreamHandler) SOAPConnectionFactory(javax.xml.soap.SOAPConnectionFactory) SOAPConnection(javax.xml.soap.SOAPConnection) IOException(java.io.IOException) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) URLConnection(java.net.URLConnection) SOAPException(javax.xml.soap.SOAPException) CerberusException(org.cerberus.exception.CerberusException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 59 with SOAPException

use of javax.xml.soap.SOAPException in project arctic-sea by 52North.

the class AbstractSoapDecoder method getSOAPBodyContent.

/**
 * Parses the SOAPBody content to a text representation
 *
 * @param message
 *            SOAP message
 *
 * @return SOAPBody content as text
 *
 * @throws DecodingException
 *             * if an error occurs.
 */
protected OwsServiceRequest getSOAPBodyContent(SOAPMessage message) throws DecodingException {
    try {
        Document bodyRequestDoc = message.getSOAPBody().extractContentAsDocument();
        String xmlString = W3cHelper.nodeToXmlString(bodyRequestDoc.getDocumentElement());
        return decodeXmlElement(XmlObject.Factory.parse(xmlString, getXmlOptions()));
    } catch (SOAPException | XmlException | IOException e) {
        throw new DecodingException("Error while parsing SOAPMessage body content!", e);
    }
}
Also used : XmlException(org.apache.xmlbeans.XmlException) SOAPException(javax.xml.soap.SOAPException) DecodingException(org.n52.svalbard.decode.exception.DecodingException) IOException(java.io.IOException) Document(org.w3c.dom.Document)

Example 60 with SOAPException

use of javax.xml.soap.SOAPException in project arctic-sea by 52North.

the class Soap11Encoder method encode.

@Override
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
public SOAPMessage encode(SoapResponse soapResponse, EncodingContext additionalValues) throws EncodingException {
    if (soapResponse == null) {
        throw new UnsupportedEncoderInputException(this, soapResponse);
    }
    String soapVersion = soapResponse.getSoapVersion();
    SOAPMessage soapResponseMessage;
    String action = null;
    try {
        soapResponseMessage = SoapHelper.getSoapMessageForProtocol(soapVersion);
        if (soapResponse.getSoapFault() != null) {
            createSOAPFault(soapResponseMessage.getSOAPBody().addFault(), soapResponse.getSoapFault());
        } else {
            if (soapResponse.getException() != null) {
                action = createSOAPFaultFromExceptionResponse(soapResponseMessage.getSOAPBody().addFault(), soapResponse.getException());
                addSchemaLocationForExceptionToSOAPMessage(soapResponseMessage);
            } else {
                action = createSOAPBody(soapResponseMessage, soapResponse, soapResponse.getSoapAction());
            }
        }
        if (soapResponse.getHeader() != null) {
            List<SoapHeader> headers = soapResponse.getHeader();
            for (SoapHeader header : headers) {
                if (WsaConstants.NS_WSA.equals(header.getNamespace()) && header instanceof WsaActionHeader) {
                    ((WsaHeader) header).setValue(action);
                }
                Encoder<Map<QName, String>, SoapHeader> encoder = getEncoder(CodingHelper.getEncoderKey(header.getNamespace(), header));
                if (encoder != null) {
                    Map<QName, String> headerElements = encoder.encode(header);
                    for (Entry<QName, String> entry : headerElements.entrySet()) {
                        QName qName = entry.getKey();
                        soapResponseMessage.getSOAPHeader().addChildElement(qName).setTextContent(headerElements.get(qName));
                    }
                }
            }
        } else {
            soapResponseMessage.getSOAPHeader().detachNode();
        }
        soapResponseMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, String.valueOf(true));
        return soapResponseMessage;
    } catch (SOAPException soape) {
        throw new EncodingException("Error while encoding SOAPMessage!", soape);
    }
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) QName(javax.xml.namespace.QName) SOAPMessage(javax.xml.soap.SOAPMessage) WsaHeader(org.n52.shetland.w3c.wsa.WsaHeader) WsaActionHeader(org.n52.shetland.w3c.wsa.WsaActionHeader) UnsupportedEncoderInputException(org.n52.svalbard.encode.exception.UnsupportedEncoderInputException) SOAPException(javax.xml.soap.SOAPException) SoapHeader(org.n52.shetland.w3c.soap.SoapHeader) Map(java.util.Map) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

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