1 /* 2 * Copyright 2015 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.openehealth.ipf.platform.camel.hl7.converter; 18 19 import javax.jms.*; 20 21 import ca.uhn.hl7v2.HL7Exception; 22 import ca.uhn.hl7v2.HapiContext; 23 import org.springframework.jms.support.converter.MessageConversionException; 24 import org.springframework.jms.support.converter.MessageConverter; 25 26 /** 27 * HL7MessageConverter helps serializing a HL7 message to and from a string for the purpose of 28 * routing it through a JMS queue. The same converter should be used on consumer and producer side. 29 * <p/> 30 * HAPI HL7 message objects have a reference to a transient parser, which in return has the reference 31 * to the valid HapiContext. After deserialization, the context and parser references have to be 32 * re-established. 33 * <p/> 34 * Currently, this class accepts String and HAPI message objects only. Deserialization will always 35 * produce a HAPI Message object. 36 */ 37 public class HL7MessageConverter implements MessageConverter { 38 39 private final HapiContext hapiContext; 40 41 public HL7MessageConverter(HapiContext hapiContext) { 42 this.hapiContext = hapiContext; 43 } 44 45 /** 46 * Returns a {@link javax.jms.TextMessage} instance containing the HL7 message as string 47 * 48 * @param o incoming object, can be either String or {@link ca.uhn.hl7v2.model.Message} 49 * @param session JMS session 50 * @return a {@link javax.jms.TextMessage} instance containing the HL7 message as string 51 * @throws JMSException 52 * @throws MessageConversionException 53 */ 54 @Override 55 public Message toMessage(Object o, Session session) throws JMSException, MessageConversionException { 56 if (o instanceof String) { 57 return session.createTextMessage((String)o); 58 } 59 if (o instanceof ca.uhn.hl7v2.model.Message) { 60 try { 61 return session.createTextMessage(((ca.uhn.hl7v2.model.Message) o).encode()); 62 } catch (HL7Exception e) { 63 throw new RuntimeException(e); 64 } 65 } 66 throw new MessageConversionException("Unexpected class : " + o.getClass()); 67 } 68 69 /** 70 * Returns a {@link ca.uhn.hl7v2.model.Message} built from the JMS message type, using the specified HapiContext 71 * 72 * @param message JMS {@link javax.jms.ObjectMessage} or {@link javax.jms.TextMessage} 73 * @return a {@link ca.uhn.hl7v2.model.Message} built from the JMS message type 74 * @throws JMSException 75 * @throws MessageConversionException 76 */ 77 @Override 78 public Object fromMessage(Message message) throws JMSException, MessageConversionException { 79 if (message instanceof ObjectMessage) { 80 ca.uhn.hl7v2.model.Message msg = (ca.uhn.hl7v2.model.Message) ((ObjectMessage) message).getObject(); 81 msg.setParser(hapiContext.getGenericParser()); 82 return msg; 83 } 84 if (message instanceof TextMessage) { 85 String msg = ((TextMessage) message).getText(); 86 try { 87 return hapiContext.getGenericParser().parse(msg); 88 } catch (HL7Exception e) { 89 throw new RuntimeException(e); 90 } 91 } 92 throw new MessageConversionException("Unexpected class : " + message.getClass()); 93 } 94 } 95