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.producer;
17  
18  import ca.uhn.hl7v2.model.Message;
19  import org.apache.camel.Exchange;
20  import org.apache.camel.ExchangePattern;
21  import org.apache.commons.lang3.ClassUtils;
22  import org.openehealth.ipf.platform.camel.ihe.core.InterceptorSupport;
23  import org.openehealth.ipf.platform.camel.ihe.hl7v2.HL7v2Endpoint;
24  import org.openehealth.ipf.platform.camel.ihe.hl7v2.Hl7v2AdaptingException;
25  import org.openehealth.ipf.platform.camel.ihe.hl7v2.Hl7v2MarshalUtils;
26  
27  
28  /**
29   * Producer-side Camel interceptor which creates a {@link Message}
30   * from various possible request types.
31   *  
32   * @author Dmytro Rud
33   */
34  public class ProducerAdaptingInterceptor extends InterceptorSupport<HL7v2Endpoint> {
35  
36      private final String charsetName;
37  
38  
39      /**
40       * Constructor which enforces the use of a particular character set.
41       * The given value will be propagated to the Camel exchange property
42       * {@link Exchange#CHARSET_NAME}, rewriting its old content.
43       *
44       * @param charsetName
45       *      character set to use in all data transformations.
46       */
47      public ProducerAdaptingInterceptor(String charsetName) {
48          super();
49          this.charsetName = charsetName;
50      }
51  
52  
53      /**
54       * Constructor which does not enforce the use of a particular character set.
55       * When the Camel exchange does not contain property {@link Exchange#CHARSET_NAME},
56       * the default system character set will be used.
57       */
58      public ProducerAdaptingInterceptor() {
59          this(null);
60      }
61  
62  
63      /**
64       * Converts outgoing request to a {@link Message}
65       * and performs some exchange configuration.
66       */
67      @Override
68      public void process(Exchange exchange) throws Exception {
69          if (charsetName != null) {
70              exchange.setProperty(Exchange.CHARSET_NAME, charsetName);
71          }
72          Message msg = Hl7v2MarshalUtils.extractHapiMessage(
73                  exchange.getIn(),
74                  characterSet(exchange),
75                  getEndpoint().getHl7v2TransactionConfiguration().getParser());
76          
77          if (msg == null) {
78              throw new Hl7v2AdaptingException("Cannot create HL7v2 message from the given " +
79                      ClassUtils.getSimpleName(exchange.getIn().getBody(), "<null>"));
80          }
81          
82          exchange.getIn().setBody(msg);
83          exchange.setPattern(ExchangePattern.InOut);
84          
85          // run the route
86          getWrappedProcessor().process(exchange);
87      }
88  }