View Javadoc
1   /**
2    The contents of this file are subject to the Mozilla Public License Version 1.1
3    (the "License"); you may not use this file except in compliance with the License.
4    You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    Software distributed under the License is distributed on an "AS IS" basis,
6    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    specific language governing rights and limitations under the License.
8   
9    The Original Code is "MinLowerLayerProtocol.java".  Description:
10   "Implements the "Minimal Lower Layer Protocol" from the HL7 Implementation
11   Guide, Appendix C"
12  
13   The Initial Developer of the Original Code is University Health Network. Copyright (C)
14   2001.  All Rights Reserved.
15  
16   Contributor(s): ______________________________________.
17  
18   Alternatively, the contents of this file may be used under the terms of the
19   GNU General Public License (the "GPL"), in which case the provisions of the GPL are
20   applicable instead of those above.  If you wish to allow use of your version of this
21   file only under the terms of the GPL and not to allow others to use your version
22   of this file under the MPL, indicate your decision by deleting  the provisions above
23   and replace  them with the notice and other provisions required by the GPL License.
24   If you do not delete the provisions above, a recipient may use your version of
25   this file under either the MPL or the GPL.
26  
27   */
28  
29  package ca.uhn.hl7v2.llp;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  
35  /**
36   * Implements the "Minimal Lower Layer Protocol" from the HL7 Implementation
37   * Guide, Appendix C.  In other words, provides a reader and a writer that can be
38   * used to communicate with a server that uses the minimal LLP.
39   * <p>
40   * Note:
41   * </p>
42   * <p>
43   * The MLLP Block is framed by single-byte values. The characters transmitted within the MLLP Block
44   * have to be encoded in such a way that the HL7 Content does not conflict with the byte values used
45   * for framing. Some multi-byte character encodings (e.g. UTF-16, UTF-32) may result in byte values
46   * equal to the MLLP framing characters or byte values lower than 0x1F, resulting in errors.
47   * These character encodings are therefore not supported by MLLP.
48   * MLLP supports all single-byte character encodings (e.g. iso-8859-x, cp1252) as well as UTF-8 and Shift_JIS.
49   * The byte values used by UTF-8 do not conflict with the byte values used for MLLP framing.
50   * </p>
51   * <p>
52   * HAPI still tries to support UTF-16 and UTF-32 as best as possible. To be able to detect per-message
53   * encoding information in MSH-18 with these encodings, corresponding byte order marks (BOMs) are expected
54   * at the beginning of the message payload (i.e. AFTER the MLLP start byte) to be able to decode the
55   * message in order to read the MSH-18 field. This scheme is also followed when the MinLLPWriter
56   * sends out UTF-16 or UTF-32-encoded MLLP messages unless {@link #omitBOM} is explicitly
57   * set to true.
58   * </p>
59   * <p>
60   * In general, it is <b>NOT</b> recommended to use UTF-16 or UTF-32.
61   * </p>
62   *
63   * @author Bryan Tripp
64   * @author Christian Ohr
65   *
66   * @see MinLLPReader
67   * @see MinLLPWriter
68   */
69  public class MinLowerLayerProtocol extends LowerLayerProtocol {
70  
71      private final boolean respectMSH18;
72      private final boolean omitBOM;
73  
74  
75      public MinLowerLayerProtocol() {
76          this(false, false);
77      }
78  
79      public MinLowerLayerProtocol(boolean respectMSH18) {
80          this(respectMSH18, false);
81      }
82  
83      public MinLowerLayerProtocol(boolean respectMSH18, boolean omitBOM) {
84          this.respectMSH18 = respectMSH18;
85          this.omitBOM = omitBOM;
86      }
87  
88      /**
89       * Creates an HL7Reader that implements message reading according to
90       * this protocol.
91       */
92      public HL7Reader getReader(InputStream in) throws LLPException {
93          try {
94              return respectMSH18 ?
95                      new ExtendedMinLLPReader(in, charset) :
96                      new MinLLPReader(in, charset);
97          } catch (IOException e) {
98              throw new LLPException("Can't create Reader with the given input stream: " + e.getMessage(), e);
99          }
100     }
101 
102     /**
103      * Creates an HL7Writer that implements message writing according to
104      * this protocol.
105      */
106     public HL7Writer getWriter(OutputStream out) throws LLPException {
107         try {
108             return respectMSH18 ?
109                     new ExtendedMinLLPWriter(out, charset, omitBOM) :
110                     new MinLLPWriter(out, charset, omitBOM);
111         } catch (IOException e) {
112             throw new LLPException("Can't create Writer with the given output stream: " + e.getMessage(), e);
113         }
114     }
115 
116 }