Coverage Report - ca.uhn.hl7v2.hoh.raw.client.HohRawClientSimple
 
Classes in this File Line Coverage Branch Coverage Complexity
HohRawClientSimple
45%
18/40
41%
5/12
1.9
 
 1  
 package ca.uhn.hl7v2.hoh.raw.client;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.net.Socket;
 5  
 import java.net.URL;
 6  
 
 7  
 import ca.uhn.hl7v2.hoh.api.DecodeException;
 8  
 import ca.uhn.hl7v2.hoh.api.EncodeException;
 9  
 import ca.uhn.hl7v2.hoh.api.IAuthorizationClientCallback;
 10  
 import ca.uhn.hl7v2.hoh.api.IClientSimple;
 11  
 import ca.uhn.hl7v2.hoh.api.IReceivable;
 12  
 import ca.uhn.hl7v2.hoh.api.ISendable;
 13  
 import ca.uhn.hl7v2.hoh.api.MessageMetadataKeys;
 14  
 import ca.uhn.hl7v2.hoh.auth.SingleCredentialClientCallback;
 15  
 import ca.uhn.hl7v2.hoh.raw.api.RawSendable;
 16  
 import ca.uhn.hl7v2.hoh.sockets.TlsSocketFactory;
 17  
 
 18  
 /**
 19  
  * <p>
 20  
  * Simple raw message sender using the HL7 over HTTP specification. This implementation 
 21  
  * makes use of a single connection so it is simple, but may only be used to send
 22  
  * one message at time (per instance).
 23  
  * </p>
 24  
  * <p>
 25  
  * This client uses no external threads, so it is suitable for use within
 26  
  * J2EE containers.
 27  
  * </p>
 28  
  */
 29  
 public class HohRawClientSimple extends AbstractRawClient implements IClientSimple {
 30  
 
 31  55
         private boolean myAutoClose = true;
 32  
         private Socket mySocket;
 33  
 
 34  
         /**
 35  
          * Constructor
 36  
          * 
 37  
          * @param theHost
 38  
          *            The HOST (name/address). E.g. "192.168.1.1"
 39  
          * @param thePort
 40  
          *            The PORT. E.g. "8080"
 41  
          * @param theUriPath
 42  
          *            The URI path being requested (must either be blank or start with
 43  
          *            '/' and contain a path). E.g. "/Apps/Receiver.jsp"
 44  
          */
 45  
         public HohRawClientSimple(String theHost, int thePort, String theUriPath) {
 46  50
                 super(theHost, thePort, theUriPath);
 47  35
         }
 48  
 
 49  
         /**
 50  
          * {@inheritDoc}
 51  
          */
 52  
         @Override
 53  
         public synchronized IReceivable<String> sendAndReceive(ISendable<?> theMessageToSend) throws DecodeException, IOException, EncodeException {
 54  
                 // **** Overridden to add synchronization
 55  45
                 return super.sendAndReceive(theMessageToSend);
 56  
         }
 57  
 
 58  
         /**
 59  
          * Constructor
 60  
          * 
 61  
          * @param theUrl
 62  
          *            The URL to connect to
 63  
          */
 64  
         public HohRawClientSimple(URL theUrl) {
 65  20
                 super(theUrl);
 66  20
         }
 67  
 
 68  
         /**
 69  
          * {@inheritDoc}
 70  
          */
 71  
         public void close() {
 72  45
                 if (mySocket != null) {
 73  45
                         closeSocket(mySocket);
 74  
                 }
 75  45
         }
 76  
 
 77  
         /**
 78  
          * {@inheritDoc}
 79  
          */
 80  
         public boolean isAutoClose() {
 81  45
                 return myAutoClose;
 82  
         }
 83  
 
 84  
         /* (non-Javadoc)
 85  
          * @see ca.uhn.hl7v2.hoh.raw.client.IClientSimple#isConnected()
 86  
          */
 87  
         public boolean isConnected() {
 88  0
                 return isSocketConnected(mySocket);
 89  
         }
 90  
 
 91  
 
 92  
         /**
 93  
          * {@inheritDoc}
 94  
          */
 95  
         @Override
 96  
         protected Socket provideSocket() throws IOException {
 97  45
                 if (mySocket == null || mySocket.isClosed() || mySocket.isInputShutdown() || mySocket.isOutputShutdown()) {
 98  45
                         mySocket = connect();
 99  
                 }
 100  45
                 return mySocket;
 101  
         }
 102  
 
 103  
         /**
 104  
          * {@inheritDoc}
 105  
          */
 106  
         @Override
 107  
         protected void returnSocket(Socket theSocket) {
 108  45
                 if (isAutoClose()) {
 109  45
                         close();
 110  
                 }
 111  45
         }
 112  
 
 113  
         /**
 114  
          * {@inheritDoc}
 115  
          */
 116  
         public void setAutoClose(boolean theAutoClose) {
 117  5
                 myAutoClose = theAutoClose;
 118  5
         }
 119  
 
 120  
         public static void main(String[] args) {
 121  
 
 122  
                 /*
 123  
                  * http://localhost:8080/AppContext
 124  
                  */
 125  0
                 String host = "localhost";
 126  0
                 int port = 8080;
 127  0
                 String uri = "/AppContext";
 128  
 
 129  
                 // Create a client
 130  0
                 HohRawClientSimple client = new HohRawClientSimple(host, port, uri);
 131  
 
 132  
                 // Set the socket factory to use TLS
 133  0
                 client.setSocketFactory(new TlsSocketFactory());
 134  
 
 135  
                 // Optionally, if credentials should be sent, they
 136  
                 // can be provided using a credential callback
 137  0
                 IAuthorizationClientCallback authCalback = new SingleCredentialClientCallback("ausername", "somepassword");
 138  0
                 client.setAuthorizationCallback(authCalback);
 139  
 
 140  
                 // The ISendable defines the object that provides the actual
 141  
                 // message to send
 142  0
                 String message = "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r" + "EVN||200803051509\r" + "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r";
 143  0
                 ISendable<?> sendable = new RawSendable(message);
 144  
 
 145  
                 try {
 146  
                         // sendAndReceive actually sends the message
 147  0
                         IReceivable<String> receivable = client.sendAndReceive(sendable);
 148  
 
 149  
                         // receivavle.getRawMessage() provides the response
 150  0
                         System.out.println("Response was:\n" + receivable.getMessage());
 151  
 
 152  
                         // IReceivable also stores metadata about the message
 153  0
                         String remoteHostIp = (String) receivable.getMetadata().get(MessageMetadataKeys.REMOTE_HOST_ADDRESS);
 154  0
                         System.out.println("From:\n" + remoteHostIp);
 155  
 
 156  0
                 } catch (DecodeException e) {
 157  
                         // Thrown if the response can't be read
 158  0
                         e.printStackTrace();
 159  0
                 } catch (IOException e) {
 160  
                         // Thrown if communication fails
 161  0
                         e.printStackTrace();
 162  0
                 } catch (EncodeException e) {
 163  
                         // Thrown if the message can't be encoded (generally a programming
 164  
                         // bug)
 165  0
                         e.printStackTrace();
 166  0
                 }
 167  
 
 168  0
         }
 169  
 
 170  
 }