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.mllp.core;
17  
18  import org.apache.camel.CamelContext;
19  import org.apache.camel.Endpoint;
20  import org.apache.camel.component.hl7.HL7MLLPCodec;
21  import org.apache.camel.component.mina2.Mina2Component;
22  import org.apache.camel.component.mina2.Mina2Endpoint;
23  import org.openehealth.ipf.commons.ihe.hl7v2.audit.MllpAuditDataset;
24  import org.openehealth.ipf.platform.camel.ihe.core.InterceptableComponent;
25  import org.openehealth.ipf.platform.camel.ihe.core.Interceptor;
26  import org.openehealth.ipf.platform.camel.ihe.hl7v2.Hl7v2ConfigurationHolder;
27  import org.openehealth.ipf.platform.camel.ihe.hl7v2.intercept.consumer.ConsumerAdaptingInterceptor;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.nio.charset.Charset;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  
38  /**
39   * Generic Camel component for MLLP.
40   *
41   * @author Dmytro Rud
42   */
43  public abstract class MllpComponent<ConfigType extends MllpEndpointConfiguration, AuditDatasetType extends MllpAuditDataset>
44          extends Mina2Component implements InterceptableComponent, Hl7v2ConfigurationHolder<AuditDatasetType> {
45  
46      private static final transient Logger LOG = LoggerFactory.getLogger(MllpComponent.class);
47  
48      public static final String ACK_TYPE_CODE_HEADER = ConsumerAdaptingInterceptor.ACK_TYPE_CODE_HEADER;
49  
50      private static final String DEFAULT_HL7_CODEC_FACTORY_BEAN_NAME = "#hl7codec";
51  
52      protected MllpComponent() {
53          super();
54      }
55  
56      /**
57       * Camel context-based constructor.
58       *
59       * @param camelContext camel context
60       */
61      protected MllpComponent(CamelContext camelContext) {
62          super(camelContext);
63      }
64  
65  
66      /**
67       * Creates a configuration object.
68       *
69       * @param uri endpoint URI
70       * @param parameters URL parameters.
71       * @return configuration object filled with values from the provided parameter map.
72       */
73      protected abstract ConfigType createConfig(String uri, Map<String, Object> parameters) throws Exception;
74  
75      /**
76       * Creates a configuration object.
77       *
78       * @param parameters URL parameters.
79       * @return configuration object filled with values from the provided parameter map
80       * @deprecated use {@link #createConfig(String, Map)}
81       */
82      protected ConfigType createConfig(Map<String, Object> parameters) throws Exception {
83          return createConfig(MllpEndpointConfiguration.UNKNOWN_URI, parameters);
84      }
85  
86      /**
87       * Creates an endpoint object.
88       *
89       * @param wrappedEndpoint standard Camel MINA2 endpoint instance.
90       * @param config          endpoint configuration.
91       * @return configured MLLP endpoint instance which wraps the MINA2 one.
92       */
93      protected abstract MllpEndpoint<?, ?, ?> createEndpoint(Mina2Endpoint wrappedEndpoint, ConfigType config);
94  
95  
96      /**
97       * Creates and configures the endpoint.
98       */
99      @Override
100     protected Endpoint createEndpoint(
101             String uri,
102             String remaining,
103             Map<String, Object> parameters) throws Exception {
104         // explicitly overwrite some standard camel-mina parameters
105         if (parameters.isEmpty()) {
106             parameters = new HashMap<>();
107         }
108         parameters.put("sync", true);
109         parameters.put("lazySessionCreation", true);
110         parameters.put("transferExchange", false);
111         if (!parameters.containsKey("codec")) {
112             parameters.put("codec", DEFAULT_HL7_CODEC_FACTORY_BEAN_NAME);
113         }
114 
115         ConfigType config = createConfig(uri, parameters);
116 
117         Charset charset = null;
118         try {
119             HL7MLLPCodec codecFactory = (HL7MLLPCodec) config.getCodecFactory();
120             if (codecFactory == null) {
121                 codecFactory = new HL7MLLPCodec();
122                 LOG.warn("No HL7 codec factory found, creating new default instance {}", codecFactory);
123             }
124             charset = codecFactory.getCharset();
125         } catch (ClassCastException cce) {
126             LOG.warn("Unsupported HL7 codec factory type {}, using default character set", config.getCodecFactory().getClass().getName());
127         }
128         if (charset == null) {
129             charset = Charset.defaultCharset();
130         }
131         parameters.put("encoding", charset.name());
132 
133         // construct the endpoint
134         Endpoint endpoint = super.createEndpoint(uri, "tcp://" + remaining, parameters);
135         Mina2Endpoint minaEndpoint = (Mina2Endpoint) endpoint;
136 
137         // wrap and return
138         return createEndpoint(minaEndpoint, config);
139     }
140 
141     @Override
142     public List<Interceptor<?>> getAdditionalConsumerInterceptors() {
143         return Collections.emptyList();
144     }
145 
146     @Override
147     public List<Interceptor<?>> getAdditionalProducerInterceptors() {
148         return Collections.emptyList();
149     }
150 
151 }