1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
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
58
59
60
61 protected MllpComponent(CamelContext camelContext) {
62 super(camelContext);
63 }
64
65
66
67
68
69
70
71
72
73 protected abstract ConfigType createConfig(String uri, Map<String, Object> parameters) throws Exception;
74
75
76
77
78
79
80
81
82 protected ConfigType createConfig(Map<String, Object> parameters) throws Exception {
83 return createConfig(MllpEndpointConfiguration.UNKNOWN_URI, parameters);
84 }
85
86
87
88
89
90
91
92
93 protected abstract MllpEndpoint<?, ?, ?> createEndpoint(Mina2Endpoint wrappedEndpoint, ConfigType config);
94
95
96
97
98
99 @Override
100 protected Endpoint createEndpoint(
101 String uri,
102 String remaining,
103 Map<String, Object> parameters) throws Exception {
104
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
134 Endpoint endpoint = super.createEndpoint(uri, "tcp://" + remaining, parameters);
135 Mina2Endpoint minaEndpoint = (Mina2Endpoint) endpoint;
136
137
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 }