Author: davsclaus
Date: Mon Sep 1 21:48:32 2008
New Revision: 691104
URL: http://svn.apache.org/viewvc?rev=691104&view=rev
Log:
CAMEL-859: Added configuration for start and end bytes. Added flag to disable
the \n to \r convertions. Based on end user feedback, thanks Marek.
Added:
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java
(contents, props changed)
- copied, changed from r690934,
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java
Modified:
activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java
activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java
Modified:
activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java?rev=691104&r1=691103&r2=691104&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java
(original)
+++
activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java
Mon Sep 1 21:48:32 2008
@@ -64,11 +64,12 @@
private static final String CHARSET_DECODER = HL7MLLPCodec.class.getName()
+ ".charsetdecoder";
// HL7 MLLP start and end markers
- private static final byte START_MARKER = 0x0b; // 11 decimal
- private static final byte END_MARKER_1 = 0x1c; // 28 decimal
- private static final byte END_MARKER_2 = 0x0d; // 13 decimal
+ 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() {
@@ -77,6 +78,9 @@
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);
@@ -99,14 +103,16 @@
}
// replace \n with \r as HL7 uses 0x0d = \r as segment
termninators
- body = body.replace('\n', '\r');
+ 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(START_MARKER);
+ bb.put((byte) startByte);
bb.putString(body, encoder);
- bb.put(END_MARKER_1);
- bb.put(END_MARKER_2);
+ bb.put((byte) endByte1);
+ bb.put((byte) endByte2);
// flip the buffer so we can use it to write to the out stream
bb.flip();
@@ -131,17 +137,17 @@
int posStart = 0;
while (in.hasRemaining()) {
byte b = in.get();
- if (b == START_MARKER) {
+ if (b == startByte) {
posStart = in.position();
}
- if (b == END_MARKER_1) {
+ if (b == endByte1) {
byte next = in.get();
- if (next == END_MARKER_2) {
+ 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 marker " + END_MARKER_2 + "
was not found, but was " + b);
+ LOG.warn("The 2nd end byte " + endByte2 + " was
not found, but was " + b);
}
}
}
@@ -197,4 +203,35 @@
this.charset = Charset.forName(charsetName);
}
+ 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;
+ }
}
Modified:
activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java?rev=691104&r1=691103&r2=691104&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java
(original)
+++
activemq/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/dataformat/hl7/HL7Converter.java
Mon Sep 1 21:48:32 2008
@@ -42,7 +42,7 @@
@Converter
public static Message toMessage(String body) throws HL7Exception {
- // replace \n with \r as HL7 uses 0x0d = \r as segment terminators
+ // replace \n with \r as HL7 uses 0x0d = \r as segment terminators and
HAPI only parses with \r
body = body.replace('\n', '\r');
Parser parser = new PipeParser();
Copied:
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java
(from r690934,
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java)
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java?p2=activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java&p1=activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java&r1=690934&r2=691104&rev=691104&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecTest.java
(original)
+++
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java
Mon Sep 1 21:48:32 2008
@@ -29,19 +29,22 @@
import org.apache.camel.impl.JndiRegistry;
/**
- * Unit test for the HL7MLLP Codec.
+ * Unit test for the HL7MLLP Codec using different start and end bytes.
*/
-public class HL7MLLPCodecTest extends ContextTestSupport {
+public class HL7MLLPCodecStandAndEndBytesTest extends ContextTestSupport {
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
- // START SNIPPET: e1
HL7MLLPCodec codec = new HL7MLLPCodec();
codec.setCharset("iso-8859-1");
+ // to test with different start and end bytes.
+ codec.setStartByte('*');
+ codec.setEndByte1('#');
+ codec.setEndByte2('*');
+ codec.setConvertLFtoCR(false);
jndi.bind("hl7codec", codec);
- // END SNIPPET: e1
return jndi;
}
@@ -68,24 +71,21 @@
}
public void testSendHL7Message() throws Exception {
- // START SNIPPET: e2
String line1 =
"MSH|^~\\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.4";
String line2 =
"QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM||";
StringBuffer in = new StringBuffer();
in.append(line1);
- in.append("\n");
+ in.append("\r");
in.append(line2);
String out =
(String)template.requestBody("mina:tcp://localhost:8888?sync=true&codec=hl7codec",
in.toString());
- // END SNIPPET: e2
String[] lines = out.split("\r");
assertEquals("MSH|^~\\&|MYSENDER||||200701011539||ADR^A19||||123",
lines[0]);
assertEquals("MSA|AA|123", lines[1]);
}
- // START SNIPPET: e3
private static Message createHL7AsMessage() throws Exception {
ADR_A19 adr = new ADR_A19();
@@ -109,6 +109,5 @@
return adr.getMessage();
}
- // END SNIPPET: e3
-}
+}
\ No newline at end of file
Propchange:
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange:
activemq/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7MLLPCodecStandAndEndBytesTest.java
------------------------------------------------------------------------------
svn:mergeinfo =