Coverage Report - ca.uhn.hl7v2.app.Responder
 
Classes in this File Line Coverage Branch Coverage Complexity
Responder
30%
10/33
0%
0/2
1.75
 
 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 "Responder.java".  Description:
 10  
  * "Performs the responding role in a message exchange according to HL7's original mode
 11  
  * processing rules."
 12  
  *
 13  
  * The Initial Developer of the Original Code is University Health Network. Copyright (C)
 14  
  * 2002.  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  
 package ca.uhn.hl7v2.app;
 29  
 
 30  
 import java.io.BufferedReader;
 31  
 import java.io.File;
 32  
 import java.io.FileReader;
 33  
 import java.io.PipedInputStream;
 34  
 import java.io.PipedOutputStream;
 35  
 import java.io.Reader;
 36  
 import java.net.InetSocketAddress;
 37  
 import java.net.Socket;
 38  
 import java.util.HashMap;
 39  
 import java.util.Map;
 40  
 
 41  
 import ca.uhn.hl7v2.HL7Exception;
 42  
 import ca.uhn.hl7v2.model.Message;
 43  
 import ca.uhn.hl7v2.parser.Parser;
 44  
 import ca.uhn.hl7v2.parser.PipeParser;
 45  
 import ca.uhn.hl7v2.protocol.ApplicationRouter;
 46  
 import ca.uhn.hl7v2.protocol.Transportable;
 47  
 import ca.uhn.hl7v2.protocol.impl.TransportableImpl;
 48  
 
 49  
 /**
 50  
  * <p>
 51  
  * Performs the responding role in a message exchange (i.e receiver of the first
 52  
  * message, sender of the response; analagous to the server in a client-server
 53  
  * interaction), according to HL7's original mode processing rules.
 54  
  * </p>
 55  
  * <p>
 56  
  * At the time of writing, enhanced mode, two-phase reply, continuation
 57  
  * messages, and batch processing are unsupported.
 58  
  * </p>
 59  
  * 
 60  
  * @author Bryan Tripp
 61  
  */
 62  
 class Responder {
 63  
 
 64  
         private ApplicationRouter apps;
 65  
         private Socket inboundSocket;
 66  
 
 67  
         /**
 68  
          * Creates a new instance of Responder
 69  
          */
 70  70
         public Responder(Socket theInboundSocket) {
 71  70
                 inboundSocket = theInboundSocket;
 72  70
         }
 73  
 
 74  
 
 75  
         /**
 76  
          * Processes an incoming message string and returns the response message
 77  
          * string. Message processing consists of parsing the message, finding an
 78  
          * appropriate Application and processing the message with it, and encoding
 79  
          * the response. Applications are chosen from among those registered using
 80  
          * <code>registerApplication</code>. The Parser is obtained from the
 81  
          * Connection associated with this Responder.
 82  
          */
 83  
         protected String processMessage(String incomingMessageString)
 84  
                         throws HL7Exception {
 85  
                 
 86  117
                 Map<String, Object> metadata = new HashMap<String, Object>();
 87  117
                 InetSocketAddress remoteSocketAddress = (InetSocketAddress) inboundSocket.getRemoteSocketAddress();
 88  117
                 metadata.put(ApplicationRouter.METADATA_KEY_SENDING_IP, remoteSocketAddress.getAddress().getHostAddress());
 89  
                 
 90  117
                 Transportable response = apps.processMessage(new TransportableImpl(incomingMessageString, metadata));
 91  116
                 return response.getMessage();
 92  
         }
 93  
         
 94  
 //        /**
 95  
 //         * Logs the given exception and creates an error message to send to the
 96  
 //         * remote system.
 97  
 //         * 
 98  
 //         * @param encoding
 99  
 //         *            The encoding for the error message. If <code>null</code>, uses
 100  
 //         *            default encoding
 101  
 //         */
 102  
 //        public static String logAndMakeErrorMessage(Exception e, Segment inHeader,
 103  
 //                        Parser p, String encoding) throws HL7Exception {
 104  
 //                return ApplicationRouterImpl.logAndMakeErrorMessage(e, inHeader, p, encoding);
 105  
 //        }
 106  
 
 107  
         /**
 108  
          * Registers an Application with this Responder. The "Application", in this
 109  
          * context, is the software that uses the information in the message. If
 110  
          * multiple applications are registered, incoming Message objects will be
 111  
          * passed to each one in turn (calling <code>canProcess()</code>) until one
 112  
          * of them accepts responsibility for the message. If none of the registered
 113  
          * applications can process the message, a DefaultApplication is used, which
 114  
          * simply returns an Application Reject message.
 115  
          */
 116  
         void setApplicationRouter(ApplicationRouter router) {
 117  36
                 this.apps = router;
 118  36
         }
 119  
 
 120  
         /**
 121  
          * Test code.
 122  
          */
 123  
         @SuppressWarnings({ "unused", "resource" })
 124  
         public static void main(String args[]) {
 125  0
                 if (args.length != 1) {
 126  0
                         System.err.println("Usage: DefaultApplication message_file");
 127  0
                         System.exit(1);
 128  
                 }
 129  
 
 130  
                 // read test message file ...
 131  
                 try {
 132  0
                         File messageFile = new File(args[0]);
 133  0
                         Reader in = new BufferedReader(new FileReader(messageFile));
 134  0
                         int fileLength = (int) messageFile.length();
 135  0
                         char[] cbuf = new char[fileLength];
 136  0
                         in.read(cbuf, 0, fileLength);
 137  0
                         String messageString = new String(cbuf);
 138  
 
 139  
                         // parse inbound message ...
 140  0
                         final Parser parser = new PipeParser();
 141  0
                         Message inMessage = null;
 142  
                         try {
 143  0
                                 inMessage = parser.parse(messageString);
 144  0
                         } catch (HL7Exception e) {
 145  0
                                 e.printStackTrace();
 146  0
                         }
 147  
 
 148  
                         // process with responder ...
 149  0
                         PipedInputStream initInbound = new PipedInputStream();
 150  0
                         PipedOutputStream initOutbound = new PipedOutputStream();
 151  0
                         PipedInputStream respInbound = new PipedInputStream(initOutbound);
 152  0
                         PipedOutputStream respOutbound = new PipedOutputStream(initInbound);
 153  
 
 154  
                         /*
 155  
                          * This code won't work with new changes: final Initiator init = new
 156  
                          * Initiator(parser, new MinLowerLayerProtocol(), initInbound,
 157  
                          * initOutbound); Responder resp = new Responder(respInbound,
 158  
                          * respOutbound);
 159  
                          * 
 160  
                          * //run the initiator in a separate thread ... final Message
 161  
                          * inMessCopy = inMessage; Thread initThd = new Thread(new
 162  
                          * Runnable() { public void run() { try { Message response =
 163  
                          * init.sendAndReceive(inMessCopy);
 164  
                          * System.out.println("This is initiator writing response ...");
 165  
                          * System.out.println(parser.encode(response)); } catch (Exception
 166  
                          * ie) { if (HL7Exception.class.isAssignableFrom(ie.getClass())) {
 167  
                          * System.out.println("Error in segment " +
 168  
                          * ((HL7Exception)ie).getSegmentName() + " field " +
 169  
                          * ((HL7Exception)ie).getFieldPosition()); } ie.printStackTrace(); }
 170  
                          * } }); initThd.start();
 171  
                          * 
 172  
                          * //process the message we expect from the initiator thread ...
 173  
                          * System.out.println("Responder is going to respond now ...");
 174  
                          * resp.processOneMessage();
 175  
                          */
 176  0
                 } catch (Exception e) {
 177  0
                         e.printStackTrace();
 178  0
                 }
 179  
 
 180  0
         }
 181  
 
 182  
 
 183  
 }