|
| 1 | + |
| 2 | +/** |
| 3 | + * Copyright 2011 FuseSource |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.fusesource.customerwscamelcxfpayload; |
| 19 | + |
| 20 | +import java.io.ByteArrayInputStream; |
| 21 | +import java.io.StringWriter; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import javax.xml.transform.OutputKeys; |
| 25 | +import javax.xml.transform.Transformer; |
| 26 | +import javax.xml.transform.TransformerFactory; |
| 27 | +import javax.xml.transform.dom.DOMSource; |
| 28 | +import javax.xml.transform.stream.StreamResult; |
| 29 | +import org.apache.camel.Converter; |
| 30 | +import org.apache.camel.component.cxf.CxfPayload; |
| 31 | +import org.apache.cxf.binding.soap.SoapHeader; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | +import org.w3c.dom.Document; |
| 35 | +import org.w3c.dom.Element; |
| 36 | +import org.w3c.dom.Node; |
| 37 | + |
| 38 | +@Converter |
| 39 | +public class AdditionalCxfPayloadConverters { |
| 40 | + |
| 41 | + private static final Logger log = LoggerFactory.getLogger(AdditionalCxfPayloadConverters.class); |
| 42 | + private static javax.xml.parsers.DocumentBuilderFactory b = javax.xml.parsers.DocumentBuilderFactory.newInstance(); |
| 43 | + |
| 44 | + @Converter |
| 45 | + public static CxfPayload<SoapHeader> toCxfPayload(String xml) { |
| 46 | + // System.out.println("To CxfPayload " + xml); |
| 47 | + List<Element> elements = new ArrayList<Element>(); |
| 48 | + try { |
| 49 | + Document doc = b.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes())); |
| 50 | + elements.add(doc.getDocumentElement()); |
| 51 | + } catch (Exception ex) { |
| 52 | + log.warn("Exception while converting String payload to CxfPayload; resulting payload will be empty."); |
| 53 | + } |
| 54 | + // 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 |
| 55 | + CxfPayload<SoapHeader> ret = new CxfPayload<SoapHeader>(null, elements); |
| 56 | + return ret; |
| 57 | + } |
| 58 | + |
| 59 | + private static TransformerFactory transFactory = TransformerFactory.newInstance(); |
| 60 | + |
| 61 | + @Converter |
| 62 | + public static String toString(CxfPayload<SoapHeader> payload) { |
| 63 | + StringBuilder sb = new StringBuilder(); |
| 64 | + |
| 65 | + List<Element> elements = payload.getBody(); |
| 66 | + for (Element e : elements) { |
| 67 | + try { |
| 68 | + Transformer transformer = transFactory.newTransformer(); |
| 69 | + StringWriter buffer = new StringWriter(); |
| 70 | + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 71 | + transformer.transform(new DOMSource(e), |
| 72 | + new StreamResult(buffer)); |
| 73 | + sb.append(buffer.toString()); |
| 74 | + } catch (Exception ex) { |
| 75 | + log.warn("Warning: problems producing XML String content : " + ex.getMessage()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return sb.toString(); |
| 80 | + } |
| 81 | + |
| 82 | + @Converter |
| 83 | + public static Node toNode(CxfPayload<SoapHeader> payload) { |
| 84 | + return payload.getBody().get(0); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments