001/**
002 The contents of this file are subject to the Mozilla Public License Version 1.1
003 (the "License"); you may not use this file except in compliance with the License.
004 You may obtain a copy of the License at http://www.mozilla.org/MPL/
005 Software distributed under the License is distributed on an "AS IS" basis,
006 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007 specific language governing rights and limitations under the License.
008
009 The Original Code is "MinLowerLayerProtocol.java".  Description:
010 "Implements the "Minimal Lower Layer Protocol" from the HL7 Implementation
011 Guide, Appendix C"
012
013 The Initial Developer of the Original Code is University Health Network. Copyright (C)
014 2001.  All Rights Reserved.
015
016 Contributor(s): ______________________________________.
017
018 Alternatively, the contents of this file may be used under the terms of the
019 GNU General Public License (the "GPL"), in which case the provisions of the GPL are
020 applicable instead of those above.  If you wish to allow use of your version of this
021 file only under the terms of the GPL and not to allow others to use your version
022 of this file under the MPL, indicate your decision by deleting  the provisions above
023 and replace  them with the notice and other provisions required by the GPL License.
024 If you do not delete the provisions above, a recipient may use your version of
025 this file under either the MPL or the GPL.
026
027 */
028
029package ca.uhn.hl7v2.llp;
030
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.OutputStream;
034
035/**
036 * Implements the "Minimal Lower Layer Protocol" from the HL7 Implementation
037 * Guide, Appendix C.  In other words, provides a reader and a writer that can be
038 * used to communicate with a server that uses the minimal LLP.
039 * <p>
040 * Note:
041 * </p>
042 * <p>
043 * The MLLP Block is framed by single-byte values. The characters transmitted within the MLLP Block
044 * have to be encoded in such a way that the HL7 Content does not conflict with the byte values used
045 * for framing. Some multi-byte character encodings (e.g. UTF-16, UTF-32) may result in byte values
046 * equal to the MLLP framing characters or byte values lower than 0x1F, resulting in errors.
047 * These character encodings are therefore not supported by MLLP.
048 * MLLP supports all single-byte character encodings (e.g. iso-8859-x, cp1252) as well as UTF-8 and Shift_JIS.
049 * The byte values used by UTF-8 do not conflict with the byte values used for MLLP framing.
050 * </p>
051 * <p>
052 * HAPI still tries to support UTF-16 and UTF-32 as best as possible. To be able to detect per-message
053 * encoding information in MSH-18 with these encodings, corresponding byte order marks (BOMs) are expected
054 * at the beginning of the message payload (i.e. AFTER the MLLP start byte) to be able to decode the
055 * message in order to read the MSH-18 field. This scheme is also followed when the MinLLPWriter
056 * sends out UTF-16 or UTF-32-encoded MLLP messages unless {@link #setOmitBOM(boolean)} is explicitly
057 * set to true.
058 * </p>
059 * <p>
060 * In general, it is <b>NOT</b> recommended to use UTF-16 or UTF-32.
061 * </p>
062 *
063 * @author Bryan Tripp
064 * @author Christian Ohr
065 *
066 * @see MinLLPReader
067 * @see MinLLPWriter
068 */
069public class MinLowerLayerProtocol extends LowerLayerProtocol {
070
071    private final boolean respectMSH18;
072    private final boolean omitBOM;
073
074
075    public MinLowerLayerProtocol() {
076        this(false, false);
077    }
078
079    public MinLowerLayerProtocol(boolean respectMSH18) {
080        this(respectMSH18, false);
081    }
082
083    public MinLowerLayerProtocol(boolean respectMSH18, boolean omitBOM) {
084        this.respectMSH18 = respectMSH18;
085        this.omitBOM = omitBOM;
086    }
087
088    /**
089     * Creates an HL7Reader that implements message reading according to
090     * this protocol.
091     */
092    public HL7Reader getReader(InputStream in) throws LLPException {
093        try {
094            return respectMSH18 ?
095                    new ExtendedMinLLPReader(in, charset) :
096                    new MinLLPReader(in, charset);
097        } catch (IOException e) {
098            throw new LLPException("Can't create Reader with the given input stream: " + e.getMessage(), e);
099        }
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}