View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * <p>
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * <p>
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  package org.apache.camel.component.hl7;
18  
19  import ca.uhn.hl7v2.HapiContext;
20  import org.apache.mina.core.session.IoSession;
21  import org.apache.mina.filter.codec.ProtocolDecoder;
22  import org.apache.mina.filter.codec.ProtocolEncoder;
23  
24  import java.nio.charset.Charset;
25  import java.nio.charset.CodingErrorAction;
26  import java.util.Locale;
27  
28  /**
29   * Custom HL7 MLLP codec. Ability to set the {@link ca.uhn.hl7v2.HapiContext}, e.g. to use some other
30   * {@link ca.uhn.hl7v2.parser.ModelClassFactory}. Also allows to set error handling in case of
31   * malformed input.
32   * <p/>
33   */
34  public class CustomHL7MLLPCodec extends HL7MLLPCodec {
35  
36      private CustomHL7MLLPConfig config = new CustomHL7MLLPConfig();
37  
38      public ProtocolDecoder getDecoder(IoSession session) throws Exception {
39          return new CustomHL7MLLPDecoder(config);
40      }
41  
42      public ProtocolEncoder getEncoder(IoSession session) throws Exception {
43          return new HL7MLLPEncoder(config);
44      }
45  
46      public void setHapiContext(HapiContext context) {
47          config.setHapiContext(context);
48      }
49  
50      public HapiContext getHapiContext() {
51          return config.getHapiContext();
52      }
53  
54      public void setCharset(Charset charset) {
55          config.setCharset(charset);
56      }
57  
58      public void setCharset(String charsetName) {
59          config.setCharset(Charset.forName(charsetName));
60      }
61  
62      public Charset getCharset() {
63          return config.getCharset();
64      }
65  
66      public boolean isConvertLFtoCR() {
67          return config.isConvertLFtoCR();
68      }
69  
70      public void setConvertLFtoCR(boolean convertLFtoCR) {
71          config.setConvertLFtoCR(convertLFtoCR);
72      }
73  
74      public char getStartByte() {
75          return config.getStartByte();
76      }
77  
78      public void setStartByte(char startByte) {
79          config.setStartByte(startByte);
80      }
81  
82      public char getEndByte1() {
83          return config.getEndByte1();
84      }
85  
86      public void setEndByte1(char endByte1) {
87          config.setEndByte1(endByte1);
88      }
89  
90      public char getEndByte2() {
91          return config.getEndByte2();
92      }
93  
94      public void setEndByte2(char endByte2) {
95          config.setEndByte2(endByte2);
96      }
97  
98      public boolean isValidate() {
99          return config.isValidate();
100     }
101 
102     public void setValidate(boolean validate) {
103         config.setValidate(validate);
104     }
105 
106     public boolean isProduceString() {
107         return config.isProduceString();
108     }
109 
110     public void setProduceString(boolean apply) {
111         config.setProduceString(apply);
112     }
113 
114     public void setUnmappableCharacterErrorAction(CodingErrorAction action) {
115         config.setUnmappableCharacterErrorAction(action);
116     }
117 
118     public void setMalformedInputErrorAction(CodingErrorAction action) {
119         config.setMalformedInputErrorAction(action);
120     }
121 
122     public void setUnmappableCharacterErrorAction(String action) {
123         config.setUnmappableCharacterErrorAction(fromString(action));
124     }
125 
126     public void setMalformedInputErrorAction(String action) {
127         config.setMalformedInputErrorAction(fromString(action));
128     }
129 
130     private CodingErrorAction fromString(String action) {
131         switch (action.toUpperCase(Locale.ROOT)) {
132             case "IGNORE" : return CodingErrorAction.IGNORE;
133             case "REPLACE" : return CodingErrorAction.REPLACE;
134             case "REPORT" : return CodingErrorAction.REPORT;
135             default: throw new IllegalArgumentException(action + " is not a valid CodingErrorAction (IGNORE, REPLACE, REPORT)");
136         }
137     }
138 }