camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r798902 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/model/dataformat/ components/camel-hl7/src/main/java/org/apache/camel/component/hl7/ components/camel-hl7/src/test/java/...
Date Wed, 29 Jul 2009 13:35:59 GMT
Author: davsclaus
Date: Wed Jul 29 13:35:59 2009
New Revision: 798902

URL: http://svn.apache.org/viewvc?rev=798902&view=rev
Log:
CAMEL-1863: added validate option to camel-hl7 so you can disable HAPI parser validator.

Added:
    camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7ValidateTest.java
  (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/HL7DataFormat.java
    camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7Converter.java
    camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7DataFormat.java
    camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPCodec.java
    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

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java?rev=798902&r1=798901&r2=798902&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java Wed
Jul 29 13:35:59 2009
@@ -131,6 +131,15 @@
     }
 
     /**
+     * Uses the HL7 data format
+     */
+    public T hl7(boolean validate) {
+        HL7DataFormat hl7 = new HL7DataFormat();
+        hl7.setValidate(validate);
+        return dataFormat(hl7);
+    }
+
+    /**
      * Uses the JAXB data format
      */
     public T jaxb() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/HL7DataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/HL7DataFormat.java?rev=798902&r1=798901&r2=798902&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/HL7DataFormat.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/HL7DataFormat.java
Wed Jul 29 13:35:59 2009
@@ -18,9 +18,11 @@
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.camel.model.DataFormatDefinition;
+import org.apache.camel.spi.DataFormat;
 
 /**
  * Represents a <a href="http://camel.apache.org/hl7.html">HL7</a> {@link org.apache.camel.spi.DataFormat}.
@@ -31,8 +33,28 @@
 @XmlAccessorType(XmlAccessType.FIELD)
 public class HL7DataFormat extends DataFormatDefinition {
 
+    @XmlAttribute(required = false)
+    private Boolean validate = Boolean.TRUE;
+
     public HL7DataFormat() {
         super("org.apache.camel.component.hl7.HL7DataFormat");
     }
 
+    public Boolean isValidate() {
+        return validate;
+    }
+
+    public void setValidate(Boolean validate) {
+        this.validate = validate;
+    }
+
+    @Override
+    protected void configureDataFormat(DataFormat dataFormat) {
+        if (validate != null) {
+            setProperty(dataFormat, "validate", validate);
+        }
+    }
+
+
+
 }
\ No newline at end of file

Modified: camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7Converter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7Converter.java?rev=798902&r1=798901&r2=798902&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7Converter.java
(original)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7Converter.java
Wed Jul 29 13:35:59 2009
@@ -20,6 +20,7 @@
 import ca.uhn.hl7v2.model.Message;
 import ca.uhn.hl7v2.parser.Parser;
 import ca.uhn.hl7v2.parser.PipeParser;
+import ca.uhn.hl7v2.validation.impl.NoValidation;
 import org.apache.camel.Converter;
 
 /**
@@ -34,19 +35,31 @@
 
     @Converter
     public static String toString(Message message) throws HL7Exception {
-        Parser parser = new PipeParser();
-        String encoded = parser.encode(message);
-        return encoded;
+        return encode(message, true);
     }
 
     @Converter
     public static Message toMessage(String body) throws HL7Exception {
+        return parse(body, true);
+    }
+
+    static Message parse(String body, boolean validate) throws HL7Exception {
         // 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();
-        Message message = parser.parse(body);
-        return message;
+        if (!validate) {
+            parser.setValidationContext(new NoValidation());
+        }
+        return parser.parse(body);
+    }
+
+    static String encode(Message message, boolean validate) throws HL7Exception {
+        Parser parser = new PipeParser();
+        if (!validate) {
+            parser.setValidationContext(new NoValidation());
+        }
+        return parser.encode(message);
     }
 
 }

Modified: camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7DataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7DataFormat.java?rev=798902&r1=798901&r2=798902&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7DataFormat.java
(original)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7DataFormat.java
Wed Jul 29 13:35:59 2009
@@ -25,8 +25,6 @@
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.util.ExchangeHelper;
 
-
-
 /**
  * HL7 DataFormat (supports v2.x of the HL7 protocol).
  * <p/>
@@ -64,15 +62,17 @@
  */
 public class HL7DataFormat implements DataFormat {
 
+    private boolean validate = true;
+
     public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws
Exception {
         Message message = ExchangeHelper.convertToMandatoryType(exchange, Message.class,
body);
-        String encoded = HL7Converter.toString(message);
+        String encoded = HL7Converter.encode(message, validate);
         outputStream.write(encoded.getBytes());
     }
 
     public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception
{
         String body = ExchangeHelper.convertToMandatoryType(exchange, String.class, inputStream);
-        Message message = HL7Converter.toMessage(body);
+        Message message = HL7Converter.parse(body, validate);
 
         // add MSH fields as message out headers
         Terser terser = new Terser(message);
@@ -90,5 +90,12 @@
         return message;
     }
 
+    public boolean isValidate() {
+        return validate;
+    }
+
+    public void setValidate(boolean validate) {
+        this.validate = validate;
+    }
 }
 

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=798902&r1=798901&r2=798902&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 Jul 29 13:35:59 2009
@@ -101,4 +101,12 @@
         config.setEndByte2(endByte2);
     }
 
+    public boolean isValidate() {
+        return config.isValidate();
+    }
+
+    public void setValidate(boolean validate) {
+        config.setValidate(validate);
+    }
+
 }

Modified: 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=798902&r1=798901&r2=798902&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPConfig.java
(original)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPConfig.java
Wed Jul 29 13:35:59 2009
@@ -31,6 +31,8 @@
 
     private char endByte2 = 0x0d; // 13 decimal
 
+    private boolean validate = true;
+
     public Charset getCharset() {
         return charset;
     }
@@ -71,4 +73,11 @@
         this.endByte2 = endByte2;
     }
 
+    public boolean isValidate() {
+        return validate;
+    }
+
+    public void setValidate(boolean validate) {
+        this.validate = validate;
+    }
 }

Modified: 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=798902&r1=798901&r2=798902&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPDecoder.java
(original)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPDecoder.java
Wed Jul 29 13:35:59 2009
@@ -60,14 +60,6 @@
         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) {
@@ -89,10 +81,6 @@
         }
     }
 
-    /**
-     * @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);
@@ -107,8 +95,6 @@
      * 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
      */
@@ -126,7 +112,9 @@
                     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);
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Message starts at position " + state.posStart);
+                    }
                 }
             }
             // Check end bytes
@@ -135,7 +123,9 @@
                 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);
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Message ends at position " + state.posEnd);
+                    }
                     break;
                 } else {
                     // we expected the 2nd end marker
@@ -149,10 +139,6 @@
         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) {
@@ -185,4 +171,5 @@
             posEnd = 0;
         }
     }
+
 }

Modified: 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=798902&r1=798901&r2=798902&view=diff
==============================================================================
--- camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPEncoder.java
(original)
+++ camel/trunk/components/camel-hl7/src/main/java/org/apache/camel/component/hl7/HL7MLLPEncoder.java
Wed Jul 29 13:35:59 2009
@@ -19,8 +19,6 @@
 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;
@@ -29,8 +27,9 @@
 import org.apache.mina.filter.codec.ProtocolEncoder;
 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
 
-
-
+/**
+ * HL7 MLLP encoder
+ */
 class HL7MLLPEncoder implements ProtocolEncoder {
 
     private static final transient Log LOG = LogFactory.getLog(HL7MLLPEncoder.class);
@@ -65,8 +64,7 @@
         // convert to string
         String body;
         if (message instanceof Message) {
-            Parser parser = new PipeParser();
-            body = parser.encode((Message)message);
+            body = HL7Converter.encode((Message)message, config.isValidate());
         } else if (message instanceof String) {
             body = (String)message;
         } else if (message instanceof byte[]) {

Added: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7ValidateTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7ValidateTest.java?rev=798902&view=auto
==============================================================================
--- camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7ValidateTest.java
(added)
+++ camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7ValidateTest.java
Wed Jul 29 13:35:59 2009
@@ -0,0 +1,80 @@
+/**
+ * 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 ca.uhn.hl7v2.HL7Exception;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class HL7ValidateTest extends CamelTestSupport {
+
+// TODO: Need HL7 that can fail HL7 validator
+//    @Test
+//    public void testUnmarshalFailed() throws Exception {
+//        MockEndpoint mock = getMockEndpoint("mock:unmarshal");
+//        mock.expectedMessageCount(0);
+//
+//        String body = createHL7AsString();
+//        try {
+//            template.sendBody("direct:unmarshalFailed", body);
+//            fail("Should have thrown exception");
+//        } catch (CamelExecutionException e) {
+//            assertIsInstanceOf(HL7Exception.class, e.getCause());
+//        }
+//
+//        assertMockEndpointsSatisfied();
+//    }
+
+    @Test
+    public void testUnmarshalOk() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:unmarshal");
+        mock.expectedMessageCount(1);
+
+        String body = createHL7AsString();
+        template.sendBody("direct:unmarshalOk", body);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:unmarshalFailed").unmarshal().hl7().to("mock:unmarshal");
+
+                from("direct:unmarshalOk").unmarshal().hl7(false).to("mock:unmarshal");
+            }
+        };
+    }
+
+    private static String createHL7AsString() {
+        String line1 = "MSH|^~\\&|MYSENDER|MYSENDERAPP|MYCLIENT|MYCLIENTAPP|200612211200||QRY^A19|1234|P|2.4";
+        String line2 = "QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM||";
+
+        StringBuffer body = new StringBuffer();
+        body.append(line1);
+        body.append("\n");
+        body.append(line2);
+        return body.toString();
+    }
+
+}

Propchange: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7ValidateTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-hl7/src/test/java/org/apache/camel/component/hl7/HL7ValidateTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message