Coverage Report - ca.uhn.hl7v2.llp.MinLLPReader
 
Classes in this File Line Coverage Branch Coverage Complexity
MinLLPReader
73%
47/64
85%
17/20
3.857
 
 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 "MinLLPReader.java".  Description: 
 10  
 "Title:        HL7Reader
 11  
   Description:  A buffered reader specifically designed for use in reading
 12  
                 HL7 ASCII strings" 
 13  
 
 14  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 15  
 2001.  All Rights Reserved. 
 16  
 
 17  
 Contributor(s): ______________________________________. 
 18  
 
 19  
 Alternatively, the contents of this file may be used under the terms of the 
 20  
 GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
 21  
 applicable instead of those above.  If you wish to allow use of your version of this 
 22  
 file only under the terms of the GPL and not to allow others to use your version 
 23  
 of this file under the MPL, indicate your decision by deleting  the provisions above 
 24  
 and replace  them with the notice and other provisions required by the GPL License.  
 25  
 If you do not delete the provisions above, a recipient may use your version of 
 26  
 this file under either the MPL or the GPL. 
 27  
 
 28  
 */
 29  
 
 30  
 package ca.uhn.hl7v2.llp;
 31  
 
 32  
 import java.io.BufferedReader;
 33  
 import java.io.IOException;
 34  
 import java.io.InputStream;
 35  
 import java.io.InputStreamReader;
 36  
 import java.net.Socket;
 37  
 import java.net.SocketException;
 38  
 import java.net.SocketTimeoutException;
 39  
 import java.nio.charset.Charset;
 40  
 
 41  
 import org.slf4j.Logger;
 42  
 import org.slf4j.LoggerFactory;
 43  
 
 44  
 /**
 45  
  * Title:        HL7Reader
 46  
  * Description:  A buffered reader specifically designed for use in reading
 47  
  *               HL7 ASCII strings. This class assumes that the minimal lower
 48  
  *               layer protocol is being used.
 49  
  * Copyright:    Copyright (c) 2001
 50  
  * Company:      University Health Network
 51  
  * @author       Damian Horton (damian.horton@uhn.on.ca); mods by Bryan Tripp
 52  
  */
 53  
 
 54  
 public class MinLLPReader implements HL7Reader
 55  
 {
 56  
     
 57  
     /**
 58  
      *<p>
 59  
      * System property: If a value is set for this property, the value
 60  
      * is interpreted as a character set, and this characterset is 
 61  
      * used. A possible example is "UTF-8" if you are receiving messages
 62  
      * from a system that transmits in UTF-8.
 63  
      * </p>
 64  
      * <p>
 65  
      * If the system property is set to a value of "default" (in other words, a
 66  
      * string containing only the word default), then the platform default
 67  
      * is used.
 68  
      * </p>
 69  
      * <p>
 70  
      * If the system property is not set, US-ASCII encoding is used.
 71  
      * </p>
 72  
      */
 73  
     public static final String CHARSET_KEY = "ca.uhn.hl7v2.llp.charset";
 74  
 
 75  1
     private static final Logger log = LoggerFactory.getLogger(MinLLPReader.class);
 76  
     
 77  
     private BufferedReader myReader;
 78  
 
 79  
         private Charset charset;
 80  
 
 81  
     /**
 82  
      * Character indicating the termination of an HL7 message
 83  
      */
 84  
     public static final char END_MESSAGE = '\u001c'; 
 85  
     
 86  
     /**
 87  
      * character indicating the start of an HL7 message
 88  
      */
 89  
     public static final char START_MESSAGE = '\u000b';
 90  
     
 91  
     /**
 92  
      * The final character of a message: a carriage return. NB: This is per the minimal lower layer protocol.
 93  
      */
 94  
     public static final char LAST_CHARACTER = 13;
 95  
 
 96  
     /** Creates a MinLLPReader with no setup - setInputStream must be set later. 
 97  
     */
 98  
     public MinLLPReader() 
 99  10
     {
 100  10
     }
 101  
     
 102  
     /** Creates a MinLLPReader which reads from the given InputStream. The stream
 103  
      *  is assumed to be an ASCII bit stream.
 104  
      */
 105  
     public MinLLPReader(InputStream in) throws IOException
 106  118
     {
 107  118
         setInputStream(in);
 108  117
     }
 109  
 
 110  
     /** Creates a MinLLPReader which reads from the given InputStream. The stream
 111  
      *  is assumed to be an ASCII bit stream.
 112  
      */
 113  
     public MinLLPReader(InputStream in, Charset theCharset) throws IOException
 114  1
     {
 115  1
             charset = theCharset;
 116  1
         setInputStream(in);
 117  1
     }
 118  
 
 119  
     /**
 120  
      * Sets the InputStream from which to read messages.  The InputStream must be set 
 121  
      * before any calls to <code>getMessage()</code>.
 122  
      */
 123  
     public synchronized void setInputStream(InputStream in) throws IOException 
 124  
     {
 125  122
         if (charset != null) {
 126  1
             myReader = new BufferedReader(new InputStreamReader(in, charset));
 127  
         } else { 
 128  121
                 String charsetString = System.getProperty(CHARSET_KEY, "US-ASCII");
 129  121
                 if (charsetString.equals("default")) {
 130  0
                     myReader = new BufferedReader(new InputStreamReader(in));
 131  
                 } else {
 132  121
                     myReader = new BufferedReader(new InputStreamReader(in, charsetString));
 133  
                 }
 134  
         }
 135  119
     }
 136  
 
 137  
     /** Calls the same method in the underlying BufferedReader. */
 138  
     /**private int read(char[] parm1, int parm2, int parm3) throws java.io.IOException
 139  
     {
 140  
         return myReader.read(parm1, parm2, parm3);
 141  
     }*/
 142  
 
 143  
     /** Reads an HL7 encoded message from this Reader's InputStream.
 144  
         @return The message, in string format, without the lower level
 145  
         protocol delimeters. Returns null if a -1 is received on the initial
 146  
          read.
 147  
      */
 148  
     public synchronized String getMessage() throws LLPException, IOException 
 149  
     {
 150  380
         StringBuilder s_buffer = new StringBuilder();
 151  
 
 152  380
         boolean end_of_message = false;
 153  
                         
 154  
             int c;
 155  
             try {
 156  380
                 c = myReader.read();  
 157  34
             } catch (SocketTimeoutException e) {
 158  34
                 log.debug("SocketTimeoutException on read() attempt.");
 159  34
                 return null;               
 160  38
             } catch (SocketException e) {
 161  38
                 log.info("SocketException on read() attempt.  Socket appears to have been closed: " + e.getMessage());
 162  38
                 throw e;
 163  305
             }
 164  
 
 165  
             //trying to read when there is no data (stream may have been closed at other end)
 166  305
             if (c == -1) {
 167  48
                 log.info("End of input stream reached.");
 168  48
                 throw new SocketException("End of input stream reached."); 
 169  
             }
 170  257
             LowerLayerProtocol.logCharacterReceived(c);
 171  
 
 172  257
             if (c != START_MESSAGE)
 173  
             {
 174  6
                 throw new LLPException("Message violates the "+
 175  
                     "minimal lower layer protocol: no start of message indicator "+
 176  
                     "received. Received: " + c);
 177  
             }
 178  
 
 179  45467
             while (! end_of_message)
 180  
             {
 181  45218
                 c = myReader.read();
 182  
 
 183  45218
                 if (c == -1)
 184  
                 {
 185  1
                     throw new LLPException("Message violates the "+
 186  
                         "minimal lower protocol: message terminated without "+
 187  
                         "a terminating character.");
 188  
                 }
 189  45217
                 LowerLayerProtocol.logCharacterReceived(c);
 190  
                 
 191  45217
                 if (c == END_MESSAGE)
 192  
                 {
 193  
                     //subsequent character should be a carriage return
 194  250
                     c = myReader.read();
 195  250
                     if (c >= 0) LowerLayerProtocol.logCharacterReceived(c);
 196  250
                     if (c != LAST_CHARACTER)
 197  
                     {
 198  1
                         throw new LLPException("Message "+
 199  
                             "violates the minimal lower layer protocol: "+
 200  
                             "message terminator not followed by a return "+
 201  
                             "character.");
 202  
                     }
 203  249
                     end_of_message = true;
 204  
                 }
 205  
                 else
 206  
                 {
 207  
                 //the character wasn't the end of message, append it to the message
 208  44967
                     s_buffer.append((char) c);
 209  
                 }
 210  
             } //end while
 211  
 
 212  249
         return s_buffer.toString();
 213  
     }
 214  
 
 215  
     /** Test driver for the program. Bounces a message off of an echo socket,
 216  
      *  and ascertaines whether or not this class works.
 217  
      */
 218  
     public static void main(String args[]) {
 219  0
             Socket s = null;
 220  
         try {
 221  0
             s = new Socket("142.224.51.2", 7);
 222  
 
 223  0
             HL7Reader in = new MinLLPReader(s.getInputStream());
 224  0
             HL7Writer out = new MinLLPWriter(s.getOutputStream());
 225  
 
 226  0
             out.writeMessage("Some message.");
 227  0
             System.out.println("wrote");
 228  0
             String str = in.getMessage();
 229  0
             System.out.println("read");
 230  
 
 231  0
             System.out.println(str);
 232  0
         } catch (Exception e) {
 233  0
             System.out.println(e);
 234  
         } finally {
 235  0
                 if (s != null) {
 236  
                         try {
 237  0
                                         s.close();
 238  0
                                 } catch (IOException e) {
 239  
                     // empty
 240  0
                                 }
 241  
                 }
 242  
         }
 243  0
     }
 244  
 
 245  
     /** 
 246  
      * Closes the underlying BufferedReader.
 247  
      */
 248  
     public synchronized void close() throws java.io.IOException
 249  
     {
 250  8
         myReader.close();
 251  8
     }
 252  
     
 253  
         
 254  
 }
 255