View Javadoc
1   /*
2    * Copyright 2009 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  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   * Consumer-side HL7 marshaling/unmarshaling interceptor.
36   * <p>
37   * When the Camel exchange does not contain property {@link Exchange#CHARSET_NAME},
38   * the default system character set will be used.
39   *
40   * @author Dmytro Rud
41   */
42  public class ConsumerMarshalInterceptor extends InterceptorSupport<HL7v2Endpoint> {
43      private static final transient Logger LOG = LoggerFactory.getLogger(ConsumerMarshalInterceptor.class);
44  
45  
46      /**
47       * Unmarshals the request, passes it to the processing route, 
48       * and marshals the response.
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          // unmarshal
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              // prepare exchange
71              Message copy;
72              try {
73  
74                  copy = MessageUtils.copy(originalMessage);
75              } catch (Exception e) {
76                  // this exception will occur when the message structure (MSH-9-3) of
77                  // the original adapter is wrong or when unknown segments are present
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              // run the route
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          // marshal if necessary
97          String s = Hl7v2MarshalUtils.marshalStandardTypes(
98                  resultMessage(exchange),
99                  characterSet(exchange),
100                 parser);
101         resultMessage(exchange).setBody(s);
102     }
103 
104 }