Example usage for javax.xml.soap SOAPMessage getSOAPPart

List of usage examples for javax.xml.soap SOAPMessage getSOAPPart

Introduction

In this page you can find the example usage for javax.xml.soap SOAPMessage getSOAPPart.

Prototype

public abstract SOAPPart getSOAPPart();

Source Link

Document

Gets the SOAP part of this SOAPMessage object.

Usage

From source file:org.cleverbus.core.reqres.RequestResponseTest.java

/**
 * Test saving synchronous request/response where response is failed SOAP fault exception.
 *///from   w  ww. j av a  2 s  .co m
@Test
public void testSavingRequestWithSOAPFaultResponse() throws Exception {

    final String soapFault = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<SOAP-ENV:Fault xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "     <faultcode>SOAP-ENV:Server</faultcode>"
            + "     <faultstring>There is one error</faultstring>" + "</SOAP-ENV:Fault>";

    // prepare target route
    prepareTargetRoute(TARGET_URI, new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getOut().setBody(soapFault);

            // create SOAP Fault message
            SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
            SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
            SOAPBody soapBody = soapEnvelope.getBody();

            // Set fault code and fault string
            SOAPFault fault = soapBody.addFault();
            fault.setFaultCode(new QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"));
            fault.setFaultString("There is one error");
            SoapMessage message = new SaajSoapMessage(soapMessage);
            throw new SoapFaultClientException(message);
        }
    });

    // action
    mock.expectedMessageCount(0);

    try {
        producer.sendBody(REQUEST);
        fail("Target route was thrown exception.");
    } catch (CamelExecutionException ex) {
        assertRequestResponse(REQUEST, null,
                "org.springframework.ws.soap.client.SoapFaultClientException: There is one error", soapFault);
    }
}

From source file:cl.nic.dte.net.ConexionSii.java

@SuppressWarnings("unchecked")
private String getSemilla()
        throws UnsupportedOperationException, SOAPException, IOException, XmlException, ConexionSiiException {
    SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection con = scFactory.createConnection();
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    SOAPHeader header = envelope.getHeader();
    SOAPBody body = envelope.getBody();
    header.detachNode();//ww w  .  j  a  v a  2  s  .  co m

    String urlSolicitud = Utilities.netLabels.getString("URL_SOLICITUD_SEMILLA");

    Name bodyName = envelope.createName("getSeed", "m", urlSolicitud);

    message.getMimeHeaders().addHeader("SOAPAction", "");

    body.addBodyElement(bodyName);

    URL endpoint = new URL(urlSolicitud);

    SOAPMessage responseSII = con.call(message, endpoint);

    SOAPPart sp = responseSII.getSOAPPart();
    SOAPBody b = sp.getEnvelope().getBody();

    cl.sii.xmlSchema.RESPUESTADocument resp = null;
    for (Iterator<SOAPBodyElement> res = b.getChildElements(
            sp.getEnvelope().createName("getSeedResponse", "ns1", urlSolicitud)); res.hasNext();) {
        for (Iterator<SOAPBodyElement> ret = res.next().getChildElements(
                sp.getEnvelope().createName("getSeedReturn", "ns1", urlSolicitud)); ret.hasNext();) {

            HashMap<String, String> namespaces = new HashMap<String, String>();
            namespaces.put("", "http://www.sii.cl/XMLSchema");
            XmlOptions opts = new XmlOptions();
            opts.setLoadSubstituteNamespaces(namespaces);

            resp = RESPUESTADocument.Factory.parse(ret.next().getValue(), opts);

        }
    }

    if (resp != null && resp.getRESPUESTA().getRESPHDR().getESTADO() == 0) {
        return resp.getRESPUESTA().getRESPBODY().getSEMILLA();
    } else {
        throw new ConexionSiiException(
                "No obtuvo Semilla: Codigo: " + resp.getRESPUESTA().getRESPHDR().getESTADO() + "; Glosa: "
                        + resp.getRESPUESTA().getRESPHDR().getGLOSA());
    }
}

From source file:edu.unc.lib.dl.services.TripleStoreManagerMulgaraImpl.java

private String sendTQL(String query) {
    log.info(query);/*  w w  w . jav a 2  s .  com*/
    String result = null;
    try {
        // First create the connection
        SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = soapConnFactory.createConnection();

        // 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();
        SOAPMessage 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.jdom.Document jdomDoc = builder.build(reply.getSOAPBody().getOwnerDocument());
                log.info(new XMLOutputter().outputString(jdomDoc));
            }
        } else {
            NodeList nl = reply.getSOAPPart().getEnvelope().getBody().getElementsByTagNameNS("*",
                    "executeQueryToStringReturn");
            if (nl.getLength() > 0) {
                result = nl.item(0).getTextContent();
            }
            log.debug(result);
        }
    } catch (SOAPException e) {
        throw new Error("Cannot query triple store at " + this.getItqlEndpointURL(), e);
    }
    return result;
}

From source file:cl.nic.dte.net.ConexionSii.java

@SuppressWarnings("unchecked")
public String getToken(PrivateKey pKey, X509Certificate cert)
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException,
        XMLSignatureException, SAXException, IOException, ParserConfigurationException, XmlException,
        UnsupportedOperationException, SOAPException, ConexionSiiException {

    String urlSolicitud = Utilities.netLabels.getString("URL_SOLICITUD_TOKEN");

    String semilla = getSemilla();

    GetTokenDocument req = GetTokenDocument.Factory.newInstance();

    req.addNewGetToken().addNewItem().setSemilla(semilla);

    HashMap<String, String> namespaces = new HashMap<String, String>();
    namespaces.put("", "http://www.sii.cl/SiiDte");
    XmlOptions opts = new XmlOptions();

    opts = new XmlOptions();
    opts.setSaveImplicitNamespaces(namespaces);
    opts.setLoadSubstituteNamespaces(namespaces);
    opts.setSavePrettyPrint();/*from www.  ja v a2s.co m*/
    opts.setSavePrettyPrintIndent(0);

    req = GetTokenDocument.Factory.parse(req.newInputStream(opts), opts);

    // firmo
    req.sign(pKey, cert);

    SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection con = scFactory.createConnection();
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    SOAPHeader header = envelope.getHeader();
    SOAPBody body = envelope.getBody();
    header.detachNode();

    Name bodyName = envelope.createName("getToken", "m", urlSolicitud);
    SOAPBodyElement gltp = body.addBodyElement(bodyName);

    Name toKname = envelope.createName("pszXml");

    SOAPElement toKsymbol = gltp.addChildElement(toKname);

    opts = new XmlOptions();
    opts.setCharacterEncoding("ISO-8859-1");
    opts.setSaveImplicitNamespaces(namespaces);

    toKsymbol.addTextNode(req.xmlText(opts));

    message.getMimeHeaders().addHeader("SOAPAction", "");

    URL endpoint = new URL(urlSolicitud);

    message.writeTo(System.out);

    SOAPMessage responseSII = con.call(message, endpoint);

    SOAPPart sp = responseSII.getSOAPPart();
    SOAPBody b = sp.getEnvelope().getBody();

    cl.sii.xmlSchema.RESPUESTADocument resp = null;
    for (Iterator<SOAPBodyElement> res = b.getChildElements(
            sp.getEnvelope().createName("getTokenResponse", "ns1", urlSolicitud)); res.hasNext();) {
        for (Iterator<SOAPBodyElement> ret = res.next().getChildElements(
                sp.getEnvelope().createName("getTokenReturn", "ns1", urlSolicitud)); ret.hasNext();) {

            namespaces = new HashMap<String, String>();
            namespaces.put("", "http://www.sii.cl/XMLSchema");
            opts.setLoadSubstituteNamespaces(namespaces);

            resp = RESPUESTADocument.Factory.parse(ret.next().getValue(), opts);
        }
    }

    if (resp != null && resp.getRESPUESTA().getRESPHDR().getESTADO() == 0) {
        return resp.getRESPUESTA().getRESPBODY().getTOKEN();
    } else {
        throw new ConexionSiiException(
                "No obtuvo Semilla: Codigo: " + resp.getRESPUESTA().getRESPHDR().getESTADO() + "; Glosa: "
                        + resp.getRESPUESTA().getRESPHDR().getGLOSA());
    }

}

From source file:cl.nic.dte.net.ConexionSii.java

@SuppressWarnings("unchecked")
private RESPUESTADocument getEstadoDTE(String rutConsultante, Documento dte, String token, String urlSolicitud)
        throws UnsupportedOperationException, SOAPException, MalformedURLException, XmlException {

    String rutEmisor = dte.getEncabezado().getEmisor().getRUTEmisor();
    String rutReceptor = dte.getEncabezado().getReceptor().getRUTRecep();
    Integer tipoDTE = dte.getEncabezado().getIdDoc().getTipoDTE().intValue();
    long folioDTE = dte.getEncabezado().getIdDoc().getFolio();
    String fechaEmision = Utilities.fechaEstadoDte
            .format(dte.getEncabezado().getIdDoc().getFchEmis().getTime());
    long montoTotal = dte.getEncabezado().getTotales().getMntTotal();

    SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection con = scFactory.createConnection();
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    SOAPHeader header = envelope.getHeader();
    SOAPBody body = envelope.getBody();
    header.detachNode();// w w  w  .  ja va  2 s  . com

    Name bodyName = envelope.createName("getEstDte", "m", urlSolicitud);
    SOAPBodyElement gltp = body.addBodyElement(bodyName);

    Name toKname = envelope.createName("RutConsultante");
    SOAPElement toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(rutConsultante.substring(0, rutConsultante.length() - 2));

    toKname = envelope.createName("DvConsultante");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(rutConsultante.substring(rutConsultante.length() - 1, rutConsultante.length()));

    toKname = envelope.createName("RutCompania");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(rutEmisor.substring(0, rutEmisor.length() - 2));

    toKname = envelope.createName("DvCompania");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(rutEmisor.substring(rutEmisor.length() - 1, rutEmisor.length()));

    toKname = envelope.createName("RutReceptor");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(rutReceptor.substring(0, rutReceptor.length() - 2));

    toKname = envelope.createName("DvReceptor");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(rutReceptor.substring(rutReceptor.length() - 1, rutReceptor.length()));

    toKname = envelope.createName("TipoDte");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(Integer.toString(tipoDTE));

    toKname = envelope.createName("FolioDte");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(Long.toString(folioDTE));

    toKname = envelope.createName("FechaEmisionDte");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(fechaEmision);

    toKname = envelope.createName("MontoDte");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(Long.toString(montoTotal));

    toKname = envelope.createName("Token");
    toKsymbol = gltp.addChildElement(toKname);
    toKsymbol.addTextNode(token);

    message.getMimeHeaders().addHeader("SOAPAction", "");

    URL endpoint = new URL(urlSolicitud);

    SOAPMessage responseSII = con.call(message, endpoint);

    SOAPPart sp = responseSII.getSOAPPart();
    SOAPBody b = sp.getEnvelope().getBody();

    for (Iterator<SOAPBodyElement> res = b.getChildElements(
            sp.getEnvelope().createName("getEstDteResponse", "ns1", urlSolicitud)); res.hasNext();) {
        for (Iterator<SOAPBodyElement> ret = res.next().getChildElements(
                sp.getEnvelope().createName("getEstDteReturn", "ns1", urlSolicitud)); ret.hasNext();) {

            HashMap<String, String> namespaces = new HashMap<String, String>();
            namespaces.put("", "http://www.sii.cl/XMLSchema");
            XmlOptions opts = new XmlOptions();
            opts.setLoadSubstituteNamespaces(namespaces);

            return RESPUESTADocument.Factory.parse(ret.next().getValue(), opts);
        }
    }

    return null;

}

From source file:com.inbravo.scribe.rest.service.crm.ms.auth.SOAPExecutor.java

/**
 * Constructs XPath query over the SOAP message
 * /*from ww  w .j  av  a2 s.c  o  m*/
 * @param query XPath query
 * @param response SOAP message
 * @return XPath query
 * @throws SOAPException in case of SOAP issue
 * @throws JaxenException XPath problem
 */
public final XPath createXPath(final String query, final SOAPMessage response)
        throws SOAPException, JaxenException {

    /* Uses DOM to XPath mapping */
    final XPath xpath = new DOMXPath(query);

    /* Define a namespaces used in response */
    final SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
    final SOAPPart sp = response.getSOAPPart();
    final SOAPEnvelope env = sp.getEnvelope();
    final SOAPBody bdy = env.getBody();

    /* Add namespaces from SOAP envelope */
    addNamespaces(nsContext, env);

    /* Add namespaces of top body element */
    final Iterator<?> bodyElements = bdy.getChildElements();
    while (bodyElements.hasNext()) {
        SOAPElement element = (SOAPElement) bodyElements.next();
        addNamespaces(nsContext, element);
    }
    xpath.setNamespaceContext(nsContext);
    return xpath;
}

From source file:com.wandrell.example.swss.test.util.test.integration.endpoint.AbstractITEndpoint.java

/**
 * Tests that a valid message returns the expected value.
 *
 * @throws Exception/*ww w .  j a v a  2s .  c om*/
 *             never, this is a required declaration
 */
@Test
public final void testEndpoint_Valid_ReturnsEntity() throws Exception {
    final SOAPMessage message; // Response message
    final SOAPMessage req; // Request message
    final Entity entity; // Entity from the response

    req = getValidSoapMessage();
    if (req != null) {
        // TODO: This is just to avoid problems with the test not yet
        // working
        message = callWebService(getValidSoapMessage());

        Assert.assertNull(message.getSOAPPart().getEnvelope().getBody().getFault());

        entity = SoapMessageUtils.getEntity(message);

        Assert.assertEquals((Integer) entity.getId(), entityId);
        Assert.assertEquals(entity.getName(), entityName);
    }
}

From source file:com.centurylink.mdw.hub.servlet.SoapServlet.java

/**
 * Allow version specific factory passed in TODO: allow specifying response
 * headers// www . j a va2  s . co m
 */
protected String createSoapResponse(String soapVersion, String xml) throws SOAPException {
    try {
        SOAPMessage soapMessage = getSoapMessageFactory(soapVersion).createMessage();
        SOAPBody soapBody = soapMessage.getSOAPBody();
        soapBody.addDocument(DomHelper.toDomDocument(xml));
        return DomHelper.toXml(soapMessage.getSOAPPart().getDocumentElement());
    } catch (Exception ex) {
        throw new SOAPException(ex.getMessage(), ex);
    }
}

From source file:com.centurylink.mdw.hub.servlet.SoapServlet.java

protected String createOldStyleSoapResponse(String soapVersion, String xml) throws SOAPException {
    try {//  w ww  .jav a2 s.  c o  m
        SOAPMessage soapMessage = getSoapMessageFactory(soapVersion).createMessage();
        SOAPBody soapBody = soapMessage.getSOAPBody();
        soapBody.addDocument(DomHelper.toDomDocument(xml));
        return DomHelper.toXmlNoWhiteSpace(soapMessage.getSOAPPart());
    } catch (Exception ex) {
        throw new SOAPException(ex.getMessage(), ex);
    }
}

From source file:com.googlecode.ddom.saaj.SOAPMessageTest.java

/**
 * Tests the behavior of {@link SOAPPart#getContent()} for a plain SOAP message created from an
 * input stream./*from w w  w .  j a  v a  2  s.  c o  m*/
 * 
 * @throws Exception
 * 
 * @see SOAPPartTest#testGetContent()
 */
@Validated
@Test
public void testWriteTo() throws Exception {
    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("Content-Type", messageSet.getVersion().getContentType() + "; charset=utf-8");
    InputStream in = messageSet.getTestMessage("message.xml");
    byte[] orgContent = IOUtils.toByteArray(in);
    SOAPMessage message = getFactory().createMessage(headers, new ByteArrayInputStream(orgContent));

    // Get the content before accessing the SOAP part
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    message.writeTo(baos);
    byte[] content1 = baos.toByteArray();
    assertTrue(Arrays.equals(orgContent, content1));

    // Now access the SOAP part and get the content again
    message.getSOAPPart().getEnvelope();
    baos = new ByteArrayOutputStream();
    message.writeTo(baos);
    byte[] content2 = baos.toByteArray();
    // The content is equivalent, but not exactly the same
    assertFalse(Arrays.equals(orgContent, content2));
}