Fuse 6 is no longer supportedAs of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.
Apache Camel supports a type converter mechanism, which is used to perform implicit and explicit type conversions of message bodies and message headers. The payload demonstration requires a customer type converter that can convert String objects to CXFPayload objects. This type converter automatically gets invoked at the end of the Camel route, when the generated response message (which is a String type) gets converted into a CXFPayload object.
The String to CXFPayload type converter is implemented in the AdditionalCxfPayloadConverters class, as follows:
// Java
package com.fusesource.customerwscamelcxfpayload;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.camel.Converter;
import org.apache.camel.component.cxf.CxfPayload;
import org.apache.cxf.binding.soap.SoapHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@Converter
public class AdditionalCxfPayloadConverters {
...
@Converter
public static CxfPayload<SoapHeader> toCxfPayload(String xml) {
// System.out.println("To CxfPayload " + xml);
List<Element> elements = new ArrayList<Element>();
try {
Document doc = b.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
elements.add(doc.getDocumentElement());
} catch (Exception ex) {
log.warn("Exception while converting String payload to CxfPayload; resulting payload will be empty.");
}
// The CxfPayload is changed to use Source object under layer, the elements API only work if we already setup the list before creating the CxfPayload
CxfPayload<SoapHeader> ret = new CxfPayload<SoapHeader>(null, elements);
return ret;
}
...
}
// Java
package com.fusesource.customerwscamelcxfpayload;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.camel.Converter;
import org.apache.camel.component.cxf.CxfPayload;
import org.apache.cxf.binding.soap.SoapHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@Converter
public class AdditionalCxfPayloadConverters {
...
@Converter
public static CxfPayload<SoapHeader> toCxfPayload(String xml) {
// System.out.println("To CxfPayload " + xml);
List<Element> elements = new ArrayList<Element>();
try {
Document doc = b.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
elements.add(doc.getDocumentElement());
} catch (Exception ex) {
log.warn("Exception while converting String payload to CxfPayload; resulting payload will be empty.");
}
// The CxfPayload is changed to use Source object under layer, the elements API only work if we already setup the list before creating the CxfPayload
CxfPayload<SoapHeader> ret = new CxfPayload<SoapHeader>(null, elements);
return ret;
}
...
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.
Making open source more inclusive
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.
About Red Hat
We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.