Example usage for javax.xml.soap SOAPConnectionFactory newInstance

List of usage examples for javax.xml.soap SOAPConnectionFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.soap SOAPConnectionFactory newInstance.

Prototype

public static SOAPConnectionFactory newInstance() throws SOAPException, UnsupportedOperationException 

Source Link

Document

Creates an instance of the default SOAPConnectionFactory object.

Usage

From source file:edu.unc.lib.dl.util.TripleStoreQueryServiceMulgaraImpl.java

private String sendTQL(String query) {
    log.debug(query);/* www.  j  a  v  a2 s  . com*/
    String result = null;
    SOAPMessage reply = null;
    SOAPConnection connection = null;
    try {
        // Next, create the actual message
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        message.getMimeHeaders().setHeader("SOAPAction", "itqlbean:executeQueryToString");
        SOAPBody soapBody = message.getSOAPPart().getEnvelope().getBody();
        soapBody.addNamespaceDeclaration("xsd", JDOMNamespaceUtil.XSD_NS.getURI());
        soapBody.addNamespaceDeclaration("xsi", JDOMNamespaceUtil.XSI_NS.getURI());
        soapBody.addNamespaceDeclaration("itqlbean", this.getItqlEndpointURL());
        SOAPElement eqts = soapBody.addChildElement("executeQueryToString", "itqlbean");
        SOAPElement queryStr = eqts.addChildElement("queryString", "itqlbean");
        queryStr.setAttributeNS(JDOMNamespaceUtil.XSI_NS.getURI(), "xsi:type", "xsd:string");
        CDATASection queryCDATA = message.getSOAPPart().createCDATASection(query);
        queryStr.appendChild(queryCDATA);
        message.saveChanges();

        // First create the connection
        SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
        connection = soapConnFactory.createConnection();
        reply = connection.call(message, this.getItqlEndpointURL());

        if (reply.getSOAPBody().hasFault()) {
            reportSOAPFault(reply);
            if (log.isDebugEnabled()) {
                // log the full soap body response
                DOMBuilder builder = new DOMBuilder();
                org.jdom2.Document jdomDoc = builder.build(reply.getSOAPBody().getOwnerDocument());
                log.debug(new XMLOutputter().outputString(jdomDoc));
            }
        } else {
            NodeList nl = reply.getSOAPPart().getEnvelope().getBody().getElementsByTagNameNS("*",
                    "executeQueryToStringReturn");
            if (nl.getLength() > 0) {
                result = nl.item(0).getFirstChild().getNodeValue();
            }
            log.debug(result);
        }
    } catch (SOAPException e) {
        log.error("Failed to prepare or send iTQL via SOAP", e);
        throw new RuntimeException("Cannot query triple store at " + this.getItqlEndpointURL(), e);
    } finally {
        try {
            connection.close();
        } catch (SOAPException e) {
            log.error("Failed to close SOAP connection", e);
            throw new RuntimeException(
                    "Failed to close SOAP connection for triple store at " + this.getItqlEndpointURL(), e);
        }
    }
    return result;
}

From source file:org.apache.axis2.saaj.SOAPConnectionTest.java

public void testClose() {
    try {/*from   w  ww  .j a va2s. c  o  m*/
        SOAPConnection sCon = SOAPConnectionFactory.newInstance().createConnection();
        sCon.close();
    } catch (SOAPException e) {
        fail("Unexpected Exception " + e);
    }
}

From source file:org.apache.axis2.saaj.SOAPConnectionTest.java

public void testCloseTwice() {
    SOAPConnectionFactory soapConnectionFactory = null;
    try {/*from   w w  w. j a  va2 s . c  o m*/
        soapConnectionFactory = SOAPConnectionFactory.newInstance();
    } catch (SOAPException e) {
        fail("Unexpected Exception " + e);
    }

    SOAPConnection sCon = null;
    try {
        sCon = soapConnectionFactory.createConnection();
        sCon.close();
    } catch (SOAPException e) {
        fail("Unexpected Exception " + e);
    }

    try {
        sCon.close();
        fail("Expected Exception did not occur");
    } catch (SOAPException e) {
        assertTrue(true);
    }
}

From source file:org.apache.axis2.saaj.SOAPConnectionTest.java

public void testCallOnCloseConnection() {
    SOAPConnectionFactory soapConnectionFactory = null;
    try {//from w  w  w .  j  av a  2s  .  com
        soapConnectionFactory = SOAPConnectionFactory.newInstance();
    } catch (SOAPException e) {
        fail("Unexpected Exception " + e);
    }

    SOAPConnection sCon = null;
    try {
        sCon = soapConnectionFactory.createConnection();
        sCon.close();
    } catch (SOAPException e) {
        fail("Unexpected Exception " + e);
    }

    try {
        sCon.call(null, new Object());
        fail("Expected Exception did not occur");
    } catch (SOAPException e) {
        assertTrue(true);
    }
}

From source file:org.apache.ws.scout.transport.SaajTransport.java

public Element send(Element request, URI endpointURL) throws TransportException {
    if (log.isDebugEnabled()) {
        String requestMessage = XMLUtils.convertNodeToXMLString(request);
        log.debug("Request message: %s\n%s" + endpointURL + ":" + requestMessage);
    }//from   ww  w .  j  a va2  s  . co  m

    Element response = null;
    try {
        SOAPMessage message = this.createSOAPMessage(request);
        //Make the SAAJ Call now
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = soapConnectionFactory.createConnection();
        SOAPMessage soapResponse = connection.call(message, endpointURL.toURL());

        SOAPBody soapBody = soapResponse.getSOAPBody();
        boolean hasFault = soapBody.hasFault();
        if (hasFault) {
            SOAPFault soapFault = soapBody.getFault();
            String faultStr = soapFault.getFaultCode() + "::" + soapFault.getFaultString();
            throw new RegistryException(faultStr);
        }
        response = getFirstChildElement(soapBody);
    } catch (Exception ex) {
        log.error("Exception::" + ex.getMessage(), ex);
        throw new TransportException(ex);
    }
    if (log.isDebugEnabled()) {
        String responseMessage = XMLUtils.convertNodeToXMLString(response);
        log.debug("Response message: %s" + responseMessage);
    }

    return response;
}

From source file:org.belio.service.gateway.SafcomGateway.java

private boolean sendMessage(QueueType queueType, Outbox outbox) {
    try {/*from   w w w . j  av  a 2 s .  co m*/
        String now = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        MessageDigest md = MessageDigest.getInstance("MD5");
        String serviceId = outbox.getServiceID();
        String endpointDef = "";
        //            if (queueType.equals(QueueType.BULK)) {
        //                endpointDef = networkproperties.getProperty("safcom_mt_endpoint");
        //            } else {
        endpointDef = networkproperties.getProperty("safcom_endpoint");
        // }
        String code = outbox.getShortCode();
        String spIdString = outbox.getSdpId();
        String spPasswordString = createSpPass(spIdString, now, md);
        String recepient = "tel:" + outbox.getMsisdn();
        String actualmessage = URLDecoder.decode(URLEncoder.encode(outbox.getText(), "UTF-8"), "UTF-8");
        Launcher.LOG.info("T----------------------------------------J" + actualmessage);
        String gencorrelator = String.valueOf(outbox.getRefNo());
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
        // Send SOAP Message to SOAP Server
        SOAPMessage messageToSend = getSoapMessageFromString(getMessage(spIdString, spPasswordString, recepient,
                serviceId, now, actualmessage, code, gencorrelator, endpointDef));
        Calendar start = Calendar.getInstance();
        SOAPMessage soapResponse = soapConnection.call(messageToSend,
                networkproperties.getProperty("safcom_sms"));
        Launcher.LOG.info(
                recepient + " - took " + (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis()));
        //            SOAPMessage soapResponse = null;
        System.out.println("XXXXXXXXXXXXXXXXXXX====Sending Safaricom message");
        //            printSOAPResponse(soapResponse);//TODO log
        SOAPBody body = soapResponse.getSOAPBody();
        if (body.hasFault()) {
            SOAPFault newFault = body.getFault();
            //                QName fcode = newFault.getFaultCodeAsQName();
            //                String string = newFault.getFaultString();
            //                String actor = newFault.getFaultActor();
            // System.out.println(">>>>>>>>>>>>>"+fcode);
            System.out.println(">>>>>>>>>>>>>" + newFault.getFaultString());
            soapConnection.close();
            return false;
        } else {
            //TO DO log
            soapConnection.close();
            return true;

        }
    } catch (Exception ex) {
        Launcher.LOG.info(ex.getMessage());
    }

    return false;
}

From source file:org.cerberus.service.soap.impl.SoapService.java

@Override
public AnswerItem<SOAPExecution> callSOAP(String envelope, String servicePath, String method,
        String attachmentUrl) {/* w  ww .  j  a va2 s. c om*/
    AnswerItem result = new AnswerItem();
    SOAPExecution executionSOAP = new SOAPExecution();
    ByteArrayOutputStream out = null;
    MessageEvent message = null;

    if (StringUtils.isNullOrEmpty(servicePath)) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_SERVICEPATHMISSING);
        result.setResultMessage(message);
        return result;
    }
    if (StringUtils.isNullOrEmpty(method)) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_METHODMISSING);
        result.setResultMessage(message);
        return result;
    }
    if (StringUtils.isNullOrEmpty(envelope)) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_ENVELOPEMISSING);
        result.setResultMessage(message);
        return result;
    }

    SOAPConnectionFactory soapConnectionFactory;
    SOAPConnection soapConnection = null;
    try {
        //Initialize SOAP Connection
        soapConnectionFactory = SOAPConnectionFactory.newInstance();
        soapConnection = soapConnectionFactory.createConnection();
        MyLogger.log(SoapService.class.getName(), Level.DEBUG, "Connection opened");

        // Create SOAP Request
        MyLogger.log(SoapService.class.getName(), Level.DEBUG, "Create request");
        SOAPMessage input = createSoapRequest(envelope, method);

        //Add attachment File if specified
        //TODO: this feature is not implemented yet therefore is always empty!
        if (!StringUtils.isNullOrEmpty(attachmentUrl)) {
            this.addAttachmentPart(input, attachmentUrl);
        }

        // Store the SOAP Call
        out = new ByteArrayOutputStream();
        input.writeTo(out);
        MyLogger.log(SoapService.class.getName(), Level.DEBUG, "WS call : " + out.toString());
        executionSOAP.setSOAPRequest(input);
        result.setItem(executionSOAP);

        // Call the WS
        MyLogger.log(SoapService.class.getName(), Level.DEBUG, "Calling WS");
        SOAPMessage soapResponse = soapConnection.call(input, servicePath);
        MyLogger.log(SoapService.class.getName(), Level.DEBUG, "Called WS");
        out = new ByteArrayOutputStream();

        // Store the response
        soapResponse.writeTo(out);
        MyLogger.log(SoapService.class.getName(), Level.DEBUG, "WS response received");
        MyLogger.log(SoapService.class.getName(), Level.DEBUG, "WS response : " + out.toString());
        executionSOAP.setSOAPResponse(soapResponse);

        message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CALLSOAP);
        message.setDescription(
                message.getDescription().replace("%SERVICEPATH%", servicePath).replace("%SOAPMETHOD%", method));
        result.setItem(executionSOAP);

    } catch (SOAPException | UnsupportedOperationException | IOException | SAXException
            | ParserConfigurationException | CerberusException e) {
        MyLogger.log(SoapService.class.getName(), Level.ERROR, e.toString());
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP);
        message.setDescription(message.getDescription().replace("%SERVICEPATH%", servicePath)
                .replace("%SOAPMETHOD%", method).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) {
            Logger.getLogger(SoapService.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } finally {
            result.setResultMessage(message);
        }
    }

    return result;
}

From source file:org.energy_home.jemma.ah.internal.greenathome.GreenathomeAppliance.java

private void getPVForecast() {
    try {//  ww  w . jav a2 s.  co  m
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Calendar cal = new GregorianCalendar();
        if (cal.get(Calendar.HOUR_OF_DAY) < 11)
            cal.add(Calendar.DAY_OF_MONTH, -1);
        String date = format.format(cal.getTime());
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
        // Send SOAP Message to SOAP Server
        String url = "http://ws.i-em.eu/v4/iem.asmx";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(date), url);
        // Process the SOAP Response
        double[] val = getValuesFromSOAPResponse(soapResponse);
        if (val != null) {
            if (val.length > 0)
                forecast = new ArrayList<Double>();
            for (int i = 0; i < val.length; i++) {
                if (Double.isNaN(val[i]))
                    val[i] = 0;
                forecast.add(val[i]);
            }
        }
        forecast_debug += "---fsize: " + forecast.size();
        soapConnection.close();
    } catch (Exception e) {
        forecast_debug += "---EXCEPT: " + e.getMessage();
    }
}

From source file:org.jboss.jaxr.juddi.transport.SaajTransport.java

/**
 * @see Transport#send(org.w3c.dom.Element, java.net.URL)
 */// w  w  w  .j av a  2 s . co  m
public Element send(Element request, URL endpointURL) throws RegistryException {
    String requestMessage = XMLUtils.toString(request);
    log.debug("Request message:" + requestMessage);

    Element response = null;
    try {
        SOAPMessage message = this.createSOAPMessage(request);
        //Make the SAAJ Call now
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = soapConnectionFactory.createConnection();
        SOAPMessage soapResponse = connection.call(message, endpointURL);

        SOAPBody soapBody = soapResponse.getSOAPBody();
        boolean hasFault = soapBody.hasFault();
        if (hasFault) {
            SOAPFault soapFault = soapBody.getFault();
            String faultStr = soapFault.getFaultCode() + "::" + soapFault.getFaultString();
            throw new RegistryException(faultStr);
        }
        response = getFirstChildElement(soapBody);
    } catch (Exception ex) {
        log.error("Exception::", ex);
        throw new RegistryException(ex);
    }

    log.debug("Response message:" + XMLUtils.getText(response));
    return response;
}

From source file:org.jboss.jaxr.juddi.transport.WS4EESaajTransport.java

public Element send(Element request, URL endpointURL) throws RegistryException {
    log.debug("Request message:" + XMLUtils.toString(request));
    if ("true".equalsIgnoreCase(debugProp))
        System.out.println("Request Element:" + XMLUtils.toString(request));

    Element response = null;/*  w w  w.ja  va  2 s .  com*/
    try {
        MessageFactory msgFactory = MessageFactory.newInstance();
        SOAPMessage message = msgFactory.createMessage();
        message.getSOAPHeader().detachNode();
        SOAPPart soapPart = message.getSOAPPart();
        SOAPBody soapBody = soapPart.getEnvelope().getBody();
        soapBody.addChildElement(getSOAPElement(soapBody, request));
        //There seems to be a bug in the Saaj/Axis implementation that requires
        //message to be written to an output stream
        ByteArrayOutputStream by = new ByteArrayOutputStream();
        message.writeTo(by); //Does not do anything
        by.close();
        if ("true".equalsIgnoreCase(debugProp))
            message.writeTo(System.out);

        //Make the SAAJ Call now
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = soapConnectionFactory.createConnection();
        SOAPMessage soapResponse = connection.call(message, endpointURL);
        if ("true".equalsIgnoreCase(debugProp)) {
            System.out.println("Response is:");
            soapResponse.writeTo(System.out);
        }

        soapBody = soapResponse.getSOAPBody();
        boolean hasFault = soapBody.hasFault();
        if (hasFault) {
            SOAPFault soapFault = soapBody.getFault();
            String faultStr = soapFault.getFaultCode() + "::" + soapFault.getFaultString();
            throw new RegistryException(faultStr);
        }
        response = getFirstChildElement(soapBody);
    } catch (Exception ex) {
        ex.printStackTrace();
        log.error(ex);
        throw new RegistryException(ex);
    }

    log.debug("Response message:" + XMLUtils.getText(response));

    return response;
}