1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.ihe.hl7v2.intercept.consumer;
17
18 import ca.uhn.hl7v2.HL7Exception;
19 import ca.uhn.hl7v2.model.Message;
20 import ca.uhn.hl7v2.parser.Parser;
21 import org.apache.camel.Exchange;
22 import org.openehealth.ipf.commons.ihe.hl7v2.Constants;
23 import org.openehealth.ipf.modules.hl7.message.MessageUtils;
24 import org.openehealth.ipf.platform.camel.ihe.core.InterceptorSupport;
25 import org.openehealth.ipf.platform.camel.ihe.hl7v2.HL7v2Endpoint;
26 import org.openehealth.ipf.platform.camel.ihe.hl7v2.Hl7v2AdaptingException;
27 import org.openehealth.ipf.platform.camel.ihe.hl7v2.Hl7v2MarshalUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import static org.openehealth.ipf.platform.camel.core.util.Exchanges.resultMessage;
32
33
34
35
36
37
38
39
40
41
42 public class ConsumerMarshalInterceptor extends InterceptorSupport<HL7v2Endpoint> {
43 private static final transient Logger LOG = LoggerFactory.getLogger(ConsumerMarshalInterceptor.class);
44
45
46
47
48
49
50 @Override
51 public void process(Exchange exchange) throws Exception {
52 Message originalMessage = null;
53 Parser parser = getEndpoint().getHl7v2TransactionConfiguration().getParser();
54
55 org.apache.camel.Message inMessage = exchange.getIn();
56 String originalString = inMessage.getBody(String.class);
57
58
59 boolean unmarshallingFailed = false;
60 try {
61 originalMessage = parser.parse(originalString);
62 } catch (HL7Exception e) {
63 unmarshallingFailed = true;
64 LOG.error("Unmarshalling failed, message processing not possible", e);
65 Message nak = getEndpoint().getNakFactory().createDefaultNak(e);
66 resultMessage(exchange).setBody(nak);
67 }
68
69 if( ! unmarshallingFailed) {
70
71 Message copy;
72 try {
73
74 copy = MessageUtils.copy(originalMessage);
75 } catch (Exception e) {
76
77
78 copy = originalMessage;
79 }
80 inMessage.setBody(originalMessage);
81 inMessage.setHeader(Constants.ORIGINAL_MESSAGE_ADAPTER_HEADER_NAME, copy);
82 inMessage.setHeader(Constants.ORIGINAL_MESSAGE_STRING_HEADER_NAME, originalString);
83
84
85 try {
86 getWrappedProcessor().process(exchange);
87 } catch (Hl7v2AdaptingException mae) {
88 throw mae;
89 } catch (Exception e) {
90 LOG.error("Message processing failed", e);
91 resultMessage(exchange).setBody(
92 getEndpoint().getNakFactory().createNak(originalMessage, e));
93 }
94 }
95
96
97 String s = Hl7v2MarshalUtils.marshalStandardTypes(
98 resultMessage(exchange),
99 characterSet(exchange),
100 parser);
101 resultMessage(exchange).setBody(s);
102 }
103
104 }