001/* 002The 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. 004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005Software distributed under the License is distributed on an "AS IS" basis, 006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007specific language governing rights and limitations under the License. 008 009The Original Code is "MLLPTransport.java". Description: 010"An implementation of the HL7 Minimal Lower Layer Protocol." 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132004. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025*/ 026 027package ca.uhn.hl7v2.protocol.impl; 028 029import java.io.IOException; 030import java.io.InputStream; 031import java.util.Properties; 032 033import ca.uhn.hl7v2.llp.LLPException; 034import ca.uhn.hl7v2.llp.MinLLPReader; 035import ca.uhn.hl7v2.llp.MinLLPWriter; 036import ca.uhn.hl7v2.protocol.StreamSource; 037import ca.uhn.hl7v2.protocol.TransportException; 038import ca.uhn.hl7v2.protocol.TransportLayer; 039import ca.uhn.hl7v2.protocol.Transportable; 040 041/** 042 * An implementation of the HL7 Minimal Lower Layer Protocol (see 043 * HL7 implementation guide appendix C). Note that this is the most common 044 * protocol used in HL7 interfaces. 045 * 046 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a> 047 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $ 048 */ 049public class MLLPTransport extends AbstractTransport implements TransportLayer { 050 051 private MinLLPReader myReader; 052 private MinLLPWriter myWriter; 053 054 private final StreamSource myStreamSource; 055 private final Properties myCharsetMappings; 056 057 /** 058 * @param theStreamSource the provider of input and output streams connected 059 * to the remote server 060 */ 061 public MLLPTransport(StreamSource theStreamSource) throws TransportException { 062 myStreamSource = theStreamSource; 063 myCharsetMappings = loadCharsetMappings(); 064 } 065 066 private static Properties loadCharsetMappings() throws TransportException { 067 Properties mappings = new Properties(); 068 String resource = "ca/uhn/hl7v2/protocol/impl/charset_map.properties"; 069 InputStream in = MLLPTransport.class.getClassLoader().getResourceAsStream(resource); 070 try { 071 mappings.load(in); 072 } catch (IOException e) { 073 throw new TransportException("Can't load character set mappings from " + resource, e); 074 } 075 return mappings; 076 } 077 078 public void doSend(Transportable theMessage) throws TransportException { 079 try { 080 String charset = (String) theMessage.getMetadata().get("MSH-18"); 081 if (charset != null) { 082 charset = myCharsetMappings.getProperty(charset, charset); //default to self if no match 083 myWriter.writeMessage(theMessage.getMessage(), charset); 084 } else { 085 myWriter.writeMessage(theMessage.getMessage()); 086 } 087 } catch (LLPException | IOException e) { 088 throw new TransportException(e); 089 } 090 } 091 092 /** 093 * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doReceive() 094 */ 095 public Transportable doReceive() throws TransportException { 096 Transportable result = null; 097 try { 098 String message = myReader.getMessage(); 099 if (message != null) { 100 result = new TransportableImpl(message); 101 } 102 } catch (LLPException | IOException e) { 103 throw new TransportException(e); 104 } 105 return result; 106 } 107 108 public void doConnect() throws TransportException { 109 myStreamSource.connect(); 110 try { 111 myReader = new MinLLPReader(myStreamSource.getInboundStream()); 112 myWriter = new MinLLPWriter(myStreamSource.getOutboundStream()); 113 } catch (IOException e) { 114 throw new TransportException(e); 115 } 116 } 117 118 /** 119 * @see ca.uhn.hl7v2.protocol.TransportLayer#disconnect() 120 */ 121 public void doDisconnect() throws TransportException { 122 try { 123 if (myReader != null) myReader.close(); 124 if (myWriter != null) myWriter.close(); 125 } catch (IOException e) { 126 throw new TransportException(e); 127 } finally { 128 myReader = null; 129 myWriter = null; 130 } 131 myStreamSource.disconnect(); 132 } 133 134}