Package ca.uhn.hl7v2.protocol

Examples of ca.uhn.hl7v2.protocol.Transportable


    @Converter
    public static Message toMessage(String body) throws HL7Exception {
        // replace \n with \r as HL7 uses 0x0d = \r as segment terminators and HAPI only parses with \r
        body = body.replace('\n', '\r');

        Parser parser = new PipeParser();
        Message message = parser.parse(body);
        return message;
    }
View Full Code Here


                ack.getMSH().getFieldSeparator().setValue(HL7Constants.HL7_FIELD_SEPARATOR);
                ack.getMSH().getEncodingCharacters().setValue(HL7Constants.HL7_ENCODING_CHARS);
                ack.getMSA().getAcknowledgmentCode().setValue(HL7Constants.HL7_ACK_CODE_AR);
                ack.getERR().getErrorCodeAndLocation(0).getCodeIdentifyingError().getIdentifier()
                        .setValue("Backend service reject the value");
                String msg = new PipeParser().encode(ack);
                if (log.isDebugEnabled()) {
                    log.debug("Generate HL7 error : " + ack);
                }
                outputStream.write(msg.getBytes());
                outputStream.flush();
                outputStream.close();
                msgCtx.setProperty(Constants.Configuration.CONTENT_TYPE,HL7Constants.HL7_CONTENT_TYPE);
            } catch (DataTypeException e) {
                handleException("Error on creating HL7 Error segment", e);
            } catch (HL7Exception e) {
                handleException("Error on creating HL7 Error segment", e);
            } catch (IOException e) {
                handleException("Error on writing HL7 Error to output stream", e);
            }
        } else {
            try {
                String xmlFormat = omElement.toString();
                Message message = new DefaultXMLParser().parse(xmlFormat);
                String msg = new PipeParser().encode(message);
                if (log.isDebugEnabled()) {
                    log.debug("Message inside the formatter : " + message);
                }
                outputStream.write(msg.getBytes());
                outputStream.flush();
View Full Code Here

     * @param rowHL7
     * @return XML String
     */
    private String serializeHL7toXML(String rowHL7) {
        Parser xmlParser = new DefaultXMLParser();
        Parser ediParser = new PipeParser();
        ediParser.setValidationContext(new NoValidation());
        String xmlDoc = null;
        try {
            Message message = ediParser.parse(rowHL7);
            ConformanceProfileRule rule = new ConformanceProfileRule();
        ValidationException[] exs = rule.test(message);
        if (exs != null && exs.length > 0) {
          throw new HL7Exception(exs[0].getMessage());
       
View Full Code Here

    private SimpleServer server;

    public HL7Server(int port) {
        LowerLayerProtocol llp = LowerLayerProtocol.makeLLP(); // The transport protocol
        PipeParser parser = new PipeParser();
        server = new SimpleServer(port, llp, parser);
    }
View Full Code Here

    /**
     * {@inheritDoc}
     */
    public Message processMessage(Message theIn) throws ApplicationException, HL7Exception {

        String encodedMessage = new PipeParser().encode(theIn);
        System.out.println("Received message:\n" + encodedMessage + "\n\n");

        // Now we need to generate a message to return. This will generally be an ACK message.
        Segment msh = (Segment) theIn.get("MSH");
        Message retVal;
View Full Code Here

        System.out.println("[ Executing HL7Sender : HOST:" + host + "  ;port :" + port + " ]");
        // The connection hub connects to listening servers
        ConnectionHub connectionHub = ConnectionHub.getInstance();
        // A connection object represents a socket attached to an HL7 server
        Connection connection = connectionHub
                .attach(host, port, new PipeParser(), MinLowerLayerProtocol.class);

        // The initiator is used to transmit unsolicited messages
        Initiator initiator = connection.getInitiator();
        HL7Message sampleMessage = new HL7Message();

        //send
        Message response = null;
        try {
            response = initiator.sendAndReceive(sampleMessage.getHL7Message());
            PipeParser parser = new PipeParser();
            String responseString = parser.encode(response);
            System.out.println("Received response:\n" + responseString);
        } catch (LLPException e) {
            System.out.println("Error : " + e);
        } catch (IOException e) {
            System.out.println("Error : " + e);
View Full Code Here

    Map<String, Object> metadata = new HashMap<String, Object>();
    InetSocketAddress remoteSocketAddress = (InetSocketAddress) inboundSocket.getRemoteSocketAddress();
    metadata.put(ApplicationRouter.METADATA_KEY_SENDING_IP, remoteSocketAddress.getAddress().getHostAddress());
    metadata.put(ApplicationRouter.METADATA_KEY_SENDING_PORT, remoteSocketAddress.getPort());
   
    Transportable response = apps.processMessage(new TransportableImpl(incomingMessageString, metadata));
    return response.getMessage();
  }
View Full Code Here

        trySend(myContext.getLocallyDrivenTransportLayer(), theMessage);
       
        boolean originalMode = (needAcceptAck == null && needAppAck == null);
        if (originalMode || !NE.equals(needAcceptAck)) {
       
            Transportable response = null;
            int retries = 0;
            do {
                long until = System.currentTimeMillis() + retryIntervalMillis;
                while (response == null && System.currentTimeMillis() < until) {
                    synchronized (this) {
                        ExpiringTransportable et = myAcceptAcks.remove(controlId);
                        if (et == null) {
                            cycleIfNeeded(true);
                        } else {
                            response = et.transportable;
                        }
                    }
                    sleepIfNeeded();
                }
               
                if ((response == null && needAcceptAck != null && needAcceptAck.equals(AL))
                        || (response != null && isReject(response))) {
                    log.info("Resending message {}", controlId);
                    trySend(myContext.getLocallyDrivenTransportLayer(), theMessage);
                    response = null;                   
                }
               
                if (response != null && isError(response)) {
                    String[] errMsgPath = {"MSA-3"};
                    String[] errMsg = PreParser.getFields(response.getMessage(), errMsgPath);                   
                    throw new HL7Exception("Error message received: " + errMsg[0]);
                }
               
            } while (response == null && ++retries <= maxRetries);
        }
View Full Code Here

   
    /**
     * Tries to receive a message, and if there is an error reconnects and tries again.
     */
    private Transportable tryReceive(TransportLayer theTransport) throws TransportException {
        Transportable message = null;
        try {
            message = theTransport.receive();           
        } catch (TransportException e) {
            theTransport.disconnect();
            theTransport.connect();
View Full Code Here

     
      cleanReservations();
        cleanAcceptAcks();
        cleanReservedMessages();

        Transportable in = null;
        try {
            if (expectingAck) {
                in = tryReceive(myContext.getLocallyDrivenTransportLayer());
            } else {
                in = tryReceive(myContext.getRemotelyDrivenTransportLayer());
            }
        } catch (TransportException e) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {}
            throw e;
        }
       
        // log
        if (in != null) {
               log.debug("Received message: {}", in.getMessage());
        } else {
          log.debug("Received no message");
        }
       
        // If we have a message, handle it
        if (in != null) {
            String acceptAckNeeded = null;
//            String appAckNeeded = null;
            String ackCode = null;
            String ackId = null;
           
            try {
              String[] fieldPaths = {"MSH-15", "MSH-16", "MSA-1", "MSA-2"};
              String[] fields = PreParser.getFields(in.getMessage(), fieldPaths);        
        acceptAckNeeded = fields[0];
//        appAckNeeded = fields[1];
        ackCode = fields[2];
        ackId = fields[3];
            } catch (HL7Exception e) {
              log.warn("Failed to parse accept ack fields in incoming message", e);
            }
           
            if (ackId != null && ackCode != null && ackCode.startsWith("C")) {
                long expiryTime = System.currentTimeMillis() + 1000 * 60;
                myAcceptAcks.put(ackId, new ExpiringTransportable(in, expiryTime));
            } else {
                AcceptAcknowledger.AcceptACK ack = AcceptAcknowledger.validate(getContext(), in);
           
                if ((acceptAckNeeded != null && acceptAckNeeded.equals(AL))
                    || (acceptAckNeeded != null && acceptAckNeeded.equals(ER) && !ack.isAcceptable())
                    || (acceptAckNeeded != null && acceptAckNeeded.equals(SU) && ack.isAcceptable())) {
                    trySend(myContext.getRemotelyDrivenTransportLayer(), ack.getMessage());   
                }
 
                if (ack.isAcceptable()) {
                    if (isReserved(ackId)) {
                     
                      log.debug("Received expected ACK message with ACK ID: {}", ackId);
                     
                        removeReservation(ackId);
                        long expiryTime = System.currentTimeMillis() + 1000 * 60 * 5;               
                        myAvailableMessages.put(ackId, new ExpiringTransportable(in, expiryTime));
                       
                    } else {

                      log.debug("Sending message to router");
                        Transportable out = myContext.getRouter().processMessage(in);
                        sendAppResponse(out);
                       
                    }
                } else {
                  // TODO: should we do something more here? Might be nice to
View Full Code Here

TOP

Related Classes of ca.uhn.hl7v2.protocol.Transportable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.