org.apache.camel.component.cxf.CxfPayload Java Examples

The following examples show how to use org.apache.camel.component.cxf.CxfPayload. 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: SoapPayloadConverterTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void convertXmlToCxfToXml() throws IOException, XMLStreamException {

    // read XML bytes as an XML Document
    final ByteArrayInputStream bis = new ByteArrayInputStream(IOUtils.readBytesFromStream(inputStream));
    final Document request = StaxUtils.read(bis);
    bis.reset();

    // convert XML to CxfPayload
    final Exchange exchange = createExchangeWithBody(bis);
    final Message in = exchange.getIn();
    requestConverter.process(exchange);

    Assertions.assertThat(in.getBody()).isInstanceOf(CxfPayload.class);

    // convert CxfPayload back to XML
    final SoapMessage soapMessage = new SoapMessage(Soap12.getInstance());
    in.setHeader("CamelCxfMessage", soapMessage);
    responseConverter.process(exchange);

    assertIsInstanceOf(InputStream.class, in.getBody());
    Document response = StaxUtils.read((InputStream) in.getBody());

    XMLUnit.setIgnoreAttributeOrder(true);
    assertThat(response, isSimilarTo(request).ignoreWhitespace());
}
 
Example #2
Source File: SoapPayloadConverterTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void convertXmlToValidCxfPayload() throws IOException, XMLStreamException {

    // read XML bytes as an XML Document
    ByteArrayInputStream bis = new ByteArrayInputStream(IOUtils.readBytesFromStream(inputStream));
    StaxUtils.read(bis);
    bis.reset();

    // convert XML to CxfPayload
    final Exchange exchange = createExchangeWithBody(bis);
    final Message in = exchange.getIn();
    requestConverter.process(exchange);

    final Object body = in.getBody();
    Assertions.assertThat(body).isInstanceOf(CxfPayload.class);
    @SuppressWarnings("unchecked")
    final CxfPayload<Source> cxfPayload = (CxfPayload<Source>) body;

    // validate every header and body part XML
    for (Source headerPart : cxfPayload.getHeaders()) {
        validateXml(headerPart);
    }
    for (Source bodyPart : cxfPayload.getBodySources()) {
        validateXml(bodyPart);
    }
}
 
Example #3
Source File: MdwProducer.java    From mdw with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void handleResponse(Exchange exchange, Object response) throws Exception {
    if ("org.apache.camel.component.cxf.CxfPayload".equals(exchange.getIn().getBody().getClass().getName())) {
        if ("org.apache.camel.component.cxf.CxfPayload".equals(response.getClass().getName())) {
            exchange.getOut().setBody(response, CxfPayload.class);
        }
        else {
            List<Element> outElements = new ArrayList<Element>();
            if (response instanceof Element) {
                outElements.add((Element)response);
            }
            else if (response instanceof String) {
                outElements.add(DomHelper.toDomDocument((String)response).getDocumentElement());
            }
            CxfPayload responsePayload = new CxfPayload(null, outElements);
            exchange.getOut().setBody(responsePayload);
        }
    }
    else {
        if (response instanceof String) {
            exchange.getOut().setBody(response, String.class);
        }
    }
}
 
Example #4
Source File: DefaultNotifyHandler.java    From mdw with Apache License 2.0 6 votes vote down vote up
public Object notify(String eventId, Long docId, Message request, int delay) {
    try {
        String requestStr = null;
        if ("org.apache.camel.component.cxf.CxfPayload".equals(request.getBody().getClass().getName())) {
            // special handling to extract XML
            @SuppressWarnings("rawtypes")
            CxfPayload cxfPayload = (CxfPayload) request.getBody();
            requestStr = DomHelper.toXml((Node)cxfPayload.getBody().get(0));
        }
        else {
            requestStr = request.getBody(String.class);
        }

        notifyProcesses(eventId, docId, requestStr, delay);
        return getResponse(0, "Success");
    }
    catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        return getResponse(1, ex.toString());
    }
}
 
Example #5
Source File: DefaultProcessLaunchHandler.java    From mdw with Apache License 2.0 6 votes vote down vote up
public Object invoke(Long processId, Long owningDocId, String masterRequestId, Message request,
        Map<String,Object> parameters) throws Exception {

    String requestStr = null;
    if ("org.apache.camel.component.cxf.CxfPayload".equals(request.getBody().getClass().getName())) {
        // special handling to extract XML
        @SuppressWarnings("rawtypes")
        CxfPayload cxfPayload = (CxfPayload) request.getBody();
        requestStr = DomHelper.toXml((Node)cxfPayload.getBody().get(0));
    }
    else {
        requestStr = request.getBody(String.class);
    }

    Process processVO = ProcessCache.getProcess(processId);

    if (processVO.getProcessType().equals(ProcessVisibilityConstant.SERVICE)) {
        String responseVarName = getResponseVariable();
        return invokeServiceProcess(processId, owningDocId, masterRequestId, requestStr, parameters, responseVarName);
    }
    else {
        Long instanceId = launchProcess(processId, owningDocId, masterRequestId, parameters);
        return getResponse(0, "Process '" + processVO.getLabel() + "' instance ID " + instanceId + " launched.");
    }
}
 
Example #6
Source File: RequestSoapPayloadConverter.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
protected void convertMessage(final Message in) {
    try {
        final Source body = bodyAsSource(in);

        // extract body as stream, and headers as elements in single DOM
        final XMLEventReader bodyReader = XML_INPUT_FACTORY.createXMLEventReader(body);
        final SoapPayloadReaderFilter payloadFilter = new SoapPayloadReaderFilter(soapVersion);
        final XMLEventReader eventReader = XML_INPUT_FACTORY.createFilteredReader(bodyReader, payloadFilter);

        // all the work is done in the filter, so we ignore the event writer output
        try (OutputStream bos = new ByteArrayOutputStream()) {
            XMLEventWriter target = XML_OUTPUT_FACTORY.createXMLEventWriter(bos);
            target.add(eventReader);
        }

        // convert filtered parts to CxfPayload
        final CxfPayload<Source> cxfPayload = payloadFilter.getCxfPayload();

        // add existing SOAP headers
        final List<?> existingHeaders = (List<?>) in.getHeader(Header.HEADER_LIST);
        if (existingHeaders != null) {
            final List<Source> headers = cxfPayload.getHeaders();
            for (Object header : existingHeaders) {
                if (header instanceof Source) {
                    headers.add((Source) header);
                } else {
                    // wrap dom node
                    headers.add(new DOMSource((Node)header));
                }
            }
        }

        in.setBody(cxfPayload);

    } catch (XMLStreamException | InvalidPayloadException | IOException e) {
        throw new RuntimeCamelException("Error creating SOAP message from request message: " + e.getMessage(), e);
    }
}
 
Example #7
Source File: HeaderProcessor.java    From container with Apache License 2.0 5 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {
    final CxfPayload<SoapHeader> payload = exchange.getIn().getBody(CxfPayload.class);

    final Map<String, Object> headers = exchange.getIn().getHeaders();
    if (!headers.containsKey("SOAPEndpoint")) {
        headers.put("SOAPEndpoint", headers.get("endpoint"));
    }
    for (final Map.Entry<String, Object> entry : headers.entrySet()) {

        if (entry.getKey().equalsIgnoreCase("ReplyTo")) {

            final String xml1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ReplyTo "
                + "xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"><wsa:Address>" + entry.getValue().toString()
                + "</wsa:Address></ReplyTo>";
            final SoapHeader replyToSoapHeader =
                new SoapHeader(new QName("http://www.w3.org/2005/08/addressing", "ReplyTo"),
                    readXml(new StringReader(xml1)).getDocumentElement());
            payload.getHeaders().add(replyToSoapHeader);
        } else if (entry.getKey().equalsIgnoreCase("MessageID")) {
            final String xml2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MessageID "
                + "xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">" + entry.getValue().toString()
                + "</MessageID>";
            final SoapHeader messageIdSoapHeader =
                new SoapHeader(new QName("http://www.w3.org/2005/08/addressing", "MessageID"),
                    readXml(new StringReader(xml2)).getDocumentElement());
            payload.getHeaders().add(messageIdSoapHeader);
        } else {
            payload.getHeaders().add(this.getSoapHeader(entry.getKey(), entry.getValue().toString()));
        }
    }
    exchange.getIn().setBody(payload);
}
 
Example #8
Source File: CxfPayloadTranslator.java    From mdw with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes","unchecked"})
@Override
public Object toObject(String str, String type) throws TranslationException {
    try {
        Document doc = DomHelper.toDomDocument(str);
        List<Element> body = new ArrayList<Element>();
        body.add((Element)doc);
        return new CxfPayload(null, body);
    }
    catch (Exception ex) {
        throw new TranslationException(ex.getMessage(), ex);
    }
}
 
Example #9
Source File: CxfPayloadTranslator.java    From mdw with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public String toString(Object obj, String variableType) throws TranslationException {
    try {
        CxfPayload cxfPayload = (CxfPayload)obj;
        return DomHelper.toXml((Node)cxfPayload.getBody().get(0));
    }
    catch (Exception ex) {
        throw new TranslationException(ex.getMessage(), ex);
    }
}
 
Example #10
Source File: CxfProcessLaunchHandler.java    From mdw with Apache License 2.0 5 votes vote down vote up
/**
 * Assumes a 'MasterRequestID' SOAP header element.  Override for something different.
 */
@SuppressWarnings("unchecked")
@Override
public String getMasterRequestId(Message request) {
    List<SoapHeader> headers = (List<SoapHeader>) ((CxfPayload<?>)request.getBody()).getHeaders();
    for (SoapHeader header : headers) {
        if (header.getName().getLocalPart().equals("MasterRequestID")) {
            Node headerNode = (Node) header.getObject();
            return headerNode.getTextContent();
        }
    }
    return null;
}
 
Example #11
Source File: AbstractFaultSoapPayloadConverter.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public void process(Exchange exchange) {
    final Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
    if (exception instanceof SoapFault) {

        SoapFault soapFault = (SoapFault) exception;
        final Message in = exchange.getIn();

        // get SOAP QNames from CxfPayload headers
        final SoapMessage soapMessage = in.getHeader("CamelCxfMessage", SoapMessage.class);
        final SoapVersion soapVersion = soapMessage.getVersion();

        try {

            // get CxfPayload body
            final CxfPayload<?> cxfPayload = in.getMandatoryBody(CxfPayload.class);

            final OutputStream outputStream = newOutputStream(in, cxfPayload);
            final XMLStreamWriter writer = newXmlStreamWriter(outputStream);

            handleFault(writer, soapFault, soapVersion);
            writer.writeEndDocument();

            final InputStream inputStream = getInputStream(outputStream, writer);

            // set the input stream as the Camel message body
            in.setBody(inputStream);

        } catch (InvalidPayloadException | XMLStreamException | IOException e) {
            throw new RuntimeCamelException("Error parsing CXF Payload: " + e.getMessage(), e);
        }
    }
}
 
Example #12
Source File: ResponseSoapPayloadConverter.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
protected void convertMessage(Message in) {
    try {
        // get SOAP QNames from CxfPayload headers
        final SoapMessage soapMessage = in.getHeader("CamelCxfMessage", SoapMessage.class);
        final SoapVersion soapVersion = soapMessage.getVersion();

        // get CxfPayload body
        final CxfPayload<?> cxfPayload = in.getMandatoryBody(CxfPayload.class);
        final List<?> headers = cxfPayload.getHeaders();
        final List<Source> body = cxfPayload.getBodySources();

        final OutputStream outputStream = newOutputStream(in, cxfPayload);
        final XMLStreamWriter writer = newXmlStreamWriter(outputStream);

        // serialize headers and body into an envelope
        writeStartEnvelopeAndHeaders(soapVersion, headers, writer);
        if (body != null && !body.isEmpty()) {
            writeBody(writer, body, soapVersion);
        }
        writer.writeEndDocument();

        final InputStream inputStream = getInputStream(outputStream, writer);

        // set the input stream as the Camel message body
        in.setBody(inputStream);

    } catch (InvalidPayloadException | XMLStreamException | IOException e) {
        throw new RuntimeCamelException("Error parsing CXF Payload: " + e.getMessage(), e);
    }
}
 
Example #13
Source File: CxfProcessLaunchHandler.java    From mdw with Apache License 2.0 4 votes vote down vote up
@Override
public Object initializeRequestDocument(Message request) throws MdwCamelException {
    return (CxfPayload<?>)request.getBody();
}
 
Example #14
Source File: CxfProcessLaunchHandler.java    From mdw with Apache License 2.0 4 votes vote down vote up
@Override
public String getRequestDocumentType(Message request) throws MdwCamelException {
    return CxfPayload.class.getName();
}
 
Example #15
Source File: CxfNotifyHandler.java    From mdw with Apache License 2.0 4 votes vote down vote up
@Override
public String getRequestDocumentType(Message request) throws MdwCamelException {
    return CxfPayload.class.getName();
}
 
Example #16
Source File: CxfNotifyHandler.java    From mdw with Apache License 2.0 4 votes vote down vote up
@Override
public Object initializeRequestDocument(Message request) throws MdwCamelException {
    return (CxfPayload<?>)request.getBody();
}
 
Example #17
Source File: CxfPayloadTranslator.java    From mdw with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public Document toDomDocument(Object obj) throws TranslationException {
    return (Document)((CxfPayload)obj).getBody().get(0);
}
 
Example #18
Source File: CxfPayloadTranslator.java    From mdw with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"rawtypes","unchecked"})
public Object fromDomNode(Node domNode) throws TranslationException {
    List nodeList = new ArrayList();
    nodeList.add(domNode);
    return new CxfPayload(null, nodeList);
}
 
Example #19
Source File: SoapPayloadReaderFilter.java    From syndesis with Apache License 2.0 4 votes vote down vote up
public CxfPayload<Source> getCxfPayload() {
    return new CxfPayload<>(getHeaders(), bodyParts, new HashMap<>());
}