You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ha...@apache.org on 2009/04/29 20:11:50 UTC

svn commit: r769861 [1/2] - in /camel/trunk/components/camel-hl7/src: main/java/org/apache/camel/component/hl7/ test/java/org/apache/camel/component/hl7/ test/resources/

Author: hadrian
Date: Wed Apr 29 18:11:48 2009
New Revision: 769861

URL: http://svn.apache.org/viewvc?rev=769861&view=rev
Log:
CAMEL-1566.  Patch applied with thanks.

Added:
    camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPConfig.java
    camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPDecoder.java
    camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPEncoder.java
    camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecLongTest.java
    camel/trunk/components/camel-hl7/src/test/resources/mdm_t02.txt
Modified:
    camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java
    camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecPlainStringTest.java
    camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java
    camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java
    camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7RouteTest.java

Modified: camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java?rev=769861&r1=769860&r2=769861&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java (original)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java Wed Apr 29 18:11:48 2009
@@ -17,19 +17,10 @@
 package org.apache.camel.component.hl7;
 
 import java.nio.charset.Charset;
-import java.nio.charset.CharsetDecoder;
-import java.nio.charset.CharsetEncoder;
 
-import ca.uhn.hl7v2.model.Message;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.mina.common.ByteBuffer;
-import org.apache.mina.common.IoSession;
 import org.apache.mina.filter.codec.ProtocolCodecFactory;
 import org.apache.mina.filter.codec.ProtocolDecoder;
-import org.apache.mina.filter.codec.ProtocolDecoderOutput;
 import org.apache.mina.filter.codec.ProtocolEncoder;
-import org.apache.mina.filter.codec.ProtocolEncoderOutput;
 
 /**
  * HL7 MLLP codec.
@@ -47,7 +38,7 @@
  * the start and end markers.
  * <p/>
  * The encoder is used for encoding from String to MLLP (bytes). The String should <b>not</b> contain
- * any of the start and end markers, the enoder will add these, and stream the string as bytes.
+ * any of the start and end markers, the encoder will add these, and stream the string as bytes.
  * Also the enocder will convert any <tt>\n</tt> (line breaks) as segment terminators to <tt>\r</tt>.
  * <p/>
  * This codes supports charset encoding/decoding between bytes and String. The JVM platform default charset
@@ -56,180 +47,58 @@
  */
 public class HL7MLLPCodec implements ProtocolCodecFactory {
 
-    private static final transient Log LOG = LogFactory.getLog(HL7MLLPCodec.class);
-
-    private static final String CHARSET_ENCODER = HL7MLLPCodec.class.getName() + ".charsetencoder";
-    private static final String CHARSET_DECODER = HL7MLLPCodec.class.getName() + ".charsetdecoder";
-
-    // HL7 MLLP start and end markers
-    private char startByte = 0x0b; // 11 decimal
-    private char endByte1 = 0x1c;  // 28 decimal
-    private char endByte2 = 0x0d;  // 13 decimal
-
-    private Charset charset = Charset.defaultCharset();
-    private boolean convertLFtoCR = true;
-
-    public ProtocolEncoder getEncoder() throws Exception {
-        return new ProtocolEncoder() {
-            public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
-                throws Exception {
-
-                if (message == null) {
-                    throw new IllegalArgumentException("Message to encode is null");
-                } else if (message instanceof Exception) {
-                    // we cant handle exceptions
-                    throw (Exception) message;
-                }
-
-                CharsetEncoder encoder = (CharsetEncoder)session.getAttribute(CHARSET_ENCODER);
-                if (encoder == null) {
-                    encoder = charset.newEncoder();
-                    session.setAttribute(CHARSET_ENCODER, encoder);
-                }
-
-                // convert to string
-                String body;
-                if (message instanceof Message) {
-                    body = HL7Converter.toString((Message)message);
-                } else if (message instanceof String) {
-                    body = (String)message;
-                } else if (message instanceof byte[]) {
-                    body = new String((byte[])message);
-                } else {
-                    throw new IllegalArgumentException("The message to encode is not a supported type: "
-                            + message.getClass().getCanonicalName());
-                }
-
-                // replace \n with \r as HL7 uses 0x0d = \r as segment termninators
-                if (convertLFtoCR) {
-                    body = body.replace('\n', '\r');
-                }
-
-                // put the data into the byte buffer
-                ByteBuffer bb = ByteBuffer.allocate(body.length() + 3).setAutoExpand(true);
-                bb.put((byte) startByte);
-                bb.putString(body, encoder);
-                bb.put((byte) endByte1);
-                bb.put((byte) endByte2);
-
-                // flip the buffer so we can use it to write to the out stream
-                bb.flip();
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Encoding HL7 from " + message.getClass().getCanonicalName() + " to byte stream");
-                }
-                out.write(bb);
-            }
-
-            public void dispose(IoSession session) throws Exception {
-                session.removeAttribute(CHARSET_ENCODER);
-            }
-        };
-    }
+    private HL7MLLPConfig config = new HL7MLLPConfig();
 
     public ProtocolDecoder getDecoder() throws Exception {
-        return new ProtocolDecoder() {
-            public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception {
-
-                // find position where we have the end1 end2 combination
-                int posEnd = 0;
-                int posStart = 0;
-                while (in.hasRemaining()) {
-                    byte b = in.get();
-                    if (b == startByte) {
-                        posStart = in.position();
-                    }
-                    if (b == endByte1) {
-                        byte next = in.get();
-                        if (next == endByte2) {
-                            posEnd = in.position() - 2; // use -2 to skip these last 2 end markers
-                            break;
-                        } else {
-                            // we expected the 2nd end marker
-                            LOG.warn("The 2nd end byte " + endByte2 + " was not found, but was " + b);
-                        }
-                    }
-                }
-
-                // okay we have computed the start and end position of the special HL7 markers
-                // rewind the bytebuffer so we can read from it again
-                in.rewind();
-
-                // narrow the buffer to only include between the start and end markers
-                in.skip(posStart);
-                if (posEnd > 0) {
-                    in.limit(posEnd);
-                }
-
-                try {
-                    // convert to string using the charset decoder
-                    CharsetDecoder decoder = (CharsetDecoder)session.getAttribute(CHARSET_DECODER);
-                    if (decoder == null) {
-                        decoder = charset.newDecoder();
-                        session.setAttribute(CHARSET_DECODER, decoder);
-                    }
-                    String body = in.getString(decoder);
-
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Decoding HL7 from byte stream to String");
-                    }
-                    out.write(body);
-                } finally {
-                    // clear the buffer now that we have transfered the data to the String
-                    in.clear();
-                }
-            }
-
-            public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
-                // do nothing
-            }
-
-            public void dispose(IoSession session) throws Exception {
-                session.removeAttribute(CHARSET_DECODER);
-            }
-        };
+        return new HL7MLLPDecoder(config);
     }
 
-    public Charset getCharset() {
-        return charset;
+    public ProtocolEncoder getEncoder() throws Exception {
+        return new HL7MLLPEncoder(config);
     }
 
     public void setCharset(Charset charset) {
-        this.charset = charset;
+        config.setCharset(charset);
     }
 
     public void setCharset(String charsetName) {
-        this.charset = Charset.forName(charsetName);
+        config.setCharset(Charset.forName(charsetName));
+    }
+
+    public Charset getCharset() {
+        return config.getCharset();
     }
 
     public boolean isConvertLFtoCR() {
-        return convertLFtoCR;
+        return config.isConvertLFtoCR();
     }
 
     public void setConvertLFtoCR(boolean convertLFtoCR) {
-        this.convertLFtoCR = convertLFtoCR;
+        config.setConvertLFtoCR(convertLFtoCR);
     }
 
     public char getStartByte() {
-        return startByte;
+        return config.getStartByte();
     }
 
     public void setStartByte(char startByte) {
-        this.startByte = startByte;
+        config.setStartByte(startByte);
     }
 
     public char getEndByte1() {
-        return endByte1;
+        return config.getEndByte1();
     }
 
     public void setEndByte1(char endByte1) {
-        this.endByte1 = endByte1;
+        config.setEndByte1(endByte1);
     }
 
     public char getEndByte2() {
-        return endByte2;
+        return config.getEndByte2();
     }
 
     public void setEndByte2(char endByte2) {
-        this.endByte2 = endByte2;
+        config.setEndByte2(endByte2);
     }
+
 }

Added: camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPConfig.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPConfig.java?rev=769861&view=auto
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPConfig.java (added)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPConfig.java Wed Apr 29 18:11:48 2009
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.hl7;
+
+import java.nio.charset.Charset;
+
+class HL7MLLPConfig {
+
+    private Charset charset = Charset.defaultCharset();
+
+    private boolean convertLFtoCR = true;
+
+    // HL7 MLLP start and end markers
+    private char startByte = 0x0b; // 11 decimal
+
+    private char endByte1 = 0x1c; // 28 decimal
+
+    private char endByte2 = 0x0d; // 13 decimal
+
+    public Charset getCharset() {
+        return charset;
+    }
+
+    public void setCharset(Charset charset) {
+        this.charset = charset;
+    }
+
+    public boolean isConvertLFtoCR() {
+        return convertLFtoCR;
+    }
+
+    public void setConvertLFtoCR(boolean convertLFtoCR) {
+        this.convertLFtoCR = convertLFtoCR;
+    }
+
+    public char getStartByte() {
+        return startByte;
+    }
+
+    public void setStartByte(char startByte) {
+        this.startByte = startByte;
+    }
+
+    public char getEndByte1() {
+        return endByte1;
+    }
+
+    public void setEndByte1(char endByte1) {
+        this.endByte1 = endByte1;
+    }
+
+    public char getEndByte2() {
+        return endByte2;
+    }
+
+    public void setEndByte2(char endByte2) {
+        this.endByte2 = endByte2;
+    }
+
+}

Added: camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPDecoder.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPDecoder.java?rev=769861&view=auto
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPDecoder.java (added)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPDecoder.java Wed Apr 29 18:11:48 2009
@@ -0,0 +1,191 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.hl7;
+
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.CharsetDecoder;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
+import org.apache.mina.filter.codec.ProtocolDecoderOutput;
+
+/**
+ * HL7MLLPDecoder that is aware that a HL7 message can span several TCP packets.
+ * In addition, it avoids rescanning packets by keeping state in the IOSession.
+ */
+class HL7MLLPDecoder extends CumulativeProtocolDecoder {
+
+    private static final transient Log LOG = LogFactory.getLog(HL7MLLPDecoder.class);
+
+    private static final String CHARSET_DECODER = HL7MLLPDecoder.class.getName() + ".charsetdecoder";
+    private static final String DECODER_STATE = HL7MLLPDecoder.class.getName() + ".STATE";
+
+    private HL7MLLPConfig config;
+
+    HL7MLLPDecoder(HL7MLLPConfig config) {
+        super();
+        this.config = config;
+    }
+
+    @Override
+    protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) {
+
+        // Scan the buffer of start and/or end bytes
+        boolean foundEnd = scan(session, in);
+
+        // Write HL7 string or wait until message end arrives or buffer ends
+        if (foundEnd) {
+            writeString(session, in, out);
+        } else {
+            LOG.debug("No complete message in this packet");
+        }
+
+        return foundEnd;
+    }
+
+    /**
+     * @param session
+     * @param in
+     * @param out
+     * @param state
+     * @return
+     * @throws RuntimeException
+     */
+    private void writeString(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) {
+        DecoderState state = decoderState(session);
+        if (state.posStart == 0) {
+            LOG.warn("No start byte found, reading from beginning of data");
+        }
+        // start reading from the buffer after the start markers
+        in.position(state.posStart);
+        try {
+            String body = in.getString(state.length(), charsetDecoder(session));
+
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Decoded HL7 from byte stream of length " + state.length() + " to String of length " + body.length());
+            }
+            out.write(body);
+            // Avoid redelivery of scanned message
+            state.reset();
+        } catch (CharacterCodingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * @param session
+     * @return the charset decoder for this IoSession
+     */
+    private CharsetDecoder charsetDecoder(IoSession session) {
+        // convert to string using the charset decoder
+        CharsetDecoder decoder = (CharsetDecoder)session.getAttribute(CHARSET_DECODER);
+        if (decoder == null) {
+            decoder = config.getCharset().newDecoder();
+            session.setAttribute(CHARSET_DECODER, decoder);
+        }
+        return decoder;
+    }
+
+    /**
+     * Scans the buffer for start and end bytes and stores its position in the
+     * session state object.
+     * 
+     * @param session
+     * @param in
+     * @return <code>true</code> if the end bytes were found, <code>false</code>
+     *         otherwise
+     */
+    private boolean scan(IoSession session, ByteBuffer in) {
+        DecoderState state = decoderState(session);
+        // Start scanning where we left
+        in.position(state.current);
+        LOG.debug("Start scanning buffer at position " + in.position());
+
+        while (in.hasRemaining()) {
+            byte b = in.get();
+            // Check start byte
+            if (b == config.getStartByte()) {
+                if (state.posStart > 0) {
+                    LOG.warn("Ignoring message start at position " + in.position() + " before previous message has ended.");
+                } else {
+                    state.posStart = in.position();
+                    LOG.debug("Message starts at position " + state.posStart);
+                }
+            }
+            // Check end bytes
+            if (b == config.getEndByte1()) {
+                byte next = in.get();
+                if (next == config.getEndByte2()) {
+                    state.posEnd = in.position() - 2; // use -2 to skip these
+                                                      // last 2 end markers
+                    LOG.debug("Message ends at position " + state.posEnd);
+                    break;
+                } else {
+                    // we expected the 2nd end marker
+                    LOG.warn("The 2nd end byte " + config.getEndByte2() + " was not found, but was " + b);
+                }
+            }
+        }
+        // Remember where we are
+        state.current = in.position();
+        in.rewind();
+        return state.posEnd > 0;
+    }
+
+    /**
+     * @param session
+     * @return the state of the current decoding process
+     */
+    private DecoderState decoderState(IoSession session) {
+        DecoderState decoderState = (DecoderState)session.getAttribute(DECODER_STATE);
+        if (decoderState == null) {
+            decoderState = new DecoderState();
+            session.setAttribute(DECODER_STATE, decoderState);
+        }
+        return decoderState;
+    }
+
+    @Override
+    public void dispose(IoSession session) throws Exception {
+        session.removeAttribute(CHARSET_DECODER);
+        session.removeAttribute(DECODER_STATE);
+    }
+
+    /**
+     * Holds the state of the decoding process
+     * 
+     * @author Christian Ohr
+     */
+    private static class DecoderState {
+        int posStart;
+        int posEnd;
+        int current;
+
+        int length() {
+            return posEnd - posStart;
+        }
+
+        void reset() {
+            posStart = 0;
+            posEnd = 0;
+        }
+    }
+
+}

Added: camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPEncoder.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPEncoder.java?rev=769861&view=auto
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPEncoder.java (added)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPEncoder.java Wed Apr 29 18:11:48 2009
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.hl7;
+
+import java.nio.charset.CharsetEncoder;
+
+import ca.uhn.hl7v2.model.Message;
+import ca.uhn.hl7v2.parser.Parser;
+import ca.uhn.hl7v2.parser.PipeParser;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.codec.ProtocolEncoder;
+import org.apache.mina.filter.codec.ProtocolEncoderOutput;
+
+
+
+class HL7MLLPEncoder implements ProtocolEncoder {
+
+    private static final transient Log LOG = LogFactory.getLog(HL7MLLPEncoder.class);
+
+    private static final String CHARSET_ENCODER = HL7MLLPCodec.class.getName() + ".charsetencoder";
+
+    private HL7MLLPConfig config;
+
+    HL7MLLPEncoder(HL7MLLPConfig config) {
+        super();
+        this.config = config;
+    }
+
+    public void dispose(IoSession session) throws Exception {
+        session.removeAttribute(CHARSET_ENCODER);
+    }
+
+    public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
+        if (message == null) {
+            throw new IllegalArgumentException("Message to encode is null");
+        } else if (message instanceof Exception) {
+            // we cannot handle exceptions
+            throw (Exception)message;
+        }
+
+        CharsetEncoder encoder = (CharsetEncoder)session.getAttribute(CHARSET_ENCODER);
+        if (encoder == null) {
+            encoder = config.getCharset().newEncoder();
+            session.setAttribute(CHARSET_ENCODER, encoder);
+        }
+
+        // convert to string
+        String body;
+        if (message instanceof Message) {
+            Parser parser = new PipeParser();
+            body = parser.encode((Message)message);
+        } else if (message instanceof String) {
+            body = (String)message;
+        } else if (message instanceof byte[]) {
+            body = new String((byte[])message);
+        } else {
+            throw new IllegalArgumentException("The message to encode is not a supported type: "
+                                               + message.getClass().getCanonicalName());
+        }
+
+        // replace \n with \r as HL7 uses 0x0d = \r as segment termninators
+        if (config.isConvertLFtoCR()) {
+            body = body.replace('\n', '\r');
+        }
+
+        // put the data into the byte buffer
+        ByteBuffer buf = ByteBuffer.allocate(body.length() + 3).setAutoExpand(true);
+        buf.put((byte)config.getStartByte());
+        buf.putString(body, encoder);
+        buf.put((byte)config.getEndByte1());
+        buf.put((byte)config.getEndByte2());
+
+        // flip the buffer so we can use it to write to the out stream
+        buf.flip();
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Encoding HL7 from " + message.getClass().getCanonicalName() + " to byte stream");
+        }
+        out.write(buf);
+    }
+
+}

Added: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecLongTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecLongTest.java?rev=769861&view=auto
==============================================================================
--- camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecLongTest.java (added)
+++ camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecLongTest.java Wed Apr 29 18:11:48 2009
@@ -0,0 +1,85 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.hl7;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+import ca.uhn.hl7v2.model.Message;
+import ca.uhn.hl7v2.model.v25.message.MDM_T02;
+import ca.uhn.hl7v2.model.v25.segment.MSH;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+
+
+/**
+ * Unit test for the HL7MLLP Codec.
+ */
+public class HL7MLLPCodecLongTest extends ContextTestSupport {
+
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+
+        // START SNIPPET: e1
+        HL7MLLPCodec codec = new HL7MLLPCodec();
+        codec.setCharset("iso-8859-1");
+
+        jndi.bind("hl7codec", codec);
+        // END SNIPPET: e1
+
+        return jndi;
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        assertEquals(70011, exchange.getIn().getBody().toString().length());
+                        MDM_T02 input = (MDM_T02)exchange.getIn().getBody(Message.class);
+                        assertEquals("2.5", input.getVersion());
+                        MSH msh = input.getMSH();
+                        assertEquals("20071129144629", msh.getDateTimeOfMessage().getTime().getValue());
+                        exchange.getOut().setBody("some response");
+                    }
+                }).to("mock:result");
+            }
+        };
+    }
+
+    public void testSendHL7Message() throws Exception {
+        // START SNIPPET: e2
+        BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/mdm_t02.txt")));
+        String line = "";
+        String message = "";
+        while (line != null) {
+            if ((line = in.readLine()) != null) {
+                message += line + "\r";
+            }
+        }
+        message = message.substring(0, message.length() - 1);
+        assertEquals(70011, message.length());
+        String out = (String)template.requestBody("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec", message);
+        assertEquals("some response", out);
+        // END SNIPPET: e2
+    }
+
+}

Modified: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecPlainStringTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecPlainStringTest.java?rev=769861&r1=769860&r2=769861&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecPlainStringTest.java (original)
+++ camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecPlainStringTest.java Wed Apr 29 18:11:48 2009
@@ -46,7 +46,7 @@
         mock.expectedBodiesReceived("Bye World");
 
         // send plain hello world as String
-        Object out = template.requestBody("mina:tcp://localhost:8888?sync=true&codec=#hl7codec", "Hello World");
+        Object out = template.requestBody("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec", "Hello World");
 
         assertMockEndpointsSatisfied();
 
@@ -59,7 +59,7 @@
         return new RouteBuilder() {
             public void configure() throws Exception {
                 // START SNIPPET: e2
-                from("mina:tcp://localhost:8888?sync=true&codec=#hl7codec")
+                from("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec")
                     .process(new Processor() {
                         public void process(Exchange exchange) throws Exception {
                             // use plain String as message format

Modified: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java?rev=769861&r1=769860&r2=769861&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java (original)
+++ camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java Wed Apr 29 18:11:48 2009
@@ -52,7 +52,7 @@
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from("mina:tcp://localhost:8888?sync=true&codec=#hl7codec")
+                from("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec")
                     .process(new Processor() {
                         public void process(Exchange exchange) throws Exception {
                             Message input = exchange.getIn().getBody(Message.class);
@@ -79,7 +79,7 @@
         in.append("\r");
         in.append(line2);
 
-        String out = (String)template.requestBody("mina:tcp://localhost:8888?sync=true&codec=#hl7codec", in.toString());
+        String out = (String)template.requestBody("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec", in.toString());
 
         String[] lines = out.split("\r");
         assertEquals("MSH|^~\\&|MYSENDER||||200701011539||ADR^A19||||123", lines[0]);

Modified: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java?rev=769861&r1=769860&r2=769861&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java (original)
+++ camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java Wed Apr 29 18:11:48 2009
@@ -49,7 +49,7 @@
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from("mina:tcp://localhost:8888?sync=true&codec=#hl7codec")
+                from("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec")
                     .process(new Processor() {
                         public void process(Exchange exchange) throws Exception {
                             Message input = exchange.getIn().getBody(Message.class);
@@ -77,7 +77,7 @@
         in.append("\n");
         in.append(line2);
 
-        String out = (String)template.requestBody("mina:tcp://localhost:8888?sync=true&codec=#hl7codec", in.toString());
+        String out = (String)template.requestBody("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec", in.toString());
         // END SNIPPET: e2
 
         String[] lines = out.split("\r");

Modified: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7RouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7RouteTest.java?rev=769861&r1=769860&r2=769861&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7RouteTest.java (original)
+++ camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7RouteTest.java Wed Apr 29 18:11:48 2009
@@ -62,7 +62,7 @@
         in.append("\n");
         in.append(line2);
 
-        String out = (String) template.requestBody("mina:tcp://localhost:8888?sync=true&codec=#hl7codec", in.toString());
+        String out = (String) template.requestBody("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec", in.toString());
 
         String[] lines = out.split("\r");
         assertEquals("MSH|^~\\&|MYSENDER||||200701011539||ADR^A19||||123", lines[0]);
@@ -84,7 +84,7 @@
         in.append("\n");
         in.append(line2);
 
-        String out = (String) template.requestBody("mina:tcp://localhost:8888?sync=true&codec=#hl7codec", in.toString());
+        String out = (String) template.requestBody("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec", in.toString());
         String[] lines = out.split("\r");
         assertEquals("MSH|^~\\&|MYSENDER||||200701011539||ADT^A01||||123", lines[0]);
         assertEquals("PID|||123456||Doe^John", lines[1]);
@@ -105,7 +105,7 @@
         in.append("\n");
         in.append(line2);
 
-        template.requestBody("mina:tcp://localhost:8888?sync=true&codec=#hl7codec", in.toString());
+        template.requestBody("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec", in.toString());
 
         assertMockEndpointsSatisfied();
     }
@@ -117,7 +117,7 @@
                 // START SNIPPET: e1
                 DataFormat hl7 = new HL7DataFormat();
                 // we setup or HL7 listener on port 8888 (using the hl7codec) and in sync mode so we can return a response
-                from("mina:tcp://localhost:8888?sync=true&codec=#hl7codec")
+                from("mina:tcp://0.0.0.0:8888?sync=true&codec=#hl7codec")
                     // we use the HL7 data format to unmarshal from HL7 stream to the HAPI Message model
                     // this ensures that the camel message has been enriched with hl7 specific headers to
                     // make the routing much easier (see below)