1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.openehealth.ipf.platform.camel.ihe.core;
18
19 import org.apache.camel.Consumer;
20 import org.apache.camel.Endpoint;
21 import org.apache.camel.Processor;
22 import org.apache.camel.Producer;
23 import org.openehealth.ipf.commons.ihe.core.chain.ChainUtils;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.function.Function;
28
29
30
31
32
33
34
35
36
37 public interface InterceptableEndpoint<
38 ConfigType extends InterceptableEndpointConfiguration,
39 ComponentType extends InterceptableComponent> extends Endpoint {
40
41
42
43
44
45
46
47
48
49
50 @Override
51 default Producer createProducer() throws Exception {
52 Producer producer = doCreateProducer();
53
54 List<Interceptor> initialChain = createInitialProducerInterceptorChain();
55 List<Interceptor> additionalInterceptors = new ArrayList<>();
56 additionalInterceptors.addAll(getInterceptableComponent().getAdditionalProducerInterceptors());
57
58 additionalInterceptors.addAll(getCustomInterceptors());
59 List<Interceptor> producerInterceptorChain = ChainUtils.createChain(initialChain, additionalInterceptors);
60
61 return InterceptorUtils.adaptProducerChain(
62 producerInterceptorChain,
63 this,
64 producer);
65 }
66
67
68
69
70
71
72
73
74
75
76 @Override
77 default Consumer createConsumer(Processor originalProcessor) throws Exception {
78
79 List<Interceptor> initialChain = createInitialConsumerInterceptorChain();
80
81 List<Interceptor> additionalInterceptors = new ArrayList<>();
82 additionalInterceptors.addAll(getInterceptableComponent().getAdditionalConsumerInterceptors());
83
84 additionalInterceptors.addAll(getCustomInterceptors());
85 List<Interceptor> consumerInterceptorChain = ChainUtils.createChain(initialChain, additionalInterceptors);
86
87 Processor processor = originalProcessor;
88 for (int i = consumerInterceptorChain.size() - 1; i >= 0; --i) {
89 Interceptor interceptor = consumerInterceptorChain.get(i);
90 interceptor.setWrappedProcessor(processor);
91 interceptor.setEndpoint(this);
92 processor = interceptor;
93 }
94
95 return doCreateConsumer(processor);
96 }
97
98
99
100
101
102
103 default List<Interceptor> getCustomInterceptors() {
104 List<Interceptor> result = new ArrayList<>();
105 List<InterceptorFactory> factories = getInterceptableConfiguration().getCustomInterceptorFactories();
106 factories.stream()
107 .map((Function<InterceptorFactory, Interceptor>) InterceptorFactory::getNewInstance)
108 .forEach(result::add);
109 return result;
110 }
111
112
113
114
115
116 Producer doCreateProducer() throws Exception;
117
118
119
120
121
122 Consumer doCreateConsumer(Processor processor) throws Exception;
123
124
125
126
127 ComponentType getInterceptableComponent();
128
129
130
131
132 ConfigType getInterceptableConfiguration();
133
134
135
136
137 List<Interceptor> createInitialConsumerInterceptorChain();
138
139
140
141
142 List<Interceptor> createInitialProducerInterceptorChain();
143 }