Created
January 28, 2015 09:44
-
-
Save niels-s/cdecb063305c0bc53724 to your computer and use it in GitHub Desktop.
Camel Edifact producer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.apache.camel.Endpoint; | |
| import org.apache.camel.Exchange; | |
| import org.apache.camel.impl.DefaultProducer; | |
| import org.milyn.Smooks; | |
| import org.milyn.smooks.edi.unedifact.UNEdifactReaderConfigurator; | |
| import javax.xml.transform.stream.StreamResult; | |
| import javax.xml.transform.stream.StreamSource; | |
| import java.io.ByteArrayInputStream; | |
| import java.io.StringWriter; | |
| public class XmlProducer extends DefaultProducer { | |
| private final Smooks smooks = new Smooks(); | |
| public XmlProducer(Endpoint endpoint, Type edifactType) { | |
| super(endpoint); | |
| smooks.setReaderConfig(new UNEdifactReaderConfigurator(edifactType.urn())); | |
| } | |
| @Override | |
| public void process(Exchange exchange) throws Exception { | |
| String exchangeBody = exchange.getIn().getBody(String.class); | |
| ByteArrayInputStream edifact = new ByteArrayInputStream(exchangeBody.getBytes()); | |
| StringWriter xml = new StringWriter(); | |
| // stream source must be backed by a byte stream | |
| smooks.filterSource(new StreamSource(edifact), new StreamResult(xml)); | |
| exchange.getIn().setBody(xml.toString()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment