parser.setValidationContext( validationContext ); Message msg = parser.parse( message ); Structure structure = msg.get( "MSH" ); String messageType = null; String version = msg.getVersion();
private String handleProcessMessageException(String incomingMessageString, Map<String, Object> theMetadata, Message incomingMessageObject, Exception e) throws HL7Exception { String outgoingMessageString; Segment inHeader = incomingMessageObject != null ? (Segment) incomingMessageObject.get("MSH") : null; outgoingMessageString = logAndMakeErrorMessage(e, inHeader, myParser, myParser.getEncoding(incomingMessageString)); if (outgoingMessageString != null && myExceptionHandler != null) { outgoingMessageString = myExceptionHandler.processException(incomingMessageString, theMetadata, outgoingMessageString, e); } return outgoingMessageString; }
private String handleProcessMessageException(String incomingMessageString, Map<String, Object> theMetadata, Message incomingMessageObject, Exception e) throws HL7Exception { String outgoingMessageString; Segment inHeader = incomingMessageObject != null ? (Segment) incomingMessageObject.get("MSH") : null; outgoingMessageString = logAndMakeErrorMessage(e, inHeader, myParser, myParser.getEncoding(incomingMessageString)); if (outgoingMessageString != null && myExceptionHandler != null) { outgoingMessageString = myExceptionHandler.processException(incomingMessageString, theMetadata, outgoingMessageString, e); } return outgoingMessageString; }
/** * {@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. MSH msh = (MSH)theIn.get("MSH"); ACK retVal; try { // This method takes in the MSH segment of an incoming message, and generates an // appropriate ACK retVal = (ACK)DefaultApplication.makeACK(msh); } catch (IOException e) { throw new HL7Exception(e); } return retVal; }
/** * @deprecated use {@link Message#generateACK()} */ public static Message makeACK(Segment inboundHeader) throws HL7Exception, IOException { // Make sure that the referenced message is not just a dummy DeepCopy.copy(inboundHeader, (Segment)inboundHeader.getMessage().get("MSH")); return inboundHeader.getMessage().generateACK(); }
/** * @deprecated use {@link Message#generateACK()} */ public static Message makeACK(Segment inboundHeader) throws HL7Exception, IOException { // Make sure that the referenced message is not just a dummy DeepCopy.copy(inboundHeader, (Segment)inboundHeader.getMessage().get("MSH")); return inboundHeader.getMessage().generateACK(); }
/** * Formats a Message object into an HL7 message string using this parser's * default encoding ("VB"). * * @throws HL7Exception * if the data fields in the message do not permit encoding * (e.g. required fields are null) */ protected String doEncode(Message source) throws HL7Exception { // get encoding characters ... Segment msh = (Segment) source.get("MSH"); String fieldSepString = Terser.get(msh, 1, 0, 1, 1); if (fieldSepString == null) throw new HL7Exception("Can't encode message: MSH-1 (field separator) is missing"); char fieldSep = '|'; if (fieldSepString.length() > 0) fieldSep = fieldSepString.charAt(0); EncodingCharacters en = getValidEncodingCharacters(fieldSep, msh); // pass down to group encoding method which will operate recursively on // children ... return encode(source, en, getParserConfiguration(), ""); }
/** * Formats a Message object into an HL7 message string using this parser's * default encoding ("VB"). * * @throws HL7Exception * if the data fields in the message do not permit encoding * (e.g. required fields are null) */ protected String doEncode(Message source) throws HL7Exception { // get encoding characters ... Segment msh = (Segment) source.get("MSH"); String fieldSepString = Terser.get(msh, 1, 0, 1, 1); if (fieldSepString == null) throw new HL7Exception("Can't encode message: MSH-1 (field separator) is missing"); char fieldSep = '|'; if (fieldSepString.length() > 0) fieldSep = fieldSepString.charAt(0); EncodingCharacters en = getValidEncodingCharacters(fieldSep, msh); // pass down to group encoding method which will operate recursively on // children ... return encode(source, en, getParserConfiguration(), ""); }
Segment msh = (Segment) source.get("MSH"); String fieldSepString = Terser.get(msh, 1, 0, 1, 1);
Segment msh = (Segment) source.get("MSH"); String fieldSepString = Terser.get(msh, 1, 0, 1, 1);
private Message getInMessage(Segment inHeader) throws HL7Exception, IOException { Message in; if (inHeader != null) { in = inHeader.getMessage(); // the message may be a dummy message, whose MSH segment is incomplete DeepCopy.copy(inHeader, (Segment) in.get("MSH")); } else { in = Version.highestAvailableVersionOrDefault().newGenericMessage(myParser.getFactory()); ((GenericMessage) in).initQuickstart("ACK", "", ""); } return in; }
private Message getInMessage(Segment inHeader) throws HL7Exception, IOException { Message in; if (inHeader != null) { in = inHeader.getMessage(); // the message may be a dummy message, whose MSH segment is incomplete DeepCopy.copy(inHeader, (Segment) in.get("MSH")); } else { in = Version.highestAvailableVersionOrDefault().newGenericMessage(myParser.getFactory()); ((GenericMessage) in).initQuickstart("ACK", "", ""); } return in; }
public Message createNak0(Message originalMessage, HL7Exception e, AcknowledgmentCode ackTypeCode) throws HL7Exception { AbstractMessage ack = (AbstractMessage) MessageUtils.response( originalMessage, messageType, triggerEvent); LOG.info("Creating NAK response event of type {}", ack.getClass().getName()); e.populateResponse(ack, ackTypeCode, 0); Segment msa = (Segment) ack.get("MSA"); Terser.set(msa, 1, 0, 1, 1, ackTypeCode.name()); Segment ackQak = (Segment) ack.get("QAK"); Segment origQpd = (Segment) originalMessage.get("QPD"); if (origQpd != null) { String queryTag = Terser.get(origQpd, 2, 0, 1, 1); Terser.set(ackQak, 1, 0, 1, 1, queryTag); LOG.debug("Set QAK-1 to {}", queryTag); } Terser.set(ackQak, 2, 0, 1, 1, "AE"); // create a dummy QPD segment, it will be replaced with proper contents by // org.openehealth.ipf.platform.camel.ihe.hl7v2.intercept.consumer.ConsumerSegmentEchoingInterceptor Segment ackQpd = (Segment) ack.get("QPD"); Terser.set(ackQpd, 1, 0, 1, 1, "dummy"); return ack; }
/** * Fill segments for HL7 versions before 2.5. * <p/> * HL7 versions before 2.5 require to set MSA-1 and MSA-3. The ERR segment only has one * repeatable field (ERR-1) with components containing details about the exception. * * @param response the raw response message * @param acknowledgmentCode acknowledgment code * @param repetition repetition of the ERR segment that shall be popualted * @throws HL7Exception */ private Message populateResponseBefore25(Message response, AcknowledgmentCode acknowledgmentCode, int repetition) throws HL7Exception { // TODO define what should happen if there is no MSA or ERR Segment msa = (Segment) response.get("MSA"); Terser.set(msa, 1, 0, 1, 1, acknowledgmentCode.name()); Terser.set(msa, 3, 0, 1, 1, errorCode.getMessage()); Segment err = (Segment) response.get("ERR"); if (location != null) { if (location.getSegmentName() != null) Terser.set(err, 1, repetition, 1, 1, location.getSegmentName()); if (location.getField() > 0) Terser.set(err, 1, repetition, 3, 1, Integer.toString(location.getField())); } Terser.set(err, 1, repetition, 4, 1, Integer.toString(errorCode.getCode())); Terser.set(err, 1, repetition, 4, 2, errorCode.getMessage()); Terser.set(err, 1, repetition, 4, 3, ErrorCode.codeTable()); Terser.set(err, 1, repetition, 4, 5, getMessage()); return response; }
/** * Fill segments for HL7 versions before 2.5. * <p/> * HL7 versions before 2.5 require to set MSA-1 and MSA-3. The ERR segment only has one * repeatable field (ERR-1) with components containing details about the exception. * * @param response the raw response message * @param acknowledgmentCode acknowledgment code * @param repetition repetition of the ERR segment that shall be popualted * @throws HL7Exception */ private Message populateResponseBefore25(Message response, AcknowledgmentCode acknowledgmentCode, int repetition) throws HL7Exception { // TODO define what should happen if there is no MSA or ERR Segment msa = (Segment) response.get("MSA"); Terser.set(msa, 1, 0, 1, 1, acknowledgmentCode.name()); Terser.set(msa, 3, 0, 1, 1, errorCode.getMessage()); Segment err = (Segment) response.get("ERR"); if (location != null) { if (location.getSegmentName() != null) Terser.set(err, 1, repetition, 1, 1, location.getSegmentName()); if (location.getField() > 0) Terser.set(err, 1, repetition, 3, 1, Integer.toString(location.getField())); } Terser.set(err, 1, repetition, 4, 1, Integer.toString(errorCode.getCode())); Terser.set(err, 1, repetition, 4, 2, errorCode.getMessage()); Terser.set(err, 1, repetition, 4, 3, ErrorCode.codeTable()); Terser.set(err, 1, repetition, 4, 5, getMessage()); return response; }
/** * Performs acceptance test of the given message. * * @param message HAPI {@link Message} object. * @param isRequest <code>true</code> iff the message is a request. * @throws Hl7v2AcceptanceException when the message is not acceptable. */ public void checkMessageAcceptance( Message message, boolean isRequest) throws Hl7v2AcceptanceException { try { Segment msh = (Segment) message.get("MSH"); checkMessageAcceptance( Terser.get(msh, 9, 0, 1, 1), Terser.get(msh, 9, 0, 2, 1), Terser.get(msh, 9, 0, 3, 1), Terser.get(msh, 12, 0, 1, 1), isRequest); } catch (Hl7v2AcceptanceException e) { throw e; } catch (HL7Exception e) { throw new Hl7v2AcceptanceException("Missing or invalid MSH segment: " + e.getMessage(), ErrorCode.APPLICATION_INTERNAL_ERROR); } }
/** * Generates an empty response message. This class generates an * ACKnowledgement using the code returned by {@link #getSuccessAcknowledgementCode()}. * * @param request request message, either a {@link String} or a * {@link Message} * @return acknowledgment to the request * @throws HL7Exception */ protected Message generateResponseMessage(Object request) throws HL7Exception { try { Message in; if (request instanceof String) { Segment s = getHapiContext().getGenericParser().getCriticalResponseData( (String)request); in = s.getMessage(); DeepCopy.copy(s, (Segment) in.get("MSH")); } else if (request instanceof Message) { in = (Message) request; } else { throw new HL7Exception("Validated message must be either Message or String"); } return in.generateACK(getSuccessAcknowledgementCode(), null); } catch (IOException e) { throw new HL7Exception(e); } }
/** * Generates an empty response message. This class generates an * ACKnowledgement using the code returned by {@link #getSuccessAcknowledgementCode()}. * * @param request request message, either a {@link String} or a * {@link Message} * @return acknowledgment to the request * @throws HL7Exception */ protected Message generateResponseMessage(Object request) throws HL7Exception { try { Message in; if (request instanceof String) { Segment s = getHapiContext().getGenericParser().getCriticalResponseData( (String)request); in = s.getMessage(); DeepCopy.copy(s, (Segment) in.get("MSH")); } else if (request instanceof Message) { in = (Message) request; } else { throw new HL7Exception("Validated message must be either Message or String"); } return in.generateACK(getSuccessAcknowledgementCode(), null); } catch (IOException e) { throw new HL7Exception(e); } }
private static Transportable makeAcceptAck(Transportable theMessage, String theAckCode, ErrorCode theErrorCode, String theDescription) throws HL7Exception { Segment header = ourParser.getCriticalResponseData(theMessage.getMessage()); Message dummy = header.getMessage(); // MSH header refers to dummy, but not the other way round! DeepCopy.copy(header, (Segment)dummy.get("MSH")); try { HL7Exception hl7e = new HL7Exception(theDescription, theErrorCode); AcknowledgmentCode code = theAckCode == null ? AcknowledgmentCode.CR : AcknowledgmentCode.valueOf(theAckCode); Message out = dummy.generateACK(code, hl7e); String originalEncoding = ourParser.getEncoding(theMessage.getMessage()); String ackText = ourParser.encode(out, originalEncoding); return new TransportableImpl(ackText); } catch (IOException e) { throw new HL7Exception(e); } }
private static Transportable makeAcceptAck(Transportable theMessage, String theAckCode, ErrorCode theErrorCode, String theDescription) throws HL7Exception { Segment header = ourParser.getCriticalResponseData(theMessage.getMessage()); Message dummy = header.getMessage(); // MSH header refers to dummy, but not the other way round! DeepCopy.copy(header, (Segment)dummy.get("MSH")); try { HL7Exception hl7e = new HL7Exception(theDescription, theErrorCode); AcknowledgmentCode code = theAckCode == null ? AcknowledgmentCode.CR : AcknowledgmentCode.valueOf(theAckCode); Message out = dummy.generateACK(code, hl7e); String originalEncoding = ourParser.getEncoding(theMessage.getMessage()); String ackText = ourParser.encode(out, originalEncoding); return new TransportableImpl(ackText); } catch (IOException e) { throw new HL7Exception(e); } }